|
| 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 { VerticalTokenMeter } from "./RightSidebar/VerticalTokenMeter"; |
| 9 | +import { calculateTokenMeterData } from "@/utils/tokens/tokenMeterUtils"; |
| 10 | + |
| 11 | +interface SidebarContainerProps { |
| 12 | + collapsed: boolean; |
| 13 | +} |
| 14 | + |
| 15 | +const SidebarContainer = styled.div<SidebarContainerProps>` |
| 16 | + width: ${(props) => (props.collapsed ? "20px" : "300px")}; |
| 17 | + background: #252526; |
| 18 | + border-left: 1px solid #3e3e42; |
| 19 | + display: flex; |
| 20 | + flex-direction: column; |
| 21 | + overflow: hidden; |
| 22 | + transition: width 0.2s ease; |
| 23 | + flex-shrink: 0; |
| 24 | +
|
| 25 | + /* Keep vertical bar always visible when collapsed */ |
| 26 | + ${(props) => |
| 27 | + props.collapsed && |
| 28 | + ` |
| 29 | + position: sticky; |
| 30 | + right: 0; |
| 31 | + z-index: 10; |
| 32 | + box-shadow: -2px 0 4px rgba(0, 0, 0, 0.2); |
| 33 | + `} |
| 34 | +`; |
| 35 | + |
| 36 | +const FullView = styled.div<{ visible: boolean }>` |
| 37 | + display: ${(props) => (props.visible ? "flex" : "none")}; |
| 38 | + flex-direction: column; |
| 39 | + height: 100%; |
| 40 | +`; |
| 41 | + |
| 42 | +const CollapsedView = styled.div<{ visible: boolean }>` |
| 43 | + display: ${(props) => (props.visible ? "flex" : "none")}; |
| 44 | + height: 100%; |
| 45 | +`; |
| 46 | + |
| 47 | +const ContentScroll = styled.div` |
| 48 | + flex: 1; |
| 49 | + overflow-y: auto; |
| 50 | + padding: 15px; |
| 51 | +`; |
| 52 | + |
| 53 | +interface ChatMetaSidebarProps { |
| 54 | + workspaceId: string; |
| 55 | + chatAreaRef: React.RefObject<HTMLDivElement>; |
| 56 | +} |
| 57 | + |
| 58 | +const ChatMetaSidebarComponent: React.FC<ChatMetaSidebarProps> = ({ workspaceId, chatAreaRef }) => { |
| 59 | + const usage = useWorkspaceUsage(workspaceId); |
| 60 | + const [use1M] = use1MContext(); |
| 61 | + const chatAreaSize = useResizeObserver(chatAreaRef); |
| 62 | + |
| 63 | + const lastUsage = usage?.usageHistory[usage.usageHistory.length - 1]; |
| 64 | + |
| 65 | + // Memoize vertical meter data calculation to prevent unnecessary re-renders |
| 66 | + const verticalMeterData = React.useMemo(() => { |
| 67 | + // Get model from last usage |
| 68 | + const model = lastUsage?.model ?? "unknown"; |
| 69 | + return lastUsage |
| 70 | + ? calculateTokenMeterData(lastUsage, model, use1M, true) |
| 71 | + : { segments: [], totalTokens: 0, totalPercentage: 0 }; |
| 72 | + }, [lastUsage, use1M]); |
| 73 | + |
| 74 | + // Calculate if we should show collapsed view with hysteresis |
| 75 | + // Strategy: Observe ChatArea width directly (independent of sidebar width) |
| 76 | + // - ChatArea has min-width: 750px and flex: 1 |
| 77 | + // - Use hysteresis to prevent oscillation: |
| 78 | + // * Collapse when chatAreaWidth <= 800px (tight space) |
| 79 | + // * Expand when chatAreaWidth >= 1100px (lots of space) |
| 80 | + // * Between 800-1100: maintain current state (dead zone) |
| 81 | + const COLLAPSE_THRESHOLD = 800; // Collapse below this |
| 82 | + const EXPAND_THRESHOLD = 1100; // Expand above this |
| 83 | + const chatAreaWidth = chatAreaSize?.width ?? 1000; // Default to large to avoid flash |
| 84 | + |
| 85 | + // Persist collapsed state globally (not per-workspace) since chat area width is shared |
| 86 | + // This prevents animation flash when switching workspaces - sidebar maintains its state |
| 87 | + const [showCollapsed, setShowCollapsed] = usePersistedState<boolean>( |
| 88 | + "chat-meta-sidebar:collapsed", |
| 89 | + false |
| 90 | + ); |
| 91 | + |
| 92 | + React.useEffect(() => { |
| 93 | + if (chatAreaWidth <= COLLAPSE_THRESHOLD) { |
| 94 | + setShowCollapsed(true); |
| 95 | + } else if (chatAreaWidth >= EXPAND_THRESHOLD) { |
| 96 | + setShowCollapsed(false); |
| 97 | + } |
| 98 | + // Between thresholds: maintain current state (no change) |
| 99 | + }, [chatAreaWidth, setShowCollapsed]); |
| 100 | + |
| 101 | + return ( |
| 102 | + <SidebarContainer |
| 103 | + collapsed={showCollapsed} |
| 104 | + role="complementary" |
| 105 | + aria-label="Workspace insights" |
| 106 | + > |
| 107 | + <FullView visible={!showCollapsed}> |
| 108 | + <ContentScroll role="region" aria-label="Cost breakdown"> |
| 109 | + <CostsTab workspaceId={workspaceId} /> |
| 110 | + </ContentScroll> |
| 111 | + </FullView> |
| 112 | + <CollapsedView visible={showCollapsed}> |
| 113 | + <VerticalTokenMeter data={verticalMeterData} /> |
| 114 | + </CollapsedView> |
| 115 | + </SidebarContainer> |
| 116 | + ); |
| 117 | +}; |
| 118 | + |
| 119 | +// Memoize to prevent re-renders when parent (AIView) re-renders during streaming |
| 120 | +// Only re-renders when workspaceId or chatAreaRef changes, or internal state updates |
| 121 | +export const ChatMetaSidebar = React.memo(ChatMetaSidebarComponent); |
0 commit comments