All files / src/components TabBar.tsx

92.15% Statements 47/51
79.54% Branches 35/44
91.66% Functions 11/12
92.15% Lines 47/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211                                        3x   3x     29x 29x 29x   29x 2x 2x 2x   2x 2x 2x 2x             29x 2x 2x   2x 2x 2x     29x 2x 2x 2x 1x 1x     1x 1x 1x 2x     29x 2x 2x   1x 1x 1x     2x     29x               29x               36x 36x 36x 36x       2x   2x                                   2x                                                 1x 1x                                                                                                                            
import { memo, useState, useRef, useCallback } from 'react'
import type { VaultEntry } from '../types'
import { cn } from '@/lib/utils'
import { X } from 'lucide-react'
import { Plus, Columns, ArrowsOutSimple } from '@phosphor-icons/react'
 
interface Tab {
  entry: VaultEntry
  content: string
}
 
interface TabBarProps {
  tabs: Tab[]
  activeTabPath: string | null
  onSwitchTab: (path: string) => void
  onCloseTab: (path: string) => void
  onCreateNote?: () => void
  onReorderTabs?: (fromIndex: number, toIndex: number) => void
}
 
const DISABLED_ICON_STYLE = { opacity: 0.4, cursor: 'not-allowed' } as const
 
export const TabBar = memo(function TabBar({
  tabs, activeTabPath, onSwitchTab, onCloseTab, onCreateNote, onReorderTabs,
}: TabBarProps) {
  const [dragIndex, setDragIndex] = useState<number | null>(null)
  const [dropIndex, setDropIndex] = useState<number | null>(null)
  const dragNodeRef = useRef<HTMLDivElement | null>(null)
 
  const handleDragStart = useCallback((e: React.DragEvent<HTMLDivElement>, index: number) => {
    setDragIndex(index)
    e.dataTransfer.effectAllowed = 'move'
    e.dataTransfer.setData('text/plain', String(index))
    // Make the drag image slightly transparent
    Eif (e.currentTarget) {
      dragNodeRef.current = e.currentTarget
      requestAnimationFrame(() => {
        Iif (dragNodeRef.current) {
          dragNodeRef.current.style.opacity = '0.5'
        }
      })
    }
  }, [])
 
  const handleDragEnd = useCallback(() => {
    Eif (dragNodeRef.current) {
      dragNodeRef.current.style.opacity = ''
    }
    dragNodeRef.current = null
    setDragIndex(null)
    setDropIndex(null)
  }, [])
 
  const handleDragOver = useCallback((e: React.DragEvent<HTMLDivElement>, index: number) => {
    e.preventDefault()
    e.dataTransfer.dropEffect = 'move'
    if (dragIndex === null || dragIndex === index) {
      setDropIndex(null)
      return
    }
    // Determine drop position based on cursor within the tab
    const rect = e.currentTarget.getBoundingClientRect()
    const midpoint = rect.left + rect.width / 2
    const insertIndex = e.clientX < midpoint ? index : index + 1
    setDropIndex(insertIndex)
  }, [dragIndex])
 
  const handleDrop = useCallback((e: React.DragEvent<HTMLDivElement>) => {
    e.preventDefault()
    if (dragIndex !== null && dropIndex !== null && dragIndex !== dropIndex && onReorderTabs) {
      // Adjust target index: if dropping after the dragged item, account for removal
      const toIndex = dropIndex > dragIndex ? dropIndex - 1 : dropIndex
      Eif (toIndex !== dragIndex) {
        onReorderTabs(dragIndex, toIndex)
      }
    }
    handleDragEnd()
  }, [dragIndex, dropIndex, onReorderTabs, handleDragEnd])
 
  const handleDragLeave = useCallback((e: React.DragEvent<HTMLDivElement>) => {
    // Only clear if we're leaving the tab bar entirely
    const relatedTarget = e.relatedTarget as HTMLElement | null
    if (!e.currentTarget.contains(relatedTarget)) {
      setDropIndex(null)
    }
  }, [])
 
  return (
    <div
      className="flex shrink-0 items-stretch"
      style={{ height: 45, background: 'var(--sidebar)', WebkitAppRegion: 'drag' } as React.CSSProperties}
      data-tauri-drag-region
      onDragLeave={handleDragLeave}
    >
      {tabs.map((tab, index) => {
        const isActive = tab.entry.path === activeTabPath
        const showDropBefore = dropIndex === index
        const showDropAfter = dropIndex === index + 1 && index === tabs.length - 1
        return (
          <div
            key={tab.entry.path}
            draggable
            onDragStart={(e) => handleDragStart(e, index)}
            onDragEnd={handleDragEnd}
            onDragOver={(e) => handleDragOver(e, index)}
            onDrop={handleDrop}
            className={cn(
              "group flex shrink-0 items-center gap-1.5 whitespace-nowrap max-w-[180px] transition-all relative",
              isActive
                ? "text-foreground"
                : "text-muted-foreground hover:text-secondary-foreground"
            )}
            style={{
              background: isActive ? 'var(--background)' : 'transparent',
              borderRight: `1px solid ${isActive ? 'var(--border)' : 'var(--sidebar-border)'}`,
              borderBottom: isActive ? 'none' : '1px solid var(--sidebar-border)',
              padding: '0 12px',
              fontSize: 12,
              fontWeight: isActive ? 500 : 400,
              cursor: dragIndex !== null ? 'grabbing' : 'grab',
              WebkitAppRegion: 'no-drag',
            } as React.CSSProperties}
            onClick={() => onSwitchTab(tab.entry.path)}
          >
            {showDropBefore && (
              <div
                style={{
                  position: 'absolute',
                  left: -1,
                  top: 8,
                  bottom: 8,
                  width: 2,
                  background: 'var(--primary)',
                  borderRadius: 1,
                  zIndex: 10,
                }}
              />
            )}
            <span className="truncate">{tab.entry.title}</span>
            <button
              className={cn(
                "shrink-0 rounded-sm p-0 bg-transparent border-none text-muted-foreground cursor-pointer transition-opacity hover:bg-accent hover:text-foreground",
                isActive ? "opacity-100" : "opacity-0 group-hover:opacity-100"
              )}
              style={{ lineHeight: 0 }}
              draggable={false}
              onClick={(e) => {
                e.stopPropagation()
                onCloseTab(tab.entry.path)
              }}
            >
              <X size={14} />
            </button>
            {showDropAfter && (
              <div
                style={{
                  position: 'absolute',
                  right: -1,
                  top: 8,
                  bottom: 8,
                  width: 2,
                  background: 'var(--primary)',
                  borderRadius: 1,
                  zIndex: 10,
                }}
              />
            )}
          </div>
        )
      })}
 
      <div className="flex-1" style={{ borderBottom: '1px solid var(--border)' }} />
 
      <div
        className="flex shrink-0 items-center"
        style={{
          borderLeft: '1px solid var(--border)',
          borderBottom: '1px solid var(--border)',
          gap: 12,
          padding: '0 12px',
          WebkitAppRegion: 'no-drag',
        } as React.CSSProperties}
      >
        <button
          className="flex items-center justify-center border-none bg-transparent p-0 text-muted-foreground cursor-pointer hover:text-foreground transition-colors"
          onClick={onCreateNote}
          title="New note"
        >
          <Plus size={16} />
        </button>
        <button
          className="flex items-center justify-center border-none bg-transparent p-0 text-muted-foreground"
          style={DISABLED_ICON_STYLE}
          title="Coming soon"
          tabIndex={-1}
        >
          <Columns size={16} />
        </button>
        <button
          className="flex items-center justify-center border-none bg-transparent p-0 text-muted-foreground"
          style={DISABLED_ICON_STYLE}
          title="Coming soon"
          tabIndex={-1}
        >
          <ArrowsOutSimple size={16} />
        </button>
      </div>
    </div>
  )
})