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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
import type { JSX } from "react";
import { start } from "@/logic/api";
import IO from "@/logic/requests/nostrill";
import type { ComposerData } from "@/types/ui";
import { create } from "zustand";
import type { UserProfile } from "@/types/nostrill";
import type { Event } from "@/types/nostr";
import type { FC, Poast } 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;
key: string;
nostrFeed: Event[];
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);
if ("state" in data) {
const { feed, nostr, following, relays, profiles, key } = data.state;
const flwing = new Map(Object.entries(following as Record<string, FC>));
flwing.set(api!.airlock.our!, feed);
set({
relays,
nostrFeed: nostr,
profiles: new Map(Object.entries(profiles)),
following: flwing,
key,
});
} else if ("fact" in data) {
if ("post" in data.fact) {
if ("add" in data.fact.post) {
const post: Poast = data.fact.post.add.post;
const following = get().following;
const curr = following.get(post.author);
const fc = curr ? curr : { feed: {}, start: null, end: null };
fc.feed[post.id] = post;
following.set(post.author, fc);
set({ following });
}
}
}
});
set({ api });
},
key: "",
profiles: new Map(),
relays: {},
nostrFeed: [],
following: new Map(),
followers: [],
UISettings: {},
modal: null,
setModal: (modal) => set({ modal }),
// composer data
composerData: null,
setComposerData: (composerData) => set({ composerData }),
}));
export default useLocalState;
|