|
|
@@ -1,6 +1,6 @@
|
|
|
"use client";
|
|
|
|
|
|
-import { useState } from "react";
|
|
|
+import React, { useState, useEffect } from "react";
|
|
|
import {
|
|
|
ColumnDef,
|
|
|
flexRender,
|
|
|
@@ -22,9 +22,14 @@ interface FileData {
|
|
|
updatedAt: string;
|
|
|
}
|
|
|
|
|
|
-export function FilesTable() {
|
|
|
+interface FilesTableProps {
|
|
|
+ onFileAdded?: (file: FileData) => void;
|
|
|
+}
|
|
|
+
|
|
|
+export function FilesTable({ onFileAdded }: FilesTableProps) {
|
|
|
const [sorting, setSorting] = useState<SortingState>([]);
|
|
|
const [rowSelection, setRowSelection] = useState<RowSelectionState>({});
|
|
|
+ const [files, setFiles] = useState<FileData[]>([]);
|
|
|
|
|
|
const { data, isLoading, isError, error, refetch } = useQuery({
|
|
|
queryKey: ["files"],
|
|
|
@@ -38,6 +43,30 @@ export function FilesTable() {
|
|
|
},
|
|
|
});
|
|
|
|
|
|
+ // Update local files state when data changes
|
|
|
+ useEffect(() => {
|
|
|
+ if (data) {
|
|
|
+ setFiles(data);
|
|
|
+ }
|
|
|
+ }, [data]);
|
|
|
+
|
|
|
+ // Method to add a new file to the table
|
|
|
+ const addFile = (file: FileData) => {
|
|
|
+ setFiles(prevFiles => [file, ...prevFiles]);
|
|
|
+ // Call the onFileAdded prop if provided
|
|
|
+ if (onFileAdded) {
|
|
|
+ onFileAdded(file);
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ // Make addFile available globally for the upload form
|
|
|
+ useEffect(() => {
|
|
|
+ (window as any).addFileToTable = addFile;
|
|
|
+ return () => {
|
|
|
+ delete (window as any).addFileToTable;
|
|
|
+ };
|
|
|
+ }, [onFileAdded]);
|
|
|
+
|
|
|
const columns: ColumnDef<FileData>[] = [
|
|
|
{
|
|
|
id: "select",
|
|
|
@@ -170,7 +199,7 @@ export function FilesTable() {
|
|
|
];
|
|
|
|
|
|
const table = useReactTable({
|
|
|
- data: data || [],
|
|
|
+ data: files,
|
|
|
columns,
|
|
|
state: {
|
|
|
sorting,
|
|
|
@@ -439,7 +468,7 @@ export function FilesTable() {
|
|
|
{/* Pagination */}
|
|
|
<div className="flex items-center justify-between mt-4">
|
|
|
<div className="text-sm text-gray-700">
|
|
|
- Showing {table.getRowModel().rows.length} of {data.length} results
|
|
|
+ Showing {table.getRowModel().rows.length} of {files.length} results
|
|
|
</div>
|
|
|
<div className="flex gap-2">
|
|
|
<button
|