mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 15:30:41 +09:00
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
21 lines
925 B
TypeScript
21 lines
925 B
TypeScript
import { useCallback, useContext, useSyncExternalStore } from "react";
|
|
import ControlContext from "../components/ControlContext";
|
|
|
|
export function useHost(id?: number) {
|
|
const [hosts, synced, error] = useHosts();
|
|
return [id !== undefined ? hosts.get(id) : undefined, synced, error] as const;
|
|
}
|
|
|
|
export function useHosts() {
|
|
const control = useContext(ControlContext);
|
|
const subscribe = useCallback((callback: () => void) => control.hosts.subscribe(callback), [control]);
|
|
return useSyncExternalStore(subscribe, () => control.hosts.getSnapshot());
|
|
}
|
|
|
|
/** Requests from hosts waiting to be approved, empty if the account may not approve hosts */
|
|
export function useHostPairings() {
|
|
const control = useContext(ControlContext);
|
|
const subscribe = useCallback((callback: () => void) => control.hostPairings.subscribe(callback), [control]);
|
|
return useSyncExternalStore(subscribe, () => control.hostPairings.getSnapshot());
|
|
}
|