-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiffa
317 lines (308 loc) · 10.4 KB
/
diffa
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
diff --git a/src/components/aspirante/filter-bar.tsx b/src/components/aspirante/filter-bar.tsx
index 2bb4611..62129f7 100644
--- a/src/components/aspirante/filter-bar.tsx
+++ b/src/components/aspirante/filter-bar.tsx
@@ -17,70 +17,145 @@ import {
import type { AspiranteFilters } from "@/lib/data/aspirantes";
import { ComboboxFilter } from "@/components/combobox-filter";
-// Type representing the filter keys available for aspirantes
-type ComboKey = keyof AspiranteFilters;
+type ComboKey = Exclude<keyof AspiranteFilters, "nombre">;
-// State type for controlling combobox visibility and behavior
type ComboState = {
- isToggled: boolean; // Whether the combobox should be shown
- isFixed: boolean; // Whether the value is locked and cannot be changed
- fixedValue?: string; // The locked value when isFixed is true
+ isToggled: boolean;
+ fixedValue: string | null;
};
-// Configuration for all filter comboboxes
-const combos: Array<{
- key: ComboKey;
+type ComboConfig<K extends ComboKey = ComboKey> = {
+ key: K;
label: string;
+ dependsOn?: ComboKey;
items: Array<{ value: string; label: string }>;
-}> = [
- {
- key: "titulo",
- label: "puesto",
- items: Object.entries(judicatura.titulos).map(([key, value]) => ({
- value: key as TituloKey,
- label: `${v.capitalize(value.singular.F)} / ${value.singular.M}`,
- })),
- },
- {
- key: "organo",
+ getState: (filters: Record<ComboKey, string>) => ComboState;
+};
+
+type WithNombre = { kind: "withNombre"; nombre: string };
+type WithTitulo = { kind: "withTitulo"; singular: { F: string; M: string } };
+type WithValue = { kind: "withValue"; value: string };
+type ItemRecord = WithNombre | WithTitulo | WithValue;
+
+const createItemsWithNombre = <T extends string>(
+ obj: Record<T, { nombre: string }>,
+): Record<T, WithNombre> =>
+ Object.fromEntries(
+ Object.entries(obj).map(([k, v]) => [
+ k,
+ { kind: "withNombre", nombre: v.nombre },
+ ]),
+ ) as Record<T, WithNombre>;
+
+const createItems = <T extends string>(
+ obj: Record<T, ItemRecord>,
+ getLabel: (value: ItemRecord) => string,
+): Array<{ value: T; label: string }> =>
+ (Object.entries(obj) as Array<[T, ItemRecord]>).map(([key, value]) => ({
+ value: key,
+ label: getLabel(value),
+ }));
+
+const createDefaultState = (isToggled: boolean) => () => ({
+ isToggled,
+ fixedValue: null,
+});
+
+const createTepjfState =
+ (isToggled: (filters: Record<ComboKey, string>) => boolean) =>
+ (filters: Record<ComboKey, string>): ComboState => ({
+ isToggled: isToggled(filters),
+ fixedValue: null,
+ });
+
+const createTituloState = (filters: Record<ComboKey, string>): ComboState => ({
+ isToggled: true,
+ fixedValue: filters.organo
+ ? judicatura.organos[filters.organo as OrganoKey].titulo
+ : null,
+});
+
+const comboConfigs = {
+ organo: {
+ key: "organo" as const,
label: "órgano",
- items: Object.entries(judicatura.organos).map(([key, value]) => ({
- value: key as OrganoKey,
- label: value.nombre,
- })),
+ items: createItems<OrganoKey, OrganoKey>(
+ createItemsWithNombre(judicatura.organos),
+ (item): string => (item.kind === "withNombre" ? item.nombre : ""),
+ ),
+ getState: createDefaultState(true),
+ },
+ titulo: {
+ key: "titulo" as const,
+ label: "puesto",
+ dependsOn: "organo",
+ items: createItems<TituloKey, TituloKey>(
+ Object.fromEntries(
+ Object.entries(judicatura.titulos).map(([k, v]) => [
+ k,
+ { kind: "withTitulo", singular: v.singular },
+ ]),
+ ) as Record<TituloKey, WithTitulo>,
+ (item): string =>
+ item.kind === "withTitulo"
+ ? `${v.capitalize(item.singular.F)} / ${item.singular.M}`
+ : "",
+ ),
+ getState: createTituloState,
},
- {
- key: "sala",
+ sala: {
+ key: "sala" as const,
label: "sala",
- items: Object.entries(judicaturaData.organos.tepjf.salas).map(
- ([key, value]) => ({
- value: key as SalaKey,
- label: value.nombre,
- }),
+ dependsOn: "organo",
+ items: createItems<SalaKey, SalaKey>(
+ createItemsWithNombre(judicaturaData.organos.tepjf.salas),
+ (item): string => (item.kind === "withNombre" ? item.nombre : ""),
),
+ getState: createTepjfState((filters) => filters.organo === "tepjf"),
},
- {
- key: "entidad",
+ entidad: {
+ key: "entidad" as const,
label: "estado",
- items: Object.entries(judicatura.entidades).map(([key, value]) => ({
- value: key as EntidadKey,
- label: value,
- })),
+ dependsOn: "sala",
+ items: createItems<EntidadKey, EntidadKey>(
+ Object.fromEntries(
+ Object.entries(judicatura.entidades).map(([k, v]) => [
+ k,
+ { kind: "withValue", value: v },
+ ]),
+ ) as Record<EntidadKey, WithValue>,
+ (item): string => (item.kind === "withValue" ? item.value : ""),
+ ),
+ getState: createTepjfState(
+ (filters) =>
+ filters.organo !== "tepjf" ||
+ !filters.sala ||
+ judicaturaData.organos.tepjf.salas[filters.sala as SalaKey]
+ .entidades !== null,
+ ),
},
- {
- key: "circuito",
+ circuito: {
+ key: "circuito" as const,
label: "circuito",
- items: Object.entries(judicatura.circuitos).map(([key, value]) => ({
- value: key as CircuitoKey,
- label: value.nombre,
- })),
+ dependsOn: "organo",
+ items: createItems<CircuitoKey, CircuitoKey>(
+ createItemsWithNombre(judicatura.circuitos),
+ (item): string => (item.kind === "withNombre" ? item.nombre : ""),
+ ),
+ getState: createTepjfState((filters) => filters.organo !== "tepjf"),
},
-] as const;
+} as const;
+
+const combos = Object.values(comboConfigs) as ReadonlyArray<ComboConfig>;
+
+const filterDependencies = Object.fromEntries(
+ combos
+ .filter((c): c is ComboConfig & { dependsOn: ComboKey } =>
+ Boolean(c.dependsOn),
+ )
+ .map((c) => [c.dependsOn, [c.key]]),
+) as Record<ComboKey, ComboKey[]>;
-/**
- * Filter bar component for aspirantes search
- * Displays a series of comboboxes for filtering aspirantes by different criteria
- */
export function AspiranteFilterBar({
filters,
}: Readonly<{ filters: AspiranteFilters }>) {
@@ -88,85 +163,27 @@ export function AspiranteFilterBar({
const pathname = usePathname();
const searchParams = useSearchParams();
- // Merge URL search params with provided filters, preferring URL params
- const currentFilters = useMemo(() => {
- return Object.fromEntries(
- combos.map((combo) => [
- combo.key,
- searchParams.get(combo.key) ?? filters[combo.key] ?? "",
- ]),
- ) as Record<ComboKey, string>;
- }, [filters, searchParams]);
-
- // Determine visibility and behavior state for each combobox based on current filters
- const getComboState = useCallback(
- (key: ComboKey): ComboState => {
- const selectedOrgano = currentFilters.organo;
-
- switch (key) {
- case "titulo":
- // Lock titulo to the organo's required title if an organo is selected
- if (selectedOrgano) {
- const organoData = judicatura.organos[selectedOrgano as OrganoKey];
- return {
- isToggled: true,
- isFixed: true,
- fixedValue: organoData.titulo,
- };
- }
- return { isToggled: true, isFixed: false };
-
- case "sala":
- // Only show sala filter for TEPJF
- if (selectedOrgano === "tepjf") {
- return { isToggled: true, isFixed: false };
- }
- return { isToggled: false, isFixed: false };
-
- case "entidad":
- // Hide entidad filter for certain TEPJF salas that don't have entidades
- if (selectedOrgano === "tepjf" && currentFilters.sala) {
- const salaData =
- judicaturaData.organos.tepjf.salas[
- currentFilters.sala as SalaKey
- ];
- if (salaData.entidades === null) {
- return { isToggled: false, isFixed: false };
- }
- }
- return { isToggled: true, isFixed: false };
-
- case "circuito":
- // Hide circuito filter for TEPJF
- if (selectedOrgano === "tepjf") {
- return { isToggled: false, isFixed: false };
- }
- return { isToggled: true, isFixed: false };
-
- default:
- return { isToggled: true, isFixed: false };
- }
- },
- [currentFilters],
+ const getCurrentValue = useCallback(
+ (key: ComboKey) => searchParams.get(key) ?? filters[key] ?? "",
+ [searchParams, filters],
+ );
+
+ const currentFilters = useMemo(
+ () =>
+ Object.fromEntries(
+ combos.map((c) => [c.key, getCurrentValue(c.key)]),
+ ) as Record<ComboKey, string>,
+ [getCurrentValue],
);
- // Update URL search params when filters change
const updateFilters = useCallback(
- (updates: Array<[keyof AspiranteFilters, string]>) => {
+ (updates: Array<[ComboKey, string]>) => {
const params = new URLSearchParams(searchParams);
- // Clear dependent filters when parent filter changes
updates.forEach(([key, value]) => {
- if (key === "organo") {
- // Clear dependent filters when organo changes
- params.delete("sala");
- params.delete("circuito");
- if (value !== "tepjf") {
- params.delete("sala");
- }
- } else if (key === "sala") {
- // Clear dependent filters when sala changes
- params.delete("circuito");
+ const dependents = filterDependencies[key];
+ if (dependents) {
+ dependents.forEach((dep) => params.delete(dep));
}
if (value) {
@@ -185,14 +202,14 @@ export function AspiranteFilterBar({
return (
<div className="mb-6 flex flex-wrap gap-4">
{combos.map((combo) => {
- const state = getComboState(combo.key);
+ const state = combo.getState(currentFilters);
if (!state.isToggled) return null;
return (
<ComboboxFilter
key={combo.key}
- value={state.isFixed ? state.fixedValue : currentFilters[combo.key]}
- disabled={state.isFixed}
+ value={state.fixedValue ?? currentFilters[combo.key]}
+ disabled={state.fixedValue !== null}
items={combo.items}
onChange={(value: string) => updateFilters([[combo.key, value]])}
placeholder={`Filtrar por ${combo.label}`}