useQuery for model list

This commit is contained in:
JzoNg 2024-11-09 12:12:10 +08:00
parent f2bf2e4470
commit 59a9235041
2 changed files with 19 additions and 7 deletions

View File

@ -1,19 +1,14 @@
import React from 'react'
import useSWR from 'swr'
import { useTranslation } from 'react-i18next'
import { usePluginPageContext } from '@/app/components/plugins/plugin-page/context'
import ModelIcon from '@/app/components/header/account-setting/model-provider-page/model-icon'
import ModelName from '@/app/components/header/account-setting/model-provider-page/model-name'
import { fetchModelProviderModelList } from '@/service/common'
import { useModelProviderModelList } from '@/service/use-models'
const ModelList = () => {
const { t } = useTranslation()
const currentPluginDetail = usePluginPageContext(v => v.currentPluginDetail)
const { data: res } = useSWR(
`/workspaces/current/model-providers/${currentPluginDetail.plugin_id}/${currentPluginDetail.name}/models`,
fetchModelProviderModelList,
)
const { data: res } = useModelProviderModelList(`${currentPluginDetail.plugin_id}/${currentPluginDetail.name}`)
if (!res)
return null

17
web/service/use-models.ts Normal file
View File

@ -0,0 +1,17 @@
import { get } from './base'
import type {
ModelItem,
} from '@/app/components/header/account-setting/model-provider-page/declarations'
import {
useQuery,
// useQueryClient,
} from '@tanstack/react-query'
const NAME_SPACE = 'models'
export const useModelProviderModelList = (provider: string) => {
return useQuery({
queryKey: [NAME_SPACE, 'model-list', provider],
queryFn: () => get<{ data: ModelItem[] }>(`/workspaces/current/model-providers/${provider}/models`),
})
}