From b368473216d7dd9f08770443861b135e2b6b798f Mon Sep 17 00:00:00 2001
From: ZareMate <0.zaremate@gmail.com>
Date: Mon, 12 May 2025 10:11:32 +0200
Subject: [PATCH] feat: enhance file actions with toast notifications and
update public/private toggle logic
---
public/icons/private.svg | 1 +
public/icons/public.svg | 1 +
src/app/_components/ActionButtons.tsx | 138 +++++++++++++++-----------
src/app/_components/FileGrid.tsx | 4 +-
src/app/share/page.tsx | 61 +++---------
5 files changed, 96 insertions(+), 109 deletions(-)
create mode 100644 public/icons/private.svg
create mode 100644 public/icons/public.svg
diff --git a/public/icons/private.svg b/public/icons/private.svg
new file mode 100644
index 0000000..5c0eb3f
--- /dev/null
+++ b/public/icons/private.svg
@@ -0,0 +1 @@
+
\ No newline at end of file
diff --git a/public/icons/public.svg b/public/icons/public.svg
new file mode 100644
index 0000000..fc36d82
--- /dev/null
+++ b/public/icons/public.svg
@@ -0,0 +1 @@
+
\ No newline at end of file
diff --git a/src/app/_components/ActionButtons.tsx b/src/app/_components/ActionButtons.tsx
index 2253ca9..23b2f59 100644
--- a/src/app/_components/ActionButtons.tsx
+++ b/src/app/_components/ActionButtons.tsx
@@ -1,13 +1,14 @@
"use client";
import { useRef, useState } from "react";
import { useFileActions } from "~/app/_components/FileActions";
+import toast from "react-hot-toast";
export function FileActionsContainer({
fileId,
fileName,
fileUrl,
isOwner,
- isPublic,
+ isPublic: initialIsPublic, // Rename to avoid conflict with local state
}: {
fileId: string;
fileName: string;
@@ -15,77 +16,91 @@ export function FileActionsContainer({
isOwner: boolean;
isPublic: boolean;
}) {
- const { handleDownload, handleCopyUrl, handleRemove} = useFileActions(() => fileId, (description: string) => {
- if (isOwner) {
- console.log(description);
+ const [isPublic, setIsPublic] = useState(initialIsPublic); // Local state for toggle
+ const { handleDownload, handleCopyUrl, handleRemove } = useFileActions(
+ () => fileId,
+ (description: string) => {
+ if (isOwner) {
+ console.log(description);
+ }
}
- });
+ );
return (
-
+
{/* Download Button */}
handleDownload(fileId, fileName)}
- className="flex items-center justify-center rounded-full bg-blue-500 p-2 hover:bg-blue-600"
+ onClick={() => handleDownload(fileId, fileName)}
+ className="flex items-center justify-center rounded-full bg-blue-500 p-2 hover:bg-blue-600"
>
-
+
{/* Copy URL Button */}
handleCopyUrl(fileUrl)}
- className="flex items-center justify-center rounded-full bg-green-500 p-2 hover:bg-green-600"
+ onClick={() => {
+ handleCopyUrl(fileUrl);
+ toast.success("File URL copied to clipboard!");
+ }}
+ className="flex items-center justify-center rounded-full bg-green-500 p-2 hover:bg-green-600"
>
-
+
+
{/* Remove Button */}
{isOwner && (
handleRemove(fileId)}
+ onClick={async () => {
+ try {
+ await handleRemove(fileId);
+ toast.success("File removed successfully!");
+ } catch (err) {
+ toast.error("Failed to remove file.");
+ console.error(err);
+ }
+ }}
className="flex items-center justify-center rounded-full bg-red-500 p-2 hover:bg-red-600"
>
-
+
)}
+
+ {/* Public/Private Toggle */}
{isOwner && (
-
-
- Public:
-
-
- {
- const newIsPublic = e.target.checked;
- try {
- const response = await fetch(`/api/files/update-public`, {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify({ fileId: fileId, isPublic: newIsPublic }),
- });
- if (!response.ok) {
- throw new Error("Failed to update public status");
- }
- console.log("Public status updated successfully");
- } catch (err) {
- console.error("Error updating public status:", err);
- }
- }}
- className="sr-only peer"
- />
- {/* Toggle Background */}
-
- {/* Toggle Handle */}
-
-
-
+
{
+ const newIsPublic = !isPublic;
+ try {
+ const response = await fetch(`/api/files/update-public`, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ fileId: fileId,
+ isPublic: newIsPublic,
+ }),
+ });
+ if (!response.ok) {
+ throw new Error("Failed to update public status");
+ }
+ setIsPublic(newIsPublic); // Update local state
+ toast.success(
+ `File is now ${newIsPublic ? "Public" : "Private"}!`
+ );
+ } catch (err) {
+ toast.error("Error updating public status.");
+ console.error(err);
+ }
+ }}
+ className="flex items-center justify-center rounded-full bg-gray-500 p-2 hover:bg-gray-600"
+ >
+
+
)}
);
@@ -98,12 +113,15 @@ export function FileDescriptionContainer({
fileId: string;
fileDescription?: string;
}) {
-
const [description, setDescription] = useState(fileDescription || ""); // Add state for description
- const { handleDescriptionChange } = useFileActions(() => {}, (description: string) => {
- setDescription(description);
- return undefined;
- }, fileId); // Wrap setDescription in a function
+ 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) => {
@@ -111,9 +129,9 @@ export function FileDescriptionContainer({
};
return (
-
+
);
-}
\ No newline at end of file
+}
diff --git a/src/app/_components/FileGrid.tsx b/src/app/_components/FileGrid.tsx
index 091566f..93cccc7 100644
--- a/src/app/_components/FileGrid.tsx
+++ b/src/app/_components/FileGrid.tsx
@@ -15,7 +15,7 @@ interface FileDetails {
description: string;
extension: string;
isOwner: boolean; // Indicates if the user owns the file
- isPublic: boolean; // Indicates if the file is public
+ public: boolean; // Indicates if the file is public
}
interface FileGridProps {
@@ -118,7 +118,7 @@ export default function FileGrid({ session }: FileGridProps) {
fileName={file.name}
fileUrl={file.url}
isOwner={true}
- isPublic={file.isPublic}
+ isPublic={file.public}
/>
diff --git a/src/app/share/page.tsx b/src/app/share/page.tsx
index 73a017c..fa13e1a 100644
--- a/src/app/share/page.tsx
+++ b/src/app/share/page.tsx
@@ -67,9 +67,9 @@ async function fetchFileDetails(fileId: string): Promise {
try {
const response = await fetch(
`${process.env.NEXT_PUBLIC_PAGE_URL}/api/files/share?id=${encodeURIComponent(
- fileId
+ fileId,
)}`,
- { cache: "no-store" }
+ { cache: "no-store" },
);
if (!response.ok) {
@@ -96,7 +96,6 @@ export default async function FilePreviewContainer({
}
const fileDetails = await fetchFileDetails(fileId);
-
if (!fileDetails) {
return (
@@ -138,10 +137,10 @@ export default async function FilePreviewContainer({
{fileDetails.size > 1024 * 1024 * 1024
? (fileDetails.size / (1024 * 1024 * 1024)).toFixed(2) + " GB"
: fileDetails.size > 1024 * 1024
- ? (fileDetails.size / (1024 * 1024)).toFixed(2) + " MB"
- : fileDetails.size > 1024
- ? (fileDetails.size / 1024).toFixed(2) + " KB"
- : fileDetails.size + " Bytes"}
+ ? (fileDetails.size / (1024 * 1024)).toFixed(2) + " MB"
+ : fileDetails.size > 1024
+ ? (fileDetails.size / 1024).toFixed(2) + " KB"
+ : fileDetails.size + " Bytes"}
Owner: {" "}
@@ -163,46 +162,14 @@ export default async function FilePreviewContainer({
fileDescription={fileDetails.description}
/>
-
-
- Name: {fileDetails.name}
-
-
- Size: {" "}
- {fileDetails.size > 1024 * 1024 * 1024
- ? (fileDetails.size / (1024 * 1024 * 1024)).toFixed(2) + " GB"
- : fileDetails.size > 1024 * 1024
- ? (fileDetails.size / (1024 * 1024)).toFixed(2) + " MB"
- : fileDetails.size > 1024
- ? (fileDetails.size / 1024).toFixed(2) + " KB"
- : fileDetails.size + " Bytes"}
-
-
- Owner: {" "}
- {" "}
- {fileDetails.owner}
-
-
- Upload Date: {" "}
- {new Date(fileDetails.uploadDate).toLocaleString()}
-
-
- Description: {" "}
-
-
-
-
-
+
+