import type { TourActivity, TourListItem } from '@trek/shared';
import { CalendarDays, Clock, CloudOff, FileDown, Footprints, Plus, RefreshCw, Unlink } from 'lucide-react';
import { useMemo } from 'react';
import { useTranslation } from '../../../i18n';
import { useSettingsStore } from '../../../store/settingsStore';
import type { AssignmentsMap, Day } from '../../../types';
import { BOX } from '../../Planner/bookings/bookingParts';
import { SoftPill } from '../../Planner/planParts';
import ConfirmDialog from '../../shared/ConfirmDialog';
import { DialogSection, fs } from '../../shared/DialogShell';
import ElevationProfile from '../../shared/ElevationProfile';
import { Tooltip } from '../../shared/Tooltip';
import { dayMenuLabel } from '../tourDays';
import { CardAction, TourNotice } from '../tourParts';
import TourActivityCompare from './TourActivityCompare';
import TourActivityDialog from './TourActivityDialog';
import { ACTUAL_TRACK_COLOR } from './activityCompareParts';
import type { PlanValues } from './tourActivityCompare';
import { analysisOfLine, executionOf, executionsOf, recordedDay, type TourExecution } from './tourActivityModel';
import { useActivityComparison, type ActivityComparisonState } from './useActivityComparison';
import { useTourActivities, type TourActivities } from './useTourActivities';
/** What a card compares its track with: the plan's values and colour, and the map switch where there is a plan map. */
interface PlanSide {
plan: PlanValues;
planColor: string;
/** Null on the phone, whose detail has no plan map beside it. */
comparison: ActivityComparisonState | null;
}
/** The tile of a recording: footprints, with a small dot at its foot that says it was recorded. */
function RecordedTile() {
return (
);
}
/** Where and when: the execution, the file it came from, the day it was recorded, each a badge of its own. */
function ActivityBadges({ activity, execution }: { activity: TourActivity; execution: TourExecution }) {
const { t, locale } = useTranslation();
const dayLabel = execution.day ? t('dayplan.dayN', { n: execution.index + 1 }) : t('tours.activity.noDay');
const dayTitle = execution.day ? dayMenuLabel(execution.day, execution.index, t, locale) : dayLabel;
const recordedOn = recordedDay(activity.metrics.started_at, locale);
const source =
activity.track.file_name ?? t(activity.track.origin === 'wanderer' ? 'tours.badge.wanderer' : 'tours.badge.gpx');
return (
);
}
/**
* One recording: who it is, its values beside the plan's (ACT-02), its
* profile, and what the user can do with it.
*/
function ActivityCard({
activity,
execution,
state,
side,
}: {
activity: TourActivity;
execution: TourExecution;
state: TourActivities;
side: PlanSide;
}) {
const { t } = useTranslation();
const unit = useSettingsStore((store) => store.settings.distance_unit) ?? 'metric';
const analysis = useMemo(() => analysisOfLine(activity.geometry), [activity.geometry]);
const trackPlaceId = activity.track.place_id;
const { comparison } = side;
return (
{state.canEdit && (
)}
);
}
/** No recording yet: a quiet line, dimmed rather than hidden, so the place for one is known. */
function NoActivity() {
const { t } = useTranslation();
return (
{t('tours.activity.empty')}
);
}
/** What the section holds: the tracks, the empty place for one, or word that the tour's tracks could not be read. */
function ActivityList({
state,
executions,
side,
}: {
state: TourActivities;
executions: readonly TourExecution[];
side: PlanSide;
}) {
const { t } = useTranslation();
const activities = state.activities ?? [];
if (state.unavailable) {
return (
}>
{t('tours.activity.unavailable')}
);
}
if (activities.length === 0) return ;
return (
{activities.map((activity) => (
))}
);
}
interface TourActivitySectionProps {
tripId: number;
tour: TourListItem;
days: Day[];
assignments: AssignmentsMap;
canEdit: boolean;
/** ACT-02: what the plan says, for each track's values to stand beside. */
plan: PlanValues;
/** The plan's track colour, as the map and the table draw it. */
planColor: string;
/** Offers to compare a track on the plan map; only where one is beside the detail. */
mapCompare?: boolean;
onToursChanged?: (placeIds: number[]) => Promise | void;
}
/**
* ACT-01 in the tour's detail: the tracks the tour was actually done with,
* each with the values its own recording measures and its profile, apart from
* the planned values above; and for someone who may change the tour, the way
* to assign one, change it or take it off. Each track's values stand beside
* the plan's, and one track at a time can be compared with the plan on the map
* (ACT-02). A tour that is itself the recording of another shows none: it says
* so in its head instead.
*/
export default function TourActivitySection({
tripId,
tour,
days,
assignments,
canEdit,
plan,
planColor,
mapCompare = false,
onToursChanged,
}: TourActivitySectionProps) {
const { t } = useTranslation();
const executions = useMemo(() => executionsOf(tour.place_id, days, assignments), [assignments, days, tour.place_id]);
const state = useTourActivities({ tripId, tour, executions, canEdit, onToursChanged });
const { activities, unavailable } = state;
const comparison = useActivityComparison(tour.place_id, activities);
const side: PlanSide = { plan, planColor, comparison: mapCompare ? comparison : null };
if (activities === null || tour.activity_of != null || (activities.length === 0 && !canEdit && !unavailable)) {
return null;
}
return (
}
onClick={() => state.openAssign()}
disabled={state.busy || unavailable}
>
{t('tours.activity.assign')}
)
}
>
{state.assigning && (
)}
void state.remove()}
title={t('tours.activity.removeTitle')}
message={t('tours.activity.removeBody')}
confirmLabel={t('tours.activity.remove')}
/>
);
}