refactor(frontend): extract reusable hooks and page helper modules

This commit is contained in:
Sagit
2026-02-19 05:14:11 +00:00
parent 6d57b49595
commit c5124a01e6
24 changed files with 2072 additions and 0 deletions
+57
View File
@@ -0,0 +1,57 @@
const parseOrderIds = (rawValue: string | null): number[] | null => {
if (!rawValue) {
return null;
}
try {
const parsed = JSON.parse(rawValue);
if (!Array.isArray(parsed)) {
return null;
}
return parsed
.map((id) => Number(id))
.filter((id) => Number.isInteger(id) && id >= 0);
} catch {
return null;
}
};
export const loadStoredOrder = (
storageKey: string,
sourceIds: number[],
): number[] => {
const parsed = parseOrderIds(localStorage.getItem(storageKey));
if (!parsed || parsed.length === 0) {
return sourceIds;
}
const idSet = new Set(sourceIds);
const validIds: number[] = [];
parsed.forEach((id) => {
if (idSet.has(id) && !validIds.includes(id)) {
validIds.push(id);
}
});
if (validIds.length === 0) {
return sourceIds;
}
sourceIds.forEach((id) => {
if (!validIds.includes(id)) {
validIds.push(id);
}
});
return validIds;
};
export const saveOrder = (storageKey: string, ids: number[]): void => {
try {
localStorage.setItem(storageKey, JSON.stringify(ids));
} catch {}
};