mirror of
https://github.com/open-webui/open-webui.git
synced 2026-12-05 11:07:55 -05:00
129 lines
4.4 KiB
Svelte
129 lines
4.4 KiB
Svelte
<script lang="ts">
|
|
import { getContext } from 'svelte';
|
|
import { settings } from '$lib/stores';
|
|
import { getPageContext } from '$lib/utils/page';
|
|
const { goto } = getPageContext();
|
|
import type { Writable } from 'svelte/store';
|
|
import type { i18n as i18nType } from 'i18next';
|
|
|
|
import Dropdown from '$lib/components/common/Dropdown.svelte';
|
|
import DropdownMenu from '$lib/components/common/DropdownMenu.svelte';
|
|
import ChevronDown from '$lib/components/icons/ChevronDown.svelte';
|
|
import DocumentArrowDown from '$lib/components/icons/DocumentArrowDown.svelte';
|
|
import DocumentArrowUp from '$lib/components/icons/DocumentArrowUp.svelte';
|
|
import Link from '$lib/components/icons/Link.svelte';
|
|
import Pencil from '$lib/components/icons/Pencil.svelte';
|
|
import Plus from '$lib/components/icons/Plus.svelte';
|
|
|
|
type SplitCreateAction = {
|
|
id: string;
|
|
label: string;
|
|
href?: string;
|
|
onClick?: () => void | Promise<void>;
|
|
visible?: boolean;
|
|
};
|
|
|
|
export let actions: SplitCreateAction[] = [];
|
|
export let label = 'Create';
|
|
|
|
const i18n = getContext<Writable<i18nType>>('i18n');
|
|
|
|
let showMenu = false;
|
|
$: visibleActions = actions.filter((action) => action.visible ?? true);
|
|
$: primaryAction =
|
|
visibleActions.find((action) => action.id.endsWith('-new')) ?? visibleActions[0] ?? null;
|
|
|
|
const runAction = async (action: SplitCreateAction | null) => {
|
|
if (!action) return;
|
|
if (action.href) {
|
|
await goto(action.href);
|
|
} else {
|
|
await action.onClick?.();
|
|
}
|
|
};
|
|
|
|
const getActionIcon = (id: string) => {
|
|
if (id.endsWith('-new')) return Pencil;
|
|
if (id.includes('-import-link')) return Link;
|
|
if (id.includes('-import')) return DocumentArrowUp;
|
|
if (id.includes('-export')) return DocumentArrowDown;
|
|
return Pencil;
|
|
};
|
|
</script>
|
|
|
|
<!-- Same circle as the + buttons on the folder page; a split pill when there are more ways to create. -->
|
|
{#if visibleActions.length === 1}
|
|
<button
|
|
class="focus-ring relative flex size-6.5 shrink-0 items-center justify-center rounded-full border border-base-100 bg-base-50/60 text-base-500 transition-colors hover:bg-base-100 hover:text-base-900 dark:border-base-850 dark:bg-base-850/50 dark:text-base-400 dark:hover:bg-base-850 dark:hover:text-base-0"
|
|
type="button"
|
|
aria-label={$i18n.t(label)}
|
|
title={$i18n.t(label)}
|
|
on:click={() => {
|
|
runAction(primaryAction);
|
|
}}
|
|
>
|
|
<Plus className="size-3.5" strokeWidth="2" />
|
|
</button>
|
|
{:else if visibleActions.length > 1}
|
|
<div
|
|
class="flex h-6.5 shrink-0 items-center overflow-hidden rounded-full border border-base-100 bg-base-50/60 text-base-500 dark:border-base-850 dark:bg-base-850/50 dark:text-base-400"
|
|
>
|
|
<button
|
|
class="focus-ring flex h-full w-6.5 items-center justify-center transition-colors hover:bg-base-100 hover:text-base-900 dark:hover:bg-base-850 dark:hover:text-base-0"
|
|
type="button"
|
|
aria-label={$i18n.t(label)}
|
|
title={$i18n.t(label)}
|
|
on:click={() => {
|
|
runAction(primaryAction);
|
|
}}
|
|
>
|
|
<Plus className="size-3.5" strokeWidth="2" />
|
|
</button>
|
|
|
|
<Dropdown bind:show={showMenu} align="end" sideOffset={6}>
|
|
<button
|
|
class="focus-ring flex h-full items-center justify-center border-l border-base-100 transition-colors hover:bg-base-100 hover:text-base-900 aria-expanded:bg-base-100 aria-expanded:text-base-900 dark:border-base-850 dark:hover:bg-base-850 dark:hover:text-base-0 dark:aria-expanded:bg-base-850 dark:aria-expanded:text-base-0 {$settings?.highContrastMode
|
|
? 'w-6'
|
|
: 'w-5'}"
|
|
type="button"
|
|
aria-label={$i18n.t('Open create menu')}
|
|
title={$i18n.t('Open create menu')}
|
|
>
|
|
<ChevronDown className="size-2.5" strokeWidth="2.5" />
|
|
</button>
|
|
|
|
<div slot="content">
|
|
<DropdownMenu className="min-w-[10.625rem]">
|
|
{#each visibleActions as action (action.id)}
|
|
{@const Icon = getActionIcon(action.id)}
|
|
{#if action.href}
|
|
<a
|
|
href={action.href}
|
|
on:click={(e) => {
|
|
showMenu = false;
|
|
if (e.metaKey || e.ctrlKey || e.shiftKey || e.button === 1) return;
|
|
e.preventDefault();
|
|
runAction(action);
|
|
}}
|
|
>
|
|
<Icon className="size-3.5" />
|
|
<span class="self-center truncate">{action.label}</span>
|
|
</a>
|
|
{:else}
|
|
<button
|
|
on:click={async () => {
|
|
await action.onClick?.();
|
|
showMenu = false;
|
|
}}
|
|
>
|
|
<Icon className="size-3.5" />
|
|
<span class="self-center truncate">{action.label}</span>
|
|
</button>
|
|
{/if}
|
|
{/each}
|
|
</DropdownMenu>
|
|
</div>
|
|
</Dropdown>
|
|
</div>
|
|
{/if}
|