⚡️⚡️⚡️ 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
wip
daniel31x13ecc48f8fe278
diff --git a/components/ClickAwayHandler.tsx b/components/ClickAwayHandler.tsxindex e8e88784..bf0c4458 100644--- a/components/ClickAwayHandler.tsx+++ b/components/ClickAwayHandler.tsx@@ -31,6 +31,11 @@ function useOutsideAlerter(useEffect(() => {function handleClickOutside(event: MouseEvent) {const clickedElement = event.target as HTMLElement;++ if (clickedElement.closest("[data-ignore-click-away]")) {+ return;+ }+if (ref.current && !ref.current.contains(clickedElement)) {const refZIndex = getZIndex(ref.current);const clickedZIndex = getZIndex(clickedElement);diff --git a/components/LinkDetails.tsx b/components/LinkDetails.tsxindex cb392041..dfccb8e7 100644--- a/components/LinkDetails.tsx+++ b/components/LinkDetails.tsx@@ -386,7 +386,9 @@ export default function LinkDetails({)}</div>- {link.id && <Preservation link={link} standalone />}+ {link.id && (+ <Preservation link={link} standalone={standalone} />+ )}</div>)}diff --git a/components/Navbar.tsx b/components/Navbar.tsxindex 2fe20969..083ace96 100644--- a/components/Navbar.tsx+++ b/components/Navbar.tsx@@ -22,10 +22,13 @@ export default function Navbar() {const { width } = useWindowDimensions();useEffect(() => {- setSidebar(false);- document.body.style.overflow = "auto";+ if (sidebar) setSidebar(false);}, [width, router]);+ useEffect(() => {+ document.body.style.overflow = "auto";+ }, [sidebar]);+const toggleSidebar = () => {setSidebar(false);document.body.style.overflow = "auto";diff --git a/components/Preservation.tsx b/components/Preservation.tsxindex cd795a1a..436fc138 100644--- a/components/Preservation.tsx+++ b/components/Preservation.tsx@@ -1,14 +1,18 @@-import React, { useEffect, useState } from "react";+import React, { useEffect, useMemo, useRef, useState } from "react";import ReactDOM from "react-dom";import {ArchivedFormat,LinkIncludingShortenedCollectionAndTags,} 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 useWindowDimensions from "@/hooks/useWindowDimensions";+import { PreservationSkeleton } from "./Skeletons";+import remToPixels from "@/lib/client/remToPixels";+import { useReducedMotion } from "framer-motion";export default function Preservation({link,@@ -18,106 +22,82 @@ export default function Preservation({standalone?: boolean;}) {const { t } = useTranslation();+ 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+useEffect(() => {- setPdfLoaded(false);- setMonolithLoaded(false);- setImageLoaded(false);- }, [format]);+ const delay = setTimeout(() => {+ setDelayPassed(true);+ }, 250);- 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>- );+ return () => {+ clearTimeout(delay);+ };+ }, []);- const renderFormat = () => {- if (!link?.id) return null;+ const containerRef = useRef<HTMLDivElement>(null);- switch (format) {- case ArchivedFormat.readability:- return (- <div className="overflow-auto w-full h-full">- <ReadableView- link={link}- isExpanded={isExpanded}- standalone={standalone}- />- </div>- );+ const determineDefaultFormat = () => {+ if (formatAvailable(link, "readable")) return ArchivedFormat.readability;+ if (formatAvailable(link, "monolith")) return ArchivedFormat.monolith;+ if (formatAvailable(link, "image")) {+ return link?.image?.endsWith("png")+ ? ArchivedFormat.png+ : ArchivedFormat.jpeg;+ }+ if (formatAvailable(link, "pdf")) return ArchivedFormat.pdf;+ return undefined;+ };- case ArchivedFormat.monolith:- return (- <>- {!monolithLoaded && <Skeleton />}- <iframe- src={`/api/v1/archives/${link.id}?format=${- ArchivedFormat.monolith- }&_=${Date.now()}`}- className={clsx(- "w-full border-none",- monolithLoaded ? "block" : "hidden",- isExpanded ? "h-full" : "h-[calc(80vh-3.75rem)]"- )}- onLoad={() => setMonolithLoaded(true)}- />- </>- );+ const defaultFormat = useMemo(() => determineDefaultFormat(), [link]);- case ArchivedFormat.pdf:- return (- <>- {!pdfLoaded && <Skeleton />}- <iframe- src={`/api/v1/archives/${link.id}?format=${- ArchivedFormat.pdf- }&_=${Date.now()}`}- className={clsx(- "w-full border-none",- pdfLoaded ? "block" : "hidden",- isExpanded ? "h-full" : "h-[calc(80vh-3.75rem)]"- )}- onLoad={() => setPdfLoaded(true)}- />- </>- );+ useEffect(() => {+ const { format: routeFormat, expanded: routeIsExpanded } = router.query;- 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=${format}`}- className={clsx("w-fit mx-auto", !imageLoaded && "hidden")}- onLoad={() => setImageLoaded(true)}- />- </div>- </>- );+ const formatNumber = Number(routeFormat);- default:- return null;+ if (routeFormat && Object.values(ArchivedFormat).includes(formatNumber)) {+ setFormat(formatNumber);+ } else {+ setFormat(defaultFormat);}- };++ if (routeIsExpanded === "true") {+ setIsExpanded(true);+ }+ }, [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 = {+ ...router.query,+ format,+ expanded: isExpanded,+ };+ router.push({ pathname: router.pathname, query }, undefined, {+ shallow: true,+ });+ }+ }, [format, isExpanded]);const handleDownload = () => {const path = `/api/v1/archives/${link?.id}?format=${format}`;@@ -144,16 +124,13 @@ export default function Preservation({});};- const { width } = useWindowDimensions();-- const content = (+ return (<div+ ref={containerRef}className={clsx("bg-base-200",!isExpanded &&- `mx-auto relative p-2 h-full overflow-auto ${- width < 640 && "rounded-md"- }`,+ `mx-auto relative p-2 h-full overflow-auto rounded-md sm:rounded-none`,isExpanded && "fixed inset-0 w-screen h-screen z-[9999]")}style={{@@ -163,16 +140,17 @@ export default function Preservation({{!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, "pdf") && (+ {formatAvailable(link, "readable") && (<divclassName={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"+ format === ArchivedFormat.readability &&+ "bg-primary bg-opacity-50")}- onClick={() => setFormat(ArchivedFormat.pdf)}+ onClick={() => setFormat(ArchivedFormat.readability)}>- <div className="tooltip tooltip-bottom" data-tip={t("pdf")}>- <i className={`bi-file-earmark-pdf text-lg -ml-[0.3rem]`} />+ <div title={t("readable")}>+ <i className={`bi-file-earmark-text text-lg -ml-[0.3rem]`} /></div></div>)}@@ -185,28 +163,11 @@ export default function Preservation({)}onClick={() => setFormat(ArchivedFormat.monolith)}>- <div className="tooltip tooltip-bottom" data-tip={t("webpage")}>+ <div title={t("webpage")}><i className={`bi-filetype-html text-lg -ml-[0.3rem]`} /></div></div>)}- {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- className="tooltip tooltip-bottom"- data-tip={t("readable")}- >- <i className={`bi-file-earmark-text text-lg -ml-[0.3rem]`} />- </div>- </div>- )}{formatAvailable(link, "image") && (<divclassName={clsx(@@ -224,11 +185,24 @@ export default function Preservation({)}>- <div className="tooltip tooltip-bottom" data-tip={t("image")}>+ <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>{format !== undefined && (@@ -251,9 +225,194 @@ export default function Preservation({)}<div className={clsx("w-full", isExpanded ? "h-full" : "h-auto")}>- {renderFormat()}+ {format !== undefined && link.id && delayPassed ? (+ <RenderFormat+ link={link}+ format={format}+ isExpanded={isExpanded}+ setMonolithLoaded={setMonolithLoaded}+ setPdfLoaded={setPdfLoaded}+ setImageLoaded={setImageLoaded}+ monolithLoaded={monolithLoaded}+ pdfLoaded={pdfLoaded}+ imageLoaded={imageLoaded}+ standalone={standalone}+ containerRef={containerRef}+ setIsExpanded={setIsExpanded}+ />+ ) : (+ <PreservationSkeleton />+ )}</div>+ </div>+ );+}++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;+}) => {+ if (!link?.id) return <></>;++ const [containerRect, setContainerRect] = useState<DOMRect | null>(null);++ const [delayPassed, setDelayPassed] = useState(false); // to get the content in place before animating++ useEffect(() => {+ const delay = setTimeout(() => {+ setDelayPassed(true);+ }, 50);++ return () => {+ clearTimeout(delay);+ };+ }, []);+ useEffect(() => {+ if (!isExpanded) {+ const measure = () => {+ if (containerRef.current) {+ setContainerRect(containerRef.current.getBoundingClientRect());+ }+ };++ measure();+ window.addEventListener("resize", measure);+ return () => {+ window.removeEventListener("resize", measure);+ };+ }+ }, [containerRef, isExpanded]);++ const topPadding = remToPixels(3.75);++ const shouldReduceMotion = useReducedMotion();++ const style = isExpanded+ ? {+ position: "fixed" as const,+ top: 0,+ left: 0,+ width: "100vw",+ height: "100vh",+ zIndex: 9999,+ transitionProperty:+ delayPassed && !shouldReduceMotion+ ? "width, height, top, left"+ : "none",+ }+ : {+ zIndex: 40,+ position: "absolute" as const,+ top: (containerRect?.top ?? 0) + window.scrollY + topPadding,+ left: (containerRect?.left ?? 0) + window.scrollX,+ width: containerRect?.width ?? 0,+ height: containerRect?.height ? containerRect.height - topPadding : 0,+ transitionProperty:+ delayPassed && !shouldReduceMotion+ ? "width, height, top, left"+ : "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+ }&_=${Date.now()}`}+ 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+ }&_=${Date.now()}`}+ 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}`}+ className={clsx("w-fit mx-auto", !imageLoaded && "hidden")}+ onLoad={() => setImageLoaded(true)}+ />+ </div>+ </>+ );+ default:+ return <></>;+ }+ };++ return ReactDOM.createPortal(+ <div+ style={style}+ className={clsx(+ "z-[40] overflow-hidden ease-in-out duration-100",+ !isExpanded && "rounded-bl-2xl"+ )}+ data-ignore-click-away+ >+ {content()}{isExpanded && (<divclassName="absolute top-3 right-3 btn btn-circle btn-primary btn-sm"@@ -262,12 +421,7 @@ export default function Preservation({<i className="bi-arrows-angle-contract text-lg" /></div>)}- </div>+ </div>,+ document.body);-- if (isExpanded) {- return ReactDOM.createPortal(content, document.body);- }-- return content;-}+};diff --git a/components/ReadableView.tsx b/components/ReadableView.tsxindex e261e306..26fd267d 100644--- a/components/ReadableView.tsx+++ b/components/ReadableView.tsx@@ -51,12 +51,13 @@ export default function ReadableView({ link, isExpanded, standalone }: Props) {openOnClick: false,}),],+ immediatelyRender: false,content: linkContent || "<p></p>",editable: false,editorProps: {attributes: {class: clsx(- "rounded-md bg-base-200 focus:outline-none border-neutral-content focus:border-primary border-solid border p-3 overflow-auto duration-100",+ "rounded-md focus:outline-none border-neutral-content focus:border-primary border-solid border p-3 overflow-auto duration-100",isExpanded ? "h-[calc(100vh-7.25rem)]" : "h-[calc(80vh-10.75rem)]"),},@@ -126,7 +127,7 @@ export default function ReadableView({ link, isExpanded, standalone }: Props) {};return (- <div className="flex flex-col gap-3 items-start p-3 max-w-screen-lg mx-auto">+ <div className="flex flex-col gap-3 items-start p-3 max-w-screen-lg mx-auto bg-base-200">{!isEditing && (<div className="flex gap-3 items-start"><div className="flex flex-col w-full gap-1">diff --git a/components/Skeletons.tsx b/components/Skeletons.tsxnew file mode 100644index 00000000..c98ff751--- /dev/null+++ b/components/Skeletons.tsx@@ -0,0 +1,17 @@+export const PreservationSkeleton = () => (+ <div className="p-5 m-auto w-full flex flex-col items-center gap-5 h-full bg-base-200">+ <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>+);++export const ImageSkeleton = () => (+ <div className="w-[80%] h-[80%] bg-neutral-content rounded-2xl mx-auto"></div>+);diff --git a/lib/client/remToPixels.ts b/lib/client/remToPixels.tsnew file mode 100644index 00000000..fb92d934--- /dev/null+++ b/lib/client/remToPixels.ts@@ -0,0 +1,3 @@+export default function remToPixels(rem: number): number {+ return rem * parseFloat(getComputedStyle(document.documentElement).fontSize);+}diff --git a/lib/shared/getLinkTypeFromFormat.ts b/lib/shared/getLinkTypeFromFormat.tsindex b6cdcd9d..3045c735 100644--- a/lib/shared/getLinkTypeFromFormat.ts+++ b/lib/shared/getLinkTypeFromFormat.ts@@ -4,15 +4,15 @@ const getLinkTypeFromFormat = (format: ArchivedFormat): "image" | "pdf" | "readable" | "monolith" => {switch (format) {+ case ArchivedFormat.readability:+ return "readable";+ case ArchivedFormat.monolith:+ return "monolith";case ArchivedFormat.jpeg:case ArchivedFormat.png:return "image";case ArchivedFormat.pdf:return "pdf";- case ArchivedFormat.readability:- return "readable";- case ArchivedFormat.monolith:- return "monolith";default:throw new Error("Invalid file type.");}