import { useState } from 'react' import { useMutation } from '@tanstack/react-query' import { Loader2 } from 'lucide-react' import { toast } from 'sonner' import type { RefreshedProviderModel } from '@shumai/dtos' import { client } from '@/ui/api/client' import { Badge } from '@/ui/components/ui/badge' import { Button } from '@/ui/components/ui/button' import { Checkbox } from '@/ui/components/ui/checkbox' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/ui/components/ui/dialog' import { Input } from '@/ui/components/ui/input' import { ScrollArea } from '@/ui/components/ui/scroll-area' import { m } from '@/ui/paraglide/messages.js' interface RefreshProviderModelsDialogProps { teamId: string providerId: string providerName: string models: RefreshedProviderModel[] onClose: () => void onCloseAutoFocus: () => void onImported: () => void } export function RefreshProviderModelsDialog({ teamId, providerId, providerName, models, onClose, onCloseAutoFocus, onImported, }: RefreshProviderModelsDialogProps) { const [selectedIds, setSelectedIds] = useState(() => new Set()) const [search, setSearch] = useState('') const query = search.trim().toLowerCase() const filteredModels = models.filter( ({ name, modelId }) => name.toLowerCase().includes(query) || modelId.toLowerCase().includes(query), ) const allVisibleSelected = filteredModels.length > 0 && filteredModels.every(({ modelId }) => selectedIds.has(modelId)) const importMutation = useMutation({ mutationFn: async () => { const res = await client.api.teams[':teamId'].providers[':providerId'].models.import.$post({ param: { teamId, providerId }, json: { models: models.filter(({ modelId }) => selectedIds.has(modelId)) }, }) if (res.status === 409) throw new Error(m.provider_refresh_stale()) if (!res.ok) throw new Error(m.provider_refresh_import_failed()) return res.json() }, onSuccess: ({ addedModels }) => { if (addedModels > 0) { toast.success(m.provider_refresh_imported({ count: addedModels, name: providerName })) } else { toast.info(m.provider_refresh_no_new_models({ name: providerName })) } onImported() onClose() }, }) function selectModels(modelIds: string[], selected: boolean) { setSelectedIds((previous) => { const next = new Set(previous) for (const modelId of modelIds) { if (selected) next.add(modelId) else next.delete(modelId) } return next }) } return ( !open && !importMutation.isPending && onClose()}> { event.preventDefault() onCloseAutoFocus() }} > {m.provider_refresh_dialog_title({ name: providerName })} {m.provider_refresh_dialog_description()} {models.length > 5 && (
setSearch(event.target.value)} />
)}
    {filteredModels.map(({ name, modelId, config }) => (
  • ))}
{filteredModels.length === 0 && (

{m.no_matches_found()}

)}
{importMutation.error && (

{importMutation.error.message}

)}
) }