2026-02-01 13:30:30 -03:00
|
|
|
import React from 'react';
|
|
|
|
|
import { ScrollableOverlay } from '@/components/ui/ScrollableOverlay';
|
|
|
|
|
import { ProviderLogo } from '@/components/ui/ProviderLogo';
|
2026-03-20 01:01:03 +02:00
|
|
|
import { Button } from '@/components/ui/button';
|
2026-02-01 13:30:30 -03:00
|
|
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
|
2026-05-13 13:26:15 +03:00
|
|
|
import { Icon } from "@/components/icon/Icon";
|
2026-02-01 13:30:30 -03:00
|
|
|
import { cn } from '@/lib/utils';
|
|
|
|
|
import { QUOTA_PROVIDERS, resolveUsageTone } from '@/lib/quota';
|
|
|
|
|
import { useQuotaStore } from '@/stores/useQuotaStore';
|
|
|
|
|
import { updateDesktopSettings } from '@/lib/persistence';
|
2026-04-26 14:03:39 +03:00
|
|
|
import { useI18n } from '@/lib/i18n';
|
2026-07-18 00:11:05 +03:00
|
|
|
import { SETTINGS_PANEL_TITLE_CLASS } from '@/components/sections/shared/SettingsSection';
|
2026-02-01 13:30:30 -03:00
|
|
|
|
|
|
|
|
interface UsageSidebarProps {
|
|
|
|
|
onItemSelect?: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getUsagePercent = (usage: { windows?: Record<string, { usedPercent: number | null }> } | null | undefined) => {
|
|
|
|
|
const windows = usage?.windows ?? {};
|
|
|
|
|
const values = Object.values(windows)
|
|
|
|
|
.map((window) => window.usedPercent)
|
|
|
|
|
.filter((value): value is number => typeof value === 'number');
|
|
|
|
|
if (values.length === 0) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return Math.max(...values);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const UsageSidebar: React.FC<UsageSidebarProps> = ({ onItemSelect }) => {
|
2026-04-26 14:03:39 +03:00
|
|
|
const { t } = useI18n();
|
2026-02-01 13:30:30 -03:00
|
|
|
const results = useQuotaStore((state) => state.results);
|
|
|
|
|
const selectedProviderId = useQuotaStore((state) => state.selectedProviderId);
|
|
|
|
|
const setSelectedProvider = useQuotaStore((state) => state.setSelectedProvider);
|
|
|
|
|
const fetchAllQuotas = useQuotaStore((state) => state.fetchAllQuotas);
|
|
|
|
|
const isLoading = useQuotaStore((state) => state.isLoading);
|
2026-02-04 01:13:23 -08:00
|
|
|
const usageDisplayMode = useQuotaStore((state) => state.displayMode);
|
|
|
|
|
const setUsageDisplayMode = useQuotaStore((state) => state.setDisplayMode);
|
2026-02-01 13:30:30 -03:00
|
|
|
const loadUsageSettings = useQuotaStore((state) => state.loadSettings);
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
void loadUsageSettings();
|
|
|
|
|
}, [loadUsageSettings]);
|
|
|
|
|
|
2026-08-11 12:58:21 +03:00
|
|
|
const persistUsageSettings = React.useCallback(async (changes: { usageDisplayMode?: 'usage' | 'remaining'; usageDropdownProviders?: string[] }) => {
|
2026-02-01 13:30:30 -03:00
|
|
|
try {
|
|
|
|
|
await updateDesktopSettings(changes);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.warn('Failed to save usage settings:', error);
|
|
|
|
|
}
|
|
|
|
|
}, []);
|
|
|
|
|
|
2026-02-04 01:13:23 -08:00
|
|
|
const handleUsageDisplayModeChange = React.useCallback((value: string) => {
|
|
|
|
|
if (value !== 'usage' && value !== 'remaining') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
setUsageDisplayMode(value);
|
|
|
|
|
void persistUsageSettings({ usageDisplayMode: value });
|
|
|
|
|
}, [persistUsageSettings, setUsageDisplayMode]);
|
|
|
|
|
|
2026-02-24 03:28:30 +02:00
|
|
|
const bgClass = 'bg-background';
|
2026-02-01 13:30:30 -03:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={cn('flex h-full flex-col', bgClass)}>
|
2026-02-24 03:28:30 +02:00
|
|
|
<div className="border-b px-3 pt-4 pb-3">
|
2026-07-18 00:11:05 +03:00
|
|
|
<h2 className={`${SETTINGS_PANEL_TITLE_CLASS} mb-3`}>{t('settings.usage.sidebar.title')}</h2>
|
2026-02-01 13:30:30 -03:00
|
|
|
<div className="flex items-center justify-between gap-2">
|
2026-04-26 14:03:39 +03:00
|
|
|
<span className="typography-meta text-muted-foreground">{t('settings.usage.sidebar.total', { count: QUOTA_PROVIDERS.length })}</span>
|
2026-02-01 13:30:30 -03:00
|
|
|
<div className="flex items-center gap-2">
|
2026-03-20 01:01:03 +02:00
|
|
|
<Button size="sm"
|
2026-02-01 13:30:30 -03:00
|
|
|
variant="ghost"
|
2026-02-24 03:28:30 +02:00
|
|
|
className="h-7 w-7 px-0 text-muted-foreground"
|
2026-02-01 13:30:30 -03:00
|
|
|
onClick={() => fetchAllQuotas()}
|
2026-04-26 14:03:39 +03:00
|
|
|
aria-label={t('settings.usage.sidebar.actions.refreshAria')}
|
|
|
|
|
title={t('settings.usage.sidebar.actions.refreshTitle')}
|
2026-02-01 13:30:30 -03:00
|
|
|
disabled={isLoading}
|
|
|
|
|
>
|
2026-05-13 13:26:15 +03:00
|
|
|
<Icon name="refresh" className={cn('h-3.5 w-3.5', isLoading && 'animate-spin')} />
|
2026-03-20 01:01:03 +02:00
|
|
|
</Button>
|
2026-02-01 13:30:30 -03:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2026-02-04 01:13:23 -08:00
|
|
|
<div className="mt-2 flex items-center justify-between gap-2">
|
2026-04-26 14:03:39 +03:00
|
|
|
<span className="typography-micro text-muted-foreground">{t('settings.usage.sidebar.field.display')}</span>
|
2026-02-04 01:13:23 -08:00
|
|
|
<Select value={usageDisplayMode} onValueChange={handleUsageDisplayModeChange}>
|
2026-02-24 03:28:30 +02:00
|
|
|
<SelectTrigger className="w-fit">
|
2026-04-26 14:03:39 +03:00
|
|
|
<SelectValue placeholder={t('settings.usage.sidebar.field.displayModePlaceholder')} />
|
2026-02-04 01:13:23 -08:00
|
|
|
</SelectTrigger>
|
|
|
|
|
<SelectContent>
|
2026-04-26 14:03:39 +03:00
|
|
|
<SelectItem value="usage">{t('settings.usage.sidebar.field.displayModeUsage')}</SelectItem>
|
|
|
|
|
<SelectItem value="remaining">{t('settings.usage.sidebar.field.displayModeRemaining')}</SelectItem>
|
2026-02-04 01:13:23 -08:00
|
|
|
</SelectContent>
|
|
|
|
|
</Select>
|
|
|
|
|
</div>
|
2026-02-01 13:30:30 -03:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<ScrollableOverlay outerClassName="flex-1 min-h-0" className="space-y-1 px-3 py-2 overflow-x-hidden">
|
|
|
|
|
{QUOTA_PROVIDERS.map((provider) => {
|
|
|
|
|
const result = results.find((entry) => entry.providerId === provider.id);
|
|
|
|
|
const percent = getUsagePercent(result?.usage);
|
|
|
|
|
const tone = resolveUsageTone(percent);
|
|
|
|
|
const isSelected = provider.id === selectedProviderId;
|
2026-09-09 08:01:31 +03:00
|
|
|
const configured = result?.configured;
|
2026-02-01 13:30:30 -03:00
|
|
|
|
2026-02-01 19:37:18 +02:00
|
|
|
const statusStyle = !configured
|
|
|
|
|
? { backgroundColor: 'var(--surface-muted-foreground)', opacity: 0.4 }
|
2026-02-01 13:30:30 -03:00
|
|
|
: tone === 'critical'
|
2026-02-01 19:37:18 +02:00
|
|
|
? { backgroundColor: 'var(--status-error)' }
|
2026-02-01 13:30:30 -03:00
|
|
|
: tone === 'warn'
|
2026-02-01 19:37:18 +02:00
|
|
|
? { backgroundColor: 'var(--status-warning)' }
|
|
|
|
|
: { backgroundColor: 'var(--status-success)' };
|
2026-02-01 13:30:30 -03:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
key={provider.id}
|
|
|
|
|
className={cn(
|
|
|
|
|
'group relative flex items-center rounded-md px-1.5 py-1 transition-all duration-200',
|
2026-02-01 19:37:18 +02:00
|
|
|
isSelected ? 'bg-interactive-selection' : 'hover:bg-interactive-hover'
|
2026-02-01 13:30:30 -03:00
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setSelectedProvider(provider.id);
|
|
|
|
|
onItemSelect?.();
|
|
|
|
|
}}
|
|
|
|
|
className="flex min-w-0 flex-1 items-center gap-2 rounded-sm text-left focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary/50"
|
|
|
|
|
>
|
2026-02-01 19:37:18 +02:00
|
|
|
<span className="h-2.5 w-2.5 rounded-full flex-shrink-0" style={statusStyle} />
|
2026-02-01 13:30:30 -03:00
|
|
|
<ProviderLogo providerId={provider.id} className="h-4 w-4 flex-shrink-0" />
|
|
|
|
|
<span className="typography-ui-label font-normal truncate flex-1 min-w-0 text-foreground">
|
|
|
|
|
{provider.name}
|
|
|
|
|
</span>
|
2026-09-09 08:01:31 +03:00
|
|
|
{configured === false && (
|
2026-04-26 14:03:39 +03:00
|
|
|
<span className="typography-micro text-muted-foreground/60 flex-shrink-0">{t('settings.usage.sidebar.status.notSet')}</span>
|
2026-02-04 01:13:23 -08:00
|
|
|
)}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
2026-02-01 13:30:30 -03:00
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</ScrollableOverlay>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|