diff options
Diffstat (limited to 'front/src/state/state.ts')
-rw-r--r-- | front/src/state/state.ts | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/front/src/state/state.ts b/front/src/state/state.ts new file mode 100644 index 0000000..28f3fb2 --- /dev/null +++ b/front/src/state/state.ts @@ -0,0 +1,64 @@ +import type { JSX } from "react"; +import { start } from "@/logic/api"; +import IO from "@/logic/requests/nostril"; +import type { ComposerData } from "@/types/ui"; +import { create } from "zustand"; +import type { UserProfile } from "@/types/nostril"; +import type { Event } from "@/types/nostr"; +import type { FC } from "@/types/trill"; +// TODO handle airlock connection issues +// the SSE pipeline has a "status-update" event FWIW +// type AirlockState = "connecting" | "connected" | "failed"; +export type LocalState = { + isNew: boolean; + api: IO | null; + init: () => Promise<void>; + UISettings: Record<string, any>; + modal: JSX.Element | null; + setModal: (modal: JSX.Element | null) => void; + composerData: ComposerData | null; + setComposerData: (c: ComposerData | null) => void; + keys: string[]; + relays: Record<string, Event[]>; + profiles: Map<string, UserProfile>; // pubkey key + following: Map<string, FC>; + followers: string[]; +}; + +const creator = create<LocalState>(); +const useLocalState = creator((set, _get) => ({ + isNew: false, + api: null, + init: async () => { + const airlock = await start(); + const api = new IO(airlock); + console.log({ api }); + await api.subscribeStore((data) => { + console.log("store sub", data); + const { feed, following, relays, profiles, keys } = data; + + const flwing = new Map(Object.entries(following as Record<string, FC>)); + flwing.set(api!.airlock.our!, feed); + set({ + relays, + profiles: new Map(Object.entries(profiles)), + following: flwing, + keys, + }); + }); + set({ api }); + }, + keys: [], + profiles: new Map(), + relays: {}, + following: new Map(), + followers: [], + UISettings: {}, + modal: null, + setModal: (modal) => set({ modal }), + // composer data + composerData: null, + setComposerData: (composerData) => set({ composerData }), +})); + +export default useLocalState; |