import React, { useState } from "react"; import { Spinner } from "@/components/Spinner"; import { NavTabListItem } from "./NavTabListItem"; import { useCanvasTabsQuery, useUpdateCanvasTabMutation, } from "@/features/canvas/hooks/canvasNavigationHooks"; export const CanvasNavigationManagement = () => { const { data: tabs, isLoading, isError } = useCanvasTabsQuery(); const [draggedIndex, setDraggedIndex] = useState(null); const updateTab = useUpdateCanvasTabMutation(); const handleHideAllExternal = async () => { if (!tabs) return; for (const tab of tabs.filter( (tab) => tab.type.toLowerCase() === "external" && !tab.hidden )) { await updateTab.mutateAsync({ tabId: tab.id, hidden: true, position: tab.position, }); } }; if (isLoading) return
Loading tabs...
; if (isError || !tabs) return
Error loading tabs.
; const handleDragStart = (idx: number) => setDraggedIndex(idx); const handleDrop = async (dropIdx: number) => { if (draggedIndex === null || draggedIndex === dropIdx || !tabs) { setDraggedIndex(null); return; } const newTabs = [...tabs].sort((a, b) => a.position - b.position); const [removed] = newTabs.splice(draggedIndex, 1); newTabs.splice(dropIdx, 0, removed); // Persist new order for (let i = 0; i < newTabs.length; i++) { const tab = newTabs[i]; if (tab.position !== i + 1) { await updateTab.mutateAsync({ tabId: tab.id, position: i + 1, hidden: tab.hidden, }); } } setDraggedIndex(null); }; return (

Manage Course Navigation Tabs

{updateTab.isPending ? ( ) : ( )}
    {[...tabs] .sort((a, b) => a.position - b.position) .map((tab, idx) => ( handleDragStart(idx)} onDragOver={(e) => { e.preventDefault(); }} onDrop={() => handleDrop(idx)} /> ))}
); };