Skip to content

Commit 9600397

Browse files
committed
sync kanban task modal with url param via hook
1 parent d6d4a1c commit 9600397

3 files changed

Lines changed: 19 additions & 7 deletions

File tree

‎apps/codebility/app/home/kanban/[projectId]/[id]/_components/KanbanBoard.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22

3-
import { memo, useCallback, useEffect, useMemo, useState } from "react";
3+
import { memo, useCallback, useEffect, useMemo, useRef, useState } from "react";
44
import Link from "next/link";
55
import KanbanBoardsSearch from "@/app/home/kanban/_components/KanbanBoardsSearch";
66
import { getMembers } from "@/app/home/projects/actions";
@@ -18,6 +18,7 @@ import KanbanColumnAddButton from "./kanban_modals/KanbanColumnAddButton";
1818
import KanbanColumnAddModal from "./kanban_modals/KanbanColumnAddModal";
1919
import KanbanBoardColumnContainer from "./KanbanBoardColumnContainer";
2020
import UserTaskFilter from "./UserTaskFilter";
21+
import { useKanbanTaskUrlModal } from "@/hooks/useKanbanTaskUrlModal";
2122

2223
interface KanbanBoardProps {
2324
boardData: KanbanBoardType & { kanban_columns: KanbanColumnType[] };
@@ -32,6 +33,9 @@ interface KanbanBoardProps {
3233
function KanbanBoard({ boardData, projectId }: KanbanBoardProps) {
3334
const user = useUserStore((state) => state.user);
3435

36+
// Sync the Kanban task modal with the URL ?taskId param
37+
useKanbanTaskUrlModal(boardData);
38+
3539
// User permissions - simple boolean checks following YAGNI principle
3640
const canAddColumn = user?.role_id === 1 || user?.role_id === 5;
3741
const canAddMember = canAddColumn;

‎apps/codebility/app/home/kanban/[projectId]/[id]/_components/kanban_modals/KanbanTaskViewEditModal.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { ReactNode, memo, useCallback } from "react";
44
import { useModal } from "@/hooks/use-modal";
55
import { ExtendedTask } from "@/types/home/codev";
6+
import { usePathname, useRouter } from "next/navigation";
67

78
interface Props {
89
children: ReactNode;
@@ -15,9 +16,12 @@ function KanbanTaskViewEditModal({
1516
task,
1617
onComplete,
1718
}: Props) {
19+
const router = useRouter();
20+
const pathname = usePathname();
1821
const { onOpen } = useModal();
1922

2023
const handleClick = useCallback(() => {
24+
router.push(`${pathname}?taskId=${task.id}`, { scroll: false });
2125
onOpen("taskViewModal", task, onComplete);
2226
}, [onOpen, task, onComplete]);
2327

‎apps/codebility/hooks/useKanbanTaskUrlModal.ts‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
"use client";
22

3-
import { useEffect, useMemo } from "react";
3+
import { useEffect, useMemo, useRef } from "react";
44
import { usePathname, useRouter, useSearchParams } from "next/navigation";
55
import { useModal } from "@/hooks/use-modal";
66
import { KanbanBoardType, KanbanColumnType } from "@/types/home/codev";
77

88
/**
99
* 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.
1210
*/
1311
export function useKanbanTaskUrlModal(
1412
boardData: KanbanBoardType & { kanban_columns: KanbanColumnType[] }
@@ -24,25 +22,31 @@ export function useKanbanTaskUrlModal(
2422
[boardData.kanban_columns]
2523
);
2624

25+
// Track whether we've already opened the modal from the URL
26+
const openedFromUrlRef = useRef(false);
27+
2728
useEffect(() => {
2829
const taskId = searchParams.get("taskId");
2930

30-
// Open modal if taskId exists and modal is not already open
31-
if (taskId && !isOpen) {
31+
// Open modal only once from URL
32+
if (taskId && !openedFromUrlRef.current) {
3233
const task = tasks.find((t) => t.id === taskId);
3334
if (task) {
3435
onOpen("taskViewModal", task);
36+
openedFromUrlRef.current = true;
3537
}
3638
}
3739

38-
// Close modal → clean URL
40+
// Clear URL if modal closed
3941
if (!isOpen && searchParams.has("taskId")) {
4042
router.replace(pathname, { scroll: false });
43+
openedFromUrlRef.current = false; // reset so next URL param works
4144
}
4245

4346
// If modal type changes away from taskViewModal, remove param
4447
if (isOpen && type !== "taskViewModal" && searchParams.has("taskId")) {
4548
router.replace(pathname, { scroll: false });
49+
openedFromUrlRef.current = false;
4650
}
4751
}, [isOpen, type, tasks, onOpen, pathname, searchParams, router]);
4852
}

0 commit comments

Comments
 (0)