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

usageShortcuts.ts

39 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​
19/** Resolves page shortcuts without taking letters from fields or popup controls. */
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('[role="dialog"], [aria-modal="true"], [data-slot$="popup"]'))
32 ) {
33 return null;
34 }
35​
36 return resolveShortcutCommand(event, keybindings, {
37 context: { usagePageOpen: true },
38 });