mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-15 15:24:52 +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
24 lines
939 B
TypeScript
24 lines
939 B
TypeScript
import { useCallback, useContext, useSyncExternalStore } from "react";
|
|
import ControlContext from "../components/ControlContext";
|
|
import { defaultModPackStore } from "../store/default_mod_pack_store";
|
|
|
|
|
|
export function useModPack(id?: number) {
|
|
const [modPacks, synced, error] = useModPacks();
|
|
return [id !== undefined ? modPacks.get(id) : undefined, synced, error] as const;
|
|
}
|
|
|
|
export function useModPacks() {
|
|
const control = useContext(ControlContext);
|
|
const subscribe = useCallback((callback: () => void) => control.modPacks.subscribe(callback), [control]);
|
|
return useSyncExternalStore(subscribe, () => control.modPacks.getSnapshot());
|
|
}
|
|
|
|
export function useDefaultModPack() {
|
|
const control = useContext(ControlContext);
|
|
const subscribe = useCallback(
|
|
(callback: () => void) => defaultModPackStore.subscribe(control, callback), [control],
|
|
);
|
|
return useSyncExternalStore(subscribe, () => defaultModPackStore.getSnapshot());
|
|
}
|