/*
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 .
For commercial licensing, please contact support@quantumnous.com
*/
/* eslint-disable react-refresh/only-export-components */
import React, { createContext, useContext, useState, useCallback } from 'react'
import { useQueryClient } from '@tanstack/react-query'
import { useChannelUpstreamUpdates } from '../hooks/use-channel-upstream-updates'
import { channelsQueryKeys } from '../lib'
import type { Channel } from '../types'
// ============================================================================
// Types
// ============================================================================
type DialogType =
| 'create-channel'
| 'update-channel'
| 'test-channel'
| 'balance-query'
| 'fetch-models'
| 'ollama-models'
| 'multi-key-manage'
| 'tag-batch-edit'
| 'edit-tag'
| 'copy-channel'
| null
type UpstreamUpdateState = ReturnType
type ChannelsContextType = {
open: DialogType
setOpen: (open: DialogType) => void
currentRow: Channel | null
setCurrentRow: (row: Channel | null) => void
currentTag: string | null
setCurrentTag: (tag: string | null) => void
enableTagMode: boolean
setEnableTagMode: (enabled: boolean) => void
idSort: boolean
setIdSort: (enabled: boolean) => void
upstream: UpstreamUpdateState
}
// ============================================================================
// Context
// ============================================================================
const ChannelsContext = createContext(
undefined
)
// ============================================================================
// Provider
// ============================================================================
export function ChannelsProvider({ children }: { children: React.ReactNode }) {
const [open, setOpen] = useState(null)
const [currentRow, setCurrentRow] = useState(null)
const [currentTag, setCurrentTag] = useState(null)
const [enableTagMode, setEnableTagMode] = useState(() => {
return localStorage.getItem('enable-tag-mode') === 'true'
})
const [idSort, setIdSort] = useState(() => {
return localStorage.getItem('channels-id-sort') === 'true'
})
const queryClient = useQueryClient()
const refreshChannels = useCallback(async () => {
await queryClient.invalidateQueries({ queryKey: channelsQueryKeys.all })
}, [queryClient])
const upstream = useChannelUpstreamUpdates(refreshChannels)
return (
{children}
)
}
// ============================================================================
// Hook
// ============================================================================
export function useChannels() {
const context = useContext(ChannelsContext)
if (!context) {
throw new Error('useChannels must be used within ChannelsProvider')
}
return context
}