mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-19 23:33:13 +09:00
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
This commit is contained in:
1 parent
928e03ce02
commit
2e1710d3a9
18 files changed
+233
-112
No files matched your search
@@ -0,0 +1,37 @@
|
||||
import { useRef } from "react";
|
||||
|
||||
export type ResourceStatus = "loading" | "ok" | "missing" | "deleted" | "error";
|
||||
|
||||
export function getResourceStatus(
|
||||
exists: boolean,
|
||||
seen: boolean,
|
||||
synced: boolean,
|
||||
error: Error | null,
|
||||
): ResourceStatus {
|
||||
if (exists) {
|
||||
return "ok";
|
||||
}
|
||||
// A retry only clears the error when its response arrives, which can be after the update that synced it
|
||||
if (synced) {
|
||||
return seen ? "deleted" : "missing";
|
||||
}
|
||||
return error ? "error" : "loading";
|
||||
}
|
||||
|
||||
/**
|
||||
* Status of the resource shown on a view page, for use with the ResourceState component
|
||||
*
|
||||
* @param value - The resource, undefined or null if the subscription does not have it.
|
||||
* @param synced - The synced property from the subscription.
|
||||
* @param error - The error property from the subscription.
|
||||
* @param id - Identifies the resource shown, the router reuses the page when navigating between resources.
|
||||
* @returns "ok" if there is a value to show, otherwise why there isn't.
|
||||
*/
|
||||
export default function useResourceState(value: unknown, synced: boolean, error: Error | null, id: number | string) {
|
||||
const seenId = useRef<number | string>(undefined);
|
||||
const exists = value !== undefined && value !== null;
|
||||
if (exists) {
|
||||
seenId.current = id;
|
||||
}
|
||||
return getResourceStatus(exists, seenId.current === id, synced, error);
|
||||
}
|
||||
Reference in new issue
Block a user