mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-14 07:24:52 +09:00
17 lines
684 B
TypeScript
17 lines
684 B
TypeScript
import { useCallback, useContext, useSyncExternalStore } from "react";
|
|
import ControlContext from "../components/ControlContext";
|
|
|
|
import * as lib from "@clusterio/lib";
|
|
|
|
export function useNote(resourceType: string, resourceId?: string | number) {
|
|
const [notes, synced] = useNotes();
|
|
const note = resourceId !== undefined ? notes.get(lib.Note.idFor(resourceType, resourceId)) : undefined;
|
|
return [note, synced] as const;
|
|
}
|
|
|
|
export function useNotes() {
|
|
const control = useContext(ControlContext);
|
|
const subscribe = useCallback((callback: () => void) => control.notes.subscribe(callback), [control]);
|
|
return useSyncExternalStore(subscribe, () => control.notes.getSnapshot());
|
|
}
|