mirror of
https://github.com/open-webui/open-webui.git
synced 2026-11-26 07:11:00 -05:00
141 lines
4.2 KiB
Svelte
141 lines
4.2 KiB
Svelte
<script lang="ts">
|
|
import type { MachineTarget } from '$lib/apis/machines';
|
|
export let target: MachineTarget;
|
|
import { onMount, onDestroy, getContext, createEventDispatcher } from 'svelte';
|
|
import type { ListeningPort } from '$lib/apis/machines';
|
|
import { getListeningPorts, getPortProxyUrl, getMachineFeatures } from '$lib/apis/machines';
|
|
import { watchMachinePorts } from '$lib/apis/machines/events';
|
|
import Tooltip from '$lib/components/common/Tooltip.svelte';
|
|
import Icon from './Icon.svelte';
|
|
|
|
const i18n = getContext<any>('i18n');
|
|
const dispatch = createEventDispatcher<{ previewPort: number }>();
|
|
|
|
export let active = true;
|
|
export let previewBaseUrl: string | null = null;
|
|
|
|
let ports: ListeningPort[] = [];
|
|
let loading = false;
|
|
let mounted = false;
|
|
let context = '';
|
|
let generation = 0;
|
|
let stopWatching: (() => void) | undefined;
|
|
|
|
const loadPorts = async () => {
|
|
const current = generation;
|
|
loading = true;
|
|
const next = await getListeningPorts(target);
|
|
if (current !== generation) return;
|
|
ports = next;
|
|
loading = false;
|
|
};
|
|
|
|
const configure = async (key: string, enabled: boolean) => {
|
|
const current = ++generation;
|
|
stopWatching?.();
|
|
stopWatching = undefined;
|
|
if (context !== key) ports = [];
|
|
context = key;
|
|
loading = false;
|
|
if (!enabled || !target.baseUrl) return;
|
|
const features = await getMachineFeatures(target);
|
|
if (current !== generation) return;
|
|
if (features.events === true) {
|
|
loading = true;
|
|
stopWatching = watchMachinePorts(
|
|
target,
|
|
(next) => {
|
|
ports = next;
|
|
loading = false;
|
|
},
|
|
() => {
|
|
loading = false;
|
|
}
|
|
);
|
|
} else {
|
|
await loadPorts();
|
|
}
|
|
};
|
|
|
|
const previewPort = (port: number) => {
|
|
dispatch('previewPort', port);
|
|
};
|
|
|
|
const openPortExternal = (port: number) => {
|
|
const url = getPortProxyUrl(previewBaseUrl ?? target.baseUrl, port);
|
|
window.open(url, '_blank', 'noopener,noreferrer');
|
|
};
|
|
|
|
// Only discover ports while the browser start view is visible.
|
|
$: configure(JSON.stringify([target.baseUrl, target.key, target.context]), mounted && active);
|
|
onMount(() => {
|
|
mounted = true;
|
|
});
|
|
onDestroy(() => {
|
|
++generation;
|
|
stopWatching?.();
|
|
});
|
|
</script>
|
|
|
|
<div>
|
|
<div
|
|
class="flex h-7 items-center gap-2 w-full text-xs leading-4 font-normal text-base-500 dark:text-base-400"
|
|
>
|
|
<div class="flex h-full flex-1 items-center gap-1.5">
|
|
{$i18n.t('Ports')}
|
|
{#if ports.length > 0}
|
|
<span class="text-[11px] tabular-nums text-base-400 dark:text-base-500">
|
|
{ports.length}
|
|
</span>
|
|
{/if}
|
|
</div>
|
|
<span class="flex items-center gap-1">
|
|
<Tooltip content={$i18n.t('Refresh')}>
|
|
<button
|
|
class="flex size-6 items-center justify-center rounded transition-colors duration-100 text-base-400 dark:text-base-500 hover:text-base-600 dark:hover:text-base-300"
|
|
on:click|stopPropagation={loadPorts}
|
|
disabled={loading}
|
|
aria-label={$i18n.t('Refresh')}
|
|
>
|
|
<Icon name="refresh" size={11} strokeWidth={1.4} class={loading ? 'animate-spin' : ''} />
|
|
</button>
|
|
</Tooltip>
|
|
</span>
|
|
</div>
|
|
|
|
<div class="mt-1 max-h-60 overflow-y-auto scrollbar-hidden">
|
|
{#if ports.length === 0}
|
|
<div class="text-xs leading-5 text-base-400 dark:text-base-500 py-2">
|
|
{$i18n.t('No servers detected')}
|
|
</div>
|
|
{:else}
|
|
{#each ports as port}
|
|
<div class="flex h-8 items-center w-full gap-2 text-xs leading-4 group">
|
|
<button
|
|
class="flex min-w-0 flex-1 items-center gap-2 self-stretch"
|
|
on:click={() => previewPort(port.port)}
|
|
>
|
|
<span
|
|
class="w-14 font-mono text-base-700 dark:text-base-200 shrink-0 text-left group-hover:text-blue-500 group-focus-within:text-blue-500 transition-colors"
|
|
>
|
|
:{port.port}
|
|
</span>
|
|
<span class="text-base-500 dark:text-base-400 truncate flex-1 text-left">
|
|
{port.process ?? ''}
|
|
</span>
|
|
</button>
|
|
<Tooltip content={$i18n.t('Open in new tab')}>
|
|
<button
|
|
class="text-base-400 dark:text-base-500 transition shrink-0 flex size-6 items-center justify-center rounded hover:text-base-600 dark:hover:text-base-300"
|
|
aria-label={$i18n.t('Open in new tab')}
|
|
on:click={() => openPortExternal(port.port)}
|
|
>
|
|
<Icon name="external-link" size={12} strokeWidth={1.4} />
|
|
</button>
|
|
</Tooltip>
|
|
</div>
|
|
{/each}
|
|
{/if}
|
|
</div>
|
|
</div>
|