Files
factorio-clusterio/packages/web_ui/src/components/ResourceState.tsx
T
Danielv123 2e1710d3a9 Show the same page for missing, deleted and unloadable resources
Every view page handled a resource that can't be shown its own way, and
users without the subscribe permission were left on a spinner forever.

Adds useResourceState and a ResourceState component, used by the host,
instance, mod pack, icon reference, user, role and plugin pages and
exported for plugin pages. The single item hooks now also return the
subscription error.

Resolves #1165
2026-11-01 17:40:22 +01:00

50 lines
1.5 KiB
TypeScript

import React from "react";
import { Result, ResultProps, Spin } from "antd";
import Link from "./Link";
import PageHeader from "./PageHeader";
import PageLayout from "./PageLayout";
import type { ResourceStatus } from "../util/useResourceState";
type ResourceStateProps = {
/** Status from useResourceState */
state: ResourceStatus;
/** Capitalised name of the kind of resource, e.g. "Mod pack" */
type: string;
/** Id or name of the resource */
id: number | string;
nav: React.ComponentProps<typeof PageLayout>["nav"];
/** Page listing this kind of resource */
listPath: string;
/** Error from the subscription */
error: Error | null;
};
/** Whole page to render in place of a view page when its resource can't be shown */
export default function ResourceState(props: ResourceStateProps) {
const { state, type, id, nav } = props;
if (state === "ok") {
return null;
}
if (state === "loading") {
return <PageLayout nav={nav}>
<PageHeader title={String(id)} />
<Spin size="large" />
</PageLayout>;
}
const results: Record<typeof state, ResultProps> = {
missing: { status: "warning", title: `${type} not found`, subTitle: `${type} ${id} was not found.` },
deleted: { status: "warning", title: `${type} ${id} has been deleted` },
error: { status: "error", title: `Could not load ${type.toLowerCase()}`, subTitle: props.error?.message },
};
return <PageLayout nav={nav}>
<Result
{...results[state]}
extra={<Link to={props.listPath}>Back to {type.toLowerCase()} list</Link>}
/>
</PageLayout>;
}