mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-03 06:24:30 +00:00
refactor(file-browser): address code review feedback for copy submenu
This commit is contained in:
@@ -6,8 +6,7 @@ import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import type { InferRequestType, InferResponseType } from 'hono/client'
|
||||
import { useState } from 'react'
|
||||
import { toast } from 'sonner'
|
||||
import { copyToClipboard, copyToClipboardDeferred, copyImageToClipboard } from '@/ui/lib/clipboard'
|
||||
import { type CopyKind, formatCopyText, pickClipboardTranscode } from './copy-options'
|
||||
import { useCopyActions } from './use-copy-actions'
|
||||
import { m } from '@/ui/paraglide/messages.js'
|
||||
|
||||
interface UseFileActionsProps {
|
||||
@@ -317,183 +316,12 @@ export function useFileActions({
|
||||
}
|
||||
}
|
||||
|
||||
const handleCopy = async (kind: CopyKind, items: AssetInfo[]) => {
|
||||
if (items.length === 0) return
|
||||
|
||||
if (kind === 'image') {
|
||||
const item = items[0]
|
||||
const transcode = pickClipboardTranscode(item.media?.imageTranscodes)
|
||||
|
||||
if (!transcode && !item.preview?.thumbnailUrl && item.status !== 'processed') {
|
||||
toast.error(m.copy_image_failed())
|
||||
return
|
||||
}
|
||||
|
||||
const urlPromise = (async () => {
|
||||
if (transcode?.url) return transcode.url
|
||||
if (transcode?.key && item.id) {
|
||||
if (isPublic && shareId) {
|
||||
const password = localStorage.getItem(`share_pwd_${shareId}`) || ''
|
||||
const res = await client.api.shares[':shareId'].files[':fileId']['download-url'].$post(
|
||||
{
|
||||
param: { shareId, fileId: item.id },
|
||||
json: { key: transcode.key },
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
'x-share-password': password,
|
||||
},
|
||||
},
|
||||
)
|
||||
if (res.ok) {
|
||||
const { url } = await res.json()
|
||||
return url
|
||||
}
|
||||
} else {
|
||||
const res = await client.api.files['download-url'].$post({
|
||||
json: { key: transcode.key, assetId: item.id },
|
||||
})
|
||||
if (res.ok) {
|
||||
const { url } = await res.json()
|
||||
return url
|
||||
}
|
||||
}
|
||||
}
|
||||
if (item.preview?.thumbnailUrl) {
|
||||
return item.preview.thumbnailUrl
|
||||
}
|
||||
throw new Error('No image URL available')
|
||||
})()
|
||||
|
||||
const ok = await copyImageToClipboard(urlPromise)
|
||||
if (ok) {
|
||||
toast.success(m.copied_image_toast())
|
||||
} else {
|
||||
toast.error(m.copy_image_failed())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if (kind === 'download-link') {
|
||||
const targetFiles = items.filter((i) => i.type !== 'folder')
|
||||
if (targetFiles.length === 0) return
|
||||
|
||||
try {
|
||||
const fetchDownloadLinksPromise = (async () => {
|
||||
const res = await getDownloadLinks({
|
||||
json: { ids: targetFiles.map((i) => i.id!) },
|
||||
})
|
||||
const files = res.files
|
||||
if (files.length === 0) {
|
||||
if ((res.skippedArchivedCount ?? 0) > 0) {
|
||||
toast.error(m.download_all_archived_error())
|
||||
}
|
||||
return ''
|
||||
}
|
||||
return files.map((f) => f.url).join('\n')
|
||||
})()
|
||||
|
||||
const ok = await copyToClipboardDeferred(fetchDownloadLinksPromise)
|
||||
if (ok) {
|
||||
const skippedFolders = items.length - targetFiles.length
|
||||
if (targetFiles.length === 1 && skippedFolders === 0) {
|
||||
toast.success(m.copied_download_link_toast())
|
||||
} else {
|
||||
const baseToast = m.copied_download_links_toast({ count: targetFiles.length })
|
||||
const msg =
|
||||
skippedFolders > 0
|
||||
? `${baseToast} ${m.copied_skipped_folders_suffix({ count: skippedFolders })}`
|
||||
: baseToast
|
||||
toast.success(msg)
|
||||
}
|
||||
} else {
|
||||
toast.error(m.copy_failed())
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to copy download links:', err)
|
||||
toast.error(m.copy_failed())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if (kind === 'storage-path') {
|
||||
const targetFiles = items.filter((i) => i.type !== 'folder')
|
||||
if (targetFiles.length === 0) return
|
||||
|
||||
try {
|
||||
const fetchStoragePathsPromise = (async () => {
|
||||
const res = await client.api.files['storage-paths'].$post({
|
||||
json: { ids: targetFiles.map((i) => i.id!) },
|
||||
})
|
||||
if (!res.ok) throw new Error('Failed to fetch storage paths')
|
||||
const data = (await res.json()) as { paths: Array<{ path: string }> }
|
||||
return data.paths.map((p) => p.path).join('\n')
|
||||
})()
|
||||
|
||||
const ok = await copyToClipboardDeferred(fetchStoragePathsPromise)
|
||||
if (ok) {
|
||||
const skippedFolders = items.length - targetFiles.length
|
||||
if (targetFiles.length === 1 && skippedFolders === 0) {
|
||||
toast.success(m.copied_storage_path_toast())
|
||||
} else {
|
||||
const baseToast = m.copied_storage_paths_toast({ count: targetFiles.length })
|
||||
const msg =
|
||||
skippedFolders > 0
|
||||
? `${baseToast} ${m.copied_skipped_folders_suffix({ count: skippedFolders })}`
|
||||
: baseToast
|
||||
toast.success(msg)
|
||||
}
|
||||
} else {
|
||||
toast.error(m.copy_failed())
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to copy storage paths:', err)
|
||||
toast.error(m.copy_failed())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Synchronous text copy
|
||||
const origin = typeof window !== 'undefined' ? window.location.origin : ''
|
||||
const text = formatCopyText(kind, items, { origin, projectId })
|
||||
const ok = await copyToClipboard(text)
|
||||
|
||||
if (ok) {
|
||||
switch (kind) {
|
||||
case 'name':
|
||||
toast.success(
|
||||
items.length === 1
|
||||
? m.copied_name_toast()
|
||||
: m.copied_names_toast({ count: items.length }),
|
||||
)
|
||||
break
|
||||
case 'link':
|
||||
toast.success(
|
||||
items.length === 1
|
||||
? m.copied_link_toast()
|
||||
: m.copied_links_toast({ count: items.length }),
|
||||
)
|
||||
break
|
||||
case 'id':
|
||||
toast.success(
|
||||
items.length === 1 ? m.copied_id_toast() : m.copied_ids_toast({ count: items.length }),
|
||||
)
|
||||
break
|
||||
case 'markdown':
|
||||
toast.success(
|
||||
items.length === 1
|
||||
? m.copied_markdown_link_toast()
|
||||
: m.copied_markdown_links_toast({ count: items.length }),
|
||||
)
|
||||
break
|
||||
case 'tsv':
|
||||
toast.success(m.copied_table_toast({ count: items.length }))
|
||||
break
|
||||
}
|
||||
} else {
|
||||
toast.error(m.copy_failed())
|
||||
}
|
||||
}
|
||||
const { handleCopy } = useCopyActions({
|
||||
projectId,
|
||||
isPublic,
|
||||
shareId,
|
||||
getDownloadLinks: (args) => getDownloadLinks(args),
|
||||
})
|
||||
|
||||
const handleArchive = (items: AssetInfo[]) => {
|
||||
if (items.length === 0) return
|
||||
@@ -702,10 +530,6 @@ export function useFileActions({
|
||||
}
|
||||
}
|
||||
|
||||
const handleCopyNameAndDownloadLink = (items: AssetInfo[]) => {
|
||||
handleCopy('download-link', items)
|
||||
}
|
||||
|
||||
return {
|
||||
editingItemId,
|
||||
setEditingItemId,
|
||||
@@ -714,7 +538,6 @@ export function useFileActions({
|
||||
handleRestore,
|
||||
handleDownload,
|
||||
handleCopy,
|
||||
handleCopyNameAndDownloadLink,
|
||||
handleArchive,
|
||||
handleRestoreArchive,
|
||||
handleNewFolder,
|
||||
|
||||
Reference in New Issue
Block a user