Files
dashtab/app/components/openrouter-usage.gjs
raucao 4f3c4334bd Use only normal API keys for OpenRouter spend
OpenRouter now rejects management keys from the browser on both
/api/v1/keys and /api/v1/analytics/query, so the management-key flow can
no longer work in the extension. Read the configured key's own usage from
GET /api/v1/key instead, which returns UTC daily, weekly, and monthly
buckets and works from the browser.

Drop the key-type selection, key listing/selection, pagination, and the
management/analytics endpoints. Validate the key against /key before
persisting, and write a fresh record so legacy keyType/keyIds fields are
dropped. Keep the browser-block hint for anyone still storing a
management key.
2026-11-16 22:16:35 +04:00

104 lines
2.7 KiB
Plaintext

import Component from '@glimmer/component';
import { action } from '@ember/object';
import { on } from '@ember/modifier';
import { service } from '@ember/service';
import { formFor } from '../config/integration-forms';
import {
OPENROUTER_ICON_PATH,
OPENROUTER_ICON_TRANSFORM,
} from '../config/openrouter';
export default class OpenRouterUsageComponent extends Component {
@service openrouter;
@service modal;
get hasData() {
return this.openrouter.usage !== null;
}
get formattedUsage() {
return this.format(this.openrouter.usage);
}
// Explains which number the widget shows. Kept in the title attribute so the
// visible "Today"/"This week" words do not alter the dashboard text.
get valueDescription() {
return `OpenRouter spend, ${this.granularityLabel} (UTC)`;
}
get granularityLabel() {
if (this.openrouter.granularity === 'this-week') {
return 'this week';
}
if (this.openrouter.granularity === 'this-month') {
return 'this month';
}
return 'today';
}
format(value) {
if (value == null) {
return '—';
}
return new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
maximumFractionDigits: 2,
}).format(value);
}
@action
openSettings() {
this.modal.open({
component: formFor('openrouter'),
props: { mode: 'edit' },
size: 'md',
});
}
<template>
{{#if this.openrouter.enabled}}
<button
type="button"
data-test-openrouter-usage
title={{this.valueDescription}}
{{on "click" this.openSettings}}
aria-label="Edit OpenRouter Spend settings"
class="cursor-pointer rounded-lg text-white text-shadow-md focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-white/70"
>
<div class="flex items-center gap-1.5">
<span
class="flex size-4 flex-none items-center justify-center rounded-full bg-indigo-600"
aria-hidden="true"
>
<svg
viewBox="0 0 24 24"
fill="currentColor"
class="size-2.5 text-white"
>
<path
d={{OPENROUTER_ICON_PATH}}
transform={{OPENROUTER_ICON_TRANSFORM}}
/>
</svg>
</span>
<span class="text-lg font-semibold">{{this.formattedUsage}}</span>
</div>
{{#if this.openrouter.loading}}
{{#unless this.hasData}}
<div class="text-xs text-white/70">Loading…</div>
{{/unless}}
{{/if}}
{{#if this.openrouter.error}}
<div class="text-xs text-red-300">{{this.openrouter.error}}</div>
{{/if}}
</button>
{{/if}}
</template>
}