swadhinbiswas Initial release v0.1.0 · 1/16/2026
ed7a4e4
173 lines · 6.8 KB
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
import { motion } from 'framer-motion';
import { X, Plus, MoreVertical } from 'lucide-react';
import { useTabStore } from '../../stores/tabStore';
import { getBoardTheme } from '../../lib/design-tokens';
import { useState, useRef, useEffect } from 'react';

export default function TabBar() {
    const tabs = useTabStore((state) => state.tabs);
    const activeTabId = useTabStore((state) => state.activeTabId);
    const setActiveTab = useTabStore((state) => state.setActiveTab);
    const closeTab = useTabStore((state) => state.closeTab);
    const openTab = useTabStore((state) => state.openTab);
    const closeAllTabs = useTabStore((state) => state.closeAllTabs);
    const closeOtherTabs = useTabStore((state) => state.closeOtherTabs);

    const [contextMenu, setContextMenu] = useState<{
        x: number;
        y: number;
        tabId: string;
    } | null>(null);

    const contextMenuRef = useRef<HTMLDivElement>(null);

    useEffect(() => {
        const handleClickOutside = (e: MouseEvent) => {
            if (contextMenuRef.current && !contextMenuRef.current.contains(e.target as Node)) {
                setContextMenu(null);
            }
        };

        if (contextMenu) {
            document.addEventListener('mousedown', handleClickOutside);
            return () => document.removeEventListener('mousedown', handleClickOutside);
        }
    }, [contextMenu]);

    const handleTabClick = (tabId: string) => {
        setActiveTab(tabId);
    };

    const handleCloseTab = (e: React.MouseEvent, tabId: string) => {
        e.stopPropagation();
        closeTab(tabId);
    };

    const handleContextMenu = (e: React.MouseEvent, tabId: string) => {
        e.preventDefault();
        setContextMenu({
            x: e.clientX,
            y: e.clientY,
            tabId,
        });
    };

    const handleNewTab = () => {
        // Open a default board or show board selector
        openTab({
            type: 'catalog',
            title: '/g/ - Technology',
            board: 'g',
        });
    };

    return (
        <>
            <div className="bg-dark-surface border-b border-dark-border flex items-center h-10 overflow-x-auto scrollbar-hide">
                <div className="flex items-center h-full">
                    {tabs.map((tab) => {
                        const isActive = tab.id === activeTabId;
                        const themeColor = tab.board ? getBoardTheme(tab.board) : '#6b7280';

                        return (
                            <motion.button
                                key={tab.id}
                                onClick={() => handleTabClick(tab.id)}
                                onContextMenu={(e) => handleContextMenu(e, tab.id)}
                                className={`tab relative flex items-center gap-2 px-4 h-full border-r border-dark-border min-w-[120px] max-w-[200px] group ${isActive ? 'tab-active bg-dark-elevated' : 'tab-inactive'
                                    }`}
                                whileHover={{ backgroundColor: isActive ? undefined : 'rgba(255, 255, 255, 0.05)' }}
                            >
                                {/* Board color indicator */}
                                {tab.board && (
                                    <div
                                        className="w-2 h-2 rounded-full flex-shrink-0"
                                        style={{ backgroundColor: themeColor }}
                                    />
                                )}

                                {/* Tab title */}
                                <span className="truncate flex-1 text-sm">
                                    {tab.title}
                                </span>

                                {/* New replies indicator */}
                                {tab.hasNewReplies && (
                                    <div className="w-2 h-2 rounded-full bg-primary-500 flex-shrink-0" />
                                )}

                                {/* Close button */}
                                <button
                                    onClick={(e) => handleCloseTab(e, tab.id)}
                                    className="hover:bg-dark-hover rounded p-0.5 opacity-0 group-hover:opacity-100 transition-opacity"
                                >
                                    <X className="w-3 h-3" />
                                </button>

                                {/* Active indicator */}
                                {isActive && (
                                    <motion.div
                                        layoutId="activeTab"
                                        className="absolute bottom-0 left-0 right-0 h-0.5 bg-primary-500"
                                        initial={false}
                                        transition={{ type: 'spring', stiffness: 500, damping: 30 }}
                                    />
                                )}
                            </motion.button>
                        );
                    })}

                    {/* New Tab Button */}
                    <button
                        onClick={handleNewTab}
                        className="tab tab-inactive flex items-center justify-center px-3 h-full hover:bg-dark-hover"
                    >
                        <Plus className="w-4 h-4" />
                    </button>
                </div>
            </div>

            {/* Context Menu */}
            {contextMenu && (
                <div
                    ref={contextMenuRef}
                    className="context-menu fixed"
                    style={{
                        left: contextMenu.x,
                        top: contextMenu.y,
                        zIndex: 1000,
                    }}
                >
                    <button
                        onClick={() => {
                            closeTab(contextMenu.tabId);
                            setContextMenu(null);
                        }}
                        className="context-menu-item w-full text-left"
                    >
                        Close
                    </button>
                    <button
                        onClick={() => {
                            closeOtherTabs(contextMenu.tabId);
                            setContextMenu(null);
                        }}
                        className="context-menu-item w-full text-left"
                    >
                        Close Others
                    </button>
                    <button
                        onClick={() => {
                            closeAllTabs();
                            setContextMenu(null);
                        }}
                        className="context-menu-item w-full text-left"
                    >
                        Close All
                    </button>
                </div>
            )}
        </>
    );
}