|
1 | 1 | import { create } from 'zustand' |
2 | 2 | import { ModelDirectory, type ModelFile } from '../models' |
| 3 | +import { type ModelArtifact } from '@models/artifact' |
3 | 4 |
|
4 | 5 | interface ProjectStore { |
| 6 | + activeRange: ModelArtifact[] |
| 7 | + setActiveRange: (activeRange?: ModelArtifact[]) => void |
5 | 8 | project: ModelDirectory |
6 | 9 | setProject: (project?: ModelDirectory) => void |
7 | 10 | files: Map<ID, ModelFile> |
8 | 11 | setFiles: (files: ModelFile[]) => void |
9 | 12 | selectedFile?: ModelFile |
10 | 13 | setSelectedFile: (selectedFile?: ModelFile) => void |
| 14 | + findArtifactByPath: (path: string) => ModelArtifact | undefined |
| 15 | + findParentByPath: (path: string) => ModelDirectory | undefined |
| 16 | + refreshFiles: () => void |
| 17 | + inActiveRange: (artifact: ModelArtifact) => boolean |
11 | 18 | } |
12 | 19 |
|
13 | 20 | export const useStoreProject = create<ProjectStore>((set, get) => ({ |
14 | | - selectedFile: undefined, |
15 | 21 | project: new ModelDirectory(), |
| 22 | + selectedFile: undefined, |
16 | 23 | files: new Map(), |
| 24 | + activeRange: [], |
| 25 | + setActiveRange(activeRange) { |
| 26 | + const s = get() |
| 27 | + |
| 28 | + set(() => ({ |
| 29 | + activeRange: s.project.allVisibleArtifacts.filter(artifact => |
| 30 | + inActiveRange(artifact, activeRange ?? s.activeRange), |
| 31 | + ), |
| 32 | + })) |
| 33 | + }, |
| 34 | + inActiveRange(artifact) { |
| 35 | + const s = get() |
| 36 | + |
| 37 | + return inActiveRange(artifact, s.activeRange) |
| 38 | + }, |
17 | 39 | setProject(project) { |
18 | 40 | set(() => ({ |
19 | 41 | project, |
20 | 42 | })) |
21 | 43 | }, |
22 | 44 | setFiles(files) { |
| 45 | + const s = get() |
| 46 | + |
| 47 | + s.files.clear() |
| 48 | + |
23 | 49 | set(() => ({ |
24 | | - files: files.reduce((acc, file) => acc.set(file.id, file), new Map()), |
| 50 | + files: new Map( |
| 51 | + files.reduce((acc, file) => acc.set(file.id, file), s.files), |
| 52 | + ), |
25 | 53 | })) |
26 | 54 | }, |
27 | 55 | setSelectedFile(selectedFile) { |
28 | 56 | set(() => ({ |
29 | 57 | selectedFile, |
30 | 58 | })) |
31 | 59 | }, |
| 60 | + findArtifactByPath(path) { |
| 61 | + const s = get() |
| 62 | + |
| 63 | + return ModelDirectory.findArtifactByPath(s.project, path) |
| 64 | + }, |
| 65 | + findParentByPath(path) { |
| 66 | + const s = get() |
| 67 | + |
| 68 | + return ModelDirectory.findParentByPath(s.project, path) |
| 69 | + }, |
| 70 | + refreshFiles() { |
| 71 | + const s = get() |
| 72 | + |
| 73 | + s.setFiles(s.project.allFiles) |
| 74 | + }, |
32 | 75 | })) |
| 76 | + |
| 77 | +function inActiveRange( |
| 78 | + artifact: ModelArtifact, |
| 79 | + activeRange: ModelArtifact[], |
| 80 | +): boolean { |
| 81 | + return activeRange.includes(artifact) |
| 82 | +} |
0 commit comments