Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadSelectionStore.ts

113 lines3.5 KB

1import { create } from "zustand";
2​
3export interface ThreadSelectionState {
4 selectedThreadKeys: ReadonlySet<string>;
5 anchorThreadKey: string | null;
6}
7​
8interface ThreadSelectionStore extends ThreadSelectionState {
9 toggleThread: (threadKey: string) => void;
10 /**
11 * Requires the ordered list of scoped thread keys within the same project so the store can
12 * compute which threads fall between anchor and target.
13 */
14 rangeSelectTo: (threadKey: string, orderedThreadKeys: readonly string[]) => void;
15 clearSelection: () => void;
16 removeFromSelection: (threadKeys: readonly string[]) => void;
17 setAnchor: (threadKey: string) => void;
18 hasSelection: () => boolean;
20​
21const EMPTY_SET = new Set<string>();
22​
23export function getThreadKeysToDeselectAfterDelete(
24 selectedThreadKeys: readonly string[],
25 deletedThreadKeys: ReadonlySet<string>,
26 hasThread: (threadKey: string) => boolean,
27): string[] {
28 return selectedThreadKeys.filter((key) => deletedThreadKeys.has(key) || !hasThread(key));
30​
31export const useThreadSelectionStore = create<ThreadSelectionStore>((set, get) => ({
32 selectedThreadKeys: EMPTY_SET,
33 anchorThreadKey: null,
34​
35 toggleThread: (threadKey) => {
36 set((state) => {
37 const next = new Set(state.selectedThreadKeys);
38 if (next.has(threadKey)) {
39 next.delete(threadKey);
40 } else {
41 next.add(threadKey);
42 }
43 return {
44 selectedThreadKeys: next,
45 anchorThreadKey: next.has(threadKey) ? threadKey : state.anchorThreadKey,
46 };
47 });
48 },
49​
50 rangeSelectTo: (threadKey, orderedThreadKeys) => {
51 set((state) => {
52 const anchor = state.anchorThreadKey;
53 if (anchor === null) {
54 const next = new Set(state.selectedThreadKeys);
55 next.add(threadKey);
56 return { selectedThreadKeys: next, anchorThreadKey: threadKey };
57 }
58​
59 const anchorIndex = orderedThreadKeys.indexOf(anchor);
60 const targetIndex = orderedThreadKeys.indexOf(threadKey);
61 if (anchorIndex === -1 || targetIndex === -1) {
62 const next = new Set(state.selectedThreadKeys);
63 next.add(threadKey);
64 return { selectedThreadKeys: next, anchorThreadKey: threadKey };
65 }
66​
67 const start = Math.min(anchorIndex, targetIndex);
68 const end = Math.max(anchorIndex, targetIndex);
69 const next = new Set(state.selectedThreadKeys);
70 for (let i = start; i <= end; i++) {
71 const key = orderedThreadKeys[i];
72 if (key !== undefined) {
73 next.add(key);
74 }
75 }
76 return { selectedThreadKeys: next, anchorThreadKey: anchor };
77 });
78 },
79​
80 clearSelection: () => {
81 const state = get();
82 if (state.selectedThreadKeys.size === 0 && state.anchorThreadKey === null) return;
83 set({ selectedThreadKeys: EMPTY_SET, anchorThreadKey: null });
84 },
85​
86 setAnchor: (threadKey) => {
87 if (get().anchorThreadKey === threadKey) return;
88 set({ anchorThreadKey: threadKey });
89 },
90​
91 removeFromSelection: (threadKeys) => {
92 set((state) => {
93 const toRemove = new Set(threadKeys);
94 let changed = false;
95 const next = new Set<string>();
96 for (const key of state.selectedThreadKeys) {
97 if (toRemove.has(key)) {
98 changed = true;
99 } else {
100 next.add(key);
101 }
102 }
103 if (!changed) return state;
104 const newAnchor =
105 state.anchorThreadKey !== null && toRemove.has(state.anchorThreadKey)
106 ? null
107 : state.anchorThreadKey;
108 return { selectedThreadKeys: next, anchorThreadKey: newAnchor };
109 });
110 },
111​
112 hasSelection: () => get().selectedThreadKeys.size > 0,
113}));