forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage-logs-provider.tsx
More file actions
104 lines (90 loc) 路 3.43 KB
/
Copy pathusage-logs-provider.tsx
File metadata and controls
104 lines (90 loc) 路 3.43 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
/*
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
*/
/* eslint-disable react-refresh/only-export-components */
import { createContext, useContext, useState, type ReactNode } from 'react'
import { useIsAdmin } from '@/hooks/use-admin'
import type { ChannelAffinityInfo } from '../types'
export type LogsViewScope = 'all' | 'self'
interface UsageLogsContextValue {
selectedUserId: number | null
setSelectedUserId: (userId: number | null) => void
userInfoDialogOpen: boolean
setUserInfoDialogOpen: (open: boolean) => void
affinityTarget: ChannelAffinityInfo | null
setAffinityTarget: (target: ChannelAffinityInfo | null) => void
affinityDialogOpen: boolean
setAffinityDialogOpen: (open: boolean) => void
sensitiveVisible: boolean
setSensitiveVisible: (visible: boolean) => void
viewScope: LogsViewScope
setViewScope: (scope: LogsViewScope) => void
}
const UsageLogsContext = createContext<UsageLogsContextValue | undefined>(
undefined
)
export function UsageLogsProvider({ children }: { children: ReactNode }) {
const [selectedUserId, setSelectedUserId] = useState<number | null>(null)
const [userInfoDialogOpen, setUserInfoDialogOpen] = useState(false)
const [affinityTarget, setAffinityTarget] =
useState<ChannelAffinityInfo | null>(null)
const [affinityDialogOpen, setAffinityDialogOpen] = useState(false)
const [sensitiveVisible, setSensitiveVisible] = useState(true)
const [viewScope, setViewScope] = useState<LogsViewScope>('all')
return (
<UsageLogsContext.Provider
value={{
selectedUserId,
setSelectedUserId,
userInfoDialogOpen,
setUserInfoDialogOpen,
affinityTarget,
setAffinityTarget,
affinityDialogOpen,
setAffinityDialogOpen,
sensitiveVisible,
setSensitiveVisible,
viewScope,
setViewScope,
}}
>
{children}
</UsageLogsContext.Provider>
)
}
export function useUsageLogsContext() {
const context = useContext(UsageLogsContext)
if (!context) {
throw new Error('useUsageLogsContext must be used within UsageLogsProvider')
}
return context
}
/**
* Resolves the effective admin scope for usage logs: whether the current
* user is allowed to view all users' logs (`canManageScope`), and whether
* their current view preference (`viewScope`) has that scope active
* (`isAdminView`). Data fetching and admin-only UI should key off
* `isAdminView` rather than raw role, so an admin who switches to "only
* mine" is treated exactly like a regular user for that view.
*/
export function useLogsViewScope() {
const canManageScope = useIsAdmin()
const { viewScope, setViewScope } = useUsageLogsContext()
return {
canManageScope,
viewScope,
setViewScope,
isAdminView: canManageScope && viewScope === 'all',
}
}