Skip to content

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

Start endpoint

Isaacc99067ac3731
diff --git a/apps/web/components/DashboardLayoutDropdown.tsx b/apps/web/components/DashboardLayoutDropdown.tsx
index e413665f..2770acb1 100644
--- a/apps/web/components/DashboardLayoutDropdown.tsx
+++ b/apps/web/components/DashboardLayoutDropdown.tsx
@@ -1,4 +1,4 @@
-import React, { useState, useMemo } from "react";
+import React, { useState, useMemo, useEffect } from "react";
import {
DropdownMenu,
DropdownMenuTrigger,
@@ -10,6 +10,7 @@ import TextInput from "./TextInput";
import { useCollections } from "@linkwarden/router/collections";
import { DashboardSection, DashboardSectionType } from "@linkwarden/prisma/client";
import { useUser } from "@linkwarden/router/user";
+import { useUpdateDashboardLayout } from "@linkwarden/router/dashboardData";
interface DashboardSectionOption {
type: DashboardSectionType;
@@ -23,6 +24,7 @@ export default function DashboardLayoutDropdown() {
const { t } = useTranslation();
const { data: user } = useUser();
const { data: collections = [] } = useCollections();
+ const updateDashboardLayout = useUpdateDashboardLayout();
const [searchTerm, setSearchTerm] = useState("");
const dashboardSections: DashboardSection[] = user?.dashboardSections || [];
@@ -42,7 +44,7 @@ export default function DashboardLayoutDropdown() {
);
};
- const defaultSections: DashboardSectionOption[] = [
+ const defaultSections: DashboardSectionOption[] = useMemo(() => [
{
type: DashboardSectionType.STATS,
name: t("dashboard_stats"),
@@ -61,17 +63,21 @@ export default function DashboardLayoutDropdown() {
enabled: isSectionEnabled(DashboardSectionType.PINNED_LINKS),
order: getSectionOrder(DashboardSectionType.PINNED_LINKS)
},
- ];
-
- const collectionSections: DashboardSectionOption[] = collections.map((collection) => ({
- type: DashboardSectionType.COLLECTION,
- name: collection.name,
- collectionId: collection.id,
- enabled: isSectionEnabled(DashboardSectionType.COLLECTION, collection.id),
- order: getSectionOrder(DashboardSectionType.COLLECTION, collection.id)
- }));
+ ], [user]);
+
+ const collectionSections = useMemo(() =>
+ collections.map((collection) => ({
+ type: DashboardSectionType.COLLECTION,
+ name: collection.name,
+ collectionId: collection.id,
+ enabled: isSectionEnabled(DashboardSectionType.COLLECTION, collection.id),
+ order: getSectionOrder(DashboardSectionType.COLLECTION, collection.id)
+ })), [collections, user]
+ );
- const allSections = [...defaultSections, ...collectionSections];
+ const allSections = useMemo(() => [...defaultSections, ...collectionSections], [
+ collectionSections, defaultSections
+ ]);
const filteredSections = useMemo(() => {
let sections = allSections;
@@ -100,9 +106,19 @@ export default function DashboardLayoutDropdown() {
return [...enabledSections, ...disabledSections];
}, [allSections, searchTerm]);
+
const handleCheckboxChange = (section: DashboardSectionOption) => {
- // TODO: Implement checkbox change logic
- console.log('Toggle section:', section);
+ const updatedSections = allSections.map((s) => {
+ if (s.type === section.type && s.collectionId === section.collectionId) {
+ return {
+ ...s,
+ enabled: !s.enabled,
+ };
+ }
+ return s;
+ });
+
+ updateDashboardLayout.mutateAsync(updatedSections)
};
return (
diff --git a/apps/web/components/Navbar.tsx b/apps/web/components/Navbar.tsx
index 304880a7..be2d3b58 100644
--- a/apps/web/components/Navbar.tsx
+++ b/apps/web/components/Navbar.tsx
@@ -110,7 +110,7 @@ export default function Navbar() {
</DropdownMenuContent>
</DropdownMenu>
- <ProfileDropdown />
+ {/* <ProfileDropdown /> */}
</div>
<MobileNavigation />
diff --git a/apps/web/lib/api/controllers/dashboard/updateDashboardLayout.ts b/apps/web/lib/api/controllers/dashboard/updateDashboardLayout.ts
new file mode 100644
index 00000000..8a9d8fd5
--- /dev/null
+++ b/apps/web/lib/api/controllers/dashboard/updateDashboardLayout.ts
@@ -0,0 +1,57 @@
+import { UpdateDashboardLayoutSchema, UpdateDashboardLayoutSchemaType } from "@linkwarden/lib/schemaValidation";
+import { prisma } from "@linkwarden/prisma";
+
+export default async function updateDashboardLayout(
+ userId: number,
+ body: UpdateDashboardLayoutSchemaType[]
+): Promise<any> {
+ const dataValidation = UpdateDashboardLayoutSchema.safeParse(body);
+
+ if (!dataValidation.success) {
+ console.log(dataValidation.error.issues[0].message)
+ return {
+ response: `Error: ${dataValidation.error.issues[0].message
+ } [${dataValidation.error.issues[0].path.join(", ")}]`,
+ status: 400,
+ };
+ }
+
+ const data = dataValidation.data;
+
+ for (const section of data) {
+
+ const existingSection = await prisma.dashboardSection.findFirst({
+ where: {
+ userId: userId,
+ type: section.type,
+ collectionId: section.collectionId || undefined,
+ },
+ });
+
+ if (existingSection && !section.enabled) {
+ await prisma.dashboardSection.delete({
+ where: {
+ id: existingSection.id
+ }
+ })
+ } else if (existingSection) {
+ // update
+ } else if (section.enabled) {
+ await prisma.dashboardSection.create({
+ data: {
+ userId: userId,
+ type: section.type,
+ collectionId: section.collectionId || undefined,
+ order: section.order || 0
+ }
+ });
+ }
+
+ }
+
+
+ return {
+ status: 200,
+ response: data
+ }
+}
\ No newline at end of file
diff --git a/apps/web/pages/api/v2/dashboard/index.ts b/apps/web/pages/api/v2/dashboard/index.ts
index b4c85a88..d7c9e797 100644
--- a/apps/web/pages/api/v2/dashboard/index.ts
+++ b/apps/web/pages/api/v2/dashboard/index.ts
@@ -2,6 +2,8 @@ import type { NextApiRequest, NextApiResponse } from "next";
import { LinkRequestQuery } from "@linkwarden/types";
import getDashboardDataV2 from "@/lib/api/controllers/dashboard/getDashboardDataV2";
import verifyUser from "@/lib/api/verifyUser";
+import { UpdateDashboardLayoutSchema } from "@linkwarden/lib/schemaValidation";
+import updateDashboardLayout from "@/lib/api/controllers/dashboard/updateDashboardLayout";
export default async function dashboard(
req: NextApiRequest,
@@ -28,4 +30,10 @@ export default async function dashboard(
return res.status(statusCode).json(data);
}
+
+ if (req.method === "PUT") {
+ const { status, response } = await updateDashboardLayout(user.id, req.body);
+
+ return res.status(status).json(response);
+ }
}
diff --git a/packages/lib/schemaValidation.ts b/packages/lib/schemaValidation.ts
index 656fbe70..6d130a9d 100644
--- a/packages/lib/schemaValidation.ts
+++ b/packages/lib/schemaValidation.ts
@@ -1,6 +1,7 @@
import { ArchivedFormat, TokenExpiry } from "@linkwarden/types";
-import { AiTaggingMethod, LinksRouteTo } from "@linkwarden/prisma/client";
+import { AiTaggingMethod, LinksRouteTo, DashboardSectionType } from "@linkwarden/prisma/client";
import { z } from "zod";
+import collection from "@/pages/api/v1/public/collections/[id]";
// const stringField = z.string({
// errorMap: (e) => ({
@@ -257,6 +258,13 @@ export const LinkArchiveActionSchema = z.object({
export type LinkArchiveActionSchemaType = z.infer<typeof LinkArchiveActionSchema>;
-export const UpdateDashboardLayoutSchema = z.object({
+export const UpdateDashboardLayoutSchema = z.array(
+ z.object({
+ type: z.nativeEnum(DashboardSectionType),
+ collectionId: z.number().optional(),
+ enabled: z.boolean(),
+ order: z.number().optional(),
+ })
+)
-});
\ No newline at end of file
+export type UpdateDashboardLayoutSchemaType = z.infer<typeof UpdateDashboardLayoutSchema>;
\ No newline at end of file
diff --git a/packages/router/dashboardData.tsx b/packages/router/dashboardData.tsx
index 07026081..7e348f1b 100644
--- a/packages/router/dashboardData.tsx
+++ b/packages/router/dashboardData.tsx
@@ -1,3 +1,4 @@
+import { UpdateDashboardLayoutSchemaType } from "@linkwarden/lib/schemaValidation";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { useSession } from "next-auth/react";
@@ -20,11 +21,11 @@ const useUpdateDashboardLayout = () => {
const queryClient = useQueryClient();
return useMutation({
- mutationFn: async (layout: any) => {
+ mutationFn: async (body: UpdateDashboardLayoutSchemaType) => {
const response = await fetch("/api/v2/dashboard", {
- method: "UPDATE",
+ method: "PUT",
headers: { "Content-Type": "application/json" },
- body: JSON.stringify(layout),
+ body: JSON.stringify(body),
});
const data = await response.json();
@@ -35,7 +36,11 @@ const useUpdateDashboardLayout = () => {
return data;
},
+ onSuccess: async () => {
+ await queryClient.invalidateQueries({ queryKey: ["user"] });
+ await queryClient.invalidateQueries({ queryKey: ["dashboardData"] });
+ },
});
-}
+};
export { useDashboardData, useUpdateDashboardLayout };