mirror of
https://wget.la/https://github.com/leookun/cursor-byok
synced 2026-12-01 01:05:10 +08:00
feat(desktop): add tooltip provider and model management
This commit is contained in:
1 parent
37f9c4c4c8
commit
4700d3abc0
25 files changed
+717
-262
No files matched your search
@@ -15,6 +15,25 @@ import { useI18n } from "../../i18n/store";
|
||||
|
||||
type TimeRange = { startMs: number; endMs: number };
|
||||
|
||||
const CALENDAR_DAYS = 365;
|
||||
const DAY_MS = 24 * 60 * 60_000;
|
||||
|
||||
function contributionCalendarData(overview: Overview, endMs: number) {
|
||||
const tokensByDate = new Map<string, number>();
|
||||
for (const bucket of overview.token_usage_series) {
|
||||
const date = new Date(bucket.bucket_start_ms).toISOString().slice(0, 10);
|
||||
const tokens = bucket.input_tokens + bucket.cache_read_tokens + bucket.cache_write_tokens + bucket.output_tokens;
|
||||
tokensByDate.set(date, (tokensByDate.get(date) ?? 0) + tokens);
|
||||
}
|
||||
const lastDay = new Date(Math.max(0, endMs - 1));
|
||||
lastDay.setUTCHours(0, 0, 0, 0);
|
||||
const firstDayMs = lastDay.getTime() - (CALENDAR_DAYS - 1) * DAY_MS;
|
||||
return Array.from({ length: CALENDAR_DAYS }, (_, offset) => {
|
||||
const date = new Date(firstDayMs + offset * DAY_MS).toISOString().slice(0, 10);
|
||||
return { date, tokens: tokensByDate.get(date) ?? 0 };
|
||||
});
|
||||
}
|
||||
|
||||
function presetRange(preset: Exclude<OverviewRangePreset, "custom">, now = new Date()): TimeRange {
|
||||
const endMs = now.getTime();
|
||||
if (preset === "today") {
|
||||
@@ -74,10 +93,7 @@ export function HomePage() {
|
||||
cacheWriteTokens: bucket.cache_write_tokens,
|
||||
outputTokens: bucket.output_tokens,
|
||||
}));
|
||||
const contribution = overview.token_usage_series.map((bucket) => ({
|
||||
date: new Date(bucket.bucket_start_ms).toISOString().slice(0, 10),
|
||||
tokens: bucket.input_tokens + bucket.cache_read_tokens + bucket.cache_write_tokens + bucket.output_tokens,
|
||||
}));
|
||||
const contribution = contributionCalendarData(filteredOverview, selectedRange?.endMs ?? Date.now());
|
||||
const metrics = {
|
||||
llmCalls: filteredOverview.metrics.llm_calls,
|
||||
successfulCalls: filteredOverview.metrics.successful_calls,
|
||||
@@ -107,13 +123,15 @@ export function HomePage() {
|
||||
setCustomOpen(false);
|
||||
};
|
||||
const selectQuick = (durationMs: number, bucketMs?: number) => {
|
||||
const endMs = Date.now();
|
||||
setCustomRange({ startMs: endMs - durationMs, endMs });
|
||||
const end = new Date();
|
||||
const start = new Date(end.getTime() - durationMs);
|
||||
setCustomStart(formatTimeInput(start));
|
||||
setCustomEnd(formatTimeInput(end));
|
||||
setCustomRange({ startMs: start.getTime(), endMs: end.getTime() });
|
||||
setAppliedModels(selectedModels);
|
||||
setQuick(durationMs === 4 * 60 * 60_000 ? "four-hours" : "twenty-four-hours");
|
||||
setFourHourBucket(durationMs === 4 * 60 * 60_000 ? bucketMs : undefined);
|
||||
setPreset("custom");
|
||||
setCustomOpen(false);
|
||||
};
|
||||
const selectPreset = (value: Exclude<OverviewRangePreset, "custom">) => {
|
||||
setPreset(value);
|
||||
|
||||
@@ -2,7 +2,7 @@ import { useLayoutEffect, useMemo, useRef, useState } from "react";
|
||||
import { init, Rect, type ElementEvent } from "zrender";
|
||||
import type { Locale } from "../../../i18n/runtime";
|
||||
import { useI18n } from "../../../i18n/store";
|
||||
import { Tooltip, type TooltipAnchor } from "../../../shared/ui/Tooltip";
|
||||
import { useTooltip, type TooltipAnchor } from "../../../shared/ui/Tooltip";
|
||||
import styles from "./ContributionCalendarChart.module.scss";
|
||||
|
||||
export type ContributionDay = {
|
||||
@@ -28,12 +28,6 @@ type CellExtra = CalendarCell & {
|
||||
height: number;
|
||||
};
|
||||
|
||||
type TooltipState = {
|
||||
date: string;
|
||||
tokens: number;
|
||||
anchor: TooltipAnchor;
|
||||
};
|
||||
|
||||
type AxisLabel = {
|
||||
key: string;
|
||||
text: string;
|
||||
@@ -101,7 +95,7 @@ export function ContributionCalendarChart({ data }: ContributionCalendarChartPro
|
||||
const canvasRef = useRef<HTMLDivElement>(null);
|
||||
const layoutRef = useRef<ReturnType<typeof buildCalendarLayout>>(null);
|
||||
const scheduleDrawRef = useRef<() => void>(() => undefined);
|
||||
const [tooltip, setTooltip] = useState<TooltipState | null>(null);
|
||||
const { show: showTooltip, hide: hideTooltip } = useTooltip();
|
||||
const [axisLabels, setAxisLabels] = useState<AxisLabel[]>([]);
|
||||
const layout = useMemo(() => buildCalendarLayout(data, locale), [data, locale]);
|
||||
const tokenFormatter = useMemo(() => new Intl.NumberFormat(locale), [locale]);
|
||||
@@ -129,10 +123,13 @@ export function ContributionCalendarChart({ data }: ContributionCalendarChartPro
|
||||
return new DOMRect(bounds.left + extra.x, bounds.top + extra.y, extra.width, extra.height);
|
||||
},
|
||||
};
|
||||
setTooltip({ date: extra.date, tokens: extra.tokens, anchor });
|
||||
showTooltip(anchor, undefined, <div className={styles.tooltipContent}>
|
||||
<strong>{extra.date}</strong>
|
||||
<span>{t("Token 用量:{tokens}", { tokens: tokenFormatter.format(extra.tokens) })}</span>
|
||||
</div>);
|
||||
};
|
||||
const handleMouseOut = (event: ElementEvent) => {
|
||||
if (isCellExtra(event.target?.extra)) setTooltip(null);
|
||||
if (isCellExtra(event.target?.extra)) hideTooltip();
|
||||
};
|
||||
|
||||
chart.on("mouseover", handleMouseOver);
|
||||
@@ -226,7 +223,7 @@ export function ContributionCalendarChart({ data }: ContributionCalendarChartPro
|
||||
cellRects.set(cell.date, rect);
|
||||
chart.add(rect);
|
||||
}
|
||||
setTooltip(null);
|
||||
hideTooltip();
|
||||
};
|
||||
const scheduleDraw = () => {
|
||||
window.cancelAnimationFrame(drawFrame);
|
||||
@@ -264,12 +261,6 @@ export function ContributionCalendarChart({ data }: ContributionCalendarChartPro
|
||||
{axisLabels.map((label) => <span key={label.key} style={{ left: label.left }}>{label.text}</span>)}
|
||||
</div>
|
||||
</div>
|
||||
<Tooltip anchor={tooltip?.anchor ?? null}>
|
||||
<div className={styles.tooltipContent}>
|
||||
<strong>{tooltip?.date}</strong>
|
||||
<span>{t("Token 用量:{tokens}", { tokens: tokenFormatter.format(tooltip?.tokens ?? 0) })}</span>
|
||||
</div>
|
||||
</Tooltip>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
import { useState } from "react";
|
||||
import { formatCompactInteger, formatInteger } from "../../../shared/utils/numberFormat";
|
||||
import { Icon } from "../../../shared/ui/Icon";
|
||||
import { Tooltip, type TooltipAnchor } from "../../../shared/ui/Tooltip";
|
||||
import { useTooltip, type TooltipAnchor } from "../../../shared/ui/Tooltip";
|
||||
import { informationOutlineIcon } from "../../../shared/ui/icons";
|
||||
import { CacheHitRateChart } from "./CacheHitRateChart";
|
||||
import styles from "./HomeMetrics.module.scss";
|
||||
@@ -53,22 +52,17 @@ function elementAnchor(element: HTMLElement): TooltipAnchor {
|
||||
}
|
||||
|
||||
function InfoTooltip({ content }: { content: string }) {
|
||||
const [anchor, setAnchor] = useState<TooltipAnchor | null>(null);
|
||||
const { show, hide } = useTooltip();
|
||||
|
||||
return <>
|
||||
<button
|
||||
type="button"
|
||||
className={styles.info}
|
||||
aria-label={t("查看说明")}
|
||||
onMouseEnter={(event) => setAnchor(elementAnchor(event.currentTarget))}
|
||||
onMouseLeave={() => setAnchor(null)}
|
||||
onFocus={(event) => setAnchor(elementAnchor(event.currentTarget))}
|
||||
onBlur={() => setAnchor(null)}
|
||||
><Icon icon={informationOutlineIcon} size="1.1em" /></button>
|
||||
<Tooltip anchor={anchor}>
|
||||
<div className={styles.tooltipText}>{content}</div>
|
||||
</Tooltip>
|
||||
</>;
|
||||
return <button
|
||||
type="button"
|
||||
className={styles.info}
|
||||
aria-label={t("查看说明")}
|
||||
onMouseEnter={(event) => show(elementAnchor(event.currentTarget), undefined, <div className={styles.tooltipText}>{content}</div>)}
|
||||
onMouseLeave={hide}
|
||||
onFocus={(event) => show(elementAnchor(event.currentTarget), undefined, <div className={styles.tooltipText}>{content}</div>)}
|
||||
onBlur={hide}
|
||||
><Icon icon={informationOutlineIcon} size="1.1em" /></button>;
|
||||
}
|
||||
|
||||
export function HomeMetrics({ data, refreshVersion = 0 }: { data: HomeMetricsData; refreshVersion?: number }) {
|
||||
|
||||
@@ -157,7 +157,10 @@ export function OverviewTimeRangeFilter({ value, quick, fourHourBucket, customOp
|
||||
aria-controls={granularityOpen ? granularityMenuId : undefined}
|
||||
aria-expanded={granularityOpen}
|
||||
aria-pressed={quick === "four-hours"}
|
||||
onClick={() => setGranularityOpen((current) => !current)}
|
||||
onClick={() => {
|
||||
onQuickSelect(4 * 60 * 60_000, fourHourBucket);
|
||||
setGranularityOpen((current) => !current);
|
||||
}}
|
||||
>{preset.label}</button>
|
||||
: <button
|
||||
key={preset.value}
|
||||
|
||||
Reference in new issue
Block a user