Files
open-webui/src/lib/components/automations/ModelDropdown.svelte
T
2026-11-21 13:27:07 +04:00

137 lines
4.6 KiB
Svelte

<script lang="ts">
import { getContext } from 'svelte';
import type { Model } from '$lib/stores';
import { WEBUI_API_BASE_URL } from '$lib/constants';
import Dropdown from '$lib/components/common/Dropdown.svelte';
import Search from '$lib/components/icons/Search.svelte';
const i18n = getContext('i18n');
export let model_id = '';
export let items: Model[] = [];
export let disabled = false;
export let side: 'top' | 'bottom' = 'top';
export let align: 'start' | 'end' = 'start';
/** Render the trigger as a bare value, for label | value rows. */
export let plain = false;
/** Optional callback when selection changes */
export let onChange: () => void = () => {};
let showDropdown = false;
let modelSearch = '';
$: modelLabel = model_id
? items.find((m) => m.id === model_id)?.name || model_id
: $i18n.t('Select model');
$: filteredModels = (
modelSearch
? items.filter(
(m) =>
m.name.toLowerCase().includes(modelSearch.toLowerCase()) ||
m.id.toLowerCase().includes(modelSearch.toLowerCase())
)
: items
).filter((m) => !(m?.info?.meta?.hidden ?? false));
</script>
<Dropdown bind:show={showDropdown} {side} {align}>
<button
{disabled}
type="button"
class={plain
? 'flex w-full min-w-0 items-center text-left text-xs font-normal leading-6 text-base-700 dark:text-base-300'
: 'flex items-center gap-1.5 px-2.5 py-1.5 rounded-2xl text-xs transition text-base-600 hover:text-base-900 dark:text-base-400 dark:hover:text-base-100'}
>
{#if !plain}<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="size-3.5 shrink-0"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M9.813 15.904 9 18.75l-.813-2.846a4.5 4.5 0 0 0-3.09-3.09L2.25 12l2.846-.813a4.5 4.5 0 0 0 3.09-3.09L9 5.25l.813 2.846a4.5 4.5 0 0 0 3.09 3.09L15.75 12l-2.846.813a4.5 4.5 0 0 0-3.09 3.09ZM18.259 8.715 18 9.75l-.259-1.035a3.375 3.375 0 0 0-2.455-2.456L14.25 6l1.036-.259a3.375 3.375 0 0 0 2.455-2.456L18 2.25l.259 1.035a3.375 3.375 0 0 0 2.455 2.456L21.75 6l-1.036.259a3.375 3.375 0 0 0-2.455 2.456ZM16.894 20.567 16.5 21.75l-.394-1.183a2.25 2.25 0 0 0-1.423-1.423L13.5 18.75l1.183-.394a2.25 2.25 0 0 0 1.423-1.423l.394-1.183.394 1.183a2.25 2.25 0 0 0 1.423 1.423l1.183.394-1.183.394a2.25 2.25 0 0 0-1.423 1.423Z"
/>
</svg>{/if}
<span class={plain ? 'truncate' : 'whitespace-nowrap max-w-32 truncate'}>{modelLabel}</span>
{#if !plain}<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="2"
stroke="currentColor"
class="size-2.5"
>
<path stroke-linecap="round" stroke-linejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" />
</svg>{/if}
</button>
<div
slot="content"
class="rounded-xl shadow-lg border border-base-200 dark:border-base-800 flex flex-col bg-base-0 dark:bg-base-850 w-72 p-0.5"
>
<div class="flex items-center gap-1.5 px-2 py-1">
<Search className="size-3.5" strokeWidth="2.5" />
<input
bind:value={modelSearch}
class="w-full text-[0.8125rem] bg-transparent outline-hidden"
placeholder={$i18n.t('Search a model')}
autocomplete="off"
on:click={(e) => e.stopPropagation()}
/>
</div>
<div class="overflow-y-auto scrollbar-thin max-h-60">
<div class="px-2 text-[0.6875rem] text-base-500 py-0.5">
{$i18n.t('Models')}
</div>
{#each filteredModels as model (model.id)}
<button
class="h-[1.6875rem] px-2 rounded-xl w-full text-left text-[0.8125rem] {model_id ===
model.id
? 'text-base-900 dark:text-base-100'
: 'text-base-700 hover:text-base-900 dark:text-base-300 dark:hover:text-base-100'}"
type="button"
on:click={() => {
model_id = model.id;
showDropdown = false;
modelSearch = '';
onChange();
}}
>
<div class="flex text-base-1000 dark:text-base-100 line-clamp-1">
<img
src={`${WEBUI_API_BASE_URL}/models/model/profile/image?id=${encodeURIComponent(model.id)}`}
alt={model?.name ?? model.id}
class="rounded-full size-5 items-center mr-2"
loading="lazy"
on:error={(e) => {
// LICENSE covers this Open WebUI fallback logo.
// Do not alter, remove, obscure, or replace it except as LICENSE permits:
// https://docs.openwebui.com/license.
e.currentTarget.src = '/favicon.png';
}}
/>
<div class="truncate">
{model.name}
</div>
</div>
</button>
{:else}
<div class="block px-2 py-1.5 text-[0.8125rem] text-base-700 dark:text-base-100">
{$i18n.t('No results found')}
</div>
{/each}
</div>
</div>
</Dropdown>