forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
194 lines (178 loc) · 5.93 KB
/
Copy pathindex.tsx
File metadata and controls
194 lines (178 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import { getRouteApi, useNavigate } from '@tanstack/react-router'
import { useCallback, useMemo } from 'react'
import { useTranslation } from 'react-i18next'
import { SectionPageLayout } from '@/components/layout'
import type { NavGroup } from '@/components/layout/types'
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs'
import { CacheStatsDialog } from '@/features/system-settings/general/channel-affinity/cache-stats-dialog'
import { useSidebarConfig } from '@/hooks/use-sidebar-config'
import { UserInfoDialog } from './components/dialogs/user-info-dialog'
import {
type LogsViewScope,
UsageLogsProvider,
useLogsViewScope,
useUsageLogsContext,
} from './components/usage-logs-provider'
import { UsageLogsTable } from './components/usage-logs-table'
import {
isUsageLogsSectionId,
USAGE_LOGS_DEFAULT_SECTION,
type UsageLogsSectionId,
} from './section-registry'
const route = getRouteApi('/_authenticated/usage-logs/$section')
const TASK_LOG_SECTIONS = ['drawing', 'task'] as const
const SECTION_META: Record<UsageLogsSectionId, { titleKey: string }> = {
common: {
titleKey: 'Common Logs',
},
drawing: {
titleKey: 'Drawing Logs',
},
task: {
titleKey: 'Task Logs',
},
}
function UsageLogsContent() {
const { t } = useTranslation()
const navigate = useNavigate()
const params = route.useParams()
const activeCategory: UsageLogsSectionId =
params.section && isUsageLogsSectionId(params.section)
? params.section
: USAGE_LOGS_DEFAULT_SECTION
const {
selectedUserId,
userInfoDialogOpen,
setUserInfoDialogOpen,
affinityTarget,
affinityDialogOpen,
setAffinityDialogOpen,
} = useUsageLogsContext()
const { canManageScope, viewScope, setViewScope } = useLogsViewScope()
const tabNavGroups = useMemo<NavGroup[]>(
() => [
{
title: 'Task Logs',
items: TASK_LOG_SECTIONS.map((section) => ({
title: SECTION_META[section].titleKey,
url: `/usage-logs/${section}`,
})),
},
],
[]
)
const filteredTabGroups = useSidebarConfig(tabNavGroups)
const visibleSections = useMemo(
() =>
(filteredTabGroups[0]?.items ?? [])
.map((item) => {
if (!('url' in item) || typeof item.url !== 'string') return null
return item.url.split('/').pop() ?? null
})
.filter((section): section is UsageLogsSectionId =>
Boolean(section && isUsageLogsSectionId(section))
),
[filteredTabGroups]
)
const handleSectionChange = useCallback(
(section: string) => {
void navigate({
to: '/usage-logs/$section',
params: { section: section as UsageLogsSectionId },
})
},
[navigate]
)
const handleViewScopeChange = useCallback(
(scope: string) => {
if (scope === 'all' || scope === 'self') {
setViewScope(scope as LogsViewScope)
}
},
[setViewScope]
)
const pageMeta =
activeCategory === 'common' ? SECTION_META.common : SECTION_META.task
const showTaskSwitcher =
activeCategory !== 'common' && visibleSections.length > 1
return (
<>
<SectionPageLayout fixedContent>
<SectionPageLayout.Title>
{t(pageMeta.titleKey)}
</SectionPageLayout.Title>
{canManageScope && (
<SectionPageLayout.Actions>
<Tabs value={viewScope} onValueChange={handleViewScopeChange}>
<TabsList>
<TabsTrigger value='all'>{t('All')}</TabsTrigger>
<TabsTrigger value='self'>{t('Only Mine')}</TabsTrigger>
</TabsList>
</Tabs>
</SectionPageLayout.Actions>
)}
<SectionPageLayout.Content>
<div className='flex h-full min-h-0 flex-col gap-4'>
{showTaskSwitcher && (
<Tabs value={activeCategory} onValueChange={handleSectionChange}>
<TabsList className='max-w-full flex-wrap justify-start group-data-horizontal/tabs:h-auto'>
{visibleSections.map((section) => (
<TabsTrigger key={section} value={section}>
{t(SECTION_META[section].titleKey)}
</TabsTrigger>
))}
</TabsList>
</Tabs>
)}
<div className='min-h-0 flex-1'>
<UsageLogsTable logCategory={activeCategory} />
</div>
</div>
</SectionPageLayout.Content>
</SectionPageLayout>
<UserInfoDialog
userId={selectedUserId}
open={userInfoDialogOpen}
onOpenChange={setUserInfoDialogOpen}
/>
<CacheStatsDialog
open={affinityDialogOpen}
onOpenChange={setAffinityDialogOpen}
target={
affinityTarget
? {
rule_name: affinityTarget.rule_name || '',
using_group:
affinityTarget.using_group ||
affinityTarget.selected_group ||
'',
key_hint: affinityTarget.key_hint || '',
key_fp: affinityTarget.key_fp || '',
}
: null
}
/>
</>
)
}
export function UsageLogs() {
return (
<UsageLogsProvider>
<UsageLogsContent />
</UsageLogsProvider>
)
}