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.

6.5 KB
Raw Blame
import { useState } from 'react'
import { Loader2, RotateCcw, Trash2 } from 'lucide-react'
import MSheet from '../../../components/MSheet'
import MConfirmSheet from '../../settings/MConfirmSheet'
import type { TripPlanner } from '../MTripShell'
import { TileHeader } from '../sheets/MTripSheetUi'
import { formatFileDate, getFileTypeMeta } from './filesModel'
import { useTranslation } from '../../../../i18n'
import { formatSize } from '../../../../components/Files/FileManager.helpers'
import { useFileTrash } from '../../../../components/Files/useFileTrash'

interface MFileTrashSheetProps {
  planner: TripPlanner
  open: boolean
  onClose: () => void
}

/**
 * Trash sheet (spec 03 §5.3 trashGo, §7.3): the trashed files load on open
 * through useFileTrash, the same hook the desktop trash view reads. Restore,
 * permanent delete and empty run there too; this sheet adds the confirm sheets
 * and the busy markers.
 */
export default function MFileTrashSheet({ planner, open, onClose }: MFileTrashSheetProps) {
  const { t, tripId, can, trip, toast, tripActions } = planner
  const { locale } = useTranslation()
  const { files, loading, ...trash } = useFileTrash({ tripId, t, toast, onRestored: () => tripActions.loadFiles(tripId), open })
  const [busyId, setBusyId] = useState<number | null>(null)
  const [emptying, setEmptying] = useState(false)
  const [confirmEmpty, setConfirmEmpty] = useState(false)
  const [confirmDeleteId, setConfirmDeleteId] = useState<number | null>(null)

  const canDelete = can('file_delete', trip)

  const restore = (id: number) => {
    setBusyId(id)
    void trash.restore(id).finally(() => setBusyId(null))
  }

  const permanentDelete = (id: number) => {
    setConfirmDeleteId(null)
    setBusyId(id)
    void trash.permanentDelete(id).finally(() => setBusyId(null))
  }

  // Row actions stay disabled while this runs so a restore cannot race the empty.
  const emptyTrash = () => {
    setConfirmEmpty(false)
    setEmptying(true)
    void trash.emptyTrash().finally(() => setEmptying(false))
  }

  return (
    <>
      <MSheet open={open} onClose={onClose} variant="card" material="glass" ariaLabel={t('files.trash')}>
        <div className="flex-none px-[18px] pt-4">
          <TileHeader icon={<Trash2 size={19} strokeWidth={1.8} />} title={t('files.trash')} onClose={onClose} closeLabel={t('common.close')} />
        </div>

        <div className="min-h-0 flex-1 overflow-y-auto px-[18px] pb-[18px]">
          {files.length > 0 && canDelete && (
            <div className="mt-3 flex justify-end">
              <button
                type="button"
                onClick={() => setConfirmEmpty(true)}
                disabled={emptying}
                className="font-geist text-[0.6875rem] font-bold text-[color:var(--m-st-danger)] disabled:opacity-50"
              >
                {t('files.emptyTrash')}
              </button>
            </div>
          )}

          {loading ? (
            <div className="flex justify-center py-10">
              <Loader2 size={20} className="animate-spin text-m-faint" />
            </div>
          ) : files.length === 0 ? (
            <div className="py-10 text-center font-geist text-[0.78125rem] text-m-faint">{t('files.trashEmpty')}</div>
          ) : (
            <div className="mt-3 flex flex-col gap-2">
              {files.map(file => {
                const meta = getFileTypeMeta(file)
                const TypeIcon = meta.icon
                const busy = busyId === file.id
                return (
                  <div key={file.id} className="flex items-center gap-[10px] rounded-2xl border border-[color:var(--m-rowbr)] bg-m-sheetop px-3 py-[9px] opacity-80">
                    <span
                      className="flex h-9 w-9 flex-none items-center justify-center rounded-[10px]"
                      style={{ background: `${meta.color}22`, color: meta.color }}
                    >
                      <TypeIcon size={15} strokeWidth={2} />
                    </span>
                    <div className="min-w-0 flex-1">
                      <div dir="auto" className="truncate text-[0.78125rem] font-semibold text-m-ink">{file.original_name}</div>
                      <div className="mt-[2px] font-geist text-[0.625rem] text-m-faint">
                        {[formatSize(file.file_size), formatFileDate(file.created_at, locale)].filter(Boolean).join(' · ')}
                      </div>
                    </div>
                    {canDelete && (
                      <div className="flex flex-none items-center gap-1">
                        <button
                          type="button"
                          aria-label={t('files.restore')}
                          onClick={() => restore(file.id)}
                          disabled={busy || emptying}
                          className="flex h-8 w-8 items-center justify-center rounded-full bg-[color:var(--m-ic)] text-m-muted disabled:opacity-50"
                        >
                          {busy ? <Loader2 size={13} className="animate-spin" /> : <RotateCcw size={13} strokeWidth={2} />}
                        </button>
                        <button
                          type="button"
                          aria-label={t('common.delete')}
                          onClick={() => setConfirmDeleteId(file.id)}
                          disabled={busy || emptying}
                          className="flex h-8 w-8 items-center justify-center rounded-full bg-[color:var(--m-ic)] text-[color:var(--m-st-danger)] disabled:opacity-50"
                        >
                          <Trash2 size={13} strokeWidth={2} />
                        </button>
                      </div>
                    )}
                  </div>
                )
              })}
            </div>
          )}
        </div>
      </MSheet>

      <MConfirmSheet
        open={confirmEmpty}
        onClose={() => setConfirmEmpty(false)}
        title={t('files.emptyTrash')}
        message={t('files.confirm.emptyTrash')}
        confirmLabel={t('files.emptyTrash')}
        cancelLabel={t('common.cancel')}
        danger
        onConfirm={emptyTrash}
      />
      <MConfirmSheet
        open={confirmDeleteId != null}
        onClose={() => setConfirmDeleteId(null)}
        title={t('common.delete')}
        message={t('files.confirm.permanentDelete')}
        confirmLabel={t('common.delete')}
        cancelLabel={t('common.cancel')}
        danger
        onConfirm={() => { if (confirmDeleteId != null) permanentDelete(confirmDeleteId) }}
      />
    </>
  )
}