Skip to content

Commit d6d4a1c

Browse files
committed
create hook for kanban task modal url sync
1 parent 6fb9d65 commit d6d4a1c

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"use client";
2+
3+
import { useEffect, useMemo } from "react";
4+
import { usePathname, useRouter, useSearchParams } from "next/navigation";
5+
import { useModal } from "@/hooks/use-modal";
6+
import { KanbanBoardType, KanbanColumnType } from "@/types/home/codev";
7+
8+
/**
9+
* Syncs the Kanban task modal with the URL ?taskId param.
10+
* - Opens the modal automatically if taskId exists.
11+
* - Cleans the URL when the modal closes.
12+
*/
13+
export function useKanbanTaskUrlModal(
14+
boardData: KanbanBoardType & { kanban_columns: KanbanColumnType[] }
15+
) {
16+
const { isOpen, type, onOpen } = useModal();
17+
const router = useRouter();
18+
const pathname = usePathname();
19+
const searchParams = useSearchParams();
20+
21+
// Flatten all tasks once
22+
const tasks = useMemo(
23+
() => boardData.kanban_columns.flatMap((col) => col.tasks ?? []),
24+
[boardData.kanban_columns]
25+
);
26+
27+
useEffect(() => {
28+
const taskId = searchParams.get("taskId");
29+
30+
// Open modal if taskId exists and modal is not already open
31+
if (taskId && !isOpen) {
32+
const task = tasks.find((t) => t.id === taskId);
33+
if (task) {
34+
onOpen("taskViewModal", task);
35+
}
36+
}
37+
38+
// Close modal → clean URL
39+
if (!isOpen && searchParams.has("taskId")) {
40+
router.replace(pathname, { scroll: false });
41+
}
42+
43+
// If modal type changes away from taskViewModal, remove param
44+
if (isOpen && type !== "taskViewModal" && searchParams.has("taskId")) {
45+
router.replace(pathname, { scroll: false });
46+
}
47+
}, [isOpen, type, tasks, onOpen, pathname, searchParams, router]);
48+
}

0 commit comments

Comments
 (0)