mirror of
https://github.com/open-webui/open-webui.git
synced 2026-12-05 11:07:55 -05:00
424 lines
15 KiB
Svelte
424 lines
15 KiB
Svelte
<script lang="ts">
|
|
import { getContext } from 'svelte';
|
|
import type { Task, TaskChanges } from '$lib/apis/tasks';
|
|
import { taskLabel } from '$lib/utils/tasks';
|
|
import Plus from '$lib/components/icons/Plus.svelte';
|
|
import ChevronRight from '$lib/components/icons/ChevronRight.svelte';
|
|
import FolderTags from '$lib/components/chat/Placeholder/FolderTags.svelte';
|
|
import TaskAssignee from './TaskAssignee.svelte';
|
|
import ChatParticipants from '$lib/components/common/ChatParticipants.svelte';
|
|
import TaskDueDate from './TaskDueDate.svelte';
|
|
import TaskGroupIcon from './TaskGroupIcon.svelte';
|
|
import TaskPriority from './TaskPriority.svelte';
|
|
import TaskStatus from './TaskStatus.svelte';
|
|
import TaskStatusTime from './TaskStatusTime.svelte';
|
|
import type { TaskGroup } from './groups';
|
|
|
|
const i18n = getContext<typeof import('$lib/i18n').default>('i18n');
|
|
|
|
// The folder chat board's layout: columns, optional swimlanes, and drops that set both.
|
|
export let columns: TaskGroup[] = [];
|
|
export let rows: TaskGroup[] | null = null;
|
|
export let inFolder = false;
|
|
export let showPriority = true;
|
|
export let loading = false;
|
|
export let hasMore = false;
|
|
/** Swimlanes the user folded. */
|
|
export let collapsed: Record<string, boolean> = {};
|
|
export let onCollapse: (id: string) => void;
|
|
export let onOpen: (task: Task) => void;
|
|
/** A drop sets what its column, and its row, stand for. */
|
|
export let onDrop: (task: Task, sets: TaskGroup['set'][]) => void;
|
|
export let onUpdate: (task: Task, changes: TaskChanges) => void;
|
|
export let onMore: () => void;
|
|
export let onAddStatus: (() => void) | null = null;
|
|
/** Columns the user folded into a strip; clicking a header folds or opens it. */
|
|
export let collapsedColumns: Record<string, boolean> = {};
|
|
export let onCollapseColumn: ((id: string) => void) | null = null;
|
|
/** Saves a new status order, as status ids; set only when columns are a folder's statuses. */
|
|
export let onReorder: ((ids: string[]) => Promise<void> | void) | null = null;
|
|
|
|
let dragged: Task | null = null;
|
|
let over = '';
|
|
|
|
// Status columns move by their header, live like the statuses modal.
|
|
let moving = '';
|
|
let order: string[] | null = null;
|
|
$: shownColumns = order
|
|
? order.flatMap((id) => columns.filter((column) => column.id === id))
|
|
: columns;
|
|
const movable = (column: TaskGroup) => !!onReorder && !!column.status;
|
|
function moveColumn(event: DragEvent, overId: string) {
|
|
if (!moving || !order) return false;
|
|
event.preventDefault();
|
|
const target = columns.find((column) => column.id === overId);
|
|
if (overId === moving || !target || !movable(target)) return true;
|
|
const next = order.filter((id) => id !== moving);
|
|
next.splice(order.indexOf(overId), 0, moving);
|
|
order = next;
|
|
return true;
|
|
}
|
|
async function endMove(event: DragEvent) {
|
|
const ids = order;
|
|
moving = '';
|
|
if (
|
|
ids &&
|
|
event.dataTransfer?.dropEffect !== 'none' &&
|
|
ids.join() !== columns.map((column) => column.id).join()
|
|
)
|
|
await onReorder?.(
|
|
ids.flatMap((id) => columns.find((column) => column.id === id)?.status?.id ?? [])
|
|
);
|
|
order = null;
|
|
}
|
|
|
|
const cellKey = (column: string, row: string | null) => `${column}\u0000${row ?? ''}`;
|
|
const setsFor = (column: TaskGroup, row: TaskGroup | null) => [column.set, row?.set ?? null];
|
|
const canDrop = (column: TaskGroup, row: TaskGroup | null) =>
|
|
setsFor(column, row).some((set) => set);
|
|
$: draggableBoard = columns.some((column) => canDrop(column, rows?.[0] ?? null));
|
|
|
|
function drop(column: TaskGroup, row: TaskGroup | null) {
|
|
if (dragged && canDrop(column, row)) onDrop(dragged, setsFor(column, row));
|
|
dragged = null;
|
|
over = '';
|
|
}
|
|
|
|
const dropzone = (column: TaskGroup, row: TaskGroup | null) => ({
|
|
dragover: (event: DragEvent) => {
|
|
if (moveColumn(event, column.id) || !dragged || !canDrop(column, row)) return;
|
|
event.preventDefault();
|
|
over = cellKey(column.id, row?.id ?? null);
|
|
if (event.dataTransfer) event.dataTransfer.dropEffect = 'move';
|
|
},
|
|
dragleave: (event: DragEvent) => {
|
|
const target = event.currentTarget as HTMLElement;
|
|
if (!target.contains(event.relatedTarget as Node | null)) over = '';
|
|
},
|
|
drop: (event: DragEvent) => {
|
|
event.preventDefault();
|
|
drop(column, row);
|
|
}
|
|
});
|
|
|
|
const cellTasks = (column: TaskGroup, row: TaskGroup) =>
|
|
column.tasks.filter((task) => row.tasks.some((item) => item.id === task.id));
|
|
const rowCount = (row: TaskGroup) =>
|
|
new Set(columns.flatMap((column) => cellTasks(column, row).map((task) => task.id))).size;
|
|
|
|
function open(event: MouseEvent, task: Task) {
|
|
if ((event.target as Element).closest('a, button, input, [role="menu"]')) return;
|
|
if (window.getSelection()?.toString()) return;
|
|
onOpen(task);
|
|
}
|
|
</script>
|
|
|
|
{#snippet columnHeader(column: TaskGroup)}
|
|
<!-- svelte-ignore a11y_no_static_element_interactions, a11y_no_noninteractive_tabindex -->
|
|
<header
|
|
class="flex h-9 shrink-0 items-center gap-2 px-3 {movable(column)
|
|
? 'cursor-grab active:cursor-grabbing'
|
|
: onCollapseColumn
|
|
? 'cursor-pointer'
|
|
: ''}"
|
|
role={onCollapseColumn ? 'button' : undefined}
|
|
tabindex={onCollapseColumn ? 0 : undefined}
|
|
aria-expanded={onCollapseColumn ? true : undefined}
|
|
aria-label={onCollapseColumn ? $i18n.t('Collapse {{name}}', { name: column.label }) : undefined}
|
|
on:click={() => onCollapseColumn?.(column.id)}
|
|
on:keydown={(event) => {
|
|
if (onCollapseColumn && (event.key === 'Enter' || event.key === ' ')) {
|
|
event.preventDefault();
|
|
onCollapseColumn(column.id);
|
|
}
|
|
}}
|
|
draggable={movable(column) && !loading}
|
|
on:dragstart={(event) => {
|
|
moving = column.id;
|
|
order = columns.map((item) => item.id);
|
|
event.dataTransfer?.setData('text/plain', column.id);
|
|
if (event.dataTransfer) event.dataTransfer.effectAllowed = 'move';
|
|
}}
|
|
on:dragend={endMove}
|
|
>
|
|
<TaskGroupIcon group={column} />
|
|
<span class="min-w-0 truncate text-xs text-base-700 dark:text-base-200">
|
|
{column.label}
|
|
</span>
|
|
<span class="text-xs tabular-nums text-base-400 dark:text-base-500">{column.tasks.length}</span>
|
|
</header>
|
|
{/snippet}
|
|
|
|
{#snippet strip(column: TaskGroup)}
|
|
<!-- A folded column: its icon, name down the strip, and count. -->
|
|
<button
|
|
type="button"
|
|
class="flex w-full flex-col items-center gap-2 py-3"
|
|
aria-expanded="false"
|
|
aria-label={$i18n.t('Expand {{name}}', { name: column.label })}
|
|
title={column.label}
|
|
on:click={() => onCollapseColumn?.(column.id)}
|
|
>
|
|
<TaskGroupIcon group={column} />
|
|
<span class="text-xs text-base-700 [writing-mode:vertical-rl] dark:text-base-200"
|
|
>{column.label}</span
|
|
>
|
|
<span class="text-xs tabular-nums text-base-400 dark:text-base-500">{column.tasks.length}</span>
|
|
</button>
|
|
{/snippet}
|
|
|
|
{#snippet more()}
|
|
{#if hasMore}
|
|
<button
|
|
type="button"
|
|
class="flex h-7 w-full items-center justify-center rounded-lg text-xs text-base-400 transition-colors hover:bg-base-100 hover:text-base-700 dark:text-base-500 dark:hover:bg-base-800/60 dark:hover:text-base-300"
|
|
disabled={loading}
|
|
on:click={onMore}
|
|
>
|
|
{loading ? $i18n.t('Loading…') : $i18n.t('Load more')}
|
|
</button>
|
|
{/if}
|
|
{/snippet}
|
|
|
|
{#snippet card(task: Task)}
|
|
{@const editable = !!task.write_access}
|
|
<!-- svelte-ignore a11y_click_events_have_key_events, a11y_no_noninteractive_element_interactions -->
|
|
<article
|
|
data-task-row={task.id}
|
|
class="group cursor-pointer rounded-xl border border-base-1000/[0.06] bg-base-0 px-3 py-2 shadow-[0_1px_2px_rgba(0,0,0,0.03)] transition hover:border-base-1000/[0.09] dark:border-base-0/[0.06] dark:bg-base-900 dark:hover:border-base-0/[0.09] {dragged?.id ===
|
|
task.id
|
|
? 'opacity-40'
|
|
: ''}"
|
|
draggable={draggableBoard && editable && !loading}
|
|
on:click={(event) => open(event, task)}
|
|
on:dragstart={(event) => {
|
|
dragged = task;
|
|
event.dataTransfer?.setData('text/plain', task.id);
|
|
if (event.dataTransfer) event.dataTransfer.effectAllowed = 'move';
|
|
}}
|
|
on:dragend={() => {
|
|
dragged = null;
|
|
over = '';
|
|
}}
|
|
>
|
|
<div class="flex items-start gap-1.5">
|
|
<!-- Status, its time, then priority, on the title's first line. -->
|
|
<div class="-ml-0.5 flex shrink-0 items-center gap-0.5">
|
|
<TaskStatus
|
|
{task}
|
|
disabled={!editable || loading}
|
|
onChange={(status) => onUpdate(task, { status })}
|
|
/>
|
|
<span class="flex h-5 shrink-0 items-center"><TaskStatusTime {task} /></span>
|
|
{#if showPriority}
|
|
<TaskPriority
|
|
{task}
|
|
disabled={!editable || loading}
|
|
onChange={(priority) => onUpdate(task, { priority })}
|
|
/>
|
|
{/if}
|
|
</div>
|
|
<span
|
|
class="line-clamp-2 min-w-0 flex-1 text-[0.8125rem] leading-5 text-base-700 dark:text-base-200"
|
|
>{task.title}</span
|
|
>
|
|
</div>
|
|
|
|
<!-- Lines up with the title, past the status and priority icons. -->
|
|
<div class="mt-2 flex min-h-5 items-center gap-2 {showPriority ? 'pl-11.5' : 'pl-6'}">
|
|
<div class="flex min-w-0 flex-1">
|
|
<FolderTags resourceType="tasks" chat={task} folderId={task.folder_id} {editable} />
|
|
</div>
|
|
<TaskDueDate
|
|
date={task.due_date}
|
|
disabled={!editable}
|
|
hideEmpty
|
|
onChange={(due_date) => onUpdate(task, { due_date })}
|
|
/>
|
|
<TaskAssignee
|
|
folderId={task.folder_id}
|
|
assignee={task.assignee}
|
|
disabled={!editable}
|
|
onChange={(person) => onUpdate(task, { assignee_id: person?.id ?? null })}
|
|
/>
|
|
{#if task.user}
|
|
<div class="flex" title={$i18n.t('Created by {{name}}', { name: task.user.name })}>
|
|
<ChatParticipants owner={task.user} />
|
|
</div>
|
|
{/if}
|
|
<span
|
|
class="max-w-24 shrink-0 truncate text-right text-[0.6875rem] tabular-nums text-base-400 dark:text-base-500"
|
|
title={taskLabel(task, inFolder)}>{taskLabel(task, inFolder)}</span
|
|
>
|
|
</div>
|
|
</article>
|
|
{/snippet}
|
|
|
|
{#snippet emptyCell(column: string, row: string | null)}
|
|
<div
|
|
class="flex h-20 items-center justify-center rounded-xl border border-dashed text-xs text-base-400 transition-colors dark:text-base-500 {over ===
|
|
cellKey(column, row)
|
|
? 'border-base-300 dark:border-base-700'
|
|
: 'border-base-200/70 dark:border-base-800'}"
|
|
>
|
|
{$i18n.t('No tasks')}
|
|
</div>
|
|
{/snippet}
|
|
|
|
{#snippet addStatus()}
|
|
{#if onAddStatus}
|
|
<button
|
|
type="button"
|
|
class="flex h-9 w-36 shrink-0 items-center gap-1.5 rounded-2xl px-3 text-xs text-base-400 transition-colors hover:bg-base-50 hover:text-base-700 dark:text-base-500 dark:hover:bg-base-850/40 dark:hover:text-base-200"
|
|
on:click={onAddStatus}
|
|
>
|
|
<Plus className="size-3" strokeWidth="2" />{$i18n.t('Add status')}
|
|
</button>
|
|
{/if}
|
|
{/snippet}
|
|
|
|
{#if !rows}
|
|
<!-- Columns only: each column runs full height and scrolls on its own. -->
|
|
<div
|
|
class="scrollbar-hidden flex h-full min-h-0 gap-2 overflow-x-auto px-3 pt-1 pb-2"
|
|
aria-label={$i18n.t('Task board')}
|
|
>
|
|
{#each shownColumns as column (column.id)}
|
|
{@const zone = dropzone(column, null)}
|
|
{@const folded = !!collapsedColumns[column.id]}
|
|
<section
|
|
class="flex min-h-0 flex-col rounded-2xl transition-colors {folded
|
|
? 'w-10 shrink-0'
|
|
: 'w-72 min-w-64 flex-1'} {moving === column.id ? 'opacity-40' : ''} {over ===
|
|
cellKey(column.id, null)
|
|
? 'bg-base-100 ring-1 ring-base-200 dark:bg-base-850 dark:ring-base-800'
|
|
: 'bg-base-50/80 dark:bg-base-850/40'}"
|
|
aria-label={column.label}
|
|
on:dragover={zone.dragover}
|
|
on:dragleave={zone.dragleave}
|
|
on:drop={zone.drop}
|
|
>
|
|
{#if folded}
|
|
{@render strip(column)}
|
|
{:else}
|
|
{@render columnHeader(column)}
|
|
<div class="scrollbar-hidden min-h-0 flex-1 space-y-1.5 overflow-y-auto px-1.5 pb-1.5">
|
|
{#each column.tasks as task (task.id)}
|
|
{@render card(task)}
|
|
{/each}
|
|
{#if !loading && !column.tasks.length}
|
|
{@render emptyCell(column.id, null)}
|
|
{/if}
|
|
{@render more()}
|
|
</div>
|
|
{/if}
|
|
</section>
|
|
{/each}
|
|
{@render addStatus()}
|
|
</div>
|
|
{:else}
|
|
<!-- Swimlanes: the board scrolls as one; column headers stick to the top. -->
|
|
<div
|
|
class="scrollbar-hidden h-full min-h-0 overflow-auto px-3 pt-1 pb-2"
|
|
aria-label={$i18n.t('Task board')}
|
|
>
|
|
<div class="inline-flex min-w-full flex-col">
|
|
<div
|
|
class="sticky top-0 z-[2] flex gap-2 bg-base-0/80 pb-1 backdrop-blur-md dark:bg-base-900/80"
|
|
>
|
|
{#each shownColumns as column (column.id)}
|
|
<div
|
|
class="shrink-0 rounded-2xl bg-base-50/80 dark:bg-base-850/40 {collapsedColumns[
|
|
column.id
|
|
]
|
|
? 'w-10'
|
|
: 'w-72'} {moving === column.id ? 'opacity-40' : ''}"
|
|
role="group"
|
|
aria-label={column.label}
|
|
on:dragover={(event) => moveColumn(event, column.id)}
|
|
on:drop|preventDefault
|
|
>
|
|
{#if collapsedColumns[column.id]}
|
|
{@render strip(column)}
|
|
{:else}
|
|
{@render columnHeader(column)}
|
|
{/if}
|
|
</div>
|
|
{/each}
|
|
{@render addStatus()}
|
|
</div>
|
|
|
|
{#each rows as row (row.id)}
|
|
{@const count = rowCount(row)}
|
|
{#if count}
|
|
<!-- The whole lane header toggles it, as list group headers do. -->
|
|
<h3 class="sticky left-0 z-[1] w-fit">
|
|
<button
|
|
type="button"
|
|
class="group/header flex h-8 items-center gap-2 pr-3 text-left"
|
|
aria-expanded={!collapsed[row.id]}
|
|
aria-label={collapsed[row.id]
|
|
? $i18n.t('Expand {{name}}', { name: row.label })
|
|
: $i18n.t('Collapse {{name}}', { name: row.label })}
|
|
on:click={() => onCollapse(row.id)}
|
|
>
|
|
<span
|
|
class="flex size-5 items-center justify-center text-base-400 transition-colors group-hover/header:text-base-700 dark:text-base-500 dark:group-hover/header:text-base-200"
|
|
>
|
|
<ChevronRight
|
|
className="size-3 transition-transform {collapsed[row.id] ? '' : 'rotate-90'}"
|
|
strokeWidth="2"
|
|
/>
|
|
</span>
|
|
<TaskGroupIcon group={row} />
|
|
<span class="text-xs text-base-700 dark:text-base-200">{row.label}</span>
|
|
<span class="text-xs tabular-nums text-base-400 dark:text-base-500">{count}</span>
|
|
</button>
|
|
</h3>
|
|
|
|
{#if !collapsed[row.id]}
|
|
<div class="flex gap-2 pb-2">
|
|
{#each shownColumns as column (column.id)}
|
|
{@const tasks = cellTasks(column, row)}
|
|
{@const zone = dropzone(column, row)}
|
|
<div
|
|
role="group"
|
|
aria-label={`${column.label} · ${row.label}`}
|
|
class="shrink-0 space-y-1.5 rounded-2xl p-1.5 transition-colors {collapsedColumns[
|
|
column.id
|
|
]
|
|
? 'w-10 text-center'
|
|
: 'w-72'} {over === cellKey(column.id, row.id)
|
|
? 'bg-base-100 ring-1 ring-base-200 dark:bg-base-850 dark:ring-base-800'
|
|
: 'bg-base-50/80 dark:bg-base-850/40'}"
|
|
on:dragover={zone.dragover}
|
|
on:dragleave={zone.dragleave}
|
|
on:drop={zone.drop}
|
|
>
|
|
{#if collapsedColumns[column.id]}
|
|
<!-- A folded column's cell only counts its tasks. -->
|
|
<div
|
|
class="flex h-10 items-center justify-center text-xs tabular-nums text-base-400 dark:text-base-500"
|
|
>
|
|
{tasks.length || ''}
|
|
</div>
|
|
{:else}
|
|
{#each tasks as task (task.id)}
|
|
{@render card(task)}
|
|
{/each}
|
|
{#if !tasks.length}
|
|
<div class="h-10"></div>
|
|
{/if}
|
|
{/if}
|
|
</div>
|
|
{/each}
|
|
</div>
|
|
{/if}
|
|
{/if}
|
|
{/each}
|
|
|
|
<div class="w-72">{@render more()}</div>
|
|
</div>
|
|
</div>
|
|
{/if}
|