mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-29 12:24:39 +00:00
188 lines
6.4 KiB
TypeScript
188 lines
6.4 KiB
TypeScript
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
|
|
providerUpdatedAt: string
|
|
providerName: string
|
|
models: RefreshedProviderModel[]
|
|
onClose: () => void
|
|
onCloseAutoFocus: () => void
|
|
onImported: () => void
|
|
}
|
|
|
|
export function RefreshProviderModelsDialog({
|
|
teamId,
|
|
providerId,
|
|
providerUpdatedAt,
|
|
providerName,
|
|
models,
|
|
onClose,
|
|
onCloseAutoFocus,
|
|
onImported,
|
|
}: RefreshProviderModelsDialogProps) {
|
|
const [selectedIds, setSelectedIds] = useState(() => new Set<string>())
|
|
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: { providerUpdatedAt, modelIds: [...selectedIds] },
|
|
})
|
|
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 (
|
|
<Dialog open onOpenChange={(open) => !open && !importMutation.isPending && onClose()}>
|
|
<DialogContent
|
|
className="sm:max-w-lg"
|
|
showCloseButton={!importMutation.isPending}
|
|
onCloseAutoFocus={(event) => {
|
|
event.preventDefault()
|
|
onCloseAutoFocus()
|
|
}}
|
|
>
|
|
<DialogHeader className="pr-6">
|
|
<DialogTitle>{m.provider_refresh_dialog_title({ name: providerName })}</DialogTitle>
|
|
<DialogDescription>{m.provider_refresh_dialog_description()}</DialogDescription>
|
|
</DialogHeader>
|
|
{models.length > 5 && (
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
<Input
|
|
className="min-w-36 flex-1"
|
|
aria-label={m.search_models_placeholder()}
|
|
placeholder={m.search_models_placeholder()}
|
|
value={search}
|
|
onChange={(event) => setSearch(event.target.value)}
|
|
/>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
size="sm"
|
|
disabled={filteredModels.length === 0 || importMutation.isPending}
|
|
onClick={() =>
|
|
selectModels(
|
|
filteredModels.map(({ modelId }) => modelId),
|
|
!allVisibleSelected,
|
|
)
|
|
}
|
|
>
|
|
{allVisibleSelected
|
|
? m.provider_refresh_deselect_visible()
|
|
: m.provider_refresh_select_visible()}
|
|
</Button>
|
|
</div>
|
|
)}
|
|
<ScrollArea className="[&>[data-slot=scroll-area-viewport]]:max-h-[min(24rem,45dvh)]">
|
|
<ul className="divide-y divide-border pr-3">
|
|
{filteredModels.map(({ name, modelId, config }) => (
|
|
<li key={modelId}>
|
|
<label className="flex min-h-11 cursor-pointer items-center gap-3 rounded-sm px-1 py-2">
|
|
<Checkbox
|
|
checked={selectedIds.has(modelId)}
|
|
disabled={importMutation.isPending}
|
|
onCheckedChange={(checked) => selectModels([modelId], checked === true)}
|
|
/>
|
|
<span className="min-w-0 flex-1 wrap-anywhere text-sm font-medium">
|
|
{name}
|
|
{name !== modelId && (
|
|
<span className="block text-xs font-normal text-muted-foreground">
|
|
{modelId}
|
|
</span>
|
|
)}
|
|
</span>
|
|
{config.reasoning && (
|
|
<Badge variant="secondary" className="font-normal">
|
|
{m.reasoning_support()}
|
|
</Badge>
|
|
)}
|
|
</label>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
{filteredModels.length === 0 && (
|
|
<p className="py-3 text-sm text-muted-foreground">{m.no_matches_found()}</p>
|
|
)}
|
|
</ScrollArea>
|
|
{importMutation.error && (
|
|
<p role="alert" className="text-sm text-destructive">
|
|
{importMutation.error.message}
|
|
</p>
|
|
)}
|
|
<DialogFooter>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
disabled={importMutation.isPending}
|
|
onClick={onClose}
|
|
>
|
|
{m.cancel()}
|
|
</Button>
|
|
<Button
|
|
type="button"
|
|
disabled={selectedIds.size === 0 || importMutation.isPending}
|
|
onClick={() => importMutation.mutate()}
|
|
>
|
|
{importMutation.isPending && (
|
|
<Loader2 aria-hidden="true" className="size-4 motion-safe:animate-spin" />
|
|
)}
|
|
{selectedIds.size
|
|
? m.provider_refresh_add_selection({ count: selectedIds.size })
|
|
: m.provider_refresh_add_selected()}
|
|
</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|