Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

13.2 KB
Raw Blame
import type { PaddleRegion, PaddleRegionCatalogEntry } from '@trek/shared';
import { Check, ChevronRight, Loader2, Plus, RefreshCw, Search, Trash2, Waves, WifiOff } from 'lucide-react';
import { useId, useState } from 'react';
import { useDateFormat } from '../../../hooks/useDateFormat';
import { useTranslation } from '../../../i18n';
import { formatDisplayDate } from '../../../utils/dateFormat';
import {
  SettingRows,
  SETTINGS_BUTTON,
  SETTINGS_ICON_BUTTON,
  SettingsHint,
  StatusPill,
} from '../../Settings/settingsKit';
import ConfirmDialog from '../../shared/ConfirmDialog';
import { EditorField, INPUT } from '../../shared/dialogParts';
import { fs } from '../../shared/DialogShell';
import ErrorBoundary from '../../shared/ErrorBoundary';
import { Tooltip } from '../../shared/Tooltip';
import AddonSetupDialog from '../AddonSetupDialog';
import AddonSetupRow from '../AddonSetupRow';
import { AutoLoadSettings, AutoOriginPill, RequestList } from './PaddleAutoLoadParts';
import { progressPercent, regionStatusTone } from './paddleRegionsModel';
import { usePaddleRegionsAdmin } from './usePaddleRegionsAdmin';

type Admin = ReturnType<typeof usePaddleRegionsAdmin>;

/**
 * PAD-05A in the admin area: the regions TREK reads paddle obstacles for,
 * picked from Geofabrik's list, and (PAD-11C) how TREK loads the region of a
 * place a paddler plans in by itself, with the regions that wait for an admin.
 * The desktop admin shows it as a row of the Tours addon that opens it as a
 * dialog while the addon is on; the row counts what waits for the admin (the
 * requests) or else the regions. The phone has MAdminPaddleRegionsPanel on
 * the same hook.
 */
export default function PaddleRegionsPanel() {
  return (
    <ErrorBoundary boundaryId="paddle-regions">
      <Panel />
    </ErrorBoundary>
  );
}

function Panel() {
  const { t } = useTranslation();
  const admin = usePaddleRegionsAdmin();
  const [open, setOpen] = useState(false);
  if (!admin.toursOn) return null;
  const title = t('admin.paddleRegions.title');
  // A declined request no longer waits for anyone; only the open ones ask for a look.
  const waiting = admin.requests.filter((r) => !r.declined).length;
  const badge =
    waiting > 0 ? (
      <StatusPill tone="warning">{waiting}</StatusPill>
    ) : admin.regions.length > 0 ? (
      <StatusPill>{admin.regions.length}</StatusPill>
    ) : undefined;
  const close = () => setOpen(false);
  return (
    <>
      <AddonSetupRow label={title} onOpen={() => setOpen(true)} badge={badge} />
      {open && (
        <AddonSetupDialog
          icon={<Waves size={20} strokeWidth={2} />}
          eyebrow={t('admin.addons.catalog.tours.name')}
          title={title}
          sub={t('admin.paddleRegions.hint')}
          onClose={close}
          blocked={admin.removing !== null}
        >
          <PanelBody admin={admin} />
        </AddonSetupDialog>
      )}
    </>
  );
}

/** What the dialog holds: the state of the list, the settings, the requests, the search and the regions. */
function PanelBody({ admin }: { admin: Admin }) {
  const { t } = useTranslation();
  return (
    <div className="flex flex-col gap-4">
      {admin.offline && (
        <p
          role="status"
          className="m-0 flex items-center gap-2 rounded-[12px] bg-warning-soft px-3.5 py-2.5 text-warning"
          style={fs(12.5, 'body')}
        >
          <WifiOff size={14} className="flex-none" />
          {t('admin.paddleRegions.offline')}
        </p>
      )}
      {admin.error && !admin.offline && (
        <div
          role="alert"
          className="flex flex-wrap items-center gap-3 rounded-[12px] bg-danger-soft px-3.5 py-2.5 text-danger"
          style={fs(12.5, 'body')}
        >
          <span className="min-w-0 flex-1 basis-48">{admin.error}</span>
          <button type="button" onClick={admin.retry} className={SETTINGS_BUTTON}>
            {t('admin.paddleRegions.retry')}
          </button>
        </div>
      )}
      <fieldset disabled={admin.offline} className="m-0 flex min-w-0 flex-col gap-4 border-0 p-0">
        <AutoLoadSettings admin={admin} />
        <RequestList admin={admin} />
        <CatalogSearch admin={admin} />
        <RegionList admin={admin} />
      </fieldset>
      <SettingsHint>{t('admin.paddleRegions.attribution')}</SettingsHint>
      <ConfirmDialog
        isOpen={admin.removing !== null}
        onClose={admin.cancelRemove}
        onConfirm={admin.confirmRemove}
        title={t('admin.paddleRegions.remove')}
        message={t('admin.paddleRegions.removeMessage', { name: admin.removing?.name ?? '' })}
        confirmLabel={t('common.remove')}
      />
    </div>
  );
}

/** A Geofabrik region with the regions it lies in, outermost first, so two of one name tell apart. */
function RegionPath({ hit }: { hit: PaddleRegionCatalogEntry }) {
  if (hit.path.length === 0) return null;
  return (
    <span className="flex min-w-0 flex-wrap items-center gap-0.5 text-content-faint" style={fs(11.5)}>
      {hit.path.map((part, index) => (
        <span key={`${part}-${index}`} className="inline-flex items-center gap-0.5">
          {index > 0 && <ChevronRight size={11} className="flex-none rtl:rotate-180" aria-hidden="true" />}
          {part}
        </span>
      ))}
    </span>
  );
}

function CatalogSearch({ admin }: { admin: Admin }) {
  const { t } = useTranslation();
  const id = useId();
  const { search } = admin;
  return (
    <div className="flex flex-col gap-2">
      <EditorField label={t('admin.paddleRegions.search')} htmlFor={id}>
        <div className="relative">
          <Search
            size={14}
            className="pointer-events-none absolute start-3 top-1/2 -translate-y-1/2 text-content-faint"
            aria-hidden="true"
          />
          <input
            id={id}
            type="search"
            className={`${INPUT} ps-9`}
            value={search.query}
            onChange={(event) => search.setQuery(event.target.value)}
            placeholder={t('admin.paddleRegions.searchPlaceholder')}
            autoComplete="off"
          />
        </div>
      </EditorField>
      {search.searching && (
        <p role="status" className="m-0 flex items-center gap-2 text-content-muted" style={fs(12.5, 'body')}>
          <Loader2 size={14} className="flex-none animate-spin" />
          {t('common.loading')}
        </p>
      )}
      {search.error && (
        <p role="alert" className="m-0 text-danger" style={fs(12.5, 'body')}>
          {search.error}
        </p>
      )}
      {search.hits && search.hits.length === 0 && <SettingsHint>{t('admin.paddleRegions.noHits')}</SettingsHint>}
      {search.hits && search.hits.length > 0 && (
        <SettingRows>
          {search.hits.map((hit) => (
            <div key={hit.id} className="flex items-center gap-3 py-2 pe-2 ps-3.5" data-testid="paddle-region-hit">
              <div className="flex min-w-0 flex-1 flex-col gap-0.5">
                <span className="truncate font-medium text-content" style={fs(13, 'body')}>
                  {hit.name}
                </span>
                <RegionPath hit={hit} />
              </div>
              {admin.added.has(hit.id) ? (
                <StatusPill icon={<Check size={11} strokeWidth={2.4} />}>{t('admin.paddleRegions.added')}</StatusPill>
              ) : (
                <button
                  type="button"
                  disabled={admin.busyId !== null}
                  onClick={() => void admin.add(hit.id)}
                  className={SETTINGS_BUTTON}
                >
                  {admin.busyId === hit.id ? (
                    <Loader2 size={13} className="animate-spin" />
                  ) : (
                    <Plus size={13} strokeWidth={2.2} />
                  )}
                  {t('common.add')}
                </button>
              )}
            </div>
          ))}
        </SettingRows>
      )}
    </div>
  );
}

function RegionList({ admin }: { admin: Admin }) {
  const { t } = useTranslation();
  if (admin.loading) {
    return (
      <p role="status" className="m-0 flex items-center gap-2 text-content-muted" style={fs(12.5, 'body')}>
        <Loader2 size={14} className="flex-none animate-spin" />
        {t('common.loading')}
      </p>
    );
  }
  if (admin.regions.length === 0) {
    return (
      <div className="rounded-[12px] border border-dashed border-edge px-4 py-5 text-center">
        <SettingsHint>{t('admin.paddleRegions.empty')}</SettingsHint>
      </div>
    );
  }
  return (
    <SettingRows>
      {admin.regions.map((region) => (
        <RegionRow key={region.id} region={region} admin={admin} />
      ))}
    </SettingRows>
  );
}

function RegionStatus({ region }: { region: PaddleRegion }) {
  const { t, language } = useTranslation();
  const percent = progressPercent(region);
  const label =
    region.status === 'importing' && region.progress
      ? t(`admin.paddleRegions.phase.${region.progress.phase}`)
      : t(`admin.paddleRegions.status.${region.status}`);
  const share =
    percent === null ? '' : ` ${new Intl.NumberFormat(language, { style: 'percent' }).format(percent / 100)}`;
  return (
    <StatusPill
      tone={regionStatusTone(region.status)}
      icon={region.status === 'importing' ? <Loader2 size={11} className="animate-spin" /> : undefined}
    >
      {label}
      {share}
    </StatusPill>
  );
}

/**
 * PAD-11: the water network the router follows in a region, as one figure
 * with the rest in its tooltip. A region read before routing existed says it
 * needs reading again; a network is never claimed where none was built.
 */
function RegionNetwork({ region }: { region: PaddleRegion }) {
  const { t, language } = useTranslation();
  const { network } = region;
  if (!network) {
    if (!region.imported_at || region.status === 'importing') return null;
    return (
      <Tooltip label={t('admin.paddleRegions.network.missingHint')}>
        <span className="inline-flex">
          <StatusPill tone="warning">{t('admin.paddleRegions.network.missing')}</StatusPill>
        </span>
      </Tooltip>
    );
  }
  const km = new Intl.NumberFormat(language, { maximumFractionDigits: 0 }).format(network.water_km);
  const details = [
    t('admin.paddleRegions.network.lakes', { count: network.lakes }),
    t('admin.paddleRegions.network.portages', { count: network.portages }),
    t('admin.paddleRegions.network.inferred', { count: network.inferred_portages }),
    t('admin.paddleRegions.network.locks', { count: network.locks }),
    t('admin.paddleRegions.network.gaps', { count: network.gaps }),
  ].join(', ');
  return (
    <Tooltip label={details}>
      <span className="inline-flex" data-testid="paddle-region-network">
        <StatusPill>{t('admin.paddleRegions.network.km', { km })}</StatusPill>
      </span>
    </Tooltip>
  );
}

function RegionRow({ region, admin }: { region: PaddleRegion; admin: Admin }) {
  const { t, language } = useTranslation();
  const dateFormat = useDateFormat();
  const busy = admin.busyId === region.id;
  const date = region.source_timestamp
    ? formatDisplayDate(new Date(region.source_timestamp), language, { dateStyle: 'medium' }, dateFormat)
    : null;
  return (
    <div
      className={`flex items-center gap-3 py-2.5 pe-2 ps-3 ${busy ? 'opacity-60' : ''}`}
      data-testid="paddle-region-row"
    >
      <span
        aria-hidden
        className="grid h-8 w-8 flex-none place-items-center rounded-[10px] bg-surface-tertiary text-content-secondary"
      >
        <Waves size={15} strokeWidth={1.9} />
      </span>
      <div className="flex min-w-0 flex-1 flex-col gap-1">
        <span className="truncate font-medium text-content" style={fs(13, 'body')}>
          {region.name}
        </span>
        <span className="flex flex-wrap items-center gap-1.5">
          <RegionStatus region={region} />
          {region.origin === 'auto' && <AutoOriginPill />}
          {region.imported_at && (
            <StatusPill>{t('admin.paddleRegions.objects', { count: region.feature_count })}</StatusPill>
          )}
          {region.imported_at && date && <StatusPill>{t('admin.paddleRegions.asOf', { date })}</StatusPill>}
          <RegionNetwork region={region} />
        </span>
        {region.status === 'failed' && region.error && (
          <p className="m-0 line-clamp-2 break-words text-danger" style={fs(11.5)} title={region.error}>
            {region.error}
            {region.imported_at ? ` ${t('admin.paddleRegions.keptData')}` : ''}
          </p>
        )}
      </div>
      <Tooltip label={t('admin.paddleRegions.refresh')}>
        <button
          type="button"
          aria-label={`${t('admin.paddleRegions.refresh')} ${region.name}`}
          disabled={admin.busyId !== null || region.status === 'queued' || region.status === 'importing'}
          onClick={() => void admin.refresh(region.id)}
          className={SETTINGS_ICON_BUTTON}
        >
          <RefreshCw size={14} strokeWidth={2} />
        </button>
      </Tooltip>
      <Tooltip label={t('admin.paddleRegions.remove')}>
        <button
          type="button"
          aria-label={`${t('admin.paddleRegions.remove')} ${region.name}`}
          disabled={admin.busyId !== null}
          onClick={() => admin.askRemove(region)}
          className={`${SETTINGS_ICON_BUTTON} hover:!text-danger`}
        >
          <Trash2 size={14} strokeWidth={2} />
        </button>
      </Tooltip>
    </div>
  );
}