Hey is a decentralized and permissionless social media app built with Lens Protocol 馃尶
1import type { NewAttachment } from "@hey/types/misc";
2import { createTrackedStore } from "@/store/createTrackedStore";
3
4interface State {
5 addAttachments: (attachments: NewAttachment[]) => void;
6 attachments: NewAttachment[];
7 isUploading: boolean;
8 removeAttachments: (ids: string[]) => void;
9 setAttachments: (attachments: NewAttachment[]) => void;
10 setIsUploading: (isUploading: boolean) => void;
11 updateAttachments: (attachments: NewAttachment[]) => void;
12}
13
14const { useStore: usePostAttachmentStore } = createTrackedStore<State>(
15 (set) => ({
16 addAttachments: (newAttachments) =>
17 set((state) => {
18 return { attachments: [...state.attachments, ...newAttachments] };
19 }),
20 attachments: [],
21 isUploading: false,
22 removeAttachments: (ids) =>
23 set((state) => {
24 const attachments = [...state.attachments];
25 for (const id of ids) {
26 const index = attachments.findIndex((a) => a.id === id);
27 if (index !== -1) {
28 attachments.splice(index, 1);
29 }
30 }
31 return { attachments };
32 }),
33 setAttachments: (attachments) => set(() => ({ attachments })),
34 setIsUploading: (isUploading) => set(() => ({ isUploading })),
35 updateAttachments: (updateAttachments) =>
36 set((state) => {
37 const attachments = [...state.attachments];
38 for (const attachment of updateAttachments) {
39 const index = attachments.findIndex((a) => a.id === attachment.id);
40 if (index !== -1) {
41 attachments[index] = attachment;
42 }
43 }
44 return { attachments };
45 })
46 })
47);
48
49export { usePostAttachmentStore };