mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-17 23:33:12 +09:00
* Refactor UserManager to use Datastore * Rename lib.User to UserDetails also adds to/fromUserDetails * Fix existing tests * Add user class tests * Add tests for finaliseUsers * Move HostDetails to own file with tests * Move InstanceDetails to its own file with tests * Rename HostInfo and add tests * Rename InstanceInfo and add tests - Also changes the names of the host properties related to instances - Also removes the need to specify the location of InstanceInfo * Make json round trip test validate schema * Add InstanceManager * Fix lint errors * Extra tests * Add final test coverage * Add one last test to InstanceManager * Response to comments * Use generator rather than return anonumous generator Co-authored-by: Hornwitser <github@hornwitser.no> * Fix lint issue with generator --------- Co-authored-by: Hornwitser <github@hornwitser.no>
24 lines
815 B
TypeScript
24 lines
815 B
TypeScript
import { useCallback, useContext, useSyncExternalStore } from "react";
|
|
import ControlContext from "../components/ControlContext";
|
|
|
|
import * as lib from "@clusterio/lib";
|
|
|
|
export function useRolesOfUser(user?: lib.UserDetails) {
|
|
const [roles, synced] = useRoles();
|
|
return [
|
|
user !== undefined ? new Map([...user.roleIds].map(roleId => [roleId, roles.get(roleId)])) : new Map(),
|
|
synced,
|
|
] as const;
|
|
}
|
|
|
|
export function useRole(id?: number) {
|
|
const [roles, synced] = useRoles();
|
|
return [id !== undefined ? roles.get(id) : undefined, synced] as const;
|
|
}
|
|
|
|
export function useRoles() {
|
|
const control = useContext(ControlContext);
|
|
const subscribe = useCallback((callback: () => void) => control.roles.subscribe(callback), [control]);
|
|
return useSyncExternalStore(subscribe, () => control.roles.getSnapshot());
|
|
}
|