All files / util/bookmarks/src bookmarkShortcuts.ts

87.23% Statements 41/47
93.1% Branches 27/29
90% Functions 9/10
85% Lines 34/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 8421x 21x     20x 3x   20x 20x 20x 20x 20x         13x     21x                     280x       70x 70x     68x 68x 272x 272x   68x                                 10x 1x   9x 9x 7x 9x 9x 9x       9x 9x 36x 9x 6x 5x 5x          
export const BOOKMARK_SHORTCUTS = "BOOKMARK_SHORTCUTS_V1";
const SHORTCUT_CHANGE_EVENT = "trimet-bookmark-shortcuts-changed";
 
export function subscribeBookmarkShortcuts(listener: () => void): () => void {
  const storageChanged = (event: StorageEvent) => {
    if (event.key === BOOKMARK_SHORTCUTS || event.key === null) listener();
  };
  window.addEventListener("storage", storageChanged);
  window.addEventListener(SHORTCUT_CHANGE_EVENT, listener);
  return () => {
    window.removeEventListener("storage", storageChanged);
    window.removeEventListener(SHORTCUT_CHANGE_EVENT, listener);
  };
}
 
function notifyShortcutChange() {
  Eif (typeof window !== "undefined") window.dispatchEvent(new Event(SHORTCUT_CHANGE_EVENT));
}
 
export const SHORTCUTS = [
  { key: "home_location", label: "Home stop", kind: "stop" },
  { key: "work_location", label: "Work stop", kind: "stop" },
  { key: "home_commute", label: "Home commute", kind: "route" },
  { key: "work_commute", label: "Work commute", kind: "route" }
] as const;
 
export type ShortcutKey = typeof SHORTCUTS[number]["key"];
export type BookmarkShortcuts = Partial<Record<ShortcutKey, number>>;
 
function isBookmarkId(value: unknown): value is number {
  return typeof value === "number" && Number.isSafeInteger(value) && value > 0;
}
 
export function readBookmarkShortcuts(): BookmarkShortcuts {
  const stored: unknown = JSON.parse(localStorage.getItem(BOOKMARK_SHORTCUTS) ?? "{}");
  Iif (!stored || typeof stored !== "object" || Array.isArray(stored)) {
    throw new Error("Invalid bookmark shortcuts");
  }
  const shortcuts: BookmarkShortcuts = {};
  for (const { key } of SHORTCUTS) {
    const id = (stored as Record<string, unknown>)[key];
    if (isBookmarkId(id)) shortcuts[key] = id;
  }
  return shortcuts;
}
 
export function readAvailableBookmarkShortcuts(): BookmarkShortcuts {
  try {
    return readBookmarkShortcuts();
  } catch (error) {
    console.warn("Could not read bookmark shortcuts.", error);
    return {};
  }
}
 
export function setBookmarkShortcut(
  key: ShortcutKey,
  id: number | undefined,
  bookmarks: Record<number, unknown>
): BookmarkShortcuts {
  if (id !== undefined && (!isBookmarkId(id) || !bookmarks[id])) {
    throw new Error("Choose an existing bookmark.");
  }
  const shortcuts = readBookmarkShortcuts();
  if (id === undefined) delete shortcuts[key];
  else shortcuts[key] = id;
  localStorage.setItem(BOOKMARK_SHORTCUTS, JSON.stringify(shortcuts));
  notifyShortcutChange();
  return shortcuts;
}
 
export function removeShortcutsForBookmark(kind: "stop" | "route", id: number) {
  try {
    const shortcuts = readBookmarkShortcuts();
    const matches = SHORTCUTS.filter(slot => slot.kind === kind && shortcuts[slot.key] === id);
    if (matches.length === 0) return;
    for (const { key } of matches) delete shortcuts[key];
    localStorage.setItem(BOOKMARK_SHORTCUTS, JSON.stringify(shortcuts));
    notifyShortcutChange();
  } catch (error) {
    console.warn("Could not remove shortcut for deleted bookmark.", error);
  }
}