From 1d302eb2171d85bb335e2d3fd398d72bef125dfc Mon Sep 17 00:00:00 2001
From: ZareMate <0.zaremate@gmail.com>
Date: Sun, 4 May 2025 17:00:56 +0200
Subject: [PATCH] feat: add FileDescriptionContainer for managing file
 descriptions and integrate with FileActions
---
 src/app/_components/ActionButtons.tsx | 37 +++++++++++++++++++++++++--
 src/app/_components/FileActions.tsx   | 11 +++++---
 src/app/api/files/share/route.ts      |  3 ++-
 src/app/share/page.tsx                |  8 +++---
 4 files changed, 49 insertions(+), 10 deletions(-)
diff --git a/src/app/_components/ActionButtons.tsx b/src/app/_components/ActionButtons.tsx
index 336a711..dcd89a4 100644
--- a/src/app/_components/ActionButtons.tsx
+++ b/src/app/_components/ActionButtons.tsx
@@ -1,5 +1,6 @@
 'use client';
-import {useFileActions} from "~/app/_components/FileActions";
+import { useRef, useState } from "react";
+import { useFileActions } from "~/app/_components/FileActions";
 
 export function FileActionsContainer({
   fileId,
@@ -12,7 +13,7 @@ export function FileActionsContainer({
   fileUrl: string;
   isOwner: boolean;
 }) {
-  const { handleDownload, handleCopyUrl, handleRemove } = useFileActions(() => fileId, (description: string) => {
+  const { handleDownload, handleCopyUrl, handleRemove} = useFileActions(() => fileId, (description: string) => {
     if (isOwner) {
       console.log(description);
     }
@@ -84,4 +85,36 @@ export function FileActionsContainer({
       
     
   );
+}
+
+export function FileDescriptionContainer({
+  fileId,
+  fileDescriprtion,
+}: {
+  fileId: string;
+  fileDescriprtion?: string;
+}) {
+
+  const [description, setDescription] = useState(fileDescriprtion || ""); // Add state for description
+  const { handleDescriptionChange } = useFileActions(() => {}, (description: string) => {
+    setDescription(description);
+    return undefined;
+  }, fileId); // Wrap setDescription in a function
+  const debounceTimer = useRef(null); // Initialize debounce timer
+
+  const handleChange = (e: React.ChangeEvent) => {
+    handleDescriptionChange(e, debounceTimer); // Pass the debounce timer
+  };
+
+  return (
+    
+      
+    
+  );
 }
\ No newline at end of file
diff --git a/src/app/_components/FileActions.tsx b/src/app/_components/FileActions.tsx
index 5bda400..04843c0 100644
--- a/src/app/_components/FileActions.tsx
+++ b/src/app/_components/FileActions.tsx
@@ -4,7 +4,7 @@ import { notifyClients } from "~/utils/notifyClients";
 
 export const useFileActions = (
   setFiles: (callback: (prevFiles: any[]) => any[]) => void,
-  setDescription?: (description: string) => void,
+  setDescription?: (description: string) => undefined,
   fileId?: string
 ) => {
   const pageUrl = `${env.NEXT_PUBLIC_PAGE_URL}`;
@@ -75,7 +75,9 @@ export const useFileActions = (
     e: React.ChangeEvent,
     debounceTimer: React.RefObject
   ) => {
-    if (!setDescription) return;
+    if (setDescription === undefined) {console.error("setDescription function is not provided") 
+      return;
+    };
 
     const newDescription = e.target.value;
     setDescription(newDescription);
@@ -83,7 +85,9 @@ export const useFileActions = (
     if (debounceTimer.current) {
       clearTimeout(debounceTimer.current);
     }
+
     debounceTimer.current = setTimeout(() => {
+      console.log("Calling handleDescriptionSave"); // Debug log
       handleDescriptionSave(newDescription);
     }, 1000);
   };
@@ -99,7 +103,8 @@ export const useFileActions = (
       const response = await fetch(`/api/files/share?id=${encodeURIComponent(fileId)}`, {
         method: "PUT",
         headers: { "Content-Type": "application/json" },
-        body: JSON.stringify({ description }),
+        // pass the fileId and description in the request body
+        body: JSON.stringify({ description, id: fileId }),
       });
 
       if (response.status === 403) {
diff --git a/src/app/api/files/share/route.ts b/src/app/api/files/share/route.ts
index bfc2978..6b92650 100644
--- a/src/app/api/files/share/route.ts
+++ b/src/app/api/files/share/route.ts
@@ -48,7 +48,8 @@ export async function PUT(req: Request) {
 
   try {
     const body = (await req.json()) as { id: string; description: string } | null;
-    if (!body?.id || !body.description) {
+    if (!body?.id || body.description === undefined) {
+      // Allow empty description but ensure id is present
       return NextResponse.json({ error: "Invalid request body" }, { status: 400 });
     }
 
diff --git a/src/app/share/page.tsx b/src/app/share/page.tsx
index 4dd29c0..7b86c5e 100644
--- a/src/app/share/page.tsx
+++ b/src/app/share/page.tsx
@@ -2,7 +2,7 @@ import { notFound } from "next/navigation";
 import { FilePreview } from "~/app/_components/FilePreview";
 import { HomeButton } from "~/app/_components/HomeButton"; // Import the client component
 import { Toaster } from "react-hot-toast";
-import { FileActionsContainer } from "~/app/_components/ActionButtons"; // Import the client component
+import { FileActionsContainer, FileDescriptionContainer } from "~/app/_components/ActionButtons"; // Import the client component
 import Head from "next/head";
 
 interface FileDetails {
@@ -143,10 +143,10 @@ export default async function FilePreviewContainer({
               Upload Date:{" "}
               {new Date(fileDetails.uploadDate).toLocaleString()}
             
-            
+            
               Description:{" "}
-              {fileDetails.description || "No description available"}
-            
+              
+