Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

final touch

daniel31x13bbc2e4c457fd
diff --git a/components/CollectionSelection.tsx b/components/CollectionListing.tsx
similarity index 100%
rename from components/CollectionSelection.tsx
rename to components/CollectionListing.tsx
diff --git a/components/InputSelect/CollectionSelection.tsx b/components/InputSelect/CollectionSelection.tsx
index 5e36ff8d..99b999e3 100644
--- a/components/InputSelect/CollectionSelection.tsx
+++ b/components/InputSelect/CollectionSelection.tsx
@@ -10,11 +10,11 @@ type Props = {
onChange: any;
showDefaultValue?: boolean;
defaultValue?:
- | {
- label: string;
- value?: number;
- }
- | undefined;
+ | {
+ label: string;
+ value?: number;
+ }
+ | undefined;
creatable?: boolean;
};
@@ -44,7 +44,13 @@ export default function CollectionSelection({
useEffect(() => {
const formatedCollections = collections.map((e) => {
- return { value: e.id, label: e.name, ownerId: e.ownerId, count: e._count, parentId: e.parentId };
+ return {
+ value: e.id,
+ label: e.name,
+ ownerId: e.ownerId,
+ count: e._count,
+ parentId: e.parentId,
+ };
});
setOptions(formatedCollections);
@@ -63,26 +69,22 @@ export default function CollectionSelection({
// Have the top level parent at beginning
return parentNames.reverse();
- }
+ };
const customOption = ({ data, innerProps }: any) => {
return (
<div
{...innerProps}
- className='px-2 py-2 last:border-0 border-b border-neutral-content hover:bg-primary/20 cursor-pointer'
+ className="px-2 py-2 last:border-0 border-b border-neutral-content hover:bg-neutral-content cursor-pointer"
>
<div className="flex w-full justify-between items-center">
- <span>
- {data.label}
- </span>
- <span>
- {data.count?.links}
- </span>
+ <span>{data.label}</span>
+ <span className="text-sm text-neutral">{data.count?.links}</span>
</div>
<div className="text-xs text-gray-600 dark:text-gray-300">
{getParentNames(data?.parentId).length > 0 ? (
<>
- {getParentNames(data.parentId).join(' > ')} {'>'} {data.label}
+ {getParentNames(data.parentId).join(" > ")} {">"} {data.label}
</>
) : (
data.label
@@ -105,7 +107,7 @@ export default function CollectionSelection({
components={{
Option: customOption,
}}
- // menuPosition="fixed"
+ // menuPosition="fixed"
/>
);
} else {
@@ -118,7 +120,10 @@ export default function CollectionSelection({
options={options}
styles={styles}
defaultValue={showDefaultValue ? defaultValue : null}
- // menuPosition="fixed"
+ components={{
+ Option: customOption,
+ }}
+ // menuPosition="fixed"
/>
);
}
diff --git a/components/Sidebar.tsx b/components/Sidebar.tsx
index 66a8d233..3f3e97fe 100644
--- a/components/Sidebar.tsx
+++ b/components/Sidebar.tsx
@@ -5,7 +5,7 @@ import { useRouter } from "next/router";
import { useEffect, useState } from "react";
import { Disclosure, Transition } from "@headlessui/react";
import SidebarHighlightLink from "@/components/SidebarHighlightLink";
-import CollectionSelection from "@/components/CollectionSelection";
+import CollectionListing from "@/components/CollectionListing";
export default function Sidebar({ className }: { className?: string }) {
const [tagDisclosure, setTagDisclosure] = useState<boolean>(() => {
@@ -99,7 +99,7 @@ export default function Sidebar({ className }: { className?: string }) {
leaveTo="transform opacity-0 -translate-y-3"
>
<Disclosure.Panel>
- <CollectionSelection links={true} />
+ <CollectionListing links={true} />
</Disclosure.Panel>
</Transition>
</Disclosure>
diff --git a/lib/api/controllers/links/postLink.ts b/lib/api/controllers/links/postLink.ts
index de2d80c0..eb82949c 100644
--- a/lib/api/controllers/links/postLink.ts
+++ b/lib/api/controllers/links/postLink.ts
@@ -22,27 +22,43 @@ export default async function postLink(
};
}
- if (!link.collection.name) {
- link.collection.name = "Unorganized";
- }
+ if (!link.collection.id && link.collection.name) {
+ link.collection.name = link.collection.name.trim();
- const numberOfLinksTheUserHas = await prisma.link.count({
- where: {
- collection: {
+ // find the collection with the name and the user's id
+ const findCollection = await prisma.collection.findFirst({
+ where: {
+ name: link.collection.name,
ownerId: userId,
+ parentId: link.collection.parentId,
},
- },
- });
-
- if (numberOfLinksTheUserHas + 1 > MAX_LINKS_PER_USER)
- return {
- response: `Error: Each user can only have a maximum of ${MAX_LINKS_PER_USER} Links.`,
- status: 400,
- };
+ });
- link.collection.name = link.collection.name.trim();
+ if (findCollection) {
+ const collectionIsAccessible = await getPermission({
+ userId,
+ collectionId: findCollection.id,
+ });
+
+ const memberHasAccess = collectionIsAccessible?.members.some(
+ (e: UsersAndCollections) => e.userId === userId && e.canCreate
+ );
+
+ if (!(collectionIsAccessible?.ownerId === userId || memberHasAccess))
+ return { response: "Collection is not accessible.", status: 401 };
+
+ link.collection.id = findCollection.id;
+ } else {
+ const collection = await prisma.collection.create({
+ data: {
+ name: link.collection.name,
+ ownerId: userId,
+ },
+ });
- if (link.collection.id) {
+ link.collection.id = collection.id;
+ }
+ } else if (link.collection.id) {
const collectionIsAccessible = await getPermission({
userId,
collectionId: link.collection.id,
@@ -54,10 +70,39 @@ export default async function postLink(
if (!(collectionIsAccessible?.ownerId === userId || memberHasAccess))
return { response: "Collection is not accessible.", status: 401 };
+ } else if (!link.collection.id) {
+ link.collection.name = "Unorganized";
+ link.collection.parentId = null;
+
+ // find the collection with the name "Unorganized" and the user's id
+ const unorganizedCollection = await prisma.collection.findFirst({
+ where: {
+ name: "Unorganized",
+ ownerId: userId,
+ },
+ });
+
+ link.collection.id = unorganizedCollection?.id;
} else {
- link.collection.ownerId = userId;
+ return { response: "Uncaught error.", status: 500 };
}
+ const numberOfLinksTheUserHas = await prisma.link.count({
+ where: {
+ collection: {
+ ownerId: userId,
+ },
+ },
+ });
+
+ if (numberOfLinksTheUserHas + 1 > MAX_LINKS_PER_USER)
+ return {
+ response: `Error: Each user can only have a maximum of ${MAX_LINKS_PER_USER} Links.`,
+ status: 400,
+ };
+
+ link.collection.name = link.collection.name.trim();
+
const description =
link.description && link.description !== ""
? link.description
@@ -86,14 +131,8 @@ export default async function postLink(
description,
type: linkType,
collection: {
- connectOrCreate: {
- where: {
- id: link.collection.id ?? 0,
- },
- create: {
- name: link.collection.name.trim(),
- ownerId: userId
- },
+ connect: {
+ id: link.collection.id,
},
},
tags: {
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index f081fc53..4d9ccecf 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -163,4 +163,3 @@ model AccessToken {
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
}
-