mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 10:44:23 +00:00
248 lines
7.9 KiB
TypeScript
248 lines
7.9 KiB
TypeScript
import type { JourneyRecordedDay } from '@trek/shared';
|
|
import { FileUp, RefreshCw, Route, Trash2 } from 'lucide-react';
|
|
import type React from 'react';
|
|
import { useRef } from 'react';
|
|
import { useTranslation } from '../../i18n';
|
|
import { useSettingsStore } from '../../store/settingsStore';
|
|
import { formatDistance } from '../../utils/units';
|
|
import { ContextMenu, useContextMenu } from '../shared/ContextMenu';
|
|
import DawarichIcon from '../shared/DawarichIcon';
|
|
import { fs } from '../shared/DialogShell';
|
|
import { Tooltip } from '../shared/Tooltip';
|
|
|
|
// aria-disabled rather than disabled: a disabled button gets no pointer events, and
|
|
// the tooltip is what tells a viewer or an offline editor what they are looking at.
|
|
const ROUND =
|
|
'grid h-7 w-7 place-items-center rounded-full transition-colors hover:bg-surface-hover aria-disabled:cursor-default aria-disabled:opacity-40';
|
|
const PILL =
|
|
'inline-flex items-center gap-1.5 rounded-full px-2.5 py-1 font-bold tabular-nums transition-colors aria-disabled:cursor-default [&:not([aria-disabled=true])]:hover:bg-surface-hover';
|
|
|
|
const GPX_TYPES = '.gpx,application/gpx+xml';
|
|
|
|
/** Reading a day's route from a GPX file (#3085): open to every editor, with or without Dawarich. */
|
|
export interface DayGpx {
|
|
/** The day's route read from a file, if it keeps one. */
|
|
day: JourneyRecordedDay | undefined;
|
|
canImport: boolean;
|
|
busy: boolean;
|
|
onImport: (file: File) => void;
|
|
onRemove: () => void;
|
|
}
|
|
|
|
/**
|
|
* The recorded route of one journey day in its header (JRN-01, #3085).
|
|
*
|
|
* A day that keeps its route shows how far it went, for everyone who can open
|
|
* the journey, with whose recording it is and since when in the tooltip; an
|
|
* editor opens a Dawarich day to check it again, replace it or drop it, and a
|
|
* day read from a GPX file to replace or drop that file's route. A day without
|
|
* one offers an editor one button: take it over from Dawarich, read it from a
|
|
* GPX file, or a small menu with both when both are possible. Offline the
|
|
* Dawarich offer stays, dimmed, and says why.
|
|
*/
|
|
export default function JourneyDayRecordingButton({
|
|
day,
|
|
canEdit,
|
|
canTakeOver,
|
|
needsConnection,
|
|
onOpen,
|
|
gpx,
|
|
}: {
|
|
/** The day's route taken over from Dawarich. */
|
|
day: JourneyRecordedDay | undefined;
|
|
canEdit: boolean;
|
|
canTakeOver: boolean;
|
|
needsConnection: boolean;
|
|
onOpen: () => void;
|
|
gpx?: DayGpx;
|
|
}): React.ReactElement | null {
|
|
const { t, locale } = useTranslation();
|
|
const unit = useSettingsStore((s) => s.settings.distance_unit) ?? 'metric';
|
|
const fileRef = useRef<HTMLInputElement>(null);
|
|
const menu = useContextMenu();
|
|
|
|
const canImportGpx = !!gpx?.canImport && canEdit;
|
|
const pickFile = () => fileRef.current?.click();
|
|
const fileInput = canImportGpx ? (
|
|
<input
|
|
ref={fileRef}
|
|
type="file"
|
|
accept={GPX_TYPES}
|
|
className="hidden"
|
|
onChange={(e) => {
|
|
const file = e.target.files?.[0];
|
|
e.target.value = '';
|
|
if (file) gpx?.onImport(file);
|
|
}}
|
|
/>
|
|
) : null;
|
|
|
|
const keptLabel = (kept: JourneyRecordedDay) => {
|
|
const distance = formatDistance(kept.stats.distanceMeters / 1000, unit);
|
|
const date = new Date(kept.savedAt).toLocaleDateString(locale, { day: 'numeric', month: 'short' });
|
|
const text = kept.importedBy
|
|
? t('journey.recording.keptBy', { distance, name: kept.importedBy.username, date })
|
|
: t('journey.recording.keptOn', { distance, date });
|
|
return { distance, label: kept.provisional ? `${text} ${t('journey.recording.keptRunning')}` : text };
|
|
};
|
|
|
|
if (day) {
|
|
const { distance, label } = keptLabel(day);
|
|
return (
|
|
<Tooltip label={label} placement="top">
|
|
<button
|
|
type="button"
|
|
onClick={canEdit ? onOpen : undefined}
|
|
aria-disabled={!canEdit}
|
|
aria-label={label}
|
|
data-testid="journey-day-recording"
|
|
className={PILL}
|
|
style={{ ...fs(10.5), background: 'var(--vg-surf2)', color: 'var(--vg-ink)' }}
|
|
>
|
|
<Route size={12} strokeWidth={2.4} aria-hidden="true" />
|
|
{distance}
|
|
{day.provisional && <span className="h-1.5 w-1.5 rounded-full bg-warning" aria-hidden="true" />}
|
|
</button>
|
|
</Tooltip>
|
|
);
|
|
}
|
|
|
|
if (gpx?.day) {
|
|
return (
|
|
<GpxRecordingPill
|
|
gpx={gpx}
|
|
detail={keptLabel(gpx.day)}
|
|
canImport={canImportGpx}
|
|
fileInput={fileInput}
|
|
pickFile={pickFile}
|
|
/>
|
|
);
|
|
}
|
|
|
|
if (!canTakeOver && !needsConnection && !canImportGpx) return null;
|
|
|
|
if (canImportGpx && !canTakeOver) {
|
|
return <ImportGpxButton busy={!!gpx?.busy} fileInput={fileInput} pickFile={pickFile} />;
|
|
}
|
|
|
|
const label = canTakeOver ? t('journey.recording.takeOver') : t('dawarich.error.offline');
|
|
return (
|
|
<>
|
|
{fileInput}
|
|
<Tooltip label={label} placement="top" disabled={!!menu.menu}>
|
|
<button
|
|
type="button"
|
|
onClick={
|
|
canTakeOver
|
|
? canImportGpx
|
|
? (e) =>
|
|
menu.open(
|
|
e,
|
|
[
|
|
{ label: t('journey.recording.takeOver'), icon: Route, onClick: onOpen },
|
|
{ label: t('journey.recording.gpx.import'), icon: FileUp, onClick: pickFile },
|
|
],
|
|
true
|
|
)
|
|
: onOpen
|
|
: undefined
|
|
}
|
|
aria-disabled={!canTakeOver}
|
|
aria-label={label}
|
|
aria-haspopup={canImportGpx ? 'menu' : undefined}
|
|
data-testid="journey-day-recording"
|
|
className={ROUND}
|
|
style={{ background: 'var(--vg-surf2)' }}
|
|
>
|
|
<DawarichIcon className="size-3.5 rounded-full" />
|
|
</button>
|
|
</Tooltip>
|
|
<ContextMenu menu={menu.menu} onClose={menu.close} />
|
|
</>
|
|
);
|
|
}
|
|
|
|
function ImportGpxButton({
|
|
busy,
|
|
fileInput,
|
|
pickFile,
|
|
}: {
|
|
busy: boolean;
|
|
fileInput: React.ReactNode;
|
|
pickFile: () => void;
|
|
}) {
|
|
const { t } = useTranslation();
|
|
const label = t('journey.recording.gpx.import');
|
|
return (
|
|
<>
|
|
{fileInput}
|
|
<Tooltip label={label} placement="top">
|
|
<button
|
|
type="button"
|
|
onClick={busy ? undefined : pickFile}
|
|
aria-disabled={busy}
|
|
aria-label={label}
|
|
data-testid="journey-day-recording"
|
|
className={ROUND}
|
|
style={{ background: 'var(--vg-surf2)', color: 'var(--vg-ink3)' }}
|
|
>
|
|
<Route size={14} strokeWidth={2.2} />
|
|
</button>
|
|
</Tooltip>
|
|
</>
|
|
);
|
|
}
|
|
|
|
function GpxRecordingPill({
|
|
gpx,
|
|
detail,
|
|
canImport,
|
|
fileInput,
|
|
pickFile,
|
|
}: {
|
|
gpx: DayGpx;
|
|
detail: { distance: string; label: string };
|
|
canImport: boolean;
|
|
fileInput: React.ReactNode;
|
|
pickFile: () => void;
|
|
}) {
|
|
const { t } = useTranslation();
|
|
const menu = useContextMenu();
|
|
const { distance, label } = detail;
|
|
const actionable = canImport && !gpx.busy;
|
|
const full = `${label} ${t('journey.recording.gpx.source')}`;
|
|
return (
|
|
<>
|
|
{fileInput}
|
|
<Tooltip label={full} placement="top" disabled={!!menu.menu}>
|
|
<button
|
|
type="button"
|
|
onClick={
|
|
actionable
|
|
? (e) =>
|
|
menu.open(
|
|
e,
|
|
[
|
|
{ label: t('journey.recording.replace'), icon: RefreshCw, onClick: pickFile },
|
|
{ label: t('journey.recording.remove'), icon: Trash2, onClick: gpx.onRemove, danger: true },
|
|
],
|
|
true
|
|
)
|
|
: undefined
|
|
}
|
|
aria-disabled={!actionable}
|
|
aria-label={full}
|
|
aria-haspopup={actionable ? 'menu' : undefined}
|
|
data-testid="journey-day-recording"
|
|
className={PILL}
|
|
style={{ ...fs(10.5), background: 'var(--vg-surf2)', color: 'var(--vg-ink)' }}
|
|
>
|
|
<FileUp size={12} strokeWidth={2.4} aria-hidden="true" />
|
|
{distance}
|
|
</button>
|
|
</Tooltip>
|
|
<ContextMenu menu={menu.menu} onClose={menu.close} />
|
|
</>
|
|
);
|
|
}
|