Files
open-webui/src/lib/components/tasks/TaskModal.svelte
T
2026-12-05 07:32:11 +04:00

230 lines
7.3 KiB
Svelte
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script lang="ts">
import { getContext, tick } from 'svelte';
import { toast } from 'svelte-sonner';
import { getFolderById } from '$lib/apis/folders';
import { createTask, type Task, type TaskPerson, type TaskPriority } from '$lib/apis/tasks';
import { taskStatuses } from '$lib/utils/tasks';
import Modal from '$lib/components/common/Modal.svelte';
import Spinner from '$lib/components/common/Spinner.svelte';
import Switch from '$lib/components/common/Switch.svelte';
import Textarea from '$lib/components/common/Textarea.svelte';
import XMark from '$lib/components/icons/XMark.svelte';
import TaskAssignee from './TaskAssignee.svelte';
import TaskDueDate from './TaskDueDate.svelte';
import TaskFolder from './TaskFolder.svelte';
import TaskPriorityPicker from './TaskPriority.svelte';
import TaskStatus from './TaskStatus.svelte';
import TaskStatusesModal from './TaskStatusesModal.svelte';
import FolderTags from '$lib/components/chat/Placeholder/FolderTags.svelte';
import type { FolderTag } from '$lib/utils/tags';
const i18n = getContext<typeof import('$lib/i18n').default>('i18n');
type FolderOption = { id: string; name: string; icon?: string | null };
export let show = false;
// The folder page's folder; elsewhere the last folder a task was created in.
export let folder: FolderOption | null = null;
export let onCreated: (task: Task) => void = () => {};
const LAST_FOLDER_KEY = 'task-last-folder';
let title = '';
let description = '';
let target: FolderOption | null = null;
let statuses: unknown[] = [];
let status = '';
let priority: TaskPriority | null = null;
let assignee: TaskPerson | null = null;
let dueDate: string | null = null;
let tags: FolderTag[] = [];
let createMore = false;
let saving = false;
let showStatuses = false;
const lastFolder = (): FolderOption | null => {
try {
return JSON.parse(localStorage.getItem(LAST_FOLDER_KEY) ?? 'null');
} catch {
return null;
}
};
const defaultStatus = () => {
const list = taskStatuses(statuses, (key) => $i18n.t(key));
return (list.find((item) => item.glyph === 'todo') ?? list[0]).id;
};
// A folder brings its own statuses; the status starts again from its first to-do.
const chooseFolder = async (next: FolderOption | null) => {
if (next?.id !== target?.id) assignee = null;
target = next;
statuses = [];
if (next) {
const details = await getFolderById(localStorage.token, next.id).catch(() => null);
if (target?.id !== next.id) return;
statuses = details?.meta?.statuses ?? [];
}
status = defaultStatus();
};
const init = async () => {
title = '';
description = '';
priority = null;
assignee = null;
dueDate = null;
tags = [];
await chooseFolder(folder ?? lastFolder());
await tick();
document.getElementById('task-title')?.focus();
};
$: if (show) void init();
// The pickers take a task; this is the one being written.
$: draft = {
title: title || $i18n.t('New task'),
status,
folder: target ? { ...target, statuses } : null
} as unknown as Task;
const submit = async () => {
if (!title.trim()) return toast.error($i18n.t('Enter a title'));
if (!target) return toast.error($i18n.t('Choose a folder'));
saving = true;
const task = await createTask(localStorage.token, {
folder_id: target.id,
title: title.trim(),
description: description.trim() || null,
status,
priority,
assignee_id: assignee?.id ?? null,
due_date: dueDate,
tags: tags.map((tag) => tag.name)
}).catch((error) => {
toast.error(`${error}`);
return null;
});
saving = false;
if (!task) return;
try {
localStorage.setItem(LAST_FOLDER_KEY, JSON.stringify(target));
} catch {
// Remembering the folder is only a convenience.
}
onCreated(task);
if (createMore) {
title = '';
description = '';
await tick();
document.getElementById('task-title')?.focus();
} else {
show = false;
}
};
</script>
<Modal bind:show size="md" ariaLabel={$i18n.t('New task')}>
<form class="flex flex-col px-4 pt-3 pb-4" on:submit|preventDefault={submit}>
<div class="flex items-center justify-between pb-2">
<div class="flex min-w-0 items-center gap-1 text-xs text-base-500 dark:text-base-400">
<TaskFolder folder={target} onChange={(next) => void chooseFolder(next)} />
<span aria-hidden="true">›</span>
<span>{$i18n.t('New task')}</span>
</div>
<button
type="button"
aria-label={$i18n.t('Close')}
class="rounded-lg p-1 text-base-500 transition hover:text-base-700 dark:text-base-400 dark:hover:text-base-200"
on:click={() => (show = false)}
>
<XMark className="size-4" />
</button>
</div>
<input
id="task-title"
class="w-full bg-transparent text-base font-medium outline-hidden placeholder:text-base-300 dark:placeholder:text-base-700"
placeholder={$i18n.t('Task title')}
aria-label={$i18n.t('Task title')}
autocomplete="off"
bind:value={title}
/>
<Textarea
className="mt-1.5 w-full bg-transparent text-sm outline-hidden placeholder:text-base-300 dark:placeholder:text-base-700"
placeholder={$i18n.t('Add description…')}
ariaLabel={$i18n.t('Description')}
minSize={60}
maxSize={240}
bind:value={description}
/>
<div class="mt-3 flex flex-wrap items-center gap-1 text-base-600 dark:text-base-300">
<TaskStatus
task={draft}
labeled
disabled={!target}
onChange={(next) => (status = next)}
onManage={target ? () => (showStatuses = true) : null}
/>
<TaskPriorityPicker task={draft} labeled onChange={(next) => (priority = next)} />
{#if target}
<TaskAssignee
folderId={target.id}
{assignee}
labeled
onChange={(next) => (assignee = next)}
/>
{/if}
<TaskDueDate date={dueDate} labeled onChange={(next) => (dueDate = next)} />
{#if target}
<!-- No id yet: the picker keeps the chosen tags here until the task is created.
Not a row, so its "+ Tag" chip shows without hovering. -->
<div class="flex" data-tags-shown>
<FolderTags
resourceType="tasks"
chat={{ id: '', title: title || $i18n.t('New task'), tags }}
folderId={target.id}
editable
labeled
max={4}
onChange={(next) => {
tags = next;
}}
/>
</div>
{/if}
</div>
<div class="mt-4 flex items-center justify-end gap-3">
<label class="flex items-center gap-2 text-xs text-base-500 dark:text-base-400">
<Switch bind:state={createMore} ariaLabel={$i18n.t('Create more')} />
{$i18n.t('Create more')}
</label>
<button
class="flex h-7 shrink-0 items-center justify-center gap-1.5 rounded-lg bg-base-900 px-2.5 text-xs font-normal text-base-0 transition hover:bg-base-1000 disabled:cursor-not-allowed disabled:opacity-60 dark:bg-base-100 dark:text-base-900 dark:hover:bg-base-0"
type="submit"
disabled={saving}
>
{$i18n.t('Create task')}
{#if saving}<Spinner className="size-3" />{/if}
</button>
</div>
</form>
</Modal>
{#if target}
<!-- The folders offered here are ones the user can edit, so they may change its statuses. -->
<TaskStatusesModal
bind:show={showStatuses}
folderId={target.id}
{statuses}
onSaved={(next) => {
statuses = next;
// A status that was removed or renamed away falls back to the first to-do.
if (!taskStatuses(statuses, (key) => $i18n.t(key)).some((item) => item.id === status))
status = defaultStatus();
}}
/>
{/if}