diff --git a/ui/admin/app/components/model-providers/ModelProviderContext.tsx b/ui/admin/app/components/model-providers/ModelProviderContext.tsx deleted file mode 100644 index f369bfa32..000000000 --- a/ui/admin/app/components/model-providers/ModelProviderContext.tsx +++ /dev/null @@ -1,82 +0,0 @@ -import { - ReactNode, - createContext, - useCallback, - useContext, - useState, -} from "react"; -import useSWR from "swr"; - -import { ModelProvider } from "~/lib/model/modelProviders"; -import { ModelProviderApiService } from "~/lib/service/api/modelProviderApiService"; - -import { useAsync } from "~/hooks/useAsync"; - -interface ModelProviderContextType { - modelProviders: ModelProvider[]; - configured: boolean; - configureModelProvider: ( - modelProviderId: string, - value: Record - ) => void; - isUpdating: boolean; - error?: unknown; - lastUpdated?: Date; -} - -const ModelProviderContext = createContext< - ModelProviderContextType | undefined ->(undefined); - -export function ModelProviderProvider({ children }: { children: ReactNode }) { - const getModelProviders = useSWR( - ModelProviderApiService.getModelProviders.key(), - () => ModelProviderApiService.getModelProviders(), - { fallbackData: [] } - ); - - const [lastUpdated, setLastSaved] = useState(); - - const handleConfigureModelProvider = useCallback( - (modelProviderId: string, values: Record) => - ModelProviderApiService.configureModelProviderById( - modelProviderId, - values - ) - .then(() => { - getModelProviders.mutate(); - setLastSaved(new Date()); - }) - .catch(console.error), - [getModelProviders] - ); - - const configureModelProvider = useAsync(handleConfigureModelProvider); - const configured = getModelProviders.data.some( - (modelProvider) => modelProvider.configured - ); - return ( - - {children} - - ); -} - -export function useModelProviders() { - const context = useContext(ModelProviderContext); - if (context === undefined) { - throw new Error( - "useModelProvider must be used within a ModelProviderProvider" - ); - } - return context; -} diff --git a/ui/admin/app/components/model-providers/ModelProviderForm.tsx b/ui/admin/app/components/model-providers/ModelProviderForm.tsx index 5df85bfb9..6cff1c095 100644 --- a/ui/admin/app/components/model-providers/ModelProviderForm.tsx +++ b/ui/admin/app/components/model-providers/ModelProviderForm.tsx @@ -1,8 +1,10 @@ import { zodResolver } from "@hookform/resolvers/zod"; import { useFieldArray, useForm } from "react-hook-form"; +import { mutate } from "swr"; import { z } from "zod"; import { ModelProviderConfig } from "~/lib/model/modelProviders"; +import { ModelProviderApiService } from "~/lib/service/api/modelProviderApiService"; import { TypographyH4 } from "~/components/Typography"; import { @@ -10,7 +12,6 @@ import { ParamFormValues, } from "~/components/composed/NameDescriptionForm"; import { ControlledInput } from "~/components/form/controlledInputs"; -import { useModelProviders } from "~/components/model-providers/ModelProviderContext"; import { Button } from "~/components/ui/button"; import { Form } from "~/components/ui/form"; import { Separator } from "~/components/ui/separator"; @@ -75,7 +76,13 @@ export function ModelProviderForm({ parameters: ModelProviderConfig; requiredParameters: string[]; }) { - const { configureModelProvider } = useModelProviders(); + const configureModelProvider = useAsync( + ModelProviderApiService.configureModelProviderById, + { + onSuccess: () => + mutate(ModelProviderApiService.getModelProviders.key()), + } + ); const form = useForm({ resolver: zodResolver(formSchema), @@ -111,7 +118,10 @@ export function ModelProviderForm({ } ); - await configureModelProvider(modelProviderId, allConfigParams); + await configureModelProvider.execute( + modelProviderId, + allConfigParams + ); onSuccess(allConfigParams); } ); diff --git a/ui/admin/app/components/model-providers/ModelProviderLists.tsx b/ui/admin/app/components/model-providers/ModelProviderLists.tsx index f54330e98..e30e85e8f 100644 --- a/ui/admin/app/components/model-providers/ModelProviderLists.tsx +++ b/ui/admin/app/components/model-providers/ModelProviderLists.tsx @@ -1,11 +1,17 @@ import { BoxesIcon, CircleCheckIcon, CircleSlashIcon } from "lucide-react"; +import useSWR from "swr"; + +import { ModelProviderApiService } from "~/lib/service/api/modelProviderApiService"; import { ModelProviderConfigure } from "~/components/model-providers/ModelProviderConfigure"; -import { useModelProviders } from "~/components/model-providers/ModelProviderContext"; import { Card, CardContent, CardHeader } from "~/components/ui/card"; export function ModelProviderList() { - const { modelProviders } = useModelProviders(); + const { data: modelProviders } = useSWR( + ModelProviderApiService.getModelProviders.key(), + () => ModelProviderApiService.getModelProviders(), + { fallbackData: [] } + ); return (
diff --git a/ui/admin/app/routes/_auth.tsx b/ui/admin/app/routes/_auth.tsx index c693d0dcf..195b0f54d 100644 --- a/ui/admin/app/routes/_auth.tsx +++ b/ui/admin/app/routes/_auth.tsx @@ -7,7 +7,6 @@ import { UserService } from "~/lib/service/api/userService"; import { useAuth } from "~/components/auth/AuthContext"; import { Error, RouteError, Unauthorized } from "~/components/errors"; import { HeaderNav } from "~/components/header/HeaderNav"; -import { ModelProviderProvider } from "~/components/model-providers/ModelProviderContext"; import { Sidebar } from "~/components/sidebar"; import { SignIn } from "~/components/signin/SignIn"; @@ -15,6 +14,7 @@ export async function clientLoader() { const me = await preload(UserService.getMe.key(), () => UserService.getMe() ); + return { me }; } @@ -25,9 +25,7 @@ export default function AuthLayout() {
- - - +