mirror of
https://github.com/ilyakooo0/urbit.git
synced 2024-12-15 01:52:42 +03:00
interface: add state accessors
This commit is contained in:
parent
b6a6b26911
commit
22645b9814
@ -35,4 +35,8 @@ export function useContact(ship: string) {
|
||||
);
|
||||
}
|
||||
|
||||
export function useOurContact() {
|
||||
return useContact(`~${window.ship}`)
|
||||
}
|
||||
|
||||
export default useContactState;
|
||||
|
@ -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;
|
||||
|
@ -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] }), {}
|
||||
|
Loading…
Reference in New Issue
Block a user