⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.
TypeScript bookmarkbookmark-managercollaborationnextjsreact-nativeread-it-laterself-hostedtypescript
cleaner code
daniel31x13c79b1e649202
diff --git a/components/LinkDetails.tsx b/components/LinkDetails.tsxindex ab0955dd..ad6e5526 100644--- a/components/LinkDetails.tsx+++ b/components/LinkDetails.tsx@@ -27,7 +27,7 @@ import IconPopover from "./IconPopover";import TextInput from "./TextInput";import usePermissions from "@/hooks/usePermissions";import oklchVariableToHex from "@/lib/client/oklchVariableToHex";-import Preservation from "./Preservation";+import Preservation from "./Preservation/Preservation";import useWindowDimensions from "@/hooks/useWindowDimensions";import PreservedFormatRow from "./PreserverdFormatRow";import { BeatLoader } from "react-spinners";@@ -201,7 +201,7 @@ export default function LinkDetails({>{width >= 640 && (<div className="w-1/2 lg:w-2/3 overflow-y-auto">- {link.id && <Preservation link={link} standalone={standalone} />}+ {link.id && <Preservation link={link} />}</div>)}<div className={clsx(width >= 640 && "w-1/2 lg:w-1/3 overflow-y-auto")}>diff --git a/components/Preservation.tsx b/components/Preservation/Preservation.tsxsimilarity index 75%rename from components/Preservation.tsxrename to components/Preservation/Preservation.tsxindex 8bc4063b..ab76a1b2 100644--- a/components/Preservation.tsx+++ b/components/Preservation/Preservation.tsx@@ -6,12 +6,12 @@ import {} from "@/types/global";import { useTranslation } from "next-i18next";import { useRouter } from "next/router";-import ReadableView from "@/components/ReadableView";import clsx from "clsx";import { formatAvailable } from "@/lib/shared/formatStats";-import { PreservationSkeleton } from "./Skeletons";+import { PreservationSkeleton } from "../Skeletons";import remToPixels from "@/lib/client/remToPixels";import { useReducedMotion } from "framer-motion";+import { PreservationContent } from "./PreservationContent";export default function Preservation({link,@@ -24,9 +24,6 @@ export default function Preservation({const router = useRouter();const [format, setFormat] = useState<ArchivedFormat>();- const [pdfLoaded, setPdfLoaded] = useState(false);- const [monolithLoaded, setMonolithLoaded] = useState(false);- const [imageLoaded, setImageLoaded] = useState(false);const [isExpanded, setIsExpanded] = useState(false);const [delayPassed, setDelayPassed] = useState(false); // for the modal animation@@ -73,18 +70,6 @@ export default function Preservation({}}, [router.query, link]);- const prevFormatRef = useRef<ArchivedFormat | undefined>(undefined);-- useEffect(() => {- if (prevFormatRef.current !== format) {- setPdfLoaded(false);- setMonolithLoaded(false);- setImageLoaded(false);-- prevFormatRef.current = format;- }- }, [format]);-useEffect(() => {if (router.pathname.includes("links")) {const query = {@@ -229,13 +214,6 @@ export default function Preservation({link={link}format={format}isExpanded={isExpanded}- setMonolithLoaded={setMonolithLoaded}- setPdfLoaded={setPdfLoaded}- setImageLoaded={setImageLoaded}- monolithLoaded={monolithLoaded}- pdfLoaded={pdfLoaded}- imageLoaded={imageLoaded}- standalone={standalone}containerRef={containerRef}setIsExpanded={setIsExpanded}/>@@ -251,26 +229,12 @@ const RenderFormat = ({link,format,isExpanded,- setMonolithLoaded,- setPdfLoaded,- setImageLoaded,- monolithLoaded,- pdfLoaded,- imageLoaded,- standalone,containerRef,setIsExpanded,}: {link: LinkIncludingShortenedCollectionAndTags;format: ArchivedFormat;isExpanded: boolean;- setMonolithLoaded: (loaded: boolean) => void;- setPdfLoaded: (loaded: boolean) => void;- setImageLoaded: (loaded: boolean) => void;- monolithLoaded: boolean;- pdfLoaded: boolean;- imageLoaded: boolean;- standalone?: boolean;containerRef: React.RefObject<HTMLDivElement>;setIsExpanded: (expanded: boolean) => void;}) => {@@ -336,68 +300,6 @@ const RenderFormat = ({: "none",};- const content = () => {- switch (format) {- case ArchivedFormat.readability:- return (- <div className="overflow-auto w-full h-full">- <ReadableView- link={link}- isExpanded={isExpanded}- standalone={standalone}- />- </div>- );- case ArchivedFormat.monolith:- return (- <>- {!monolithLoaded && <PreservationSkeleton />}- <iframe- src={`/api/v1/archives/${link.id}?format=${ArchivedFormat.monolith}&_=${link.updatedAt}`}- className={clsx(- "w-full border-none",- monolithLoaded ? "block" : "hidden",- isExpanded ? "h-full" : "h-[calc(80vh-3.75rem)]"- )}- onLoad={() => setMonolithLoaded(true)}- />- </>- );- case ArchivedFormat.pdf:- return (- <>- {!pdfLoaded && <PreservationSkeleton />}- <iframe- src={`/api/v1/archives/${link.id}?format=${ArchivedFormat.pdf}&_=${link.updatedAt}`}- className={clsx(- "w-full border-none",- pdfLoaded ? "block" : "hidden",- isExpanded ? "h-full" : "h-[calc(80vh-3.75rem)]"- )}- onLoad={() => setPdfLoaded(true)}- />- </>- );- case ArchivedFormat.png:- case ArchivedFormat.jpeg:- return (- <>- {!imageLoaded && <PreservationSkeleton />}- <div className="overflow-auto w-fit mx-auto h-full">- <img- alt=""- src={`/api/v1/archives/${link.id}?format=${format}&_=${link.updatedAt}`}- className={clsx("w-fit mx-auto", !imageLoaded && "hidden")}- onLoad={() => setImageLoaded(true)}- />- </div>- </>- );- default:- return <></>;- }- };-return ReactDOM.createPortal(<divstyle={style}@@ -407,7 +309,11 @@ const RenderFormat = ({)}data-ignore-click-away>- {content()}+ <PreservationContent+ link={link}+ format={format}+ isExpanded={isExpanded}+ />{isExpanded && (<divclassName="absolute top-3 right-3 btn btn-circle btn-primary btn-sm"diff --git a/components/Preservation/PreservationContent.tsx b/components/Preservation/PreservationContent.tsxnew file mode 100644index 00000000..82b9462d--- /dev/null+++ b/components/Preservation/PreservationContent.tsx@@ -0,0 +1,97 @@+import React, { useEffect, useRef, useState } from "react";+import clsx from "clsx";+import ReadableView from "@/components/Preservation/ReadableView";+import { PreservationSkeleton } from "../Skeletons";+import {+ ArchivedFormat,+ LinkIncludingShortenedCollectionAndTags,+} from "@/types/global";++type Props = {+ format: ArchivedFormat;+ isExpanded: boolean;+ link?: LinkIncludingShortenedCollectionAndTags;+};++export const PreservationContent: React.FC<Props> = ({+ link,+ format,+ isExpanded,+}) => {+ const [pdfLoaded, setPdfLoaded] = useState(false);+ const [monolithLoaded, setMonolithLoaded] = useState(false);+ const [imageLoaded, setImageLoaded] = useState(false);+ const prevFormatRef = useRef<ArchivedFormat | undefined>(undefined);++ useEffect(() => {+ if (prevFormatRef.current !== format) {+ setPdfLoaded(false);+ setMonolithLoaded(false);+ setImageLoaded(false);++ prevFormatRef.current = format;+ }+ }, [format]);++ if (!link?.id) return <></>;++ const renderFormat = () => {+ switch (format) {+ case ArchivedFormat.readability:+ return (+ <div className="overflow-auto w-full h-full">+ <ReadableView link={link} isExpanded={isExpanded} />+ </div>+ );+ case ArchivedFormat.monolith:+ return (+ <>+ {!monolithLoaded && <PreservationSkeleton />}+ <iframe+ src={`/api/v1/archives/${link.id}?format=${ArchivedFormat.monolith}&_=${link.updatedAt}`}+ className={clsx(+ "w-full border-none",+ monolithLoaded ? "block" : "hidden",+ isExpanded ? "h-full" : "h-[calc(80vh-3.75rem)]"+ )}+ onLoad={() => setMonolithLoaded(true)}+ />+ </>+ );+ case ArchivedFormat.pdf:+ return (+ <>+ {!pdfLoaded && <PreservationSkeleton />}+ <iframe+ src={`/api/v1/archives/${link.id}?format=${ArchivedFormat.pdf}&_=${link.updatedAt}`}+ className={clsx(+ "w-full border-none",+ pdfLoaded ? "block" : "hidden",+ isExpanded ? "h-full" : "h-[calc(80vh-3.75rem)]"+ )}+ onLoad={() => setPdfLoaded(true)}+ />+ </>+ );+ case ArchivedFormat.png:+ case ArchivedFormat.jpeg:+ return (+ <>+ {!imageLoaded && <PreservationSkeleton />}+ <div className="overflow-auto w-fit mx-auto h-full">+ <img+ alt=""+ src={`/api/v1/archives/${link.id}?format=${format}&_=${link.updatedAt}`}+ className={clsx("w-fit mx-auto", !imageLoaded && "hidden")}+ onLoad={() => setImageLoaded(true)}+ />+ </div>+ </>+ );+ default:+ return <></>;+ }+ };++ return <>{renderFormat()}</>;+};diff --git a/components/Preservation/PreservationPageContent.tsx b/components/Preservation/PreservationPageContent.tsxnew file mode 100644index 00000000..c2b390bd--- /dev/null+++ b/components/Preservation/PreservationPageContent.tsx@@ -0,0 +1,45 @@+import React, { useEffect } from "react";+import { useRouter } from "next/router";+import { useGetLink } from "@/hooks/store/links";+import { PreservationContent } from "./PreservationContent";++export default function PreservationPageContent() {+ const router = useRouter();+ const isPublicRoute = router.pathname.startsWith("/public");++ const { data: link, mutateAsync: fetchLink, error } = useGetLink();++ useEffect(() => {+ fetchLink({+ id: Number(router.query.id),+ isPublicRoute,+ });++ let interval: NodeJS.Timeout | null = null;+ if (+ link &&+ (!link?.image || !link?.pdf || !link?.readable || !link?.monolith)+ ) {+ interval = setInterval(() => {+ fetchLink({ id: link.id as number });+ }, 5000);+ } else if (interval) {+ clearInterval(interval);+ }++ return () => {+ if (interval) clearInterval(interval);+ };+ }, []);++ return (+ <div className="w-screen h-screen bg-base-200">+ <PreservationContent+ link={link}+ format={Number(router.query.format)}+ isExpanded={true}+ standalone+ />+ </div>+ );+}diff --git a/components/ReadableView.tsx b/components/Preservation/ReadableView.tsxsimilarity index 84%rename from components/ReadableView.tsxrename to components/Preservation/ReadableView.tsxindex 63f2e746..2de9aa06 100644--- a/components/ReadableView.tsx+++ b/components/Preservation/ReadableView.tsx@@ -8,7 +8,7 @@ import { useRouter } from "next/router";import React, { useEffect, useState } from "react";import { useTranslation } from "next-i18next";import clsx from "clsx";-import LinkDate from "./LinkViews/LinkComponents/LinkDate";+import LinkDate from "../LinkViews/LinkComponents/LinkDate";import isValidUrl from "@/lib/shared/isValidUrl";import Link from "next/link";import unescapeString from "@/lib/client/unescapeString";@@ -20,7 +20,7 @@ import Image from "@tiptap/extension-image";import TipTapLink from "@tiptap/extension-link";import Highlight from "@tiptap/extension-highlight";import TextAlign from "@tiptap/extension-text-align";-import MenuBar from "./Editor/MenuBar";+import MenuBar from "../Editor/MenuBar";import TaskItem from "@tiptap/extension-task-item";import TaskList from "@tiptap/extension-task-list";import ListItem from "@tiptap/extension-list-item";@@ -28,10 +28,9 @@ import ListItem from "@tiptap/extension-list-item";type Props = {link: LinkIncludingShortenedCollectionAndTags;isExpanded: boolean;- standalone?: boolean;};-export default function ReadableView({ link, isExpanded, standalone }: Props) {+export default function ReadableView({ link, isExpanded }: Props) {const { t } = useTranslation();const [linkContent, setLinkContent] = useState("");const [isEditing, setIsEditing] = useState(false);@@ -152,44 +151,42 @@ export default function ReadableView({ link, isExpanded, standalone }: Props) {<div className="text-sm text-neutral flex justify-between w-full gap-2"><LinkDate link={link} />- {!isPublicRoute &&- !standalone &&- (permissions === true || permissions?.canUpdate) && (- <>- {!isEditing && linkContent ? (+ {!isPublicRoute && (permissions === true || permissions?.canUpdate) && (+ <>+ {!isEditing && linkContent ? (+ <button+ className="flex items-center gap-2 btn btn-ghost btn-sm"+ onClick={startEditing}+ >+ <i className="bi-pencil" />+ {t("edit")}+ </button>+ ) : linkContent ? (+ <div+ className={clsx(+ "flex items-center gap-2",+ isExpanded && "mr-10"+ )}+ ><button- className="flex items-center gap-2 btn btn-ghost btn-sm"- onClick={startEditing}+ className="flex items-center gap-2 btn btn-ghost btn-square btn-sm"+ onClick={cancelEditing}>- <i className="bi-pencil" />- {t("edit")}+ <i className="bi-x text-xl" /></button>- ) : linkContent ? (- <div- className={clsx(- "flex items-center gap-2",- isExpanded && "mr-10"- )}+ <button+ className="flex items-center gap-2 btn btn-primary btn-square btn-sm"+ onClick={() => {+ saveChanges();+ setIsEditing(false);+ }}>- <button- className="flex items-center gap-2 btn btn-ghost btn-square btn-sm"- onClick={cancelEditing}- >- <i className="bi-x text-xl" />- </button>- <button- className="flex items-center gap-2 btn btn-primary btn-square btn-sm"- onClick={() => {- saveChanges();- setIsEditing(false);- }}- >- <i className="bi-check2 text-xl" />- </button>- </div>- ) : null}- </>- )}+ <i className="bi-check2 text-xl" />+ </button>+ </div>+ ) : null}+ </>+ )}</div>{link?.readable?.startsWith("archives") ? (diff --git a/components/PreservationStandalone.tsx b/components/PreservationStandalone.tsxdeleted file mode 100644index fa3125cf..00000000--- a/components/PreservationStandalone.tsx+++ /dev/null@@ -1,282 +0,0 @@-import React, { useEffect, useState } from "react";-import { useRouter } from "next/router";-import { ArchivedFormat } from "@/types/global";-import { useTranslation } from "next-i18next";-import ReadableView from "@/components/ReadableView";-import { useGetLink } from "@/hooks/store/links";-import clsx from "clsx";-import { formatAvailable } from "@/lib/shared/formatStats";--export default function PreservationStandalone() {- const router = useRouter();- const isPublicRoute = router.pathname.startsWith("/public");-- const { data: link, mutateAsync: fetchLink, error } = useGetLink();- const { t } = useTranslation();-- const [format, setFormat] = useState<ArchivedFormat>(- Number(router.query.format)- );-- const [pdfLoaded, setPdfLoaded] = useState(false);- const [monolithLoaded, setMonolithLoaded] = useState(false);- const [imageLoaded, setImageLoaded] = useState(false);-- const [isExpanded, setIsExpanded] = useState(false);-- useEffect(() => {- router.push(- {- query: { id: router.query.id, format },- },- undefined,- { shallow: true }- );-- setPdfLoaded(false);- setMonolithLoaded(false);- setImageLoaded(false);- }, [format]);-- useEffect(() => {- fetchLink({- id: Number(router.query.id),- isPublicRoute,- });-- let interval: NodeJS.Timeout | null = null;- if (- link &&- (!link?.image || !link?.pdf || !link?.readable || !link?.monolith)- ) {- interval = setInterval(() => {- fetchLink({ id: link.id as number });- }, 5000);- } else if (interval) {- clearInterval(interval);- }-- return () => {- if (interval) clearInterval(interval);- };- }, []);-- const Skeleton = () => (- <div className="p-5 m-auto w-full flex flex-col items-center gap-5">- <div className="w-full mr-auto h-4 skeleton rounded-md"></div>- <div className="w-full mr-auto h-4 skeleton rounded-md"></div>- <div className="w-full mr-auto h-4 skeleton rounded-md"></div>- <div className="w-3/4 mr-auto h-4 skeleton rounded-md"></div>- <div className="w-5/6 mr-auto h-4 skeleton rounded-md"></div>- <div className="w-3/4 mr-auto h-4 skeleton rounded-md"></div>- <div className="w-full mr-auto h-4 skeleton rounded-md"></div>- <div className="w-full mr-auto h-4 skeleton rounded-md"></div>- <div className="w-5/6 mr-auto h-4 skeleton rounded-md"></div>- </div>- );-- const renderFormat = () => {- if (!link?.id) return null;-- switch (Number(router.query.format)) {- case ArchivedFormat.readability:- return (- <div className="overflow-auto w-full h-full">- <ReadableView isExpanded={isExpanded} link={link} />- </div>- );-- case ArchivedFormat.monolith:- return (- <>- {!monolithLoaded && <Skeleton />}- <iframe- src={`/api/v1/archives/${link.id}?format=${ArchivedFormat.monolith}`}- className={clsx(- "w-full border-none",- monolithLoaded ? "block" : "hidden",- isExpanded ? "h-full" : "h-[calc(100vh-4.5rem)]"- )}- onLoad={() => setMonolithLoaded(true)}- />- </>- );-- case ArchivedFormat.pdf:- return (- <>- {!pdfLoaded && <Skeleton />}- <iframe- src={`/api/v1/archives/${link.id}?format=${ArchivedFormat.pdf}`}- className={clsx(- "w-full border-none",- pdfLoaded ? "block" : "hidden",- isExpanded ? "h-full" : "h-[calc(100vh-4.5rem)]"- )}- onLoad={() => setPdfLoaded(true)}- />- </>- );-- case ArchivedFormat.png:- case ArchivedFormat.jpeg:- return (- <>- {!imageLoaded && <Skeleton />}- <div className="overflow-auto w-fit mx-auto h-full">- <img- alt=""- src={`/api/v1/archives/${link.id}?format=${Number(- router.query.format- )}`}- className={clsx(- "w-fit h-auto mx-auto", // Ensure block display and proper resizing- !imageLoaded && "hidden"- )}- onLoad={() => setImageLoaded(true)}- />- </div>- </>- );-- default:- return null;- }- };-- const handleDownload = () => {- const path = `/api/v1/archives/${link?.id}?format=${format}`;- fetch(path)- .then((response) => {- if (response.ok) {- const anchorElement = document.createElement("a");- anchorElement.href = path;- anchorElement.download =- format === ArchivedFormat.monolith- ? "Webpage"- : format === ArchivedFormat.pdf- ? "PDF"- : format === ArchivedFormat.readability- ? "Readable"- : "Screenshot";- anchorElement.click();- } else {- console.error("Failed to download file");- }- })- .catch((error) => {- console.error("Error:", error);- });- };-- return (- <div- className={clsx(- !isExpanded && "max-w-screen-lg mx-auto p-3 relative",- isExpanded && "fixed inset-0 w-screen h-screen z-50"- )}- style={{- overflow: isExpanded ? "hidden" : "visible",- }}- >- {!isExpanded && link?.id && (- <div className="text-sm text-neutral mb-3 flex justify-between items-center w-full gap-2">- <div className="flex gap-1 h-8 rounded-xl bg-neutral-content bg-opacity-50 text-base-content p-1 text-xs duration-100 select-none z-10">- {formatAvailable(link, "readable") && (- <div- className={clsx(- "py-1 px-2 cursor-pointer duration-100 rounded-lg font-semibold text-center flex justify-between items-center w-6",- format === ArchivedFormat.readability &&- "bg-primary bg-opacity-50"- )}- onClick={() => setFormat(ArchivedFormat.readability)}- >- <div title={t("readable")}>- <i className={`bi-file-earmark-text text-lg -ml-[0.3rem]`} />- </div>- </div>- )}- {formatAvailable(link, "monolith") && (- <div- className={clsx(- "py-1 px-2 cursor-pointer duration-100 rounded-lg font-semibold text-center flex justify-between items-center w-6",- format === ArchivedFormat.monolith &&- "bg-primary bg-opacity-50"- )}- onClick={() => setFormat(ArchivedFormat.monolith)}- >- <div title={t("webpage")}>- <i className={`bi-filetype-html text-lg -ml-[0.3rem]`} />- </div>- </div>- )}- {formatAvailable(link, "image") && (- <div- className={clsx(- "py-1 px-2 cursor-pointer duration-100 rounded-lg font-semibold text-center flex justify-between items-center w-6",- format ===- (link?.image?.endsWith("png")- ? ArchivedFormat.png- : ArchivedFormat.jpeg) && "bg-primary bg-opacity-50"- )}- onClick={() =>- setFormat(- link?.image?.endsWith("png")- ? ArchivedFormat.png- : ArchivedFormat.jpeg- )- }- >- <div title={t("image")}>- <i className={`bi-file-earmark-image text-lg -ml-[0.3rem]`} />- </div>- </div>- )}- {formatAvailable(link, "pdf") && (- <div- className={clsx(- "py-1 px-2 cursor-pointer duration-100 rounded-lg font-semibold text-center flex justify-between items-center w-6",- format === ArchivedFormat.pdf && "bg-primary bg-opacity-50"- )}- onClick={() => setFormat(ArchivedFormat.pdf)}- >- <div title={t("pdf")}>- <i className={`bi-file-earmark-pdf text-lg -ml-[0.3rem]`} />- </div>- </div>- )}- </div>-- <div className="flex gap-2">- <div onClick={handleDownload} className="btn btn-sm btn-circle">- <i className="bi-cloud-arrow-down text-xl text-neutral" />- </div>- <div- className="btn btn-circle btn-sm"- onClick={() => setIsExpanded(true)}- >- <i className="bi-arrows-angle-expand" />- </div>- </div>- </div>- )}-- {error ? (- <p>{t("not_found_404")}</p>- ) : (- <div className={clsx("w-full", isExpanded ? "h-full" : "h-auto")}>- {renderFormat()}- </div>- )}-- {isExpanded && (- <div- className="absolute top-3 right-3 btn btn-circle btn-sm"- onClick={() => setIsExpanded(false)}- >- <i className="bi-arrows-angle-contract" />- </div>- )}- </div>- );-}diff --git a/components/Skeletons.tsx b/components/Skeletons.tsxindex c98ff751..ade8468a 100644--- a/components/Skeletons.tsx+++ b/components/Skeletons.tsx@@ -1,5 +1,12 @@-export const PreservationSkeleton = () => (- <div className="p-5 m-auto w-full flex flex-col items-center gap-5 h-full bg-base-200">+import clsx from "clsx";++export const PreservationSkeleton = ({ className }: { className?: string }) => (+ <div+ className={clsx(+ "p-5 m-auto w-full flex flex-col items-center gap-5 h-full bg-base-200",+ className+ )}+ ><div className="w-full mr-auto h-4 skeleton rounded-md"></div><div className="w-full mr-auto h-4 skeleton rounded-md"></div><div className="w-full mr-auto h-4 skeleton rounded-md"></div>diff --git a/pages/preserved/[id].tsx b/pages/preserved/[id].tsxindex c84f8630..25e03fe6 100644--- a/pages/preserved/[id].tsx+++ b/pages/preserved/[id].tsx@@ -1,8 +1,8 @@import getServerSideProps from "@/lib/client/getServerSideProps";-import PreservationStandalone from "@/components/PreservationStandalone";+import PreservationPageContent from "@/components/Preservation/PreservationPageContent";export default function Index() {- return <PreservationStandalone />;+ return <PreservationPageContent />;}export { getServerSideProps };diff --git a/pages/public/preserved/[id].tsx b/pages/public/preserved/[id].tsxindex c84f8630..25e03fe6 100644--- a/pages/public/preserved/[id].tsx+++ b/pages/public/preserved/[id].tsx@@ -1,8 +1,8 @@import getServerSideProps from "@/lib/client/getServerSideProps";-import PreservationStandalone from "@/components/PreservationStandalone";+import PreservationPageContent from "@/components/Preservation/PreservationPageContent";export default function Index() {- return <PreservationStandalone />;+ return <PreservationPageContent />;}export { getServerSideProps };