()
| 87 | } |
| 88 | |
| 89 | export function Usage(): React.ReactNode { |
| 90 | const [utilization, setUtilization] = useState<Utilization | null>(null); |
| 91 | const [error, setError] = useState<string | null>(null); |
| 92 | const [isLoading, setIsLoading] = useState(true); |
| 93 | const { columns } = useTerminalSize(); |
| 94 | |
| 95 | const availableWidth = columns - 2; // 2 for screen padding |
| 96 | const maxWidth = Math.min(availableWidth, 80); |
| 97 | |
| 98 | const loadUtilization = React.useCallback(async () => { |
| 99 | setIsLoading(true); |
| 100 | setError(null); |
| 101 | try { |
| 102 | const data = await fetchUtilization(); |
| 103 | setUtilization(data); |
| 104 | } catch (err) { |
| 105 | logError(err as Error); |
| 106 | const axiosError = err as { response?: { data?: unknown } }; |
| 107 | const responseBody = axiosError.response?.data ? jsonStringify(axiosError.response.data) : undefined; |
| 108 | setError(responseBody ? `Failed to load usage data: ${responseBody}` : 'Failed to load usage data'); |
| 109 | } finally { |
| 110 | setIsLoading(false); |
| 111 | } |
| 112 | }, []); |
| 113 | |
| 114 | useEffect(() => { |
| 115 | void loadUtilization(); |
| 116 | }, [loadUtilization]); |
| 117 | |
| 118 | useKeybinding( |
| 119 | 'settings:retry', |
| 120 | () => { |
| 121 | void loadUtilization(); |
| 122 | }, |
| 123 | { context: 'Settings', isActive: !!error && !isLoading }, |
| 124 | ); |
| 125 | |
| 126 | if (error) { |
| 127 | return ( |
| 128 | <Box flexDirection="column" gap={1}> |
| 129 | <Text color="error">Error: {error}</Text> |
| 130 | <Text dimColor> |
| 131 | <Byline> |
| 132 | <ConfigurableShortcutHint action="settings:retry" context="Settings" fallback="r" description="retry" /> |
| 133 | <ConfigurableShortcutHint action="confirm:no" context="Settings" fallback="Esc" description="cancel" /> |
| 134 | </Byline> |
| 135 | </Text> |
| 136 | </Box> |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | if (!utilization) { |
| 141 | return ( |
| 142 | <Box flexDirection="column" gap={1}> |
| 143 | <Text dimColor>Loading usage data…</Text> |
| 144 | <Text dimColor> |
| 145 | <ConfigurableShortcutHint action="confirm:no" context="Settings" fallback="Esc" description="cancel" /> |
| 146 | </Text> |
nothing calls this directly
no test coverage detected