Spaces:
Sleeping
Sleeping
File size: 5,114 Bytes
9e42d3b | 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 | import { useSyncExternalStore } from "react";
export type AppFilters = {
q: string;
minScore: number;
maxScore: number | null;
tier: number | null;
grade: string;
subgroup: string;
consentPartner: number | null;
sortBy: "total_score" | "monetisable_value_eur";
sortOrder: "asc" | "desc";
};
export type AppPagination = {
limit: number;
offset: number;
};
export type AppFlags = {
loading: Record<string, boolean>;
errors: Record<string, string | null>;
};
export type AppState = {
currentFairId: number;
filters: AppFilters;
pagination: AppPagination;
flags: AppFlags;
};
const defaultState: AppState = {
currentFairId: 1,
filters: {
q: "",
minScore: 0,
maxScore: null,
tier: null,
grade: "",
subgroup: "",
consentPartner: null,
sortBy: "total_score",
sortOrder: "desc",
},
pagination: {
limit: 15,
offset: 0,
},
flags: {
loading: {
leads: false,
leadDetail: false,
dashboard: false,
stands: false,
},
errors: {
leads: null,
leadDetail: null,
dashboard: null,
stands: null,
},
},
};
let appState: AppState = { ...defaultState };
const listeners = new Set<() => void>();
function emitChange() {
for (const listener of listeners) {
listener();
}
}
export function getAppState(): AppState {
return appState;
}
export function setAppState(updater: Partial<AppState> | ((prev: AppState) => AppState)) {
appState = typeof updater === "function" ? updater(appState) : { ...appState, ...updater };
emitChange();
}
export function resetAppState() {
appState = { ...defaultState };
emitChange();
}
export function useAppState<T>(selector: (state: AppState) => T): T {
return useSyncExternalStore(
(listener) => {
listeners.add(listener);
return () => listeners.delete(listener);
},
() => selector(appState),
() => selector(appState),
);
}
function parseNumber(input: string | null, fallback: number): number {
if (!input) {
return fallback;
}
const n = Number(input);
return Number.isFinite(n) ? n : fallback;
}
export function hydrateFromUrl(search: URLSearchParams) {
setAppState((prev) => ({
...prev,
currentFairId: parseNumber(search.get("fair_id"), prev.currentFairId),
filters: {
...prev.filters,
q: search.get("q") ?? prev.filters.q,
minScore: parseNumber(search.get("min_score"), prev.filters.minScore),
maxScore: search.get("max_score") ? parseNumber(search.get("max_score"), 100) : null,
tier: search.get("tier") ? parseNumber(search.get("tier"), 0) : null,
grade: search.get("grade") ?? "",
subgroup: search.get("subgroup") ?? "",
consentPartner:
search.get("consent_partner") === null
? null
: parseNumber(search.get("consent_partner"), 0) >= 1
? 1
: 0,
sortBy:
search.get("sort_by") === "monetisable_value_eur"
? "monetisable_value_eur"
: "total_score",
sortOrder: search.get("sort_order") === "asc" ? "asc" : "desc",
},
pagination: {
limit: parseNumber(search.get("limit"), prev.pagination.limit),
offset: parseNumber(search.get("offset"), prev.pagination.offset),
},
}));
}
export function stateToQueryString(state: AppState): string {
const search = new URLSearchParams();
search.set("fair_id", String(state.currentFairId));
search.set("limit", String(state.pagination.limit));
search.set("offset", String(state.pagination.offset));
search.set("sort_by", state.filters.sortBy);
search.set("sort_order", state.filters.sortOrder);
if (state.filters.q) search.set("q", state.filters.q);
if (state.filters.grade) search.set("grade", state.filters.grade);
if (state.filters.subgroup) search.set("subgroup", state.filters.subgroup);
if (state.filters.minScore > 0) search.set("min_score", String(state.filters.minScore));
if (state.filters.maxScore !== null) search.set("max_score", String(state.filters.maxScore));
if (state.filters.tier !== null) search.set("tier", String(state.filters.tier));
if (state.filters.consentPartner !== null) {
search.set("consent_partner", String(state.filters.consentPartner));
}
return search.toString();
}
export function updateUrlFromState(pathname: string) {
if (typeof window === "undefined") {
return;
}
const query = stateToQueryString(appState);
const target = query.length > 0 ? `#${pathname}?${query}` : `#${pathname}`;
if (window.location.hash !== target) {
window.location.hash = target;
}
}
export function setLoadingFlag(key: keyof AppFlags["loading"], loading: boolean) {
setAppState((prev) => ({
...prev,
flags: {
...prev.flags,
loading: {
...prev.flags.loading,
[key]: loading,
},
},
}));
}
export function setErrorFlag(key: keyof AppFlags["errors"], error: string | null) {
setAppState((prev) => ({
...prev,
flags: {
...prev.flags,
errors: {
...prev.flags.errors,
[key]: error,
},
},
}));
}
|