import MainLayout from "@/layouts/MainLayout"; import { useEffect, useState } from "react"; import Link from "next/link"; import useWindowDimensions from "@/hooks/useWindowDimensions"; import React from "react"; import { toast } from "react-hot-toast"; import { MigrationFormat, MigrationRequest, ViewMode } from "@/types/global"; import DashboardItem from "@/components/DashboardItem"; import NewLinkModal from "@/components/ModalContent/NewLinkModal"; import PageHeader from "@/components/PageHeader"; import ViewDropdown from "@/components/ViewDropdown"; import { dropdownTriggerer } from "@/lib/client/utils"; import getServerSideProps from "@/lib/client/getServerSideProps"; import { useTranslation } from "next-i18next"; import { useCollections } from "@/hooks/store/collections"; import { useTags } from "@/hooks/store/tags"; import { useDashboardData } from "@/hooks/store/dashboardData"; import Links from "@/components/LinkViews/Links"; export default function Dashboard() { const { t } = useTranslation(); const { data: collections = [] } = useCollections(); const dashboardData = useDashboardData(); const { data: tags = [] } = useTags(); const [numberOfLinks, setNumberOfLinks] = useState(0); const [showLinks, setShowLinks] = useState(3); useEffect(() => { setNumberOfLinks( collections.reduce( (accumulator, collection) => accumulator + (collection._count as any).links, 0 ) ); }, [collections]); const handleNumberOfLinksToShow = () => { if (window.innerWidth > 1900) { setShowLinks(10); } else if (window.innerWidth > 1500) { setShowLinks(8); } else if (window.innerWidth > 880) { setShowLinks(6); } else if (window.innerWidth > 550) { setShowLinks(4); } else setShowLinks(2); }; const { width } = useWindowDimensions(); useEffect(() => { handleNumberOfLinksToShow(); }, [width]); const importBookmarks = async ( e: React.ChangeEvent, format: MigrationFormat ) => { const file: File | null = e.target.files && e.target.files[0]; if (file) { const reader = new FileReader(); reader.readAsText(file, "UTF-8"); reader.onload = async function (e) { const load = toast.loading("Importing..."); const request: string = e.target?.result as string; const body: MigrationRequest = { format, data: request, }; const response = await fetch("/api/v1/migration", { method: "POST", body: JSON.stringify(body), }); await response.json(); toast.dismiss(load); toast.success("Imported the Bookmarks! Reloading the page..."); setTimeout(() => { location.reload(); }, 2000); }; reader.onerror = function (e) { console.log("Error:", e); }; } }; const [newLinkModal, setNewLinkModal] = useState(false); const [viewMode, setViewMode] = useState( (localStorage.getItem("viewMode") as ViewMode) || ViewMode.Card ); return (
{t("view_all")}
{dashboardData.isLoading ? (
) : dashboardData.data && dashboardData.data[0] && !dashboardData.isLoading ? (
) : (

{t("view_added_links_here")}

{t("view_added_links_here_desc")}

{ setNewLinkModal(true); }} className="inline-flex items-center gap-2 text-sm btn btn-accent dark:border-violet-400 text-white" > {t("add_link")}

{t("import_links")}

)}
{t("view_all")}
{dashboardData.isLoading ? (
) : dashboardData.data?.some((e) => e.pinnedBy && e.pinnedBy[0]) ? (
e.pinnedBy && e.pinnedBy[0]) .slice(0, showLinks)} layout={viewMode} />
) : (

{t("pin_favorite_links_here")}

{t("pin_favorite_links_here_desc")}

)}
{newLinkModal && setNewLinkModal(false)} />}
); } export { getServerSideProps };