|
| 1 | +import React from "react"; |
| 2 | +import styled from "@emotion/styled"; |
| 3 | +import { usePersistedState } from "@/hooks/usePersistedState"; |
| 4 | +import { useWorkspaceUsage } from "@/stores/WorkspaceStore"; |
| 5 | +import { use1MContext } from "@/hooks/use1MContext"; |
| 6 | +import { useResizeObserver } from "@/hooks/useResizeObserver"; |
| 7 | +import { CostsTab } from "./RightSidebar/CostsTab"; |
| 8 | +import { ToolsTab } from "./RightSidebar/ToolsTab"; |
| 9 | +import { VerticalTokenMeter } from "./RightSidebar/VerticalTokenMeter"; |
| 10 | +import { calculateTokenMeterData } from "@/utils/tokens/tokenMeterUtils"; |
| 11 | + |
| 12 | +interface SidebarContainerProps { |
| 13 | + collapsed: boolean; |
| 14 | +} |
| 15 | + |
| 16 | +const SidebarContainer = styled.div<SidebarContainerProps>` |
| 17 | + width: ${(props) => (props.collapsed ? "20px" : "300px")}; |
| 18 | + background: #252526; |
| 19 | + border-left: 1px solid #3e3e42; |
| 20 | + display: flex; |
| 21 | + flex-direction: column; |
| 22 | + overflow: hidden; |
| 23 | + transition: width 0.2s ease; |
| 24 | + flex-shrink: 0; |
| 25 | +
|
| 26 | + /* Keep vertical bar always visible when collapsed */ |
| 27 | + ${(props) => |
| 28 | + props.collapsed && |
| 29 | + ` |
| 30 | + position: sticky; |
| 31 | + right: 0; |
| 32 | + z-index: 10; |
| 33 | + box-shadow: -2px 0 4px rgba(0, 0, 0, 0.2); |
| 34 | + `} |
| 35 | +`; |
| 36 | + |
| 37 | +const FullView = styled.div<{ visible: boolean }>` |
| 38 | + display: ${(props) => (props.visible ? "flex" : "none")}; |
| 39 | + flex-direction: column; |
| 40 | + height: 100%; |
| 41 | +`; |
| 42 | + |
| 43 | +const CollapsedView = styled.div<{ visible: boolean }>` |
| 44 | + display: ${(props) => (props.visible ? "flex" : "none")}; |
| 45 | + height: 100%; |
| 46 | +`; |
| 47 | + |
| 48 | +const TabBar = styled.div` |
| 49 | + display: flex; |
| 50 | + background: #2d2d2d; |
| 51 | + border-bottom: 1px solid #3e3e42; |
| 52 | +`; |
| 53 | + |
| 54 | +interface TabButtonProps { |
| 55 | + active: boolean; |
| 56 | +} |
| 57 | + |
| 58 | +const TabButton = styled.button<TabButtonProps>` |
| 59 | + flex: 1; |
| 60 | + padding: 10px 15px; |
| 61 | + background: ${(props) => (props.active ? "#252526" : "transparent")}; |
| 62 | + color: ${(props) => (props.active ? "#ffffff" : "#888888")}; |
| 63 | + border: none; |
| 64 | + border-bottom: 2px solid ${(props) => (props.active ? "#007acc" : "transparent")}; |
| 65 | + cursor: pointer; |
| 66 | + font-family: var(--font-primary); |
| 67 | + font-size: 13px; |
| 68 | + font-weight: 500; |
| 69 | + transition: all 0.2s ease; |
| 70 | +
|
| 71 | + &:hover { |
| 72 | + background: ${(props) => (props.active ? "#252526" : "#2d2d2d")}; |
| 73 | + color: ${(props) => (props.active ? "#ffffff" : "#cccccc")}; |
| 74 | + } |
| 75 | +`; |
| 76 | + |
| 77 | +const TabContent = styled.div` |
| 78 | + flex: 1; |
| 79 | + overflow-y: auto; |
| 80 | + padding: 15px; |
| 81 | +`; |
| 82 | + |
| 83 | +type TabType = "costs" | "tools"; |
| 84 | + |
| 85 | +interface ChatMetaSidebarProps { |
| 86 | + workspaceId: string; |
| 87 | + chatAreaRef: React.RefObject<HTMLDivElement>; |
| 88 | +} |
| 89 | + |
| 90 | +const ChatMetaSidebarComponent: React.FC<ChatMetaSidebarProps> = ({ workspaceId, chatAreaRef }) => { |
| 91 | + const [selectedTab, setSelectedTab] = usePersistedState<TabType>( |
| 92 | + `chat-meta-sidebar-tab:${workspaceId}`, |
| 93 | + "costs" |
| 94 | + ); |
| 95 | + |
| 96 | + const usage = useWorkspaceUsage(workspaceId); |
| 97 | + const [use1M] = use1MContext(); |
| 98 | + const chatAreaSize = useResizeObserver(chatAreaRef); |
| 99 | + |
| 100 | + const baseId = `chat-meta-${workspaceId}`; |
| 101 | + const costsTabId = `${baseId}-tab-costs`; |
| 102 | + const toolsTabId = `${baseId}-tab-tools`; |
| 103 | + const costsPanelId = `${baseId}-panel-costs`; |
| 104 | + const toolsPanelId = `${baseId}-panel-tools`; |
| 105 | + |
| 106 | + const lastUsage = usage?.usageHistory[usage.usageHistory.length - 1]; |
| 107 | + |
| 108 | + // Memoize vertical meter data calculation to prevent unnecessary re-renders |
| 109 | + const verticalMeterData = React.useMemo(() => { |
| 110 | + // Get model from last usage |
| 111 | + const model = lastUsage?.model ?? "unknown"; |
| 112 | + return lastUsage |
| 113 | + ? calculateTokenMeterData(lastUsage, model, use1M, true) |
| 114 | + : { segments: [], totalTokens: 0, totalPercentage: 0 }; |
| 115 | + }, [lastUsage, use1M]); |
| 116 | + |
| 117 | + // Calculate if we should show collapsed view with hysteresis |
| 118 | + // Strategy: Observe ChatArea width directly (independent of sidebar width) |
| 119 | + // - ChatArea has min-width: 750px and flex: 1 |
| 120 | + // - Use hysteresis to prevent oscillation: |
| 121 | + // * Collapse when chatAreaWidth <= 800px (tight space) |
| 122 | + // * Expand when chatAreaWidth >= 1100px (lots of space) |
| 123 | + // * Between 800-1100: maintain current state (dead zone) |
| 124 | + const COLLAPSE_THRESHOLD = 800; // Collapse below this |
| 125 | + const EXPAND_THRESHOLD = 1100; // Expand above this |
| 126 | + const chatAreaWidth = chatAreaSize?.width ?? 1000; // Default to large to avoid flash |
| 127 | + |
| 128 | + // Persist collapsed state globally (not per-workspace) since chat area width is shared |
| 129 | + // This prevents animation flash when switching workspaces - sidebar maintains its state |
| 130 | + const [showCollapsed, setShowCollapsed] = usePersistedState<boolean>( |
| 131 | + "chat-meta-sidebar:collapsed", |
| 132 | + false |
| 133 | + ); |
| 134 | + |
| 135 | + React.useEffect(() => { |
| 136 | + if (chatAreaWidth <= COLLAPSE_THRESHOLD) { |
| 137 | + setShowCollapsed(true); |
| 138 | + } else if (chatAreaWidth >= EXPAND_THRESHOLD) { |
| 139 | + setShowCollapsed(false); |
| 140 | + } |
| 141 | + // Between thresholds: maintain current state (no change) |
| 142 | + }, [chatAreaWidth, setShowCollapsed]); |
| 143 | + |
| 144 | + return ( |
| 145 | + <SidebarContainer |
| 146 | + collapsed={showCollapsed} |
| 147 | + role="complementary" |
| 148 | + aria-label="Workspace insights" |
| 149 | + > |
| 150 | + <FullView visible={!showCollapsed}> |
| 151 | + <TabBar role="tablist" aria-label="Metadata views"> |
| 152 | + <TabButton |
| 153 | + active={selectedTab === "costs"} |
| 154 | + onClick={() => setSelectedTab("costs")} |
| 155 | + id={costsTabId} |
| 156 | + role="tab" |
| 157 | + type="button" |
| 158 | + aria-selected={selectedTab === "costs"} |
| 159 | + aria-controls={costsPanelId} |
| 160 | + > |
| 161 | + Costs |
| 162 | + </TabButton> |
| 163 | + <TabButton |
| 164 | + active={selectedTab === "tools"} |
| 165 | + onClick={() => setSelectedTab("tools")} |
| 166 | + id={toolsTabId} |
| 167 | + role="tab" |
| 168 | + type="button" |
| 169 | + aria-selected={selectedTab === "tools"} |
| 170 | + aria-controls={toolsPanelId} |
| 171 | + > |
| 172 | + Tools |
| 173 | + </TabButton> |
| 174 | + </TabBar> |
| 175 | + <TabContent> |
| 176 | + {selectedTab === "costs" && ( |
| 177 | + <div role="tabpanel" id={costsPanelId} aria-labelledby={costsTabId}> |
| 178 | + <CostsTab workspaceId={workspaceId} /> |
| 179 | + </div> |
| 180 | + )} |
| 181 | + {selectedTab === "tools" && ( |
| 182 | + <div role="tabpanel" id={toolsPanelId} aria-labelledby={toolsTabId}> |
| 183 | + <ToolsTab /> |
| 184 | + </div> |
| 185 | + )} |
| 186 | + </TabContent> |
| 187 | + </FullView> |
| 188 | + <CollapsedView visible={showCollapsed}> |
| 189 | + <VerticalTokenMeter data={verticalMeterData} /> |
| 190 | + </CollapsedView> |
| 191 | + </SidebarContainer> |
| 192 | + ); |
| 193 | +}; |
| 194 | + |
| 195 | +// Memoize to prevent re-renders when parent (AIView) re-renders during streaming |
| 196 | +// Only re-renders when workspaceId or chatAreaRef changes, or internal state updates |
| 197 | +export const ChatMetaSidebar = React.memo(ChatMetaSidebarComponent); |
0 commit comments