mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-09-18 01:19:24 +00:00
Removed optionality for 'onDownloadFile', removed fallback in fileEditorThumbnail. (#4456)
Removed optionality for 'onDownloadFile', removed fallback in fileEditorThumbnail. Co-authored-by: Connor Yoh <connor@stirlingpdf.com>
This commit is contained in:
parent
6a94e10e30
commit
b51c2e42a6
@ -11,6 +11,8 @@ import FileEditorThumbnail from './FileEditorThumbnail';
|
|||||||
import FilePickerModal from '../shared/FilePickerModal';
|
import FilePickerModal from '../shared/FilePickerModal';
|
||||||
import SkeletonLoader from '../shared/SkeletonLoader';
|
import SkeletonLoader from '../shared/SkeletonLoader';
|
||||||
import { FileId, StirlingFile } from '../../types/fileContext';
|
import { FileId, StirlingFile } from '../../types/fileContext';
|
||||||
|
import { downloadBlob } from '../../utils/downloadUtils';
|
||||||
|
|
||||||
|
|
||||||
interface FileEditorProps {
|
interface FileEditorProps {
|
||||||
onOpenPageEditor?: () => void;
|
onOpenPageEditor?: () => void;
|
||||||
@ -278,7 +280,6 @@ const FileEditor = ({
|
|||||||
const handleDeleteFile = useCallback((fileId: FileId) => {
|
const handleDeleteFile = useCallback((fileId: FileId) => {
|
||||||
const record = activeStirlingFileStubs.find(r => r.id === fileId);
|
const record = activeStirlingFileStubs.find(r => r.id === fileId);
|
||||||
const file = record ? selectors.getFile(record.id) : null;
|
const file = record ? selectors.getFile(record.id) : null;
|
||||||
|
|
||||||
if (record && file) {
|
if (record && file) {
|
||||||
// Remove file from context but keep in storage (close, don't delete)
|
// Remove file from context but keep in storage (close, don't delete)
|
||||||
const contextFileId = record.id;
|
const contextFileId = record.id;
|
||||||
@ -290,6 +291,14 @@ const FileEditor = ({
|
|||||||
}
|
}
|
||||||
}, [activeStirlingFileStubs, selectors, removeFiles, setSelectedFiles, selectedFileIds]);
|
}, [activeStirlingFileStubs, selectors, removeFiles, setSelectedFiles, selectedFileIds]);
|
||||||
|
|
||||||
|
const handleDownloadFile = useCallback((fileId: FileId) => {
|
||||||
|
const record = activeStirlingFileStubs.find(r => r.id === fileId);
|
||||||
|
const file = record ? selectors.getFile(record.id) : null;
|
||||||
|
if (record && file) {
|
||||||
|
downloadBlob(file, file.name);
|
||||||
|
}
|
||||||
|
}, [activeStirlingFileStubs, selectors, setStatus]);
|
||||||
|
|
||||||
const handleViewFile = useCallback((fileId: FileId) => {
|
const handleViewFile = useCallback((fileId: FileId) => {
|
||||||
const record = activeStirlingFileStubs.find(r => r.id === fileId);
|
const record = activeStirlingFileStubs.find(r => r.id === fileId);
|
||||||
if (record) {
|
if (record) {
|
||||||
@ -401,6 +410,7 @@ const FileEditor = ({
|
|||||||
onViewFile={handleViewFile}
|
onViewFile={handleViewFile}
|
||||||
onSetStatus={setStatus}
|
onSetStatus={setStatus}
|
||||||
onReorderFiles={handleReorderFiles}
|
onReorderFiles={handleReorderFiles}
|
||||||
|
onDownloadFile={handleDownloadFile}
|
||||||
toolMode={toolMode}
|
toolMode={toolMode}
|
||||||
isSupported={isFileSupported(record.name)}
|
isSupported={isFileSupported(record.name)}
|
||||||
/>
|
/>
|
||||||
|
@ -13,6 +13,7 @@ import { StirlingFileStub } from '../../types/fileContext';
|
|||||||
import styles from './FileEditor.module.css';
|
import styles from './FileEditor.module.css';
|
||||||
import { useFileContext } from '../../contexts/FileContext';
|
import { useFileContext } from '../../contexts/FileContext';
|
||||||
import { FileId } from '../../types/file';
|
import { FileId } from '../../types/file';
|
||||||
|
import { formatFileSize } from '../../utils/fileUtils';
|
||||||
import ToolChain from '../shared/ToolChain';
|
import ToolChain from '../shared/ToolChain';
|
||||||
|
|
||||||
|
|
||||||
@ -28,7 +29,7 @@ interface FileEditorThumbnailProps {
|
|||||||
onViewFile: (fileId: FileId) => void;
|
onViewFile: (fileId: FileId) => void;
|
||||||
onSetStatus: (status: string) => void;
|
onSetStatus: (status: string) => void;
|
||||||
onReorderFiles?: (sourceFileId: FileId, targetFileId: FileId, selectedFileIds: FileId[]) => void;
|
onReorderFiles?: (sourceFileId: FileId, targetFileId: FileId, selectedFileIds: FileId[]) => void;
|
||||||
onDownloadFile?: (fileId: FileId) => void;
|
onDownloadFile: (fileId: FileId) => void;
|
||||||
toolMode?: boolean;
|
toolMode?: boolean;
|
||||||
isSupported?: boolean;
|
isSupported?: boolean;
|
||||||
}
|
}
|
||||||
@ -61,29 +62,6 @@ const FileEditorThumbnail = ({
|
|||||||
|
|
||||||
const pageCount = file.processedFile?.totalPages || 0;
|
const pageCount = file.processedFile?.totalPages || 0;
|
||||||
|
|
||||||
const downloadSelectedFile = useCallback(() => {
|
|
||||||
// Prefer parent-provided handler if available
|
|
||||||
if (typeof onDownloadFile === 'function') {
|
|
||||||
onDownloadFile(file.id);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fallback: attempt to download using the File object if provided
|
|
||||||
const maybeFile = (file as unknown as { file?: File }).file;
|
|
||||||
if (maybeFile instanceof File) {
|
|
||||||
const link = document.createElement('a');
|
|
||||||
link.href = URL.createObjectURL(maybeFile);
|
|
||||||
link.download = maybeFile.name || file.name || 'download';
|
|
||||||
document.body.appendChild(link);
|
|
||||||
link.click();
|
|
||||||
document.body.removeChild(link);
|
|
||||||
URL.revokeObjectURL(link.href);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we can't find a way to download, surface a status message
|
|
||||||
onSetStatus?.(typeof t === 'function' ? t('downloadUnavailable', 'Download unavailable for this item') : 'Download unavailable for this item');
|
|
||||||
}, [file, onDownloadFile, onSetStatus, t]);
|
|
||||||
const handleRef = useRef<HTMLSpanElement | null>(null);
|
const handleRef = useRef<HTMLSpanElement | null>(null);
|
||||||
|
|
||||||
// ---- Selection ----
|
// ---- Selection ----
|
||||||
@ -91,12 +69,7 @@ const FileEditorThumbnail = ({
|
|||||||
|
|
||||||
// ---- Meta formatting ----
|
// ---- Meta formatting ----
|
||||||
const prettySize = useMemo(() => {
|
const prettySize = useMemo(() => {
|
||||||
const bytes = file.size ?? 0;
|
return formatFileSize(file.size);
|
||||||
if (bytes === 0) return '0 B';
|
|
||||||
const k = 1024;
|
|
||||||
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
||||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
||||||
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(1))} ${sizes[i]}`;
|
|
||||||
}, [file.size]);
|
}, [file.size]);
|
||||||
|
|
||||||
const extUpper = useMemo(() => {
|
const extUpper = useMemo(() => {
|
||||||
@ -305,7 +278,7 @@ const FileEditorThumbnail = ({
|
|||||||
|
|
||||||
<button
|
<button
|
||||||
className={styles.actionRow}
|
className={styles.actionRow}
|
||||||
onClick={() => { downloadSelectedFile(); setShowActions(false); }}
|
onClick={() => { onDownloadFile(file.id); setShowActions(false); }}
|
||||||
>
|
>
|
||||||
<DownloadOutlinedIcon fontSize="small" />
|
<DownloadOutlinedIcon fontSize="small" />
|
||||||
<span>{t('download', 'Download')}</span>
|
<span>{t('download', 'Download')}</span>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user