| 1 | /** Client-only favorite quest pin (one active quest at a time). */ |
| 2 | |
| 3 | const STORAGE_KEY = "questward-favorite-quest"; |
| 4 | |
| 5 | /** |
| 6 | * @returns {Record<string, string>} |
| 7 | */ |
| 8 | function readMap () |
| 9 | { |
| 10 | try |
| 11 | { |
| 12 | const raw = localStorage.getItem(STORAGE_KEY); |
| 13 | if (!raw) |
| 14 | { return {}; } |
| 15 | const parsed = JSON.parse(raw); |
| 16 | if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) |
| 17 | { return {}; } |
| 18 | /** @type {Record<string, string>} */ |
| 19 | const out = {}; |
| 20 | for (const [key, value] of Object.entries(parsed)) |
| 21 | { |
| 22 | if (typeof value === "string" && value.trim()) |
| 23 | { out[String(key)] = value.trim(); } |
| 24 | } |
| 25 | return out; |
| 26 | } |
| 27 | catch |
| 28 | { return {}; } |
| 29 | } |
| 30 | |
| 31 | function writeMap (map) |
| 32 | { |
| 33 | try |
| 34 | { |
| 35 | localStorage.setItem(STORAGE_KEY, JSON.stringify(map)); |
| 36 | } |
| 37 | catch |
| 38 | { /* ignore quota / private mode */ } |
| 39 | } |
| 40 | |
| 41 | export function loadFavoriteQuestId (characterId) |
| 42 | { |
| 43 | const id = characterId | 0; |
| 44 | if (!id) |
| 45 | { return null; } |
| 46 | return readMap()[String(id)] ?? null; |
| 47 | } |
| 48 | |
| 49 | export function saveFavoriteQuestId (characterId, questId) |
| 50 | { |
| 51 | const id = characterId | 0; |
| 52 | if (!id) |
| 53 | { return; } |
| 54 | const map = readMap(); |
| 55 | const next = String(questId ?? "").trim(); |
| 56 | if (!next) |
| 57 | { delete map[String(id)]; } |
| 58 | else |
| 59 | { map[String(id)] = next; } |
| 60 | writeMap(map); |
| 61 | } |
| 62 | |
| 63 | export function clearFavoriteQuestId (characterId) |
| 64 | { |
| 65 | saveFavoriteQuestId(characterId, null); |
| 66 | } |