"use client" import { useCallback, useEffect, useRef, useState } from "react" import { Reorder } from "motion/react" import { FileText, GitCompare, X } from "lucide-react" import { useTranslations } from "next-intl" import { useWorkspaceContext } from "@/contexts/workspace-context" import { useShortcutSettings } from "@/hooks/use-shortcut-settings" import { matchShortcutEvent } from "@/lib/keyboard-shortcuts" import { cn } from "@/lib/utils" import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuSeparator, ContextMenuTrigger, } from "@/components/ui/context-menu" export function FileWorkspaceTabBar() { const t = useTranslations("Folder.fileWorkspace") const { mode, activePane, fileTabs, activeFileTabId, switchFileTab, closeFileTab, closeOtherFileTabs, closeAllFileTabs, reorderFileTabs, } = useWorkspaceContext() const { shortcuts } = useShortcutSettings() const scrollRef = useRef(null) const [isHovered, setIsHovered] = useState(false) const handleWheel = useCallback((e: React.WheelEvent) => { if (e.deltaY !== 0 && scrollRef.current) { e.preventDefault() scrollRef.current.scrollLeft += e.deltaY } }, []) useEffect(() => { if (!activeFileTabId || !scrollRef.current) return const el = scrollRef.current.querySelector( `[data-file-tab-id="${activeFileTabId}"]` ) el?.scrollIntoView({ block: "nearest", inline: "nearest" }) }, [activeFileTabId]) useEffect(() => { const onKeyDown = (event: KeyboardEvent) => { const shouldHandleShortcut = mode === "files" || (mode === "fusion" && activePane === "files") if (!shouldHandleShortcut) return if (matchShortcutEvent(event, shortcuts.close_all_file_tabs)) { event.preventDefault() closeAllFileTabs() return } if (!matchShortcutEvent(event, shortcuts.close_current_tab)) return if (!activeFileTabId) return event.preventDefault() closeFileTab(activeFileTabId) } window.addEventListener("keydown", onKeyDown) return () => { window.removeEventListener("keydown", onKeyDown) } }, [ activeFileTabId, closeAllFileTabs, closeFileTab, mode, activePane, shortcuts.close_all_file_tabs, shortcuts.close_current_tab, ]) if (fileTabs.length === 0) { return (
{t("files")}
) } return ( setIsHovered(true)} onMouseLeave={() => setIsHovered(false)} className={cn( "h-10 pt-1.5 px-1.5 flex items-stretch gap-1.5 border-b border-border", "overflow-x-scroll", isHovered ? [ "pb-0.5", "[&::-webkit-scrollbar]:h-1", "[&::-webkit-scrollbar-track]:bg-transparent", "[&::-webkit-scrollbar-thumb]:rounded-full", "[&::-webkit-scrollbar-thumb]:bg-border", ] : ["pb-1.5", "[&::-webkit-scrollbar]:h-0"] )} > {fileTabs.map((tab) => { const active = tab.id === activeFileTabId const isDiff = tab.kind === "diff" || tab.kind === "rich-diff" const isDirty = tab.kind === "file" && Boolean(tab.isDirty) return (
switchFileTab(tab.id)} className={cn( "group/filetab relative flex items-center h-full gap-1.5 px-3 text-xs rounded-full", "cursor-pointer select-none shrink-0 hover:bg-primary/8 transition-colors", active ? "bg-primary/10 text-foreground" : "text-muted-foreground" )} title={tab.description ?? tab.title} > {isDiff ? ( ) : ( )} {tab.title} {isDirty ? " *" : ""}
closeFileTab(tab.id)}> {t("close")} closeOtherFileTabs(tab.id)}> {t("closeOthers")} {t("closeAll")}
) })}
) }