memos/web/src/components/MemoEditor/hooks/useFileUpload.ts
Johnny e761ef8684 chore: move memo-metadata components to MemoView and MemoEditor
- Remove shared memo-metadata folder
- Move metadata display components (AttachmentList, LocationDisplay, RelationList) to MemoView/components/metadata
- Move attachment types and utilities (LocalFile, AttachmentItem, toAttachmentItems) to MemoEditor/types/attachment
- Simplify AttachmentList and AttachmentCard to work directly with Attachment proto
- Update all imports across MemoEditor and MemoView components
- Better separation of concerns: MemoView handles display, MemoEditor handles local files + attachments
2026-01-03 13:07:53 +08:00

34 lines
1.1 KiB
TypeScript

import { useRef } from "react";
import type { LocalFile } from "../types/attachment";
export const useFileUpload = (onFilesSelected: (localFiles: LocalFile[]) => void) => {
const fileInputRef = useRef<HTMLInputElement>(null);
const selectingFlagRef = useRef(false);
const handleFileInputChange = (event?: React.ChangeEvent<HTMLInputElement>) => {
const files = Array.from(fileInputRef.current?.files || event?.target.files || []);
if (files.length === 0 || selectingFlagRef.current) {
return;
}
selectingFlagRef.current = true;
const localFiles: LocalFile[] = files.map((file) => ({
file,
previewUrl: URL.createObjectURL(file),
}));
onFilesSelected(localFiles);
selectingFlagRef.current = false;
// Optionally clear input value to allow re-selecting the same file
if (fileInputRef.current) fileInputRef.current.value = "";
};
const handleUploadClick = () => {
fileInputRef.current?.click();
};
return {
fileInputRef,
selectingFlag: selectingFlagRef.current,
handleFileInputChange,
handleUploadClick,
};
};