mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 09:54:14 +00:00
refactor(files): share the file trash, paste and link rules between desktop and phone
useFileTrash holds the trashed files with their restore, permanent delete and empty actions; the desktop trash view loads it on demand and the phone sheet while it is open. fileActions holds the clipboard file picking and the two tier link toggle (own column first, then link records) that the desktop assign dialog and the phone link sheet both ran on their own.
This commit is contained in:
@@ -2,7 +2,7 @@ import { useRef, useState } from 'react'
|
||||
import { Check, Link2, Loader2, MapPin, Ticket, TrainFront } from 'lucide-react'
|
||||
import type { LucideIcon } from 'lucide-react'
|
||||
import MSheet from '../../../components/MSheet'
|
||||
import { filesApi } from '../../../../api/client'
|
||||
import { toggleFileLink, type FileLinkField } from '../../../../components/Files/fileActions'
|
||||
import type { TripFile } from '../../../../types'
|
||||
import type { TripPlanner } from '../MTripShell'
|
||||
import { Eyebrow, TileHeader } from '../sheets/MTripSheetUi'
|
||||
@@ -14,18 +14,12 @@ interface MFileLinkSheetProps {
|
||||
onClose: () => void
|
||||
}
|
||||
|
||||
interface FileLinkRecord {
|
||||
id: number
|
||||
place_id?: number | string | null
|
||||
reservation_id?: number | string | null
|
||||
}
|
||||
|
||||
/**
|
||||
* Link picker for a file (spec 03 §5.3 "Verknüpfen", §7.3 m:n): toggles the
|
||||
* file's place_id/reservation_id (first link) or an addLink/removeLink file_link
|
||||
* record (further links) — the exact two-tier pattern FileManagerAssignModal.tsx
|
||||
* uses on desktop, flattened to a single list per section instead of grouping
|
||||
* places by day (v1 simplification, see report).
|
||||
* record (further links) through toggleFileLink, the two-tier rule
|
||||
* FileManagerAssignModal.tsx runs on desktop, flattened to a single list per
|
||||
* section instead of grouping places by day (v1 simplification, see report).
|
||||
*/
|
||||
export default function MFileLinkSheet({ planner, file, onClose }: MFileLinkSheetProps) {
|
||||
const { t, tripId, places, reservations, TRANSPORT_TYPES, tripActions, toast } = planner
|
||||
@@ -49,24 +43,12 @@ export default function MFileLinkSheet({ planner, file, onClose }: MFileLinkShee
|
||||
|
||||
const refresh = () => tripActions.loadFiles(tripId)
|
||||
|
||||
const togglePlace = async (placeId: number) => {
|
||||
const toggle = async (field: FileLinkField, targetId: number) => {
|
||||
if (busyKey) return
|
||||
const key = `p${placeId}`
|
||||
const key = `${field === 'place_id' ? 'p' : 'r'}${targetId}`
|
||||
setBusyKey(key)
|
||||
try {
|
||||
if (placeIds.has(placeId)) {
|
||||
if (shown.place_id === placeId) {
|
||||
await filesApi.update(tripId, shown.id, { place_id: null })
|
||||
} else {
|
||||
const linksRes = (await filesApi.getLinks(tripId, shown.id)) as { links: FileLinkRecord[] }
|
||||
const link = (linksRes.links || []).find(l => Number(l.place_id) === placeId)
|
||||
if (link) await filesApi.removeLink(tripId, shown.id, link.id)
|
||||
}
|
||||
} else if (shown.place_id == null) {
|
||||
await filesApi.update(tripId, shown.id, { place_id: placeId })
|
||||
} else {
|
||||
await filesApi.addLink(tripId, shown.id, { place_id: placeId })
|
||||
}
|
||||
await toggleFileLink(tripId, shown, field, targetId)
|
||||
refresh()
|
||||
} catch {
|
||||
toast.error(t('files.toast.assignError'))
|
||||
@@ -75,31 +57,8 @@ export default function MFileLinkSheet({ planner, file, onClose }: MFileLinkShee
|
||||
}
|
||||
}
|
||||
|
||||
const toggleReservation = async (resId: number) => {
|
||||
if (busyKey) return
|
||||
const key = `r${resId}`
|
||||
setBusyKey(key)
|
||||
try {
|
||||
if (resIds.has(resId)) {
|
||||
if (shown.reservation_id === resId) {
|
||||
await filesApi.update(tripId, shown.id, { reservation_id: null })
|
||||
} else {
|
||||
const linksRes = (await filesApi.getLinks(tripId, shown.id)) as { links: FileLinkRecord[] }
|
||||
const link = (linksRes.links || []).find(l => Number(l.reservation_id) === resId)
|
||||
if (link) await filesApi.removeLink(tripId, shown.id, link.id)
|
||||
}
|
||||
} else if (shown.reservation_id == null) {
|
||||
await filesApi.update(tripId, shown.id, { reservation_id: resId })
|
||||
} else {
|
||||
await filesApi.addLink(tripId, shown.id, { reservation_id: resId })
|
||||
}
|
||||
refresh()
|
||||
} catch {
|
||||
toast.error(t('files.toast.assignError'))
|
||||
} finally {
|
||||
setBusyKey(null)
|
||||
}
|
||||
}
|
||||
const togglePlace = (placeId: number) => toggle('place_id', placeId)
|
||||
const toggleReservation = (resId: number) => toggle('reservation_id', resId)
|
||||
|
||||
const bookingReservations = reservations.filter(r => !TRANSPORT_TYPES.has(r.type))
|
||||
const transportReservations = reservations.filter(r => TRANSPORT_TYPES.has(r.type))
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useState } from 'react'
|
||||
import { Loader2, RotateCcw, Trash2 } from 'lucide-react'
|
||||
import MSheet from '../../../components/MSheet'
|
||||
import MConfirmSheet from '../../settings/MConfirmSheet'
|
||||
import { filesApi } from '../../../../api/client'
|
||||
import type { TripFile } from '../../../../types'
|
||||
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
|
||||
@@ -17,66 +16,38 @@ interface MFileTrashSheetProps {
|
||||
}
|
||||
|
||||
/**
|
||||
* Trash sheet (spec 03 §5.3 trashGo, §7.3): fetches the trashed files
|
||||
* (filesApi.list(tripId, true)) on open, same lazy-load-on-toggle pattern as
|
||||
* useFileManager.ts's toggleTrash/loadTrash. Restore/permanent-delete/empty
|
||||
* all bypass the store like the rest of §7.3.
|
||||
* 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, setFiles] = useState<TripFile[]>([])
|
||||
const [loading, setLoading] = useState(false)
|
||||
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)
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return
|
||||
let cancelled = false
|
||||
setLoading(true)
|
||||
filesApi.list(tripId, true)
|
||||
.then((data: { files?: TripFile[] }) => { if (!cancelled) setFiles(data.files || []) })
|
||||
.catch(() => {})
|
||||
.finally(() => { if (!cancelled) setLoading(false) })
|
||||
return () => { cancelled = true }
|
||||
}, [open, tripId])
|
||||
|
||||
const canDelete = can('file_delete', trip)
|
||||
|
||||
const restore = (id: number) => {
|
||||
setBusyId(id)
|
||||
filesApi.restore(tripId, id)
|
||||
.then(() => {
|
||||
setFiles(prev => prev.filter(f => f.id !== id))
|
||||
tripActions.loadFiles(tripId)
|
||||
toast.success(t('files.toast.restored'))
|
||||
})
|
||||
.catch(() => toast.error(t('files.toast.restoreError')))
|
||||
.finally(() => setBusyId(null))
|
||||
void trash.restore(id).finally(() => setBusyId(null))
|
||||
}
|
||||
|
||||
const permanentDelete = (id: number) => {
|
||||
setConfirmDeleteId(null)
|
||||
setBusyId(id)
|
||||
filesApi.permanentDelete(tripId, id)
|
||||
.then(() => {
|
||||
setFiles(prev => prev.filter(f => f.id !== id))
|
||||
toast.success(t('files.toast.deleted'))
|
||||
})
|
||||
.catch(() => toast.error(t('files.toast.deleteError')))
|
||||
.finally(() => setBusyId(null))
|
||||
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)
|
||||
filesApi.emptyTrash(tripId)
|
||||
.then(() => { setFiles([]); toast.success(t('files.toast.trashEmptied')) })
|
||||
.catch(() => toast.error(t('files.toast.deleteError')))
|
||||
.finally(() => setEmptying(false))
|
||||
void trash.emptyTrash().finally(() => setEmptying(false))
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -13,6 +13,7 @@ import MFileLinkSheet from './MFileLinkSheet'
|
||||
import MFileTrashSheet from './MFileTrashSheet'
|
||||
import MDocSyncSheet from './MDocSyncSheet'
|
||||
import { canManageDocSync } from '../../../../components/Files/docsync/useDocSync'
|
||||
import { filesFromClipboard } from '../../../../components/Files/fileActions'
|
||||
import { useAuthStore } from '../../../../store/authStore'
|
||||
import MFileLightbox from './MFileLightbox'
|
||||
import {
|
||||
@@ -91,15 +92,7 @@ export default function MFilesTab({ planner, shell }: MTabScreenProps) {
|
||||
|
||||
const onPaste = (e: ClipboardEvent<HTMLDivElement>) => {
|
||||
if (!planner.canUploadFiles) return
|
||||
const items = e.clipboardData?.items
|
||||
if (!items) return
|
||||
const pasted: File[] = []
|
||||
for (const item of Array.from(items)) {
|
||||
if (item.kind === 'file') {
|
||||
const f = item.getAsFile()
|
||||
if (f) pasted.push(f)
|
||||
}
|
||||
}
|
||||
const pasted = filesFromClipboard(e.clipboardData)
|
||||
if (pasted.length > 0) {
|
||||
e.preventDefault()
|
||||
void uploadFiles(pasted)
|
||||
|
||||
Reference in New Issue
Block a user