refactor(playground): extract option fallback helpers
- move model and group fallback selection into focused playground utilities. - keep the options hook focused on query results, toasts, and config updates.
This commit is contained in:
@@ -21,6 +21,7 @@ import { useQuery } from '@tanstack/react-query'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { toast } from 'sonner'
|
||||
import { getUserGroups, getUserModels } from '../api'
|
||||
import { getGroupFallback, getModelFallback } from '../lib'
|
||||
import type { GroupOption, ModelOption, PlaygroundConfig } from '../types'
|
||||
|
||||
type UsePlaygroundOptionsParams = {
|
||||
@@ -86,13 +87,10 @@ export function usePlaygroundOptions({
|
||||
if (!modelsData) return
|
||||
|
||||
setModels(modelsData)
|
||||
const fallback = getModelFallback(modelsData, currentModel)
|
||||
|
||||
const hasCurrentModel = modelsData.some(
|
||||
(model) => model.value === currentModel
|
||||
)
|
||||
|
||||
if (modelsData.length > 0 && !hasCurrentModel) {
|
||||
updateConfig('model', modelsData[0].value)
|
||||
if (fallback) {
|
||||
updateConfig('model', fallback)
|
||||
}
|
||||
}, [modelsData, currentModel, setModels, updateConfig])
|
||||
|
||||
@@ -100,16 +98,9 @@ export function usePlaygroundOptions({
|
||||
if (!groupsData) return
|
||||
|
||||
setGroups(groupsData)
|
||||
const fallback = getGroupFallback(groupsData, currentGroup)
|
||||
|
||||
const hasCurrentGroup = groupsData.some(
|
||||
(group) => group.value === currentGroup
|
||||
)
|
||||
|
||||
if (!hasCurrentGroup && groupsData.length > 0) {
|
||||
const fallback =
|
||||
groupsData.find((group) => group.value === 'default')?.value ??
|
||||
groupsData[0].value
|
||||
|
||||
if (fallback) {
|
||||
updateConfig('group', fallback)
|
||||
}
|
||||
}, [groupsData, currentGroup, setGroups, updateConfig])
|
||||
|
||||
@@ -27,3 +27,4 @@ export * from './stream-utils'
|
||||
export * from './request-error-utils'
|
||||
export * from './conversation-message-utils'
|
||||
export * from './playground-state-utils'
|
||||
export * from './playground-option-utils'
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
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 type { GroupOption, ModelOption } from '../types'
|
||||
|
||||
export function getModelFallback(
|
||||
models: ModelOption[],
|
||||
currentModel: string
|
||||
): string | null {
|
||||
const hasCurrentModel = models.some((model) => model.value === currentModel)
|
||||
|
||||
if (hasCurrentModel || models.length === 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
return models[0].value
|
||||
}
|
||||
|
||||
export function getGroupFallback(
|
||||
groups: GroupOption[],
|
||||
currentGroup: string
|
||||
): string | null {
|
||||
const hasCurrentGroup = groups.some((group) => group.value === currentGroup)
|
||||
|
||||
if (hasCurrentGroup || groups.length === 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
groups.find((group) => group.value === 'default')?.value ?? groups[0].value
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user