⚡️⚡️⚡️ 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
Danielaef0a90f4322
diff --git a/components/Dashboard/LinkItem.tsx b/components/Dashboard/LinkItem.tsxindex 3e95990f..0ebdec47 100644--- a/components/Dashboard/LinkItem.tsx+++ b/components/Dashboard/LinkItem.tsx@@ -16,7 +16,7 @@ import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";import { useState } from "react";import Image from "next/image";import useLinkStore from "@/store/links";-import EditLink from "../Modal/EditLink";+import LinkModal from "../Modal/LinkModal";import Link from "next/link";import Dropdown from "../Dropdown";import Modal from "../Modal";@@ -50,7 +50,11 @@ export default function ({ link, count }: Props) {<div className="border border-sky-100 bg-white p-5 rounded-md flex items-start relative gap-5 sm:gap-10 group/item">{editModal ? (<Modal toggleModal={toggleEditModal}>- <EditLink toggleLinkModal={toggleEditModal} link={link} />+ <LinkModal+ toggleLinkModal={toggleEditModal}+ activeLink={link}+ method="UPDATE"+ /></Modal>) : null}diff --git a/components/LinkList.tsx b/components/LinkList.tsxindex fea8cff0..733ea9f9 100644--- a/components/LinkList.tsx+++ b/components/LinkList.tsx@@ -18,7 +18,7 @@ import Image from "next/image";import Dropdown from "./Dropdown";import useLinkStore from "@/store/links";import Modal from "./Modal";-import EditLink from "./Modal/EditLink";+import LinkModal from "./Modal/LinkModal";import Link from "next/link";type Props = {@@ -50,7 +50,11 @@ export default function ({ link, count }: Props) {<div className="border border-sky-100 bg-gray-100 p-5 rounded-md flex items-start relative gap-5 sm:gap-10 group/item">{editModal ? (<Modal toggleModal={toggleEditModal}>- <EditLink toggleLinkModal={toggleEditModal} link={link} />+ <LinkModal+ toggleLinkModal={toggleEditModal}+ activeLink={link}+ method="UPDATE"+ /></Modal>) : null}diff --git a/components/Modal/AddLink.tsx b/components/Modal/AddLink.tsxdeleted file mode 100644index 2988db91..00000000--- a/components/Modal/AddLink.tsx+++ /dev/null@@ -1,149 +0,0 @@-// Copyright (C) 2022-present Daniel31x13 <[email protected]>-// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3.-// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.-// You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.--import React, { useEffect, useState } from "react";-import CollectionSelection from "@/components/InputSelect/CollectionSelection";-import TagSelection from "@/components/InputSelect/TagSelection";-import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";-import { faPlus } from "@fortawesome/free-solid-svg-icons";-import { LinkIncludingCollectionAndTags } from "@/types/global";-import useLinkStore from "@/store/links";-import { useRouter } from "next/router";-import useCollectionStore from "@/store/collections";-import RequiredBadge from "../RequiredBadge";--type Props = {- toggleLinkModal: Function;-};--export default function AddLink({ toggleLinkModal }: Props) {- const router = useRouter();- const [link, setLink] = useState<LinkIncludingCollectionAndTags>({- name: "",- url: "",- title: "",- screenshotPath: "",- pdfPath: "",- tags: [],- collection: {- name: "",- description: "",- ownerId: 1,- },- });-- const { addLink } = useLinkStore();- const { collections } = useCollectionStore();-- useEffect(() => {- if (router.query.id) {- const currentCollection = collections.find(- (e) => e.id == Number(router.query.id)- );-- if (currentCollection)- setLink({- ...link,- collection: {- id: currentCollection?.id,- name: currentCollection?.name,- ownerId: currentCollection?.ownerId,- },- });- }- }, []);-- const setTags = (e: any) => {- const tagNames = e.map((e: any) => {- return { name: e.label };- });-- setLink({ ...link, tags: tagNames });- };-- const setCollection = (e: any) => {- if (e?.__isNew__) e.value = null;-- setLink({- ...link,- collection: { id: e?.value, name: e?.label, ownerId: e?.ownerId },- });- };-- const submit = async () => {- console.log(link);-- const response = await addLink(link);-- if (response) toggleLinkModal();- };-- return (- <div className="flex flex-col gap-3 sm:w-[35rem] w-80">- <p className="text-xl text-sky-500 mb-2 text-center">New Link</p>-- <div className="grid sm:grid-cols-2 gap-3">- <div>- <p className="text-sm font-bold text-sky-300 mb-2">- Name- <RequiredBadge />- </p>- <input- value={link.name}- onChange={(e) => setLink({ ...link, name: e.target.value })}- type="text"- placeholder="e.g. Example Link"- className="w-full rounded-md p-2 border-sky-100 border-solid border outline-none focus:border-sky-500 duration-100"- />- </div>-- <div>- <p className="text-sm font-bold text-sky-300 mb-2">- URL- <RequiredBadge />- </p>- <input- value={link.url}- onChange={(e) => setLink({ ...link, url: e.target.value })}- type="text"- placeholder="e.g. http://example.com/"- className="w-full rounded-md p-2 border-sky-100 border-solid border outline-none focus:border-sky-500 duration-100"- />- </div>-- <div>- <p className="text-sm font-bold text-sky-300 mb-2">- Collection- <RequiredBadge />- </p>- <CollectionSelection- defaultValue={- link.collection.name && link.collection.id- ? {- value: link.collection.id,- label: link.collection.name,- }- : undefined- }- onChange={setCollection}- />- </div>-- <div>- <p className="text-sm font-bold text-sky-300 mb-2">Tags</p>- <TagSelection onChange={setTags} />- </div>- </div>-- <div- className="mx-auto mt-2 bg-sky-500 text-white flex items-center gap-2 py-2 px-5 rounded-md select-none font-bold cursor-pointer duration-100 hover:bg-sky-400"- onClick={submit}- >- <FontAwesomeIcon icon={faPlus} className="h-5" />- Add Link- </div>- </div>- );-}diff --git a/components/Modal/EditLink.tsx b/components/Modal/EditLink.tsxdeleted file mode 100644index 00c049d9..00000000--- a/components/Modal/EditLink.tsx+++ /dev/null@@ -1,129 +0,0 @@-// Copyright (C) 2022-present Daniel31x13 <[email protected]>-// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3.-// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.-// You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.--import React, { useState } from "react";-import CollectionSelection from "@/components/InputSelect/CollectionSelection";-import TagSelection from "@/components/InputSelect/TagSelection";-import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";-import { LinkIncludingCollectionAndTags } from "@/types/global";-import { faPenToSquare } from "@fortawesome/free-regular-svg-icons";-import useLinkStore from "@/store/links";-import { faTrashCan } from "@fortawesome/free-solid-svg-icons";-import RequiredBadge from "../RequiredBadge";--type Props = {- toggleLinkModal: Function;- link: LinkIncludingCollectionAndTags;-};--export default function EditLink({ toggleLinkModal, link }: Props) {- const [currentLink, setCurrentLink] =- useState<LinkIncludingCollectionAndTags>(link);-- const { updateLink, removeLink } = useLinkStore();-- const shortendURL = new URL(link.url).host.toLowerCase();-- const setTags = (e: any) => {- const tagNames = e.map((e: any) => {- return { name: e.label };- });-- setCurrentLink({ ...currentLink, tags: tagNames });- };-- const setCollection = (e: any) => {- if (e?.__isNew__) e.value = null;-- setCurrentLink({- ...currentLink,- collection: { id: e?.value, name: e?.label, ownerId: e?.ownerId } as any,- });- };-- const submit = async () => {- updateLink(currentLink);- toggleLinkModal();- };-- return (- <div className="flex flex-col gap-3 sm:w-96 w-80">- <p className="text-xl text-sky-500 mb-2 text-center">Edit Link</p>- <p className="text-sky-700">- <b>{shortendURL}</b> | {link.title}- </p>-- <div className="grid sm:grid-cols-2 gap-3">- <div>- <p className="text-sm font-bold text-sky-300 mb-2">- Name- <RequiredBadge />- </p>- <input- value={currentLink.name}- onChange={(e) =>- setCurrentLink({ ...currentLink, name: e.target.value })- }- type="text"- placeholder="e.g. Example Link"- className="w-full rounded-md p-2 border-sky-100 border-solid border outline-none focus:border-sky-500 duration-100"- />- </div>-- <div>- <p className="text-sm font-bold text-sky-300 mb-2">- Collection- <RequiredBadge />- </p>- <CollectionSelection- onChange={setCollection}- defaultValue={{- label: link.collection.name,- value: link.collection.id,- }}- />- </div>-- <div className="sm:col-span-2">- <p className="text-sm font-bold text-sky-300 mb-2">Tags</p>- <TagSelection- onChange={setTags}- defaultValue={link.tags.map((e) => {- return { label: e.name, value: e.id };- })}- />- </div>- </div>-- <div className="flex flex-col justify-center items-center gap-2 mt-2">- <div- className="bg-sky-500 text-white flex items-center gap-2 py-2 px-5 rounded-md select-none font-bold cursor-pointer duration-100 hover:bg-sky-400"- onClick={submit}- >- <FontAwesomeIcon icon={faPenToSquare} className="h-5" />- Edit Link- </div>-- <div className="flex items-center justify-center gap-2">- <hr className="w-16 border" />-- <p className="text-gray-400 font-bold">OR</p>-- <hr className="w-16 border" />- </div>-- <div- onClick={() => {- removeLink(link);- toggleLinkModal();- }}- className="w-fit inline-flex rounded-md cursor-pointer bg-red-500 hover:bg-red-400 duration-100 p-2"- >- <FontAwesomeIcon icon={faTrashCan} className="w-4 h-4 text-white" />- </div>- </div>- </div>- );-}diff --git a/components/Modal/LinkModal.tsx b/components/Modal/LinkModal.tsxnew file mode 100644index 00000000..30e7ab8a--- /dev/null+++ b/components/Modal/LinkModal.tsx@@ -0,0 +1,223 @@+// Copyright (C) 2022-present Daniel31x13 <[email protected]>+// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3.+// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.+// You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.++import React, { useEffect, useState } from "react";+import CollectionSelection from "@/components/InputSelect/CollectionSelection";+import TagSelection from "@/components/InputSelect/TagSelection";+import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";+import { LinkIncludingCollectionAndTags } from "@/types/global";+import { faPenToSquare } from "@fortawesome/free-regular-svg-icons";+import useLinkStore from "@/store/links";+import { faPlus, faTrashCan } from "@fortawesome/free-solid-svg-icons";+import RequiredBadge from "../RequiredBadge";+import { useSession } from "next-auth/react";+import useCollectionStore from "@/store/collections";+import { useRouter } from "next/router";++type Props =+ | {+ toggleLinkModal: Function;+ method: "CREATE";+ activeLink?: LinkIncludingCollectionAndTags;+ }+ | {+ toggleLinkModal: Function;+ method: "UPDATE";+ activeLink: LinkIncludingCollectionAndTags;+ };++export default function EditLink({+ toggleLinkModal,+ method,+ activeLink,+}: Props) {+ const { data } = useSession();++ const [link, setLink] = useState<LinkIncludingCollectionAndTags>(+ method === "UPDATE"+ ? activeLink+ : {+ name: "",+ url: "",+ title: "",+ screenshotPath: "",+ pdfPath: "",+ tags: [],+ collection: {+ name: "",+ ownerId: data?.user.id as number,+ },+ }+ );++ const { updateLink, removeLink, addLink } = useLinkStore();++ const router = useRouter();++ const { collections } = useCollectionStore();++ useEffect(() => {+ if (router.query.id) {+ const currentCollection = collections.find(+ (e) => e.id == Number(router.query.id)+ );++ if (currentCollection)+ setLink({+ ...link,+ collection: {+ id: currentCollection.id,+ name: currentCollection.name,+ ownerId: currentCollection.ownerId,+ },+ });+ }+ }, []);++ const shortendURL =+ method === "UPDATE" ? new URL(link.url).host.toLowerCase() : undefined;++ const setTags = (e: any) => {+ const tagNames = e.map((e: any) => {+ return { name: e.label };+ });++ setLink({ ...link, tags: tagNames });+ };++ const setCollection = (e: any) => {+ if (e?.__isNew__) e.value = null;++ setLink({+ ...link,+ collection: { id: e?.value, name: e?.label, ownerId: e?.ownerId },+ });+ };++ const submit = async () => {+ let response;++ if (method === "UPDATE") response = await updateLink(link);+ else if (method === "CREATE") response = await addLink(link);++ toggleLinkModal();+ };++ return (+ <div className="flex flex-col gap-3 sm:w-[35rem] w-80">+ <p className="text-xl text-sky-500 mb-2 text-center">+ {method === "UPDATE" ? "Edit" : "New"} Link+ </p>++ {method === "UPDATE" ? (+ <p className="text-sky-700">+ <b>{shortendURL}</b> | {link.title}+ </p>+ ) : null}++ <div className="grid sm:grid-cols-2 gap-3">+ <div>+ <p className="text-sm font-bold text-sky-300 mb-2">+ Name+ <RequiredBadge />+ </p>+ <input+ value={link.name}+ onChange={(e) => setLink({ ...link, name: e.target.value })}+ type="text"+ placeholder="e.g. Example Link"+ className="w-full rounded-md p-2 border-sky-100 border-solid border outline-none focus:border-sky-500 duration-100"+ />+ </div>++ {method === "CREATE" ? (+ <div>+ <p className="text-sm font-bold text-sky-300 mb-2">+ URL+ <RequiredBadge />+ </p>+ <input+ value={link.url}+ onChange={(e) => setLink({ ...link, url: e.target.value })}+ type="text"+ placeholder="e.g. http://example.com/"+ className="w-full rounded-md p-2 border-sky-100 border-solid border outline-none focus:border-sky-500 duration-100"+ />+ </div>+ ) : null}++ <div>+ <p className="text-sm font-bold text-sky-300 mb-2">+ Collection+ <RequiredBadge />+ </p>+ <CollectionSelection+ onChange={setCollection}+ // defaultValue={{+ // label: link.collection.name,+ // value: link.collection.id,+ // }}+ defaultValue={+ link.collection.name && link.collection.id+ ? {+ value: link.collection.id,+ label: link.collection.name,+ }+ : undefined+ }+ />+ </div>++ <div className={method === "UPDATE" ? "sm:col-span-2" : ""}>+ <p className="text-sm font-bold text-sky-300 mb-2">Tags</p>+ <TagSelection+ onChange={setTags}+ defaultValue={link.tags.map((e) => {+ return { label: e.name, value: e.id };+ })}+ />+ </div>+ </div>++ <div className="flex flex-col justify-center items-center gap-2 mt-2">+ <div+ className="bg-sky-500 text-white flex items-center gap-2 py-2 px-5 rounded-md select-none font-bold cursor-pointer duration-100 hover:bg-sky-400"+ onClick={submit}+ >+ <FontAwesomeIcon+ icon={method === "CREATE" ? faPlus : faPenToSquare}+ className="h-5"+ />+ {method === "CREATE" ? "Add Link" : "Edit Link"}+ </div>++ {method === "UPDATE" ? (+ <>+ <div className="flex items-center justify-center gap-2">+ <hr className="w-16 border" />++ <p className="text-gray-400 font-bold">OR</p>++ <hr className="w-16 border" />+ </div>++ <div+ onClick={() => {+ removeLink(link);+ toggleLinkModal();+ }}+ className="w-fit inline-flex rounded-md cursor-pointer bg-red-500 hover:bg-red-400 duration-100 p-2"+ >+ <FontAwesomeIcon+ icon={faTrashCan}+ className="w-4 h-4 text-white"+ />+ </div>+ </>+ ) : null}+ </div>+ </div>+ );+}diff --git a/components/Navbar.tsx b/components/Navbar.tsxindex 39efd3c0..c1e07235 100644--- a/components/Navbar.tsx+++ b/components/Navbar.tsx@@ -16,7 +16,7 @@ import {import { useEffect, useState } from "react";import Dropdown from "@/components/Dropdown";import Modal from "@/components/Modal";-import AddLink from "@/components/Modal/AddLink";+import LinkModal from "@/components/Modal/LinkModal";import ClickAwayHandler from "@/components/ClickAwayHandler";import Sidebar from "@/components/Sidebar";import { useRouter } from "next/router";@@ -129,7 +129,7 @@ export default function () {{linkModal ? (<Modal toggleModal={toggleLinkModal}>- <AddLink toggleLinkModal={toggleLinkModal} />+ <LinkModal toggleLinkModal={toggleLinkModal} method="CREATE" /></Modal>) : null}diff --git a/pages/collections/[id].tsx b/pages/collections/[id].tsxindex f75c3ef0..fa39d42d 100644--- a/pages/collections/[id].tsx+++ b/pages/collections/[id].tsx@@ -6,7 +6,7 @@import Dropdown from "@/components/Dropdown";import LinkList from "@/components/LinkList";import Modal from "@/components/Modal";-import AddLink from "@/components/Modal/AddLink";+import LinkModal from "@/components/Modal/LinkModal";import CollectionModal from "@/components/Modal/CollectionModal";import DeleteCollection from "@/components/Modal/DeleteCollection";import useCollectionStore from "@/store/collections";@@ -280,7 +280,10 @@ export default function () {{linkModal ? (<Modal toggleModal={toggleLinkModal}>- <AddLink toggleLinkModal={toggleLinkModal} />+ <LinkModal+ toggleLinkModal={toggleLinkModal}+ method="CREATE"+ /></Modal>) : null}diff --git a/store/links.ts b/store/links.tsindex 0dc3f694..79763175 100644--- a/store/links.ts+++ b/store/links.ts@@ -12,8 +12,8 @@ type LinkStore = {links: LinkIncludingCollectionAndTags[];setLinks: () => void;addLink: (body: LinkIncludingCollectionAndTags) => Promise<boolean>;- updateLink: (link: LinkIncludingCollectionAndTags) => void;- removeLink: (link: LinkIncludingCollectionAndTags) => void;+ updateLink: (link: LinkIncludingCollectionAndTags) => Promise<boolean>;+ removeLink: (link: LinkIncludingCollectionAndTags) => Promise<boolean>;};const useLinkStore = create<LinkStore>()((set) => ({@@ -70,6 +70,8 @@ const useLinkStore = create<LinkStore>()((set) => ({useTagStore.getState().setTags();useCollectionStore.getState().setCollections();}++ return response.ok;},removeLink: async (link) => {const response = await fetch("/api/routes/links", {