fix: deduplicate shared fishing plans
This commit is contained in:
@@ -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) || "[]")); }
|
||||
|
||||
Reference in New Issue
Block a user