Skip to content

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

fix: properly sort collection list on dashboard page

Kha Nguyen3bc8bb676e7b
diff --git a/apps/web/components/DashboardLayoutDropdown.tsx b/apps/web/components/DashboardLayoutDropdown.tsx
index 1409c814..badac51e 100644
--- a/apps/web/components/DashboardLayoutDropdown.tsx
+++ b/apps/web/components/DashboardLayoutDropdown.tsx
@@ -190,21 +190,32 @@ export default function DashboardLayoutDropdown() {
const handleReorder = (sourceId: string, destId: string) => {
if (sourceId === destId) return;
- const sourceIndex = filteredSections.findIndex(
+
+ // Get only enabled sections for reordering
+ const enabledSections = filteredSections.filter((s) => s.enabled);
+
+ const sourceIndex = enabledSections.findIndex(
(s) => getSectionId(s) === sourceId
);
- const destIndex = filteredSections.findIndex(
+ const destIndex = enabledSections.findIndex(
(s) => getSectionId(s) === destId
);
if (sourceIndex < 0 || destIndex < 0) return;
- const reordered = [...filteredSections];
- const [moved] = reordered.splice(sourceIndex, 1);
- reordered.splice(destIndex, 0, moved);
+ // Reorder only the enabled sections
+ const reorderedEnabled = [...enabledSections];
+ const [moved] = reorderedEnabled.splice(sourceIndex, 1);
+ reorderedEnabled.splice(destIndex, 0, moved);
- const updated = reordered.map((section, idx) =>
- section.enabled ? { ...section, order: idx } : section
- );
+ // Assign new order values based on the reordered enabled sections
+ const reorderedWithNewOrders = reorderedEnabled.map((section, idx) => ({
+ ...section,
+ order: idx,
+ }));
+
+ // Get disabled sections and combine with reordered enabled sections
+ const disabledSections = filteredSections.filter((s) => !s.enabled);
+ const updated = [...reorderedWithNewOrders, ...disabledSections];
updateDashboardLayout.mutateAsync(updated);
};
diff --git a/apps/web/lib/api/controllers/dashboard/updateDashboardLayout.ts b/apps/web/lib/api/controllers/dashboard/updateDashboardLayout.ts
index e40410d3..514a5093 100644
--- a/apps/web/lib/api/controllers/dashboard/updateDashboardLayout.ts
+++ b/apps/web/lib/api/controllers/dashboard/updateDashboardLayout.ts
@@ -3,7 +3,6 @@ import {
UpdateDashboardLayoutSchemaType,
} from "@linkwarden/lib/schemaValidation";
import { prisma } from "@linkwarden/prisma";
-import { DashboardSection } from "@linkwarden/prisma/client";
import getDashboardData from "./getDashboardDataV2";
export default async function updateDashboardLayout(
@@ -45,136 +44,29 @@ export default async function updateDashboardLayout(
section.collectionId ? collectionIds.includes(section.collectionId) : true
);
- const existingSections = await prisma.dashboardSection.findMany({
- where: {
- userId: userId,
- },
- });
-
- const existingSectionsMap = new Map(
- existingSections.map((section) => [
- `${section.type}-${section.collectionId || "default"}`,
- section,
- ])
- );
-
- const sectionsToCreate: Pick<
- DashboardSection,
- "userId" | "type" | "collectionId" | "order"
- >[] = [];
- const sectionsToUpdate: Array<{
- id: number;
- newOrder: number;
- oldOrder: number;
- }> = [];
- const sectionsToDelete: Array<{
- id: number;
- order: number;
- }> = [];
+ // No need to track individual changes since we're doing a full replace
- for (const section of parsedData) {
- const key = `${section.type}-${section.collectionId || "default"}`;
- const existingSection = existingSectionsMap.get(key);
-
- if (existingSection && !section.enabled) {
- sectionsToDelete.push({
- id: existingSection.id,
- order: existingSection.order,
- });
- } else if (existingSection && section.enabled) {
- if (existingSection.order !== section.order) {
- sectionsToUpdate.push({
- id: existingSection.id,
- newOrder: section.order!,
- oldOrder: existingSection.order,
- });
- }
- } else if (section.enabled) {
- sectionsToCreate.push({
+ await prisma.$transaction(async (tx) => {
+ // Delete all existing dashboard sections for this user
+ await tx.dashboardSection.deleteMany({
+ where: {
+ userId,
+ },
+ });
+
+ // Create all enabled sections with their new order values
+ const sectionsToCreateOrUpdate = parsedData
+ .filter((section) => section.enabled)
+ .map((section) => ({
userId: userId,
type: section.type,
collectionId: section.collectionId || null,
order: section.order!,
- });
- }
- }
-
- await prisma.$transaction(async (tx) => {
- for (const sectionToDelete of sectionsToDelete) {
- await tx.dashboardSection.delete({
- where: {
- id: sectionToDelete.id,
- },
- });
-
- // Decrement order for all sections with order greater than deleted section
- await tx.dashboardSection.updateMany({
- where: {
- userId,
- order: {
- gt: sectionToDelete.order,
- },
- },
- data: {
- order: {
- decrement: 1,
- },
- },
- });
- }
-
- // Handle order updates - need to be careful about conflicts
- for (const sectionToUpdate of sectionsToUpdate) {
- const { id, newOrder, oldOrder } = sectionToUpdate;
-
- if (newOrder > oldOrder) {
- // Moving down: decrement order for sections between oldOrder and newOrder
- await tx.dashboardSection.updateMany({
- where: {
- userId,
- order: {
- gt: oldOrder,
- lte: newOrder,
- },
- id: {
- not: id,
- },
- },
- data: {
- order: {
- decrement: 1,
- },
- },
- });
- } else if (newOrder < oldOrder) {
- await tx.dashboardSection.updateMany({
- where: {
- userId,
- order: {
- gte: newOrder,
- lt: oldOrder,
- },
- id: {
- not: id,
- },
- },
- data: {
- order: {
- increment: 1,
- },
- },
- });
- }
-
- await tx.dashboardSection.update({
- where: { id },
- data: { order: newOrder },
- });
- }
+ }));
- if (sectionsToCreate.length > 0) {
+ if (sectionsToCreateOrUpdate.length > 0) {
await tx.dashboardSection.createMany({
- data: sectionsToCreate,
+ data: sectionsToCreateOrUpdate,
});
}
});
diff --git a/packages/router/dashboardData.tsx b/packages/router/dashboardData.tsx
index e185f3a5..07300222 100644
--- a/packages/router/dashboardData.tsx
+++ b/packages/router/dashboardData.tsx
@@ -59,9 +59,14 @@ const useUpdateDashboardLayout = () => {
const previousData = queryClient.getQueryData(["user"]);
queryClient.setQueryData(["user"], (oldData: any) => {
+ // Filter enabled sections and preserve their order
+ const enabledSections = newData
+ .filter((section) => section.enabled)
+ .sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
+
return {
...oldData,
- dashboardSections: newData.filter((section) => section.enabled),
+ dashboardSections: enabledSections,
};
});