Skip to content

Commit 382c94e

Browse files
authored
[Comp-800-2] default toggle for non-officers (#741)
1 parent 863793a commit 382c94e

File tree

5 files changed

+112
-89
lines changed

5 files changed

+112
-89
lines changed

compliance-web/src/routes/_authenticated/ce-database/case-files/index.tsx

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ export function CaseFiles() {
6868
Record<string, string[] | string>
6969
>({});
7070

71-
// State for "My Files" switch - default to true for first-time users
72-
const [myFilesChecked, setMyFilesChecked] = useState(true);
71+
// State for "My Files" switch
72+
const [myFilesChecked, setMyFilesChecked] = useState(false);
7373
const [isRestored, setIsRestored] = useState(false);
7474

7575
// Ref to track if filters have been initialized
7676
const filtersInitialized = useRef(false);
7777

7878
// Get cached filters store methods
79-
const { getFilters, getExternalFilters, getSorting } = cachedFiltersStore();
79+
const { getFilters, getExternalFilters, getSorting, hasHydrated } = cachedFiltersStore();
8080
const cachedColumnFilters = getFilters(caseFilesColumnFiltersCacheKey);
8181
const cachedExternalFilters = getExternalFilters(
8282
caseFilesColumnFiltersCacheKey
@@ -92,11 +92,9 @@ export function CaseFiles() {
9292

9393
// Restore cached filters on component mount
9494
useEffect(() => {
95-
// Prevent re-initialization if already done
96-
if (filtersInitialized.current) return;
97-
98-
// Wait for currentStaff to be available before initializing
99-
if (!currentStaff) return;
95+
if (!hasHydrated) return; // Wait for store to hydrate
96+
if (filtersInitialized.current) return; // Prevent re-initialization if already don
97+
if (!currentStaff) return; // Wait for currentStaff to be available before initializing
10098

10199
let restoredFilters = false;
102100

@@ -106,8 +104,12 @@ export function CaseFiles() {
106104
restoredFilters = true;
107105
}
108106

107+
const hasCachedExternalFilters =
108+
cachedExternalFilters &&
109+
Object.keys(cachedExternalFilters).length > 0;
110+
109111
// Restore external filters
110-
if (cachedExternalFilters) {
112+
if (hasCachedExternalFilters) {
111113
const restoredExternalFilters = cachedExternalFilters as Record<
112114
string,
113115
string[] | string
@@ -165,12 +167,15 @@ export function CaseFiles() {
165167
const defaultChecked = Boolean(currentStaff.position_id &&
166168
officerPositions.includes(currentStaff.position_id));
167169

168-
const defaultExternalFilters = {
169-
primary_officer_ids: [defaultChecked ? currentStaff.id.toString() : ""],
170-
};
170+
const defaultExternalFilters: Record<string, string[] | string> = defaultChecked
171+
? { primary_officer_ids: [currentStaff.id.toString()] }
172+
: {};
173+
171174
const defaultColumnFilters = [
172-
{ id: "primary_officer", value: [defaultChecked ? currentStaff.id.toString() : ""] },
173175
{ id: "status", value: ["Open"] },
176+
...(defaultChecked
177+
? [{ id: "primary_officer", value: [currentStaff.id.toString()] }]
178+
: []),
174179
];
175180

176181
setExternalFilters(defaultExternalFilters);
@@ -182,17 +187,14 @@ export function CaseFiles() {
182187
if (cachedSorting && cachedSorting.length > 0 && cachedSorting[0]?.id) {
183188
setSorting(cachedSorting);
184189
}
185-
186-
// Mark initialization complete
187-
setTimeout(() => {
188-
filtersInitialized.current = true;
189-
setIsRestored(true);
190-
}, 0);
190+
filtersInitialized.current = true;
191+
setIsRestored(true);
191192
}, [
192193
cachedColumnFilters,
193194
cachedExternalFilters,
194195
cachedSorting,
195196
currentStaff,
197+
hasHydrated,
196198
]);
197199

198200
// Sync "My Files" toggle with primary_officer filter

compliance-web/src/routes/_authenticated/ce-database/complaints/index.tsx

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -99,46 +99,48 @@ export function Complaints() {
9999
Record<string, string[] | string>
100100
>({});
101101

102-
const [myFilesChecked, setMyFilesChecked] = useState(true);
102+
const [myFilesChecked, setMyFilesChecked] = useState(false);
103103

104104
// Ref to track if filters have been initialized
105105
const filtersInitialized = useRef(false);
106106
const [isRestored, setIsRestored] = useState(false);
107107

108108
// Get cached filters store methods
109-
const { getFilters, getExternalFilters, getSorting } = cachedFiltersStore();
109+
const { getFilters, getExternalFilters, getSorting, hasHydrated } = cachedFiltersStore();
110110

111-
const cachedData = useMemo(() => {
112-
return {
113-
columnFilters: getFilters(complaintsColumnFiltersCacheKey),
114-
externalFilters: getExternalFilters(complaintsColumnFiltersCacheKey),
115-
sorting: getSorting(complaintsColumnFiltersCacheKey),
116-
};
117-
}, [getFilters, getExternalFilters, getSorting]);
111+
const cachedColumnFilters = getFilters(complaintsColumnFiltersCacheKey);
112+
const cachedExternalFilters = getExternalFilters(complaintsColumnFiltersCacheKey);
113+
const cachedSorting = getSorting(complaintsColumnFiltersCacheKey);
118114

119115
const currentStaff = useMemo(() => {
120116
return getCurrentStaff(currentUser, staffList);
121117
}, [currentUser, staffList]);
122118

123119
useEffect(() => {
124120
// Don't initialize if already done, or if it isn't loaded yet
125-
if (filtersInitialized.current || authLoading || staffLoading || !currentStaff) {
121+
if (!hasHydrated || filtersInitialized.current || authLoading || staffLoading || !currentStaff) {
126122
return;
127123
}
128124

129125
filtersInitialized.current = true;
130126

131-
const hasCache = cachedData.columnFilters.length > 0 ||
132-
(cachedData.externalFilters && Object.keys(cachedData.externalFilters).length > 0);
127+
const hasCache =
128+
(Array.isArray(cachedColumnFilters) &&
129+
cachedColumnFilters.length > 0) ||
130+
(cachedExternalFilters &&
131+
Object.keys(cachedExternalFilters).length > 0) ||
132+
(cachedSorting &&
133+
Array.isArray(cachedSorting) &&
134+
cachedSorting.length > 0);
133135

134136
if (hasCache) {
135137
// Restore from cache
136-
if (cachedData.columnFilters.length > 0) {
137-
setColumnFilters(cachedData.columnFilters);
138+
if (cachedColumnFilters.length > 0) {
139+
setColumnFilters(cachedColumnFilters);
138140
}
139141

140-
if (cachedData.externalFilters) {
141-
const restored = cachedData.externalFilters as Record<string, string[] | string>;
142+
if (cachedExternalFilters) {
143+
const restored = cachedExternalFilters as Record<string, string[] | string>;
142144
setExternalFilters(restored);
143145

144146
if (restored.globalFilter) {
@@ -149,15 +151,17 @@ export function Complaints() {
149151
setMyFilesChecked(Boolean(restored.myFilesChecked));
150152
} else {
151153
// Get from primary officer filter
152-
const primaryOfficer = restored.primary_officer_ids || [];
153-
const derivedState = Array.isArray(primaryOfficer) && primaryOfficer.length > 0;
154+
const primaryOfficer = restored.primary_officer_ids;
155+
const derivedState =
156+
Array.isArray(primaryOfficer) &&
157+
primaryOfficer.some((id) => Boolean(id));
154158
setMyFilesChecked(derivedState);
155159
}
156160
}
157161

158-
if (cachedData.sorting && Array.isArray(cachedData.sorting) && cachedData.sorting.length > 0) {
159-
if (cachedData.sorting[0]?.id) {
160-
setSorting(cachedData.sorting);
162+
if (cachedSorting && Array.isArray(cachedSorting) && cachedSorting.length > 0) {
163+
if (cachedSorting[0]?.id) {
164+
setSorting(cachedSorting);
161165
}
162166
}
163167
} else {
@@ -176,7 +180,7 @@ export function Complaints() {
176180
}
177181

178182
setIsRestored(true);
179-
}, [authLoading, staffLoading, currentStaff, cachedData]);
183+
}, [authLoading, staffLoading, currentStaff, cachedColumnFilters, cachedExternalFilters, cachedSorting, hasHydrated]);
180184

181185
// Debounced cache persistence - only after initialization
182186
const cacheTimeoutRef = useRef<NodeJS.Timeout>();

compliance-web/src/routes/_authenticated/ce-database/inspections/index.tsx

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const createDefaultFilters = (staffId: string, defaultMyChecked: boolean): {
5656
},
5757
columnFilters: [
5858
{
59-
id: "primary_officer_ids",
59+
id: "primary_officer",
6060
value: [staffId],
6161
},
6262
],
@@ -93,7 +93,7 @@ export function Inspections() {
9393
);
9494

9595
// Initialize state with functions to avoid re-computation
96-
const [myInspectionsChecked, setMyInspectionsChecked] = useState(true);
96+
const [myInspectionsChecked, setMyInspectionsChecked] = useState(false);
9797
const [sorting, setSorting] = useState<MRT_SortingState>(() => [
9898
{ id: "start_date", desc: true },
9999
]);
@@ -118,16 +118,11 @@ export function Inspections() {
118118
const [isRestored, setIsRestored] = useState(false);
119119

120120
// Get cached filters store methods
121-
const { getFilters, getExternalFilters, getSorting } = cachedFiltersStore();
121+
const { getFilters, getExternalFilters, getSorting, hasHydrated } = cachedFiltersStore();
122122

123-
// Memoize cached values to prevent unnecessary re-reads
124-
const cachedData = useMemo(() => {
125-
return {
126-
columnFilters: getFilters(inspectionsColumnFiltersCacheKey),
127-
externalFilters: getExternalFilters(inspectionsColumnFiltersCacheKey),
128-
sorting: getSorting(inspectionsColumnFiltersCacheKey),
129-
};
130-
}, [getFilters, getExternalFilters, getSorting]);
123+
const cachedColumnFilters = getFilters(inspectionsColumnFiltersCacheKey);
124+
const cachedExternalFilters = getExternalFilters(inspectionsColumnFiltersCacheKey);
125+
const cachedSorting = getSorting(inspectionsColumnFiltersCacheKey);
131126

132127
const currentStaff = useMemo(() => {
133128
return getCurrentStaff(currentUser, staffList);
@@ -136,23 +131,29 @@ export function Inspections() {
136131
// Initialization effect
137132
useEffect(() => {
138133
// Don't initialize if already done, or if it isn't loaded yet
139-
if (filtersInitialized.current || authLoading || staffLoading || !currentStaff) {
134+
if (!hasHydrated || filtersInitialized.current || authLoading || staffLoading || !currentStaff) {
140135
return;
141136
}
142137

143138
filtersInitialized.current = true;
144139

145-
const hasCache = cachedData.columnFilters.length > 0 ||
146-
(cachedData.externalFilters && Object.keys(cachedData.externalFilters).length > 0);
140+
const hasCache =
141+
(Array.isArray(cachedColumnFilters) &&
142+
cachedColumnFilters.length > 0) ||
143+
(cachedExternalFilters &&
144+
Object.keys(cachedExternalFilters).length > 0) ||
145+
(cachedSorting &&
146+
Array.isArray(cachedSorting) &&
147+
cachedSorting.length > 0);
147148

148149
if (hasCache) {
149150
// Restore from cache
150-
if (cachedData.columnFilters.length > 0) {
151-
setColumnFilters(cachedData.columnFilters);
151+
if (cachedColumnFilters.length > 0) {
152+
setColumnFilters(cachedColumnFilters);
152153
}
153154

154-
if (cachedData.externalFilters) {
155-
const restored = cachedData.externalFilters as Record<string, string[] | string>;
155+
if (cachedExternalFilters) {
156+
const restored = cachedExternalFilters as Record<string, string[] | string>;
156157
setExternalFilters(restored);
157158

158159
if (restored.globalFilter) {
@@ -164,15 +165,17 @@ export function Inspections() {
164165
setMyInspectionsChecked(Boolean(restored.myInspectionsChecked));
165166
} else {
166167
// Get from primary_officer filter
167-
const primaryOfficer = restored.primary_officer_ids || [];
168-
const derivedState = Array.isArray(primaryOfficer) && primaryOfficer.length > 0;
168+
const primaryOfficer = restored.primary_officer_ids;
169+
const derivedState =
170+
Array.isArray(primaryOfficer) &&
171+
primaryOfficer.some((id) => Boolean(id));
169172
setMyInspectionsChecked(derivedState);
170173
}
171174
}
172175

173-
if (cachedData.sorting && Array.isArray(cachedData.sorting) && cachedData.sorting.length > 0) {
174-
if (cachedData.sorting[0]?.id) {
175-
setSorting(cachedData.sorting);
176+
if (cachedSorting && Array.isArray(cachedSorting) && cachedSorting.length > 0) {
177+
if (cachedSorting[0]?.id) {
178+
setSorting(cachedSorting);
176179
}
177180
}
178181
} else {
@@ -191,7 +194,7 @@ export function Inspections() {
191194
}
192195

193196
setIsRestored(true);
194-
}, [authLoading, staffLoading, currentStaff, cachedData]);
197+
}, [authLoading, staffLoading, currentStaff, cachedColumnFilters, cachedExternalFilters, cachedSorting, hasHydrated]);
195198

196199
// Debounced cache persistence - only after initialization
197200
const cacheTimeoutRef = useRef<NodeJS.Timeout>();
@@ -281,7 +284,6 @@ export function Inspections() {
281284
(filter) => filter.id === "primary_officer"
282285
);
283286

284-
// If user removed the primary_officer filter, re-add it
285287
if (!hasPrimaryOfficerFilter) {
286288
const primaryOfficerFilter = {
287289
id: "primary_officer",
@@ -294,7 +296,7 @@ export function Inspections() {
294296
return newFilters;
295297
});
296298
},
297-
[myInspectionsChecked, currentStaff]
299+
[currentStaff, myInspectionsChecked]
298300
);
299301

300302
// Optimize My Inspections switch handler
@@ -304,7 +306,6 @@ export function Inspections() {
304306
externalFilters: Record<string, string[] | string>;
305307
columnFilters?: MRT_TableState<Inspection>["columnFilters"];
306308
}) => {
307-
// Batch state updates
308309
setMyInspectionsChecked(filters.checked);
309310
setExternalFilters(filters.externalFilters);
310311
setPagination((prev) => ({ ...prev, pageIndex: 0 }));

0 commit comments

Comments
 (0)