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.

2.2 KB
Raw Blame
import { useState } from 'react';

import { useTripStore } from '../../../store/tripStore';
import type { Reservation } from '../../../types';
import { deleteBodyKey } from './bookingsModel';
import { type Attachment, downloadAttachment, openAttachment } from './openAttachment';
import { useReservationDelete } from './useReservationDelete';

/** What a phone card needs from the planner it sits in. */
export interface ReservationCardHost {
  settings: { blur_booking_codes?: boolean };
  handleDeleteReservation: (id: number) => unknown;
  toast: { error: (message: string) => unknown };
  t: (key: string) => string;
}

/**
 * What a card in the phone's bookings and transports tabs does besides
 * opening: its booking code blurred until tapped while the "Blur booking codes"
 * preference is on (off, the code is plain and has no toggle), the delete
 * behind a question with a toast when it fails, and its attached files, which
 * open or download from their own row. A download that fails says so with a
 * toast, since a save button that does nothing reads as broken; opening stays
 * silent as it always was. The delete question names a linked expense, which
 * goes with the booking.
 */
export function useReservationCard(res: Reservation, host: ReservationCardHost) {
  const blurCodes = host.settings.blur_booking_codes;
  const { pendingDelete, requestDelete, cancelDelete, confirmDelete } = useReservationDelete(
    host.handleDeleteReservation,
    () => host.toast.error(host.t('reservations.toast.deleteError'))
  );
  const [codeRevealed, setCodeRevealed] = useState(false);
  const deleteTakesExpense = useTripStore(
    (s) => deleteBodyKey(res.id, s.budgetItems) === 'reservations.confirm.deleteBodyWithExpense'
  );

  return {
    codeBlurred: !!blurCodes && !codeRevealed,
    toggleCode: blurCodes ? () => setCodeRevealed((v) => !v) : undefined,
    confirmingDelete: pendingDelete != null,
    deleteTakesExpense,
    askDelete: () => requestDelete(res),
    cancelDelete,
    confirmDelete: () => {
      void confirmDelete();
    },
    openFile: (f: Attachment) => openAttachment(f),
    downloadFile: (f: Attachment) => downloadAttachment(f, () => host.toast.error(host.t('files.downloadError'))),
  };
}