interface: add state accessors

This commit is contained in:
Liam Fitzgerald 2021-04-28 13:37:20 +10:00
parent b6a6b26911
commit 22645b9814
No known key found for this signature in database
GPG Key ID: D390E12C61D1CFFB
3 changed files with 20 additions and 3 deletions

View File

@ -35,4 +35,8 @@ export function useContact(ship: string) {
);
}
export function useOurContact() {
return useContact(`~${window.ship}`)
}
export default useContactState;

View File

@ -1,4 +1,5 @@
import { Graphs, decToUd, numToUd, GraphNode } from "@urbit/api";
import { Graphs, decToUd, numToUd, GraphNode, deSig, Association, resourceFromPath } from "@urbit/api";
import {useCallback} from "react";
import { BaseState, createState } from "./base";
@ -130,6 +131,18 @@ const useGraphState = createState<GraphState>('Graph', {
// },
}, ['graphs', 'graphKeys', 'looseNodes', 'graphTimesentMap']);
export function useGraph(ship: string, name: string) {
return useGraphState(
useCallback(s => s.graphs[`${deSig(ship)}/${name}`], [ship, name])
);
}
export function useGraphForAssoc(association: Association) {
const { resource } = association;
const { ship, name } = resourceFromPath(resource);
return useGraph(ship, name);
}
window.useGraphState = useGraphState;
export default useGraphState;

View File

@ -90,8 +90,8 @@ const useLocalState = create<LocalStateZus>(persist((set, get) => ({
name: 'localReducer'
}));
function withLocalState<P, S extends keyof LocalState>(Component: any, stateMemberKeys?: S[]) {
return React.forwardRef((props: Omit<P, S>, ref) => {
function withLocalState<P, S extends keyof LocalState, C extends React.ComponentType<P>>(Component: C, stateMemberKeys?: S[]) {
return React.forwardRef<C, Omit<P, S>>((props, ref) => {
const localState = stateMemberKeys ? useLocalState(
state => stateMemberKeys.reduce(
(object, key) => ({ ...object, [key]: state[key] }), {}