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.

3.0 KB
Raw Blame
import { Loader2, Wallet } from 'lucide-react';
import { useState } from 'react';
import { useTranslation } from '../../i18n';
import { useNativeCapabilities } from '../../native/capabilities';
import {
  addReservationToGoogleWallet,
  addReservationToWallet,
  bookingWallet,
  useGoogleWalletAvailable,
} from '../../native/wallet';

interface MWalletButtonProps {
  tripId: number;
  /** None yet (a booking being created): no button. */
  reservationId: number | undefined;
  /** Placement in the hosting sheet, on a wrapper around the button. */
  className?: string;
  /** Shown when the pass could not be made (a toast in the sheet that hosts the button). */
  onError: (message: string) => void;
  /** 34px beside a sheet header's close button instead of 40px in an action row. */
  compact?: boolean;
}

/**
 * "Add to Apple Wallet" on a booking in the iOS app (feature wallet), "Add to
 * Google Wallet" in the Android app (feature googleWallet): the booking becomes a
 * TREK pass that shows up around its time and place. A black circle like both
 * stores' own badges, so it reads as the wallet at a glance; the phone's action
 * rows are icons only, the name is in the label and tooltip.
 */
export default function MWalletButton({
  tripId,
  reservationId,
  onError,
  className,
  compact = false,
}: MWalletButtonProps) {
  const { t, language } = useTranslation();
  // Re-renders once the app binary has said what it can do.
  useNativeCapabilities();
  // Re-renders once the server has said whether Google Wallet works here.
  useGoogleWalletAvailable(tripId);
  const [busy, setBusy] = useState(false);
  const wallet = bookingWallet();
  if (reservationId === undefined || !wallet) return null;
  const id = reservationId;

  const add = async () => {
    if (busy) return;
    setBusy(true);
    try {
      const outcome =
        wallet === 'google'
          ? await addReservationToGoogleWallet(tripId, id, language)
          : await addReservationToWallet(tripId, id, language);
      if (outcome === 'undated') onError(t('reservations.wallet.undated'));
      else if (outcome === 'failed') onError(t('reservations.wallet.failed'));
    } finally {
      setBusy(false);
    }
  };

  const label = t(wallet === 'google' ? 'reservations.wallet.addGoogle' : 'reservations.wallet.add');
  const button = (
    <button
      type="button"
      onClick={() => void add()}
      disabled={busy}
      aria-busy={busy}
      aria-label={label}
      title={label}
      className={`flex ${compact ? 'h-[34px] w-[34px]' : 'h-10 w-10'} flex-none items-center justify-center rounded-full bg-black text-white transition-transform active:scale-[.97] disabled:opacity-70 dark:ring-1 dark:ring-white/25`} // theme-lint-disable: the wallets' badges are black in either theme
    >
      {busy ? (
        <Loader2 size={compact ? 15 : 16} className="animate-spin" />
      ) : (
        <Wallet size={compact ? 15 : 16} strokeWidth={2} />
      )}
    </button>
  );
  return className ? <div className={className}>{button}</div> : button;
}