feat(webui): add AI usage dashboard for team and member token tracking

This commit is contained in:
yiling
2026-07-23 15:03:09 +08:00
parent 2c04c97a02
commit df013aa4bb
36 changed files with 1047 additions and 11 deletions
+14 -8
View File
@@ -147,16 +147,19 @@ describe('AiUsageService', () => {
})
it('filters statistics correctly by timeframe', async () => {
const { team, user } = await createTestTeamAndUser()
vi.useFakeTimers()
const now = new Date('2026-07-23T15:30:00Z')
vi.setSystemTime(now)
const now = new Date()
try {
const { team, user } = await createTestTeamAndUser()
// 5 minutes ago (within current 1h bucket)
const recent = new Date(now.getTime() - 5 * 60 * 1000)
// 2 hours ago (within 24h, outside 1h)
const twoHoursAgo = new Date(now.getTime() - 2 * 60 * 60 * 1000)
// 3 days ago (within 7d, outside 24h)
const threeDaysAgo = new Date(now.getTime() - 3 * 24 * 60 * 60 * 1000)
// 5 minutes ago (within current 1h bucket)
const recent = new Date(now.getTime() - 5 * 60 * 1000)
// 2 hours ago (within 24h, outside 1h)
const twoHoursAgo = new Date(now.getTime() - 2 * 60 * 60 * 1000)
// 3 days ago (within 7d, outside 24h)
const threeDaysAgo = new Date(now.getTime() - 3 * 24 * 60 * 60 * 1000)
await aiUsageService.recordUsage({
teamId: team.id,
@@ -211,6 +214,9 @@ describe('AiUsageService', () => {
timeframe: '7d',
})
expect(stats7d.team?.inputTokens).toBe(700)
} finally {
vi.useRealTimers()
}
})
it('throws 404 when querying non-existent team member', async () => {
@@ -0,0 +1,265 @@
import { useState } from 'react'
import { useQuery, useQueries } from '@tanstack/react-query'
import type { Timeframe } from '@shumai/dtos'
import { client } from '@/ui/api/client'
import { m } from '@/ui/paraglide/messages.js'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/ui/components/ui/card'
import { ToggleGroup, ToggleGroupItem } from '@/ui/components/ui/toggle-group'
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@/ui/components/ui/table'
import { Avatar, AvatarFallback, AvatarImage } from '@/ui/components/ui/avatar'
import { Loader2 } from 'lucide-react'
interface AiUsageDashboardProps {
teamId: string
}
export function AiUsageDashboard({ teamId }: AiUsageDashboardProps) {
const [viewMode, setViewMode] = useState<'team' | 'members'>('team')
const [timeframe, setTimeframe] = useState<Timeframe>('1h')
// Query team-level usage
const { data: teamUsageData, isLoading: isTeamLoading } = useQuery({
queryKey: ['teams', teamId, 'ai-usage', timeframe, 'team'],
queryFn: async () => {
const res = await client.api.teams[':teamId']['ai-usage'].$get({
param: { teamId },
query: { timeframe },
})
if (!res.ok) throw new Error('Failed to fetch team AI usage')
return await res.json()
},
enabled: !!teamId && viewMode === 'team',
})
// Query member list for members view
const { data: members, isLoading: isMembersListLoading } = useQuery({
queryKey: ['teams', teamId, 'members'],
queryFn: async () => {
const res = await client.api.teams[':teamId'].members.$get({
param: { teamId },
query: {},
})
if (!res.ok) throw new Error('Failed to fetch team members')
return await res.json()
},
enabled: !!teamId && viewMode === 'members',
})
// Query each member's AI usage in parallel
const memberUsageQueries = useQueries({
queries: (members || []).map((member) => ({
queryKey: ['teams', teamId, 'ai-usage', timeframe, member.id],
queryFn: async () => {
const res = await client.api.teams[':teamId']['ai-usage'].$get({
param: { teamId },
query: { timeframe, userId: member.id },
})
if (!res.ok) throw new Error(`Failed to fetch AI usage for member ${member.id}`)
return await res.json()
},
enabled: !!teamId && viewMode === 'members',
})),
})
const isMembersLoading = isMembersListLoading || memberUsageQueries.some((q) => q.isLoading)
const getInitials = (name?: string) => {
if (!name) return 'U'
const names = name.split(' ')
if (names.length > 1) {
return `${names[0][0]}${names[names.length - 1][0]}`.toUpperCase()
}
return name.substring(0, 2).toUpperCase()
}
const localizeRole = (role?: string) => {
if (!role) return ''
switch (role.toLowerCase()) {
case 'owner':
return m.owner()
case 'editor':
return m.editor()
case 'reviewer':
return m.reviewer()
default:
return role
}
}
const formatTokens = (val?: number) => (val ?? 0).toLocaleString()
const formatCost = (val?: number) => `$${(val ?? 0).toFixed(6)}`
return (
<Card className="border-border/60 shadow-sm">
<CardHeader className="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between pb-6">
<div>
<CardTitle className="text-xl font-semibold tracking-tight">{m.ai_usage()}</CardTitle>
<CardDescription className="text-sm text-muted-foreground mt-1">
{m.ai_usage_description()}
</CardDescription>
</div>
<div className="flex flex-wrap items-center gap-3">
<ToggleGroup
type="single"
value={viewMode}
onValueChange={(val) => {
if (val) setViewMode(val as 'team' | 'members')
}}
className="border rounded-lg p-1 bg-muted/30"
>
<ToggleGroupItem value="team" className="px-3 py-1.5 text-xs font-medium">
{m.team_view()}
</ToggleGroupItem>
<ToggleGroupItem value="members" className="px-3 py-1.5 text-xs font-medium">
{m.members_view()}
</ToggleGroupItem>
</ToggleGroup>
<ToggleGroup
type="single"
value={timeframe}
onValueChange={(val) => {
if (val) setTimeframe(val as Timeframe)
}}
className="border rounded-lg p-1 bg-muted/30"
>
<ToggleGroupItem value="1h" className="px-2.5 py-1.5 text-xs font-medium">
1h
</ToggleGroupItem>
<ToggleGroupItem value="24h" className="px-2.5 py-1.5 text-xs font-medium">
24h
</ToggleGroupItem>
<ToggleGroupItem value="7d" className="px-2.5 py-1.5 text-xs font-medium">
7d
</ToggleGroupItem>
<ToggleGroupItem value="30d" className="px-2.5 py-1.5 text-xs font-medium">
30d
</ToggleGroupItem>
</ToggleGroup>
</div>
</CardHeader>
<CardContent>
{viewMode === 'team' ? (
isTeamLoading ? (
<div className="flex h-48 items-center justify-center">
<Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
</div>
) : (
<div className="rounded-md border">
<Table>
<TableHeader>
<TableRow>
<TableHead>{m.team()}</TableHead>
<TableHead className="text-right">{m.input_tokens()}</TableHead>
<TableHead className="text-right">{m.output_tokens()}</TableHead>
<TableHead className="text-right">{m.cache_read_tokens()}</TableHead>
<TableHead className="text-right">{m.total_tokens()}</TableHead>
<TableHead className="text-right">{m.cost()}</TableHead>
</TableRow>
</TableHeader>
<TableBody>
<TableRow>
<TableCell className="font-medium">{m.team()}</TableCell>
<TableCell className="text-right">
{formatTokens(teamUsageData?.team?.inputTokens)}
</TableCell>
<TableCell className="text-right">
{formatTokens(teamUsageData?.team?.outputTokens)}
</TableCell>
<TableCell className="text-right">
{formatTokens(teamUsageData?.team?.cacheReadTokens)}
</TableCell>
<TableCell className="text-right font-medium">
{formatTokens(teamUsageData?.team?.totalTokens)}
</TableCell>
<TableCell className="text-right font-medium">
{formatCost(teamUsageData?.team?.cost)}
</TableCell>
</TableRow>
</TableBody>
</Table>
</div>
)
) : isMembersLoading ? (
<div className="flex h-48 items-center justify-center">
<Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
</div>
) : (
<div className="rounded-md border">
<Table>
<TableHeader>
<TableRow>
<TableHead>{m.members()}</TableHead>
<TableHead>{m.role()}</TableHead>
<TableHead className="text-right">{m.input_tokens()}</TableHead>
<TableHead className="text-right">{m.output_tokens()}</TableHead>
<TableHead className="text-right">{m.cache_read_tokens()}</TableHead>
<TableHead className="text-right">{m.total_tokens()}</TableHead>
<TableHead className="text-right">{m.cost()}</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{(members || []).map((member, idx) => {
const usageResult = memberUsageQueries[idx]?.data?.member
return (
<TableRow key={member.id}>
<TableCell>
<div className="flex items-center gap-3">
<Avatar className="h-8 w-8 rounded-full">
{member.image && (
<AvatarImage
src={member.image}
alt={member.name}
className="object-cover"
/>
)}
<AvatarFallback className="bg-primary/20 text-xs">
{getInitials(member.name)}
</AvatarFallback>
</Avatar>
<div className="flex flex-col">
<span className="text-sm font-medium leading-none">{member.name}</span>
<span className="text-xs text-muted-foreground mt-0.5">
{member.email}
</span>
</div>
</div>
</TableCell>
<TableCell className="text-xs text-muted-foreground">
{localizeRole(member.role)}
</TableCell>
<TableCell className="text-right">
{formatTokens(usageResult?.inputTokens)}
</TableCell>
<TableCell className="text-right">
{formatTokens(usageResult?.outputTokens)}
</TableCell>
<TableCell className="text-right">
{formatTokens(usageResult?.cacheReadTokens)}
</TableCell>
<TableCell className="text-right font-medium">
{formatTokens(usageResult?.totalTokens)}
</TableCell>
<TableCell className="text-right font-medium">
{formatCost(usageResult?.cost)}
</TableCell>
</TableRow>
)
})}
</TableBody>
</Table>
</div>
)}
</CardContent>
</Card>
)
}
+13
View File
@@ -146,6 +146,19 @@ export function UserMenu() {
</DropdownMenuPortal>
</DropdownMenuSub>
<DropdownMenuSeparator />
{me?.role?.toLowerCase() === 'owner' && (
<DropdownMenuItem
onClick={() =>
navigate({
to: '/teams/$teamId/dashboard',
params: { teamId: teamId! },
})
}
disabled={!teamId}
>
{m.dashboard()}
</DropdownMenuItem>
)}
<DropdownMenuItem
onClick={() =>
navigate({
+14 -1
View File
@@ -870,5 +870,18 @@
"no_chat_agents_warning": "No chat agents found. Please create a Chat Agent below first.",
"select_agent": "Select Agent",
"delete_comment_confirm": "Delete Comment?",
"delete_comment_description": "Are you sure you want to delete this comment? This action cannot be undone."
"delete_comment_description": "Are you sure you want to delete this comment? This action cannot be undone.",
"ai_usage": "AI Token Usage",
"ai_usage_description": "Monitor AI token consumption and estimated costs for your team and members.",
"usage": "Usage",
"input_tokens": "Input Tokens",
"output_tokens": "Output Tokens",
"cache_read_tokens": "Cache Read Tokens",
"total_tokens": "Total Tokens",
"cost": "Cost",
"team_view": "Team",
"members_view": "Members",
"access_denied": "Access Denied",
"only_owner_dashboard": "Only team owners can access the dashboard.",
"role": "Role"
}
+14 -1
View File
@@ -870,5 +870,18 @@
"no_chat_agents_warning": "未找到聊天智能体。请先在下方创建聊天智能体。",
"select_agent": "选择智能体",
"delete_comment_confirm": "删除评论?",
"delete_comment_description": "您确定要删除这条评论吗?此操作将无法撤回。"
"delete_comment_description": "您确定要删除这条评论吗?此操作将无法撤回。",
"ai_usage": "AI Token 用量",
"ai_usage_description": "查看并监控团队及成员的 AI Token 消耗与预估费用。",
"usage": "用量",
"input_tokens": "输入 Token",
"output_tokens": "输出 Token",
"cache_read_tokens": "缓存读取 Token",
"total_tokens": "总 Token",
"cost": "费用",
"team_view": "团队",
"members_view": "成员",
"access_denied": "访问被拒绝",
"only_owner_dashboard": "只有团队所有者才能访问仪表盘。",
"role": "角色"
}
+13
View File
@@ -817,4 +817,17 @@ export * from "./no_chat_agents_warning.js";
export * from "./select_agent.js";
export * from "./delete_comment_confirm.js";
export * from "./delete_comment_description.js";
export * from "./ai_usage.js";
export * from "./ai_usage_description.js";
export * from "./usage.js";
export * from "./input_tokens.js";
export * from "./output_tokens.js";
export * from "./cache_read_tokens.js";
export * from "./total_tokens.js";
export * from "./cost.js";
export * from "./team_view.js";
export * from "./members_view.js";
export * from "./access_denied.js";
export * from "./only_owner_dashboard.js";
export * from "./role.js";
export type LocalizedString = import("../runtime.js").LocalizedString;
+14 -1
View File
@@ -818,4 +818,17 @@ export * from './select_chatbot_agent.js'
export * from './no_chat_agents_warning.js'
export * from './select_agent.js'
export * from './delete_comment_confirm.js'
export * from './delete_comment_description.js'
export * from './delete_comment_description.js'
export * from './ai_usage.js'
export * from './ai_usage_description.js'
export * from './usage.js'
export * from './input_tokens.js'
export * from './output_tokens.js'
export * from './cache_read_tokens.js'
export * from './total_tokens.js'
export * from './cost.js'
export * from './team_view.js'
export * from './members_view.js'
export * from './access_denied.js'
export * from './only_owner_dashboard.js'
export * from './role.js'
+16
View File
@@ -0,0 +1,16 @@
/**
* | output |
* | --- |
* | "Access Denied" |
*
* @param {Access_DeniedInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
* @returns {LocalizedString}
*/
export const access_denied: ((inputs?: Access_DeniedInputs, options?: {
locale?: "en" | "zh";
}) => LocalizedString) & import("../runtime.js").MessageMetadata<Access_DeniedInputs, {
locale?: "en" | "zh";
}, {}>;
export type LocalizedString = import("../runtime.js").LocalizedString;
export type Access_DeniedInputs = {};
+29
View File
@@ -0,0 +1,29 @@
/* eslint-disable */
import { getLocale, experimentalStaticLocale } from '../runtime.js';
/** @typedef {import('../runtime.js').LocalizedString} LocalizedString */
/** @typedef {{}} Access_DeniedInputs */
const en_access_denied = /** @type {(inputs: Access_DeniedInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`Access Denied`)
};
const zh_access_denied = /** @type {(inputs: Access_DeniedInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`访问被拒绝`)
};
/**
* | output |
* | --- |
* | "Access Denied" |
*
* @param {Access_DeniedInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
* @returns {LocalizedString}
*/
export const access_denied = /** @type {((inputs?: Access_DeniedInputs, options?: { locale?: "en" | "zh" }) => LocalizedString) & import('../runtime.js').MessageMetadata<Access_DeniedInputs, { locale?: "en" | "zh" }, {}>} */ ((inputs = {}, options = {}) => {
const locale = experimentalStaticLocale ?? options.locale ?? getLocale()
if (locale === "en") return en_access_denied(inputs)
return zh_access_denied(inputs)
});
+16
View File
@@ -0,0 +1,16 @@
/**
* | output |
* | --- |
* | "AI Token Usage" |
*
* @param {Ai_UsageInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
* @returns {LocalizedString}
*/
export const ai_usage: ((inputs?: Ai_UsageInputs, options?: {
locale?: "en" | "zh";
}) => LocalizedString) & import("../runtime.js").MessageMetadata<Ai_UsageInputs, {
locale?: "en" | "zh";
}, {}>;
export type LocalizedString = import("../runtime.js").LocalizedString;
export type Ai_UsageInputs = {};
+29
View File
@@ -0,0 +1,29 @@
/* eslint-disable */
import { getLocale, experimentalStaticLocale } from '../runtime.js';
/** @typedef {import('../runtime.js').LocalizedString} LocalizedString */
/** @typedef {{}} Ai_UsageInputs */
const en_ai_usage = /** @type {(inputs: Ai_UsageInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`AI Token Usage`)
};
const zh_ai_usage = /** @type {(inputs: Ai_UsageInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`AI Token 用量`)
};
/**
* | output |
* | --- |
* | "AI Token Usage" |
*
* @param {Ai_UsageInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
* @returns {LocalizedString}
*/
export const ai_usage = /** @type {((inputs?: Ai_UsageInputs, options?: { locale?: "en" | "zh" }) => LocalizedString) & import('../runtime.js').MessageMetadata<Ai_UsageInputs, { locale?: "en" | "zh" }, {}>} */ ((inputs = {}, options = {}) => {
const locale = experimentalStaticLocale ?? options.locale ?? getLocale()
if (locale === "en") return en_ai_usage(inputs)
return zh_ai_usage(inputs)
});
+16
View File
@@ -0,0 +1,16 @@
/**
* | output |
* | --- |
* | "Monitor AI token consumption and estimated costs for your team and members." |
*
* @param {Ai_Usage_DescriptionInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
* @returns {LocalizedString}
*/
export const ai_usage_description: ((inputs?: Ai_Usage_DescriptionInputs, options?: {
locale?: "en" | "zh";
}) => LocalizedString) & import("../runtime.js").MessageMetadata<Ai_Usage_DescriptionInputs, {
locale?: "en" | "zh";
}, {}>;
export type LocalizedString = import("../runtime.js").LocalizedString;
export type Ai_Usage_DescriptionInputs = {};
@@ -0,0 +1,29 @@
/* eslint-disable */
import { getLocale, experimentalStaticLocale } from '../runtime.js';
/** @typedef {import('../runtime.js').LocalizedString} LocalizedString */
/** @typedef {{}} Ai_Usage_DescriptionInputs */
const en_ai_usage_description = /** @type {(inputs: Ai_Usage_DescriptionInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`Monitor AI token consumption and estimated costs for your team and members.`)
};
const zh_ai_usage_description = /** @type {(inputs: Ai_Usage_DescriptionInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`查看并监控团队及成员的 AI Token 消耗与预估费用。`)
};
/**
* | output |
* | --- |
* | "Monitor AI token consumption and estimated costs for your team and members." |
*
* @param {Ai_Usage_DescriptionInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
* @returns {LocalizedString}
*/
export const ai_usage_description = /** @type {((inputs?: Ai_Usage_DescriptionInputs, options?: { locale?: "en" | "zh" }) => LocalizedString) & import('../runtime.js').MessageMetadata<Ai_Usage_DescriptionInputs, { locale?: "en" | "zh" }, {}>} */ ((inputs = {}, options = {}) => {
const locale = experimentalStaticLocale ?? options.locale ?? getLocale()
if (locale === "en") return en_ai_usage_description(inputs)
return zh_ai_usage_description(inputs)
});
+16
View File
@@ -0,0 +1,16 @@
/**
* | output |
* | --- |
* | "Cache Read Tokens" |
*
* @param {Cache_Read_TokensInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
* @returns {LocalizedString}
*/
export const cache_read_tokens: ((inputs?: Cache_Read_TokensInputs, options?: {
locale?: "en" | "zh";
}) => LocalizedString) & import("../runtime.js").MessageMetadata<Cache_Read_TokensInputs, {
locale?: "en" | "zh";
}, {}>;
export type LocalizedString = import("../runtime.js").LocalizedString;
export type Cache_Read_TokensInputs = {};
+29
View File
@@ -0,0 +1,29 @@
/* eslint-disable */
import { getLocale, experimentalStaticLocale } from '../runtime.js';
/** @typedef {import('../runtime.js').LocalizedString} LocalizedString */
/** @typedef {{}} Cache_Read_TokensInputs */
const en_cache_read_tokens = /** @type {(inputs: Cache_Read_TokensInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`Cache Read Tokens`)
};
const zh_cache_read_tokens = /** @type {(inputs: Cache_Read_TokensInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`缓存读取 Token`)
};
/**
* | output |
* | --- |
* | "Cache Read Tokens" |
*
* @param {Cache_Read_TokensInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
* @returns {LocalizedString}
*/
export const cache_read_tokens = /** @type {((inputs?: Cache_Read_TokensInputs, options?: { locale?: "en" | "zh" }) => LocalizedString) & import('../runtime.js').MessageMetadata<Cache_Read_TokensInputs, { locale?: "en" | "zh" }, {}>} */ ((inputs = {}, options = {}) => {
const locale = experimentalStaticLocale ?? options.locale ?? getLocale()
if (locale === "en") return en_cache_read_tokens(inputs)
return zh_cache_read_tokens(inputs)
});
+16
View File
@@ -0,0 +1,16 @@
/**
* | output |
* | --- |
* | "Cost" |
*
* @param {CostInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
* @returns {LocalizedString}
*/
export const cost: ((inputs?: CostInputs, options?: {
locale?: "en" | "zh";
}) => LocalizedString) & import("../runtime.js").MessageMetadata<CostInputs, {
locale?: "en" | "zh";
}, {}>;
export type LocalizedString = import("../runtime.js").LocalizedString;
export type CostInputs = {};
+29
View File
@@ -0,0 +1,29 @@
/* eslint-disable */
import { getLocale, experimentalStaticLocale } from '../runtime.js';
/** @typedef {import('../runtime.js').LocalizedString} LocalizedString */
/** @typedef {{}} CostInputs */
const en_cost = /** @type {(inputs: CostInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`Cost`)
};
const zh_cost = /** @type {(inputs: CostInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`费用`)
};
/**
* | output |
* | --- |
* | "Cost" |
*
* @param {CostInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
* @returns {LocalizedString}
*/
export const cost = /** @type {((inputs?: CostInputs, options?: { locale?: "en" | "zh" }) => LocalizedString) & import('../runtime.js').MessageMetadata<CostInputs, { locale?: "en" | "zh" }, {}>} */ ((inputs = {}, options = {}) => {
const locale = experimentalStaticLocale ?? options.locale ?? getLocale()
if (locale === "en") return en_cost(inputs)
return zh_cost(inputs)
});
+16
View File
@@ -0,0 +1,16 @@
/**
* | output |
* | --- |
* | "Input Tokens" |
*
* @param {Input_TokensInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
* @returns {LocalizedString}
*/
export const input_tokens: ((inputs?: Input_TokensInputs, options?: {
locale?: "en" | "zh";
}) => LocalizedString) & import("../runtime.js").MessageMetadata<Input_TokensInputs, {
locale?: "en" | "zh";
}, {}>;
export type LocalizedString = import("../runtime.js").LocalizedString;
export type Input_TokensInputs = {};
+29
View File
@@ -0,0 +1,29 @@
/* eslint-disable */
import { getLocale, experimentalStaticLocale } from '../runtime.js';
/** @typedef {import('../runtime.js').LocalizedString} LocalizedString */
/** @typedef {{}} Input_TokensInputs */
const en_input_tokens = /** @type {(inputs: Input_TokensInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`Input Tokens`)
};
const zh_input_tokens = /** @type {(inputs: Input_TokensInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`输入 Token`)
};
/**
* | output |
* | --- |
* | "Input Tokens" |
*
* @param {Input_TokensInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
* @returns {LocalizedString}
*/
export const input_tokens = /** @type {((inputs?: Input_TokensInputs, options?: { locale?: "en" | "zh" }) => LocalizedString) & import('../runtime.js').MessageMetadata<Input_TokensInputs, { locale?: "en" | "zh" }, {}>} */ ((inputs = {}, options = {}) => {
const locale = experimentalStaticLocale ?? options.locale ?? getLocale()
if (locale === "en") return en_input_tokens(inputs)
return zh_input_tokens(inputs)
});
+16
View File
@@ -0,0 +1,16 @@
/**
* | output |
* | --- |
* | "Members" |
*
* @param {Members_ViewInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
* @returns {LocalizedString}
*/
export const members_view: ((inputs?: Members_ViewInputs, options?: {
locale?: "en" | "zh";
}) => LocalizedString) & import("../runtime.js").MessageMetadata<Members_ViewInputs, {
locale?: "en" | "zh";
}, {}>;
export type LocalizedString = import("../runtime.js").LocalizedString;
export type Members_ViewInputs = {};
+29
View File
@@ -0,0 +1,29 @@
/* eslint-disable */
import { getLocale, experimentalStaticLocale } from '../runtime.js';
/** @typedef {import('../runtime.js').LocalizedString} LocalizedString */
/** @typedef {{}} Members_ViewInputs */
const en_members_view = /** @type {(inputs: Members_ViewInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`Members`)
};
const zh_members_view = /** @type {(inputs: Members_ViewInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`成员`)
};
/**
* | output |
* | --- |
* | "Members" |
*
* @param {Members_ViewInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
* @returns {LocalizedString}
*/
export const members_view = /** @type {((inputs?: Members_ViewInputs, options?: { locale?: "en" | "zh" }) => LocalizedString) & import('../runtime.js').MessageMetadata<Members_ViewInputs, { locale?: "en" | "zh" }, {}>} */ ((inputs = {}, options = {}) => {
const locale = experimentalStaticLocale ?? options.locale ?? getLocale()
if (locale === "en") return en_members_view(inputs)
return zh_members_view(inputs)
});
+16
View File
@@ -0,0 +1,16 @@
/**
* | output |
* | --- |
* | "Only team owners can access the dashboard." |
*
* @param {Only_Owner_DashboardInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
* @returns {LocalizedString}
*/
export const only_owner_dashboard: ((inputs?: Only_Owner_DashboardInputs, options?: {
locale?: "en" | "zh";
}) => LocalizedString) & import("../runtime.js").MessageMetadata<Only_Owner_DashboardInputs, {
locale?: "en" | "zh";
}, {}>;
export type LocalizedString = import("../runtime.js").LocalizedString;
export type Only_Owner_DashboardInputs = {};
@@ -0,0 +1,29 @@
/* eslint-disable */
import { getLocale, experimentalStaticLocale } from '../runtime.js';
/** @typedef {import('../runtime.js').LocalizedString} LocalizedString */
/** @typedef {{}} Only_Owner_DashboardInputs */
const en_only_owner_dashboard = /** @type {(inputs: Only_Owner_DashboardInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`Only team owners can access the dashboard.`)
};
const zh_only_owner_dashboard = /** @type {(inputs: Only_Owner_DashboardInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`只有团队所有者才能访问仪表盘。`)
};
/**
* | output |
* | --- |
* | "Only team owners can access the dashboard." |
*
* @param {Only_Owner_DashboardInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
* @returns {LocalizedString}
*/
export const only_owner_dashboard = /** @type {((inputs?: Only_Owner_DashboardInputs, options?: { locale?: "en" | "zh" }) => LocalizedString) & import('../runtime.js').MessageMetadata<Only_Owner_DashboardInputs, { locale?: "en" | "zh" }, {}>} */ ((inputs = {}, options = {}) => {
const locale = experimentalStaticLocale ?? options.locale ?? getLocale()
if (locale === "en") return en_only_owner_dashboard(inputs)
return zh_only_owner_dashboard(inputs)
});
+16
View File
@@ -0,0 +1,16 @@
/**
* | output |
* | --- |
* | "Output Tokens" |
*
* @param {Output_TokensInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
* @returns {LocalizedString}
*/
export const output_tokens: ((inputs?: Output_TokensInputs, options?: {
locale?: "en" | "zh";
}) => LocalizedString) & import("../runtime.js").MessageMetadata<Output_TokensInputs, {
locale?: "en" | "zh";
}, {}>;
export type LocalizedString = import("../runtime.js").LocalizedString;
export type Output_TokensInputs = {};
+29
View File
@@ -0,0 +1,29 @@
/* eslint-disable */
import { getLocale, experimentalStaticLocale } from '../runtime.js';
/** @typedef {import('../runtime.js').LocalizedString} LocalizedString */
/** @typedef {{}} Output_TokensInputs */
const en_output_tokens = /** @type {(inputs: Output_TokensInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`Output Tokens`)
};
const zh_output_tokens = /** @type {(inputs: Output_TokensInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`输出 Token`)
};
/**
* | output |
* | --- |
* | "Output Tokens" |
*
* @param {Output_TokensInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
* @returns {LocalizedString}
*/
export const output_tokens = /** @type {((inputs?: Output_TokensInputs, options?: { locale?: "en" | "zh" }) => LocalizedString) & import('../runtime.js').MessageMetadata<Output_TokensInputs, { locale?: "en" | "zh" }, {}>} */ ((inputs = {}, options = {}) => {
const locale = experimentalStaticLocale ?? options.locale ?? getLocale()
if (locale === "en") return en_output_tokens(inputs)
return zh_output_tokens(inputs)
});
+16
View File
@@ -0,0 +1,16 @@
/**
* | output |
* | --- |
* | "Role" |
*
* @param {RoleInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
* @returns {LocalizedString}
*/
export const role: ((inputs?: RoleInputs, options?: {
locale?: "en" | "zh";
}) => LocalizedString) & import("../runtime.js").MessageMetadata<RoleInputs, {
locale?: "en" | "zh";
}, {}>;
export type LocalizedString = import("../runtime.js").LocalizedString;
export type RoleInputs = {};
+29
View File
@@ -0,0 +1,29 @@
/* eslint-disable */
import { getLocale, experimentalStaticLocale } from '../runtime.js';
/** @typedef {import('../runtime.js').LocalizedString} LocalizedString */
/** @typedef {{}} RoleInputs */
const en_role = /** @type {(inputs: RoleInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`Role`)
};
const zh_role = /** @type {(inputs: RoleInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`角色`)
};
/**
* | output |
* | --- |
* | "Role" |
*
* @param {RoleInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
* @returns {LocalizedString}
*/
export const role = /** @type {((inputs?: RoleInputs, options?: { locale?: "en" | "zh" }) => LocalizedString) & import('../runtime.js').MessageMetadata<RoleInputs, { locale?: "en" | "zh" }, {}>} */ ((inputs = {}, options = {}) => {
const locale = experimentalStaticLocale ?? options.locale ?? getLocale()
if (locale === "en") return en_role(inputs)
return zh_role(inputs)
});
+16
View File
@@ -0,0 +1,16 @@
/**
* | output |
* | --- |
* | "Team" |
*
* @param {Team_ViewInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
* @returns {LocalizedString}
*/
export const team_view: ((inputs?: Team_ViewInputs, options?: {
locale?: "en" | "zh";
}) => LocalizedString) & import("../runtime.js").MessageMetadata<Team_ViewInputs, {
locale?: "en" | "zh";
}, {}>;
export type LocalizedString = import("../runtime.js").LocalizedString;
export type Team_ViewInputs = {};
+29
View File
@@ -0,0 +1,29 @@
/* eslint-disable */
import { getLocale, experimentalStaticLocale } from '../runtime.js';
/** @typedef {import('../runtime.js').LocalizedString} LocalizedString */
/** @typedef {{}} Team_ViewInputs */
const en_team_view = /** @type {(inputs: Team_ViewInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`Team`)
};
const zh_team_view = /** @type {(inputs: Team_ViewInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`团队`)
};
/**
* | output |
* | --- |
* | "Team" |
*
* @param {Team_ViewInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
* @returns {LocalizedString}
*/
export const team_view = /** @type {((inputs?: Team_ViewInputs, options?: { locale?: "en" | "zh" }) => LocalizedString) & import('../runtime.js').MessageMetadata<Team_ViewInputs, { locale?: "en" | "zh" }, {}>} */ ((inputs = {}, options = {}) => {
const locale = experimentalStaticLocale ?? options.locale ?? getLocale()
if (locale === "en") return en_team_view(inputs)
return zh_team_view(inputs)
});
+16
View File
@@ -0,0 +1,16 @@
/**
* | output |
* | --- |
* | "Total Tokens" |
*
* @param {Total_TokensInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
* @returns {LocalizedString}
*/
export const total_tokens: ((inputs?: Total_TokensInputs, options?: {
locale?: "en" | "zh";
}) => LocalizedString) & import("../runtime.js").MessageMetadata<Total_TokensInputs, {
locale?: "en" | "zh";
}, {}>;
export type LocalizedString = import("../runtime.js").LocalizedString;
export type Total_TokensInputs = {};
+29
View File
@@ -0,0 +1,29 @@
/* eslint-disable */
import { getLocale, experimentalStaticLocale } from '../runtime.js';
/** @typedef {import('../runtime.js').LocalizedString} LocalizedString */
/** @typedef {{}} Total_TokensInputs */
const en_total_tokens = /** @type {(inputs: Total_TokensInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`Total Tokens`)
};
const zh_total_tokens = /** @type {(inputs: Total_TokensInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`总 Token`)
};
/**
* | output |
* | --- |
* | "Total Tokens" |
*
* @param {Total_TokensInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
* @returns {LocalizedString}
*/
export const total_tokens = /** @type {((inputs?: Total_TokensInputs, options?: { locale?: "en" | "zh" }) => LocalizedString) & import('../runtime.js').MessageMetadata<Total_TokensInputs, { locale?: "en" | "zh" }, {}>} */ ((inputs = {}, options = {}) => {
const locale = experimentalStaticLocale ?? options.locale ?? getLocale()
if (locale === "en") return en_total_tokens(inputs)
return zh_total_tokens(inputs)
});
+16
View File
@@ -0,0 +1,16 @@
/**
* | output |
* | --- |
* | "Usage" |
*
* @param {UsageInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
* @returns {LocalizedString}
*/
export const usage: ((inputs?: UsageInputs, options?: {
locale?: "en" | "zh";
}) => LocalizedString) & import("../runtime.js").MessageMetadata<UsageInputs, {
locale?: "en" | "zh";
}, {}>;
export type LocalizedString = import("../runtime.js").LocalizedString;
export type UsageInputs = {};
+29
View File
@@ -0,0 +1,29 @@
/* eslint-disable */
import { getLocale, experimentalStaticLocale } from '../runtime.js';
/** @typedef {import('../runtime.js').LocalizedString} LocalizedString */
/** @typedef {{}} UsageInputs */
const en_usage = /** @type {(inputs: UsageInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`Usage`)
};
const zh_usage = /** @type {(inputs: UsageInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`用量`)
};
/**
* | output |
* | --- |
* | "Usage" |
*
* @param {UsageInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
* @returns {LocalizedString}
*/
export const usage = /** @type {((inputs?: UsageInputs, options?: { locale?: "en" | "zh" }) => LocalizedString) & import('../runtime.js').MessageMetadata<UsageInputs, { locale?: "en" | "zh" }, {}>} */ ((inputs = {}, options = {}) => {
const locale = experimentalStaticLocale ?? options.locale ?? getLocale()
if (locale === "en") return en_usage(inputs)
return zh_usage(inputs)
});
+21
View File
@@ -17,6 +17,7 @@ import { Route as TeamsTeamIdIndexRouteImport } from './routes/teams/$teamId/ind
import { Route as ShareShareIdIndexRouteImport } from './routes/share/$shareId/index'
import { Route as ProjectsProjectIdIndexRouteImport } from './routes/projects/$projectId/index'
import { Route as TeamsTeamIdSettingsRouteImport } from './routes/teams/$teamId/settings'
import { Route as TeamsTeamIdDashboardRouteImport } from './routes/teams/$teamId/dashboard'
import { Route as ProjectsProjectIdRecentlyDeletedRouteImport } from './routes/projects/$projectId/recently-deleted'
import { Route as ProjectsProjectIdSharesIndexRouteImport } from './routes/projects/$projectId/shares/index'
import { Route as ProjectsProjectIdCollectionsIndexRouteImport } from './routes/projects/$projectId/collections/index'
@@ -67,6 +68,11 @@ const TeamsTeamIdSettingsRoute = TeamsTeamIdSettingsRouteImport.update({
path: '/teams/$teamId/settings',
getParentRoute: () => rootRouteImport,
} as any).lazy(() => import('./routes/teams/$teamId/settings.lazy').then((d) => d.Route))
const TeamsTeamIdDashboardRoute = TeamsTeamIdDashboardRouteImport.update({
id: '/teams/$teamId/dashboard',
path: '/teams/$teamId/dashboard',
getParentRoute: () => rootRouteImport,
} as any).lazy(() => import('./routes/teams/$teamId/dashboard.lazy').then((d) => d.Route))
const ProjectsProjectIdRecentlyDeletedRoute = ProjectsProjectIdRecentlyDeletedRouteImport.update({
id: '/projects/$projectId/recently-deleted',
path: '/projects/$projectId/recently-deleted',
@@ -130,6 +136,7 @@ export interface FileRoutesByFullPath {
'/signup': typeof SignupRoute
'/share/$shareId': typeof ShareShareIdRouteWithChildren
'/projects/$projectId/recently-deleted': typeof ProjectsProjectIdRecentlyDeletedRoute
'/teams/$teamId/dashboard': typeof TeamsTeamIdDashboardRoute
'/teams/$teamId/settings': typeof TeamsTeamIdSettingsRoute
'/projects/$projectId/': typeof ProjectsProjectIdIndexRoute
'/share/$shareId/': typeof ShareShareIdIndexRoute
@@ -148,6 +155,7 @@ export interface FileRoutesByTo {
'/login': typeof LoginRoute
'/signup': typeof SignupRoute
'/projects/$projectId/recently-deleted': typeof ProjectsProjectIdRecentlyDeletedRoute
'/teams/$teamId/dashboard': typeof TeamsTeamIdDashboardRoute
'/teams/$teamId/settings': typeof TeamsTeamIdSettingsRoute
'/projects/$projectId': typeof ProjectsProjectIdIndexRoute
'/share/$shareId': typeof ShareShareIdIndexRoute
@@ -168,6 +176,7 @@ export interface FileRoutesById {
'/signup': typeof SignupRoute
'/share/$shareId': typeof ShareShareIdRouteWithChildren
'/projects/$projectId/recently-deleted': typeof ProjectsProjectIdRecentlyDeletedRoute
'/teams/$teamId/dashboard': typeof TeamsTeamIdDashboardRoute
'/teams/$teamId/settings': typeof TeamsTeamIdSettingsRoute
'/projects/$projectId/': typeof ProjectsProjectIdIndexRoute
'/share/$shareId/': typeof ShareShareIdIndexRoute
@@ -189,6 +198,7 @@ export interface FileRouteTypes {
| '/signup'
| '/share/$shareId'
| '/projects/$projectId/recently-deleted'
| '/teams/$teamId/dashboard'
| '/teams/$teamId/settings'
| '/projects/$projectId/'
| '/share/$shareId/'
@@ -207,6 +217,7 @@ export interface FileRouteTypes {
| '/login'
| '/signup'
| '/projects/$projectId/recently-deleted'
| '/teams/$teamId/dashboard'
| '/teams/$teamId/settings'
| '/projects/$projectId'
| '/share/$shareId'
@@ -226,6 +237,7 @@ export interface FileRouteTypes {
| '/signup'
| '/share/$shareId'
| '/projects/$projectId/recently-deleted'
| '/teams/$teamId/dashboard'
| '/teams/$teamId/settings'
| '/projects/$projectId/'
| '/share/$shareId/'
@@ -246,6 +258,7 @@ export interface RootRouteChildren {
SignupRoute: typeof SignupRoute
ShareShareIdRoute: typeof ShareShareIdRouteWithChildren
ProjectsProjectIdRecentlyDeletedRoute: typeof ProjectsProjectIdRecentlyDeletedRoute
TeamsTeamIdDashboardRoute: typeof TeamsTeamIdDashboardRoute
TeamsTeamIdSettingsRoute: typeof TeamsTeamIdSettingsRoute
ProjectsProjectIdIndexRoute: typeof ProjectsProjectIdIndexRoute
TeamsTeamIdIndexRoute: typeof TeamsTeamIdIndexRoute
@@ -315,6 +328,13 @@ declare module '@tanstack/react-router' {
preLoaderRoute: typeof TeamsTeamIdSettingsRouteImport
parentRoute: typeof rootRouteImport
}
'/teams/$teamId/dashboard': {
id: '/teams/$teamId/dashboard'
path: '/teams/$teamId/dashboard'
fullPath: '/teams/$teamId/dashboard'
preLoaderRoute: typeof TeamsTeamIdDashboardRouteImport
parentRoute: typeof rootRouteImport
}
'/projects/$projectId/recently-deleted': {
id: '/projects/$projectId/recently-deleted'
path: '/projects/$projectId/recently-deleted'
@@ -401,6 +421,7 @@ const rootRouteChildren: RootRouteChildren = {
SignupRoute: SignupRoute,
ShareShareIdRoute: ShareShareIdRouteWithChildren,
ProjectsProjectIdRecentlyDeletedRoute: ProjectsProjectIdRecentlyDeletedRoute,
TeamsTeamIdDashboardRoute: TeamsTeamIdDashboardRoute,
TeamsTeamIdSettingsRoute: TeamsTeamIdSettingsRoute,
ProjectsProjectIdIndexRoute: ProjectsProjectIdIndexRoute,
TeamsTeamIdIndexRoute: TeamsTeamIdIndexRoute,
@@ -0,0 +1,91 @@
import { createLazyFileRoute } from '@tanstack/react-router'
import { useState } from 'react'
import { useQuery } from '@tanstack/react-query'
import { client } from '@/ui/api/client'
import { cn } from '@/ui/lib/utils'
import { m } from '@/ui/paraglide/messages.js'
import { AiUsageDashboard } from '@/ui/components/dashboard/AiUsageDashboard'
import { BarChart2, Loader2, ShieldAlert } from 'lucide-react'
type DashboardTab = 'ai-usage'
function TeamDashboardPage() {
const { teamId } = Route.useParams()
const [activeTab, setActiveTab] = useState<DashboardTab>('ai-usage')
const { data: me, isLoading: isMeLoading } = useQuery({
queryKey: ['teams', teamId, 'me'],
queryFn: async () => {
const res = await client.api.teams[':teamId'].me.$get({
param: { teamId: teamId },
})
if (!res.ok) throw new Error('Failed to fetch me')
return await res.json()
},
enabled: !!teamId,
})
if (isMeLoading) {
return (
<div className="flex h-screen items-center justify-center">
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
</div>
)
}
if (me?.role?.toLowerCase() !== 'owner') {
return (
<div className="flex h-full flex-col items-center justify-center p-8 text-center">
<ShieldAlert className="h-12 w-12 text-destructive mb-4" />
<h2 className="text-xl font-bold tracking-tight mb-2">{m.access_denied()}</h2>
<p className="text-sm text-muted-foreground max-w-md">{m.only_owner_dashboard()}</p>
</div>
)
}
return (
<div className="h-full bg-background font-sans selection:bg-primary/20 transition-colors duration-300">
<div className="flex flex-col md:flex-row h-full">
{/* Sidebar */}
<div className="w-full h-full md:w-72 bg-sidebar border-b md:border-b-0 md:border-r border-sidebar-border z-10 md:left-16 overflow-y-auto transition-colors duration-300">
<nav className="p-4 space-y-1 mt-4">
<div className="mb-2 px-4 text-xs font-semibold text-sidebar-foreground/60 uppercase tracking-wider">
{m.dashboard()}
</div>
<div className="mt-4 mb-2 px-4 text-xs font-semibold text-sidebar-foreground/60 uppercase tracking-wider pt-4 border-t border-sidebar-border">
{m.ai()}
</div>
<button
onClick={() => setActiveTab('ai-usage')}
className={cn(
'w-full flex items-center gap-3 px-4 py-3 text-sm font-medium rounded-lg transition-all',
activeTab === 'ai-usage'
? 'bg-sidebar-accent text-sidebar-accent-foreground ring-1 ring-sidebar-border'
: 'text-sidebar-foreground/75 hover:bg-sidebar-accent hover:text-sidebar-accent-foreground',
)}
>
<BarChart2 className="w-5 h-5" />
{m.usage()}
{activeTab === 'ai-usage' && (
<div className="ml-auto w-1.5 h-1.5 rounded-full bg-sidebar-primary" />
)}
</button>
</nav>
</div>
{/* Main Content Area */}
<div className="flex-1 overflow-y-auto p-4 md:p-8">
<div className="max-w-6xl mx-auto space-y-8">
{activeTab === 'ai-usage' && <AiUsageDashboard teamId={teamId} />}
</div>
</div>
</div>
</div>
)
}
export const Route = createLazyFileRoute('/teams/$teamId/dashboard')({
component: TeamDashboardPage,
})
@@ -0,0 +1,3 @@
import { createFileRoute } from '@tanstack/react-router'
export const Route = createFileRoute('/teams/$teamId/dashboard')({})