11"use client" ;
22
3- import { useEffect , useMemo } from "react" ;
3+ import { useEffect , useMemo , useRef } from "react" ;
44import { usePathname , useRouter , useSearchParams } from "next/navigation" ;
55import { useModal } from "@/hooks/use-modal" ;
66import { 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 */
1311export 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