File size: 2,638 Bytes
4e1096a | 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 { create } from 'zustand';
interface ParallelViewState {
parallelViews: Set<string>[];
setParallel: (bookKeys: string[]) => void;
unsetParallel: (bookKeys: string[]) => void;
areParallels: (bookKey1: string, bookKey2: string) => boolean;
getParallels: (bookKey: string) => Set<string> | null;
}
export const useParallelViewStore = create<ParallelViewState>((set, get) => ({
parallelViews: [],
setParallel: (bookKeys: string[]) => {
set((state) => {
const uniqueKeys = [...new Set(bookKeys.filter((key) => key.trim() !== ''))];
if (uniqueKeys.length < 2) {
return state;
}
const newGroups = [...state.parallelViews];
const existingGroups = newGroups.filter((group) => uniqueKeys.some((key) => group.has(key)));
let targetGroup: Set<string>;
if (existingGroups.length === 0) {
targetGroup = new Set(uniqueKeys);
newGroups.push(targetGroup);
} else if (existingGroups.length === 1) {
targetGroup = existingGroups[0]!;
uniqueKeys.forEach((key) => targetGroup.add(key));
} else {
targetGroup = existingGroups[0]!;
existingGroups.slice(1).forEach((group) => {
group.forEach((key) => targetGroup.add(key));
const index = newGroups.indexOf(group);
if (index > -1) {
newGroups.splice(index, 1);
}
});
uniqueKeys.forEach((key) => targetGroup.add(key));
}
console.log('Set parallel groups:', newGroups);
return { parallelViews: newGroups };
});
},
unsetParallel: (bookKeys: string[]) => {
set((state) => {
const uniqueKeys = [...new Set(bookKeys.filter((key) => key.trim() !== ''))];
if (uniqueKeys.length === 0) {
return state;
}
const newGroups = [...state.parallelViews];
const affectedGroups = newGroups.filter((group) => uniqueKeys.some((key) => group.has(key)));
affectedGroups.forEach((group) => {
uniqueKeys.forEach((key) => group.delete(key));
if (group.size <= 1) {
const index = newGroups.indexOf(group);
if (index > -1) {
newGroups.splice(index, 1);
}
}
});
console.log('Unset parallel groups:', newGroups);
return { parallelViews: newGroups };
});
},
areParallels(bookKey1, bookKey2) {
const { parallelViews } = get();
return parallelViews.some((group) => group.has(bookKey1) && group.has(bookKey2));
},
getParallels(bookKey) {
const { parallelViews } = get();
return parallelViews.find((group) => group.has(bookKey)) || null;
},
}));
|