import React, { useState, useRef, useEffect, act } from "react"; import img from "../../assets/image/image.png"; import { useNavigate } from "react-router-dom"; import { getUserData } from "../../functions/getUserData"; import { useLoadingProgress, useProjectName, useSocketStore } from "../../store/builder/store"; import { viewProject } from "../../services/dashboard/viewProject"; import OuterClick from "../../utils/outerClick"; import { KebabIcon } from "../icons/ExportCommonIcons"; import { getAllProjects } from "../../services/dashboard/getAllProjects"; interface DashBoardCardProps { projectName: string; thumbnail: any; projectId: string; createdAt?: string; isViewed?: string; createdBy?: { _id: string, userName: string }; handleDeleteProject?: (projectId: string) => Promise; handleTrashDeleteProject?: (projectId: string) => Promise; handleRestoreProject?: (projectId: string) => Promise; handleDuplicateWorkspaceProject?: ( projectId: string, projectName: string, thumbnail: string, userId?: string ) => Promise; handleDuplicateRecentProject?: ( projectId: string, projectName: string, thumbnail: string ) => Promise; active?: string; setIsSearchActive?: React.Dispatch>; setRecentDuplicateData?: React.Dispatch>; setProjectDuplicateData?: React.Dispatch>; setActiveFolder?: React.Dispatch>; } type RelativeTimeFormatUnit = any; const DashboardCard: React.FC = ({ projectName, thumbnail, projectId, active, handleDeleteProject, handleRestoreProject, handleTrashDeleteProject, handleDuplicateWorkspaceProject, handleDuplicateRecentProject, createdAt, createdBy, setRecentDuplicateData, setProjectDuplicateData, setActiveFolder }) => { const navigate = useNavigate(); const { setProjectName } = useProjectName(); const { userId, organization, userName } = getUserData(); const [isKebabOpen, setIsKebabOpen] = useState(false); const [renameValue, setRenameValue] = useState(projectName); const [isRenaming, setIsRenaming] = useState(false); const { projectSocket } = useSocketStore(); const { setLoadingProgress } = useLoadingProgress(); const kebabRef = useRef(null); const navigateToProject = async (e: any) => { console.log('active: ', active); if (active && active == "trash") return; try { const viewProjects = await viewProject(organization, projectId, userId) console.log('viewProjects: ', viewProjects); console.log('projectName: ', projectName); setLoadingProgress(1) setProjectName(projectName); navigate(`/${projectId}`); } catch { } }; const handleOptionClick = async (option: string) => { switch (option) { case "delete": if (handleDeleteProject) { handleDeleteProject(projectId); } else if (handleTrashDeleteProject) { handleTrashDeleteProject(projectId); } break; case "restore": if (handleRestoreProject) { await handleRestoreProject(projectId); } break; case "open in new tab": try { if (active === "shared" && createdBy) { console.log("ihreq"); const newTab = await viewProject(organization, projectId, createdBy?._id); console.log('newTab: ', newTab); } else { const newTab = await viewProject(organization, projectId, userId); console.log('newTab: ', newTab); setProjectName(projectName); setIsKebabOpen(false); } } catch (error) { } window.open(`/${projectId}`, "_blank"); break; case "rename": setIsRenaming(true); break; case "duplicate": if (handleDuplicateWorkspaceProject) { setProjectDuplicateData && setProjectDuplicateData({ projectId, projectName, thumbnail, }); await handleDuplicateWorkspaceProject(projectId, projectName, thumbnail, userId); if (active === "shared" && setActiveFolder) { setActiveFolder("myProjects") } } else if (handleDuplicateRecentProject) { setRecentDuplicateData && setRecentDuplicateData({ projectId, projectName, thumbnail, userId }); await handleDuplicateRecentProject(projectId, projectName, thumbnail); } break; default: break; } setIsKebabOpen(false); }; OuterClick({ contextClassName: ["kebab-wrapper", "kebab-options-wrapper"], setMenuVisible: () => setIsKebabOpen(false), }); const handleProjectName = async (projectName: string) => { setRenameValue(projectName); if (!projectId) return; try { const projects = await getAllProjects(userId, organization); if (!projects || !projects.Projects) return; let projectUuid = projects.Projects.find( (val: any) => val.projectUuid === projectId || val._id === projectId ); const updateProjects = { projectId: projectUuid, organization, userId, projectName, thumbnail: undefined, }; if (projectSocket) { projectSocket.emit("v1:project:update", updateProjects); } } catch (error) { } }; function getRelativeTime(dateString: string): string { const date = new Date(dateString); const now = new Date(); const diffInSeconds = Math.floor((now.getTime() - date.getTime()) / 1000); const intervals: Record = { year: 31536000, month: 2592000, week: 604800, day: 86400, hour: 3600, minute: 60, second: 1, }; const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" }); for (const key in intervals) { const unit = key as RelativeTimeFormatUnit; const diff = Math.floor(diffInSeconds / intervals[unit]); if (diff >= 1) { return rtf.format(-diff, unit); } } return "just now"; } const kebabOptionsMap: Record = { default: ["rename", "delete", "duplicate", "open in new tab"], trash: ["restore", "delete"], shared: ["duplicate", "open in new tab"], }; const getOptions = () => { if (active === "trash") return kebabOptionsMap.trash; if (active === "shared") return kebabOptionsMap.shared; if (createdBy && createdBy?._id !== userId) return kebabOptionsMap.shared; return kebabOptionsMap.default; }; return ( {isKebabOpen && (
{getOptions().map((option) => ( ))}
)} ); }; export default DashboardCard;