Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

usageShortcuts.ts

41 lines1.6 KB

1import { msg } from "gt-react";
2import type { KeybindingCommand, ResolvedKeybindingsConfig } from "@t3tools/contracts";
3import type { UsageChartMetric } from "./UsageProviderChart";
4import { resolveShortcutCommand, type ShortcutEventLike } from "../../keybindings";
5​
6export type UsageMetric = UsageChartMetric | "limits";
7export const METRIC_OPTIONS = [
8 { value: "cost", label: msg("Cost"), command: "usage.cost" },
9 { value: "tokens", label: msg("Tokens"), command: "usage.tokens" },
10 { value: "limits", label: msg("Limits"), command: "usage.limits" },
11] as const satisfies readonly { value: UsageMetric; label: string; command: KeybindingCommand }[];
12​
13export const WINDOW_OPTIONS = [
14 { days: 1, label: msg("Past 24h"), command: "usage.period.day" },
15 { days: 7, label: msg("7 days"), command: "usage.period.week" },
16 { days: 30, label: msg("30 days"), command: "usage.period.month" },
17 { days: 90, label: msg("90 days"), command: "usage.period.quarter" },
18] as const;
19​
20export function resolveUsageShortcut(
21 event: ShortcutEventLike & { target: EventTarget | null },
22 keybindings: ResolvedKeybindingsConfig,
23) {
24 const target = event.target;
25 if (
26 target instanceof HTMLElement &&
27 (target.tagName === "INPUT" ||
28 target.tagName === "TEXTAREA" ||
29 target.tagName === "SELECT" ||
30 target.isContentEditable ||
31 target.closest(
32 '[role="dialog"]:not([data-app-modal]), [aria-modal="true"]:not([data-app-modal]), [data-slot$="popup"]:not([data-app-modal])',
33 ))
34 ) {
35 return null;
36 }
37​
38 return resolveShortcutCommand(event, keybindings, {
39 context: { usagePageOpen: true },
40 });