All files / applications/go-by-transit/src/view/bookmarks/component BookmarkShortcutsEditor.tsx

0% Statements 0/10
0% Branches 0/21
0% Functions 0/4
0% Lines 0/10

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                                                                                                                                           
import {
  Card,
  CardBody,
  CardHeader,
  FieldGroup,
  Select,
} from "@wintermuted/ui-react";
import React from "react";
import { useBookmarkShortcuts } from "@overworld/bookmarks/react";
import { SHORTCUTS } from "../../../api/localstorage/bookmarkShortcuts";
import { useBookmarks } from "../../../state/bookmarks/BookmarksProvider";
 
export default function BookmarkShortcutsEditor() {
  const { stops, routes } = useBookmarks();
  const { shortcuts, error, setShortcut } = useBookmarkShortcuts(stops, routes);
 
  return (
    <Card>
      <CardHeader as="h2">Home and Work shortcuts</CardHeader>
      <CardBody>
        <p>
          Choose bookmarked stops and optional commute routes. Shortcuts stay in
          this browser.
        </p>
        {error && <p role="alert">{error}</p>}
        <div className="bookmark-shortcut-fields">
          {SHORTCUTS.map(({ key, kind, label }) => {
            const bookmarks = kind === "stop" ? stops : routes;
            const id = shortcuts[key];
            const options = Object.entries(bookmarks);
            return (
              <FieldGroup key={key} label={label} htmlFor={`shortcut-${key}`}>
                <Select
                  id={`shortcut-${key}`}
                  value={id ?? ""}
                  disabled={options.length === 0 && id === undefined}
                  onChange={(event) => {
                    const nextId = event.target.value
                      ? Number(event.target.value)
                      : undefined;
                    setShortcut(key, nextId);
                  }}
                >
                  <option value="">
                    {options.length || id !== undefined
                      ? "Not set"
                      : `No bookmarked ${kind}s`}
                  </option>
                  {id !== undefined && !bookmarks[id] && (
                    <option value={id} disabled>
                      Bookmark no longer available
                    </option>
                  )}
                  {options.map(([bookmarkId, bookmark]) => (
                    <option key={bookmarkId} value={bookmarkId}>
                      {bookmark.desc ||
                        (kind === "stop" ? "Stop" : "Transit route")}{" "}
                      ({bookmarkId})
                    </option>
                  ))}
                </Select>
              </FieldGroup>
            );
          })}
        </div>
      </CardBody>
    </Card>
  );
}