/*
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
*/
import { useCallback, useEffect, useMemo, useState } from 'react'
import { type ColumnDef, type RowSelectionState } from '@tanstack/react-table'
import { Search } from 'lucide-react'
import { useTranslation } from 'react-i18next'
import { Button } from '@/components/ui/button'
import { Checkbox } from '@/components/ui/checkbox'
import { Input } from '@/components/ui/input'
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select'
import {
DataTablePagination,
DataTableView,
useDataTable,
} from '@/components/data-table'
import { Dialog } from '@/components/dialog'
import { StatusBadge } from '@/components/status-badge'
import type { UpstreamChannel } from '../types'
import {
CHANNEL_STATUS_CONFIG,
DEFAULT_ENDPOINT,
ENDPOINT_OPTIONS,
MODELS_DEV_PRESET_ID,
OFFICIAL_CHANNEL_ID,
} from './constants'
type ChannelSelectorDialogProps = {
open: boolean
onOpenChange: (open: boolean) => void
channels: UpstreamChannel[]
selectedChannelIds: number[]
onSelectedChannelIdsChange: (ids: number[]) => void
channelEndpoints: Record
onChannelEndpointsChange: (endpoints: Record) => void
onConfirm: (selectedIds: number[]) => void
}
// Synthesized presets from `controller/ratio_sync.go` always carry stable
// negative IDs, so matching by ID alone is reliable and self-documenting.
function isOfficialChannel(channel: UpstreamChannel): boolean {
return (
channel.id === OFFICIAL_CHANNEL_ID || channel.id === MODELS_DEV_PRESET_ID
)
}
export function ChannelSelectorDialog({
open,
onOpenChange,
channels,
selectedChannelIds,
onSelectedChannelIdsChange,
channelEndpoints,
onChannelEndpointsChange,
onConfirm,
}: ChannelSelectorDialogProps) {
const { t } = useTranslation()
const [search, setSearch] = useState('')
const [rowSelection, setRowSelection] = useState({})
useEffect(() => {
if (!selectedChannelIds.length) {
setRowSelection({})
return
}
const availableChannelIds = new Set(channels.map((channel) => channel.id))
const newSelection: RowSelectionState = {}
selectedChannelIds.forEach((id) => {
if (availableChannelIds.has(id)) {
newSelection[id.toString()] = true
}
})
setRowSelection(newSelection)
}, [selectedChannelIds, channels])
const updateEndpoint = useCallback(
(channelId: number, endpoint: string) => {
onChannelEndpointsChange({
...channelEndpoints,
[channelId]: endpoint,
})
},
[channelEndpoints, onChannelEndpointsChange]
)
const getEndpointType = (endpoint: string) => {
const option = ENDPOINT_OPTIONS.find((opt) => opt.value === endpoint)
return option ? endpoint : 'custom'
}
const columns = useMemo[]>(
() => [
{
id: 'select',
header: ({ table }) => (
table.toggleAllPageRowsSelected(!!value)
}
aria-label='Select all'
/>
),
cell: ({ row }) => (
row.toggleSelected(!!value)}
aria-label='Select row'
/>
),
enableSorting: false,
enableHiding: false,
},
{
accessorKey: 'name',
header: t('Name'),
cell: ({ row }) => {
const name = row.getValue('name') as string
const channel = row.original
const isOfficial = isOfficialChannel(channel)
return (