feat: created the Link component

This commit is contained in:
Daniel
2023-03-10 22:55:33 +03:30
parent 0d5579b56d
commit 48bdf29161
10 changed files with 144 additions and 33 deletions
+109 -11
View File
@@ -1,24 +1,122 @@
import { LinkAndTags } from "@/types/global"; import { ExtendedLink } from "@/types/global";
import {
faFolder,
faArrowUpRightFromSquare,
faCaretRight,
faEllipsis,
faFileImage,
faFilePdf,
} from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { useState } from "react";
export default function ({ export default function ({
link, link,
count, count,
}: { }: {
link: LinkAndTags; link: ExtendedLink;
count: number; count: number;
}) { }) {
const [archiveLabel, setArchiveLabel] = useState("Archived Formats");
const shortendURL = new URL(link.url).host.toLowerCase();
const formattedDate = new Date(link.createdAt).toLocaleString("en-US", {
year: "numeric",
month: "short",
day: "numeric",
});
return ( return (
<div className="border border-sky-100 mb-5 bg-gray-100 p-5 rounded"> <div className="border border-sky-100 mb-5 bg-gray-100 p-5 rounded">
{/* <div className="flex items-baseline gap-1"> <div className="flex justify-between h-full">
<p className="text-sm text-sky-600">{count + 1}.</p> <div>
<p className="text-lg text-sky-500">{link.name}</p> <div className="flex items-baseline gap-1">
<p className="text-sm text-sky-300 font-bold">{count + 1}.</p>
<p className="text-lg text-sky-600">{link.name}</p>
</div>
<p className="text-sky-400 text-sm font-medium">{link.title}</p>
<div className="flex gap-3 items-center flex-wrap my-3">
<div className="flex items-center gap-1 cursor-pointer">
<FontAwesomeIcon icon={faFolder} className="w-4 text-sky-300" />
<p className="text-sky-900">{link.collection.name}</p>
</div>
<div className="flex gap-1 items-center flex-wrap">
{link.tags.map((e, i) => (
<p
key={i}
className="px-2 py-1 bg-sky-200 text-sky-700 text-xs rounded-3xl cursor-pointer"
>
# {e.name}
</p>
))}
</div>
</div>
<div className="flex gap-2 items-center">
<p className="text-gray-500">{formattedDate}</p>
<FontAwesomeIcon
icon={faCaretRight}
className="w-3 text-gray-400"
/>
<a href={link.url} className="group">
<div className="text-gray-500 font-bold flex items-center gap-1">
<p>{shortendURL}</p>
<FontAwesomeIcon
icon={faArrowUpRightFromSquare}
className="w-3 opacity-0 group-hover:opacity-100 duration-75"
/>
</div>
</a>
</div>
</div>
<div className="flex flex-col justify-between items-end">
<FontAwesomeIcon
icon={faEllipsis}
className="w-6 h-6 text-gray-500 cursor-pointer"
/>
<div>
<p className="text-center text-sky-500 text-sm font-bold">
{archiveLabel}
</p>
<div
className="flex justify-between mt-3 gap-3"
onMouseLeave={() => setArchiveLabel("Archived Formats")}
>
<a
href={`/api/archives/${link.collectionId}/${encodeURIComponent(
link.screenshotPath
)}`}
onMouseEnter={() => setArchiveLabel("Screenshot")}
>
<FontAwesomeIcon
icon={faFileImage}
className="w-8 h-8 text-sky-600 cursor-pointer"
/>
</a>
<a
href={`/api/archives/${link.collectionId}/${encodeURIComponent(
link.pdfPath
)}`}
onMouseEnter={() => setArchiveLabel("PDF")}
>
<FontAwesomeIcon
icon={faFilePdf}
className="w-8 h-8 text-sky-600 cursor-pointer"
/>
</a>
<FontAwesomeIcon
icon={faArrowUpRightFromSquare}
className="w-8 h-8 text-sky-600 cursor-pointer"
onMouseEnter={() => setArchiveLabel("Web.archive.org")}
/>
</div>
</div>
</div>
</div> </div>
<div className="flex gap-1 items-baseline"> {/* <br />
{link.tags.map((e, i) => ( <hr />
<p key={i}>{e.name}</p> <br />
))} <p className="break-words text-sm">{JSON.stringify(link)}</p> */}
</div> */}
{JSON.stringify(link)}
</div> </div>
); );
} }
+2 -2
View File
@@ -8,7 +8,7 @@ import {
faPlus, faPlus,
faFolder, faFolder,
faBox, faBox,
faTag, faHashtag,
faBookmark, faBookmark,
faMagnifyingGlass, faMagnifyingGlass,
IconDefinition, IconDefinition,
@@ -49,7 +49,7 @@ export default function () {
setPageName(activeTag?.name); setPageName(activeTag?.name);
} }
setPageIcon(faTag); setPageIcon(faHashtag);
} else if (router.route === "/collections") { } else if (router.route === "/collections") {
setPageName("All Collections"); setPageName("All Collections");
setPageIcon(faBox); setPageIcon(faBox);
+2 -2
View File
@@ -12,9 +12,9 @@ interface SidebarItemProps {
export default function ({ text, icon, path }: SidebarItemProps) { export default function ({ text, icon, path }: SidebarItemProps) {
return ( return (
<Link href={path}> <Link href={path}>
<div className="hover:bg-gray-50 duration-100 text-sky-900 rounded my-1 p-3 cursor-pointer flex items-center gap-2"> <div className="hover:bg-gray-50 duration-100 rounded my-1 p-3 cursor-pointer flex items-center gap-2">
<FontAwesomeIcon icon={icon} className="w-4 text-sky-300" /> <FontAwesomeIcon icon={icon} className="w-4 text-sky-300" />
<p>{text}</p> <p className="text-sky-900">{text}</p>
</div> </div>
</Link> </Link>
); );
+2 -2
View File
@@ -9,7 +9,7 @@ import {
faChevronDown, faChevronDown,
faFolder, faFolder,
faBox, faBox,
faTag, faHashtag,
faBookmark, faBookmark,
} from "@fortawesome/free-solid-svg-icons"; } from "@fortawesome/free-solid-svg-icons";
import SidebarItem from "./SidebarItem"; import SidebarItem from "./SidebarItem";
@@ -110,7 +110,7 @@ export default function () {
<SidebarItem <SidebarItem
key={i} key={i}
text={e.name} text={e.name}
icon={faTag} icon={faHashtag}
path={`/tags/${e.id}`} path={`/tags/${e.id}`}
/> />
); );
+4 -1
View File
@@ -24,7 +24,10 @@ export default async function (
], ],
}, },
}, },
include: { tags: true }, include: {
tags: true,
collection: true,
},
}); });
return res.status(200).json({ return res.status(200).json({
+18 -6
View File
@@ -1,12 +1,13 @@
import type { NextApiRequest, NextApiResponse } from "next"; import type { NextApiRequest, NextApiResponse } from "next";
import { prisma } from "@/lib/api/db"; import { prisma } from "@/lib/api/db";
import { Session } from "next-auth"; import { Session } from "next-auth";
import { LinkAndTags, NewLink } from "@/types/global"; import { ExtendedLink, NewLink } from "@/types/global";
import { existsSync, mkdirSync } from "fs"; import { existsSync, mkdirSync } from "fs";
import getTitle from "../../getTitle"; import getTitle from "../../getTitle";
import archive from "../../archive"; import archive from "../../archive";
import { Link } from "@prisma/client"; import { Link, UsersAndCollections } from "@prisma/client";
import AES from "crypto-js/aes"; import AES from "crypto-js/aes";
import hasAccessToCollection from "@/lib/api/hasAccessToCollection";
export default async function ( export default async function (
req: NextApiRequest, req: NextApiRequest,
@@ -44,9 +45,8 @@ export default async function (
const checkIfCollectionExists = findCollection?.collections[0]; const checkIfCollectionExists = findCollection?.collections[0];
if (checkIfCollectionExists) { if (checkIfCollectionExists)
return res.status(400).json({ response: "Collection already exists." }); return res.status(400).json({ response: "Collection already exists." });
}
const newCollection = await prisma.collection.create({ const newCollection = await prisma.collection.create({
data: { data: {
@@ -68,6 +68,18 @@ export default async function (
const collectionId = link.collection.id as number; const collectionId = link.collection.id as number;
const collectionIsAccessible = await hasAccessToCollection(
session.user.id,
collectionId
);
const memberHasAccess = collectionIsAccessible?.members.some(
(e: UsersAndCollections) => e.userId === session.user.id && e.canCreate
);
if (!(collectionIsAccessible?.ownerId === session.user.id || memberHasAccess))
return res.status(401).json({ response: "Collection is not accessible." });
const title = await getTitle(link.url); const title = await getTitle(link.url);
const newLink: Link = await prisma.link.create({ const newLink: Link = await prisma.link.create({
@@ -116,10 +128,10 @@ export default async function (
AES_SECRET AES_SECRET
).toString(); ).toString();
const updatedLink: LinkAndTags = await prisma.link.update({ const updatedLink: ExtendedLink = await prisma.link.update({
where: { id: newLink.id }, where: { id: newLink.id },
data: { screenshotPath: screenShotHashedPath, pdfPath: pdfHashedPath }, data: { screenshotPath: screenShotHashedPath, pdfPath: pdfHashedPath },
include: { tags: true }, include: { tags: true, collection: true },
}); });
archive(updatedLink.url, updatedLink.collectionId, updatedLink.id); archive(updatedLink.url, updatedLink.collectionId, updatedLink.id);
+1 -1
View File
@@ -1,7 +1,7 @@
import { prisma } from "@/lib/api/db"; import { prisma } from "@/lib/api/db";
export default async (userId: number, collectionId: number) => { export default async (userId: number, collectionId: number) => {
const check: any = await prisma.collection.findFirst({ const check = await prisma.collection.findFirst({
where: { where: {
AND: { AND: {
id: collectionId, id: collectionId,
-3
View File
@@ -18,9 +18,6 @@ export default async function (
return res.status(401).json({ response: "You must be logged in." }); return res.status(401).json({ response: "You must be logged in." });
} }
// Check if user is unauthorized to the collection (If isn't owner or doesn't has the required permission...)
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if (req.method === "GET") return await getLinks(req, res, session); if (req.method === "GET") return await getLinks(req, res, session);
if (req.method === "POST") return await postLink(req, res, session); if (req.method === "POST") return await postLink(req, res, session);
} }
+3 -3
View File
@@ -1,11 +1,11 @@
import { create } from "zustand"; import { create } from "zustand";
import { LinkAndTags, NewLink } from "@/types/global"; import { ExtendedLink, NewLink } from "@/types/global";
type LinkSlice = { type LinkSlice = {
links: LinkAndTags[]; links: ExtendedLink[];
setLinks: () => void; setLinks: () => void;
addLink: (linkName: NewLink) => Promise<boolean>; addLink: (linkName: NewLink) => Promise<boolean>;
updateLink: (link: LinkAndTags) => void; updateLink: (link: ExtendedLink) => void;
removeLink: (linkId: number) => void; removeLink: (linkId: number) => void;
}; };
+3 -2
View File
@@ -1,7 +1,8 @@
import { Link, Tag } from "@prisma/client"; import { Collection, Link, Tag } from "@prisma/client";
export interface LinkAndTags extends Link { export interface ExtendedLink extends Link {
tags: Tag[]; tags: Tag[];
collection: Collection;
} }
export interface NewLink { export interface NewLink {