-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreducer.ts
197 lines (184 loc) · 6.6 KB
/
reducer.ts
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { makeReducer } from "@aics/redux-utils";
import { castArray, difference, omit } from "lodash";
import interaction from "../interaction";
import { AnnotationName, PAST_YEAR_FILTER } from "../../constants";
import Annotation from "../../entity/Annotation";
import FileFilter from "../../entity/FileFilter";
import FileFolder from "../../entity/FileFolder";
import FileSelection from "../../entity/FileSelection";
import {
DESELECT_DISPLAY_ANNOTATION,
SELECT_DISPLAY_ANNOTATION,
SET_ANNOTATION_HIERARCHY,
SET_AVAILABLE_ANNOTATIONS,
SET_FILE_FILTERS,
SET_FILE_SELECTION,
SET_OPEN_FILE_FOLDERS,
RESIZE_COLUMN,
RESET_COLUMN_WIDTH,
SORT_COLUMN,
SET_SORT_COLUMN,
CHANGE_COLLECTION,
CHANGE_VIEW,
SELECT_TUTORIAL,
ADJUST_GLOBAL_FONT_SIZE,
} from "./actions";
import FileSort, { SortOrder } from "../../entity/FileSort";
import Tutorial from "../../entity/Tutorial";
import { Dataset } from "../../services/DatasetService";
export interface SelectionStateBranch {
annotationHierarchy: Annotation[];
availableAnnotationsForHierarchy: string[];
availableAnnotationsForHierarchyLoading: boolean;
collection?: Dataset;
columnWidths: {
[index: string]: number; // columnName to widthPercent mapping
};
displayAnnotations: Annotation[];
fileSelection: FileSelection;
filters: FileFilter[];
openFileFolders: FileFolder[];
shouldDisplaySmallFont: boolean;
sortColumn?: FileSort;
tutorial?: Tutorial;
}
export const initialState = {
annotationHierarchy: [],
availableAnnotationsForHierarchy: [],
availableAnnotationsForHierarchyLoading: false,
columnWidths: {
[AnnotationName.FILE_NAME]: 0.4,
[AnnotationName.KIND]: 0.2,
[AnnotationName.TYPE]: 0.25,
[AnnotationName.FILE_SIZE]: 0.15,
},
displayAnnotations: [],
fileSelection: new FileSelection(),
filters: [PAST_YEAR_FILTER],
openFileFolders: [],
shouldDisplaySmallFont: false,
sortColumn: new FileSort(AnnotationName.UPLOADED, SortOrder.DESC),
};
export default makeReducer<SelectionStateBranch>(
{
[SELECT_TUTORIAL]: (state, action) => ({
...state,
tutorial: action.payload,
}),
[ADJUST_GLOBAL_FONT_SIZE]: (state, action) => ({
...state,
shouldDisplaySmallFont: action.payload,
}),
[SET_FILE_FILTERS]: (state, action) => ({
...state,
filters: action.payload,
// Reset file selections when file filters change
fileSelection: new FileSelection(),
}),
[SORT_COLUMN]: (state, action) => {
if (state.sortColumn?.annotationName === action.payload) {
// If already sorting by this column descending
// try sorting ascending
if (state.sortColumn?.order === SortOrder.DESC) {
return {
...state,
sortColumn: new FileSort(action.payload, SortOrder.ASC),
};
}
// Otherwise already sorted ascending so remove sort
return {
...state,
sortColumn: undefined,
};
}
// Default to sorting descending on initial sort
return {
...state,
sortColumn: new FileSort(action.payload, SortOrder.DESC),
};
},
[CHANGE_COLLECTION]: (state, action) => ({
...state,
annotationHierarchy: [],
collection: action.payload,
filters: [],
fileSelection: new FileSelection(),
openFileFolders: [],
}),
[CHANGE_VIEW]: (state) => ({
...state,
fileSelection: new FileSelection(),
openFileFolders: [],
}),
[SET_SORT_COLUMN]: (state, action) => ({
...state,
sortColumn: action.payload,
}),
[DESELECT_DISPLAY_ANNOTATION]: (state, action) => {
// remove deselected annotation from state.displayAnnotations
const displayAnnotations = state.displayAnnotations.filter(
(annotation) => annotation.name !== action.payload.annotation.name
);
const columnWidthsToPrune = difference(
Object.keys(state.columnWidths),
displayAnnotations.map((annotation) => annotation.name)
);
return {
...state,
displayAnnotations,
columnWidths: omit(state.columnWidths, columnWidthsToPrune),
};
},
[interaction.actions.REFRESH]: (state) => ({
...state,
availableAnnotationsForHierarchyLoading: true,
fileSelection: new FileSelection(),
}),
[RESET_COLUMN_WIDTH]: (state, action) => ({
...state,
columnWidths: omit(state.columnWidths, [action.payload]),
}),
[RESIZE_COLUMN]: (state, action) => ({
...state,
columnWidths: {
...state.columnWidths,
[action.payload.columnHeader]: action.payload.widthPercent,
},
}),
[SELECT_DISPLAY_ANNOTATION]: (state, action) => {
const displayAnnotations = action.payload.replace
? castArray(action.payload.annotation)
: [...state.displayAnnotations, ...castArray(action.payload.annotation)];
return {
...state,
displayAnnotations,
};
},
[SET_FILE_SELECTION]: (state, action) => ({
...state,
fileSelection: action.payload,
}),
[SET_ANNOTATION_HIERARCHY]: (state, action) => ({
...state,
annotationHierarchy: action.payload,
availableAnnotationsForHierarchyLoading: true,
// Reset file selections when annotation hierarchy changes
fileSelection: new FileSelection(),
}),
[SET_AVAILABLE_ANNOTATIONS]: (state, action) => ({
...state,
availableAnnotationsForHierarchy: action.payload,
availableAnnotationsForHierarchyLoading: false,
}),
[SET_OPEN_FILE_FOLDERS]: (state, action) => ({
...state,
openFileFolders: action.payload,
}),
[interaction.actions.SET_FILE_EXPLORER_SERVICE_BASE_URL]: (state) => ({
...state,
// Reset file selections when pointed at a new backend
fileSelection: new FileSelection(),
}),
},
initialState
);