summaryrefslogtreecommitdiff
path: root/front/src/state/state.ts
blob: 28f3fb2323c43f26b2b736998a1f99412a5d9049 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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;