fix: deduplicate shared fishing plans

This commit is contained in:
ik
2026-09-21 18:15:36 +07:00
parent 2d56a67461
commit d32a1973b2
3 changed files with 26 additions and 3 deletions
+7 -2
View File
@@ -16,10 +16,15 @@ import Layout from "../layouts/Layout.astro";
const planText = (key: string, value: unknown) => typeof value === "string" ? value.slice(0, planFieldLimits[key] ?? 240) : "";
const normalisePlan = (value: unknown): Array<Record<string, string>> => {
if (!Array.isArray(value)) return [];
return value.filter(item => item && typeof item === "object" && typeof item.key === "string" && item.key.startsWith("/spots/")).slice(0, 5).map(item => {
const seen = new Set<string>();
return value.filter(item => item && typeof item === "object" && typeof item.key === "string" && item.key.startsWith("/spots/")).map(item => {
const source = item as Record<string, unknown>;
return Object.fromEntries(["key", "waterbody", "coordinates", "baits", "method", "retrieve", "risk", "freshness", "confidence"].map(key => [key, planText(key, source[key])]));
});
}).filter(item => {
if (seen.has(item.key)) return false;
seen.add(item.key);
return true;
}).slice(0, 5);
};
const readPlan = (): Array<Record<string, string>> => {
try { return normalisePlan(JSON.parse(localStorage.getItem(planStorageKey) || "[]")); }