"use client"; import { useKindeBrowserClient } from "@kinde-oss/kinde-auth-nextjs"; import { useState } from "react"; import { useQueryClient } from "@tanstack/react-query"; interface FileData { id: string; filename: string; mimetype: string; size: number; createdAt: string; updatedAt: string; } export const UploadForm = ({ onFileUploaded }: { onFileUploaded?: (file: FileData) => void }) => { const [isUploading, setIsUploading] = useState(false); const queryClient = useQueryClient(); const { user } = useKindeBrowserClient(); const handleFileUpload = async (e: React.ChangeEvent) => { if (e.target.files && e.target.files[0]) { setIsUploading(true); const file = e.target.files[0]; try { // Import the server action const { uploadFile } = await import('../actions/file-upload'); const userId = user?.id ? user.id : "unknown-user-id"; const uploadedFile = await uploadFile(file, userId); // Invalidate the files query to trigger a refresh await queryClient.invalidateQueries({ queryKey: ["files"] }); // Call the callback if provided if (onFileUploaded) { onFileUploaded(uploadedFile); } // Reset the file input e.target.value = ''; } catch (error) { console.error("Upload error:", error); alert("Failed to upload file"); } finally { setIsUploading(false); } } }; return (
{isUploading && (
Uploading...
)}
); };