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

usageShortcuts.ts

40 lines1.5 KB

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