mirror of
https://github.com/open-webui/open-webui.git
synced 2026-12-05 11:07:55 -05:00
314 lines
11 KiB
Svelte
314 lines
11 KiB
Svelte
<script lang="ts">
|
|
import { getContext, tick } from 'svelte';
|
|
import { toast } from 'svelte-sonner';
|
|
|
|
import {
|
|
createNewTeam,
|
|
updateTeamById,
|
|
type Team,
|
|
type TeamPermissions,
|
|
type TeamRole
|
|
} from '$lib/apis/teams';
|
|
import { user } from '$lib/stores';
|
|
|
|
import Modal from '$lib/components/common/Modal.svelte';
|
|
import SettingsSelect from '$lib/components/common/SettingsSelect.svelte';
|
|
import UserSettingRow from '$lib/components/chat/Settings/UserSettingRow.svelte';
|
|
import EmojiPicker from '$lib/components/common/EmojiPicker.svelte';
|
|
import Spinner from '$lib/components/common/Spinner.svelte';
|
|
import Textarea from '$lib/components/common/Textarea.svelte';
|
|
import XMark from '$lib/components/layout/Sidebar/icons/XMark.svelte';
|
|
import ChevronLeft from '$lib/components/icons/ChevronLeft.svelte';
|
|
import SettingsAlt from '$lib/components/icons/SettingsAlt.svelte';
|
|
import Lock from '$lib/components/icons/Lock.svelte';
|
|
import TeamIcon from './TeamIcon.svelte';
|
|
|
|
const i18n = getContext<any>('i18n');
|
|
|
|
export let show = false;
|
|
// The team to edit; null creates one.
|
|
export let team: Team | null = null;
|
|
export let onSaved: (team: Team) => void = () => {};
|
|
|
|
let name = '';
|
|
let identifier = '';
|
|
let description = '';
|
|
let icon = '';
|
|
let loading = false;
|
|
|
|
// What owners can open to all members (TeamPermissions in models/teams.py); unset means owners only.
|
|
// Literal $i18n.t calls, so i18n:parse keeps the keys.
|
|
let permissionRows: { key: keyof TeamPermissions; label: string; description: string }[];
|
|
$: permissionRows = [
|
|
{
|
|
key: 'manage_team',
|
|
label: $i18n.t('settings.team.manageTeam.label'),
|
|
description: $i18n.t('settings.team.manageTeam.description')
|
|
},
|
|
{
|
|
key: 'manage_pins',
|
|
label: $i18n.t('settings.team.managePins.label'),
|
|
description: $i18n.t('settings.team.managePins.description')
|
|
},
|
|
{
|
|
key: 'manage_members',
|
|
label: $i18n.t('settings.team.manageMembers.label'),
|
|
description: $i18n.t('settings.team.manageMembers.description')
|
|
}
|
|
];
|
|
let permissions: TeamPermissions = {};
|
|
let isPrivate = true;
|
|
// General and Permissions are tabs, as Edit Folder's General and Personalization are; General on each open.
|
|
let selectedTab: 'general' | 'permissions' = 'general';
|
|
// The creator becomes an owner, so access and permissions can be set when creating too.
|
|
$: canSetPermissions = !team || team.role === 'owner' || $user?.role === 'admin';
|
|
|
|
const init = async () => {
|
|
name = team?.name ?? '';
|
|
identifier = team?.identifier ?? '';
|
|
description = team?.description ?? '';
|
|
icon = team?.meta?.icon ?? '';
|
|
permissions = { ...(team?.permissions ?? {}) };
|
|
isPrivate = team?.is_private ?? true;
|
|
selectedTab = 'general';
|
|
await tick();
|
|
const input = document.getElementById('team-name') as HTMLInputElement | null;
|
|
input?.focus();
|
|
input?.select();
|
|
};
|
|
|
|
$: if (show) init();
|
|
|
|
// FolderModal's tab buttons.
|
|
const tabButtonClass = (active: boolean) =>
|
|
`flex items-center gap-1.5 h-7 px-2 md:w-full shrink-0 rounded-lg text-xs text-left transition-colors duration-75 ${
|
|
active
|
|
? 'font-medium text-base-900 dark:text-base-0 bg-base-50 dark:bg-base-0/[0.04]'
|
|
: 'text-base-600 hover:text-base-700 dark:text-base-400 dark:hover:text-base-300'
|
|
}`;
|
|
|
|
const submitHandler = async () => {
|
|
if (!name.trim()) return toast.error($i18n.t('Enter team name'));
|
|
loading = true;
|
|
const form = {
|
|
name,
|
|
identifier: identifier.trim().toUpperCase() || null,
|
|
description: description.trim(),
|
|
meta: { icon: icon || null },
|
|
...(canSetPermissions ? { is_private: isPrivate, permissions } : {})
|
|
};
|
|
const res = await (
|
|
team
|
|
? updateTeamById(localStorage.token, team.id, form)
|
|
: createNewTeam(localStorage.token, form)
|
|
).catch((error) => {
|
|
toast.error(`${error}`);
|
|
return null;
|
|
});
|
|
loading = false;
|
|
if (res) {
|
|
show = false;
|
|
onSaved(res);
|
|
}
|
|
};
|
|
</script>
|
|
|
|
<Modal
|
|
ariaLabel={team ? $i18n.t('Edit Team') : $i18n.t('Create Team')}
|
|
size={canSetPermissions ? 'lg' : 'md'}
|
|
className="bg-base-0 dark:bg-base-900 rounded-3xl {canSetPermissions
|
|
? 'flex flex-col md:flex-row overflow-hidden md:h-[36rem] md:min-h-0 max-h-[calc(100dvh-1.5rem)]'
|
|
: ''}"
|
|
bind:show
|
|
>
|
|
{#if canSetPermissions}
|
|
<!-- Edit Folder's side nav: Back, then the tabs. -->
|
|
<nav
|
|
class="flex min-w-0 shrink-0 flex-col border-b border-base-100/30 md:min-h-0 md:w-[12rem] md:border-r md:border-b-0 dark:border-base-0/[0.02]"
|
|
>
|
|
<div class="m-1 flex shrink-0 items-center gap-1 md:mb-0">
|
|
<button
|
|
class="flex h-7 flex-1 items-center gap-1.5 rounded-lg px-2 text-xs text-base-600 transition-colors duration-75 hover:text-base-700 dark:text-base-500 dark:hover:text-base-300"
|
|
type="button"
|
|
on:click={() => (show = false)}
|
|
>
|
|
<ChevronLeft className="size-3" strokeWidth="2" />
|
|
<span>{$i18n.t('Back')}</span>
|
|
</button>
|
|
</div>
|
|
<div
|
|
role="tablist"
|
|
aria-label={team ? $i18n.t('Edit Team') : $i18n.t('Create Team')}
|
|
class="tabs flex min-w-0 gap-px overflow-x-auto p-1 scrollbar-none md:flex-col"
|
|
>
|
|
<button
|
|
type="button"
|
|
role="tab"
|
|
aria-selected={selectedTab === 'general'}
|
|
class={tabButtonClass(selectedTab === 'general')}
|
|
on:click={() => (selectedTab = 'general')}
|
|
>
|
|
<SettingsAlt className="size-3.5" strokeWidth="2" />
|
|
<span>{$i18n.t('General')}</span>
|
|
</button>
|
|
<button
|
|
type="button"
|
|
role="tab"
|
|
aria-selected={selectedTab === 'permissions'}
|
|
class={tabButtonClass(selectedTab === 'permissions')}
|
|
on:click={() => (selectedTab = 'permissions')}
|
|
>
|
|
<Lock className="size-3.5" strokeWidth="2" />
|
|
<span>{$i18n.t('Permissions')}</span>
|
|
</button>
|
|
</div>
|
|
</nav>
|
|
{/if}
|
|
|
|
<div class={canSetPermissions ? 'flex min-h-0 min-w-0 flex-1 flex-col' : ''}>
|
|
<div class="flex shrink-0 justify-between px-4 pt-3 pb-1 dark:text-base-300">
|
|
<div class="self-center text-sm">
|
|
{#if canSetPermissions}
|
|
{selectedTab === 'permissions' ? $i18n.t('Permissions') : $i18n.t('General')}
|
|
{:else}
|
|
{team ? $i18n.t('Edit Team') : $i18n.t('Create Team')}
|
|
{/if}
|
|
</div>
|
|
{#if !canSetPermissions}
|
|
<button
|
|
aria-label={$i18n.t('Close')}
|
|
class="self-center rounded-lg px-0 py-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>
|
|
{/if}
|
|
</div>
|
|
|
|
<div
|
|
class="flex w-full px-4 dark:text-base-200 {canSetPermissions
|
|
? 'min-h-0 flex-1 overflow-y-auto'
|
|
: ''}"
|
|
>
|
|
<form id="team-form" class="flex w-full flex-col" on:submit|preventDefault={submitHandler}>
|
|
<div class="flex flex-col" class:hidden={selectedTab !== 'general'}>
|
|
<div class="mt-1 flex w-full flex-col">
|
|
<label for="team-name" class="mb-1 block text-xs text-base-500">{$i18n.t('Name')}</label
|
|
>
|
|
<div class="flex items-center gap-2">
|
|
<EmojiPicker
|
|
side="bottom"
|
|
selected={icon || null}
|
|
onSubmit={(value: string | null) => (icon = value ?? '')}
|
|
>
|
|
<button
|
|
type="button"
|
|
class="flex size-7 shrink-0 items-center justify-center rounded-full text-base-500 hover:bg-base-50/40 dark:text-base-400 dark:hover:bg-base-800/40"
|
|
aria-label={$i18n.t('Change team icon')}
|
|
title={$i18n.t('Change team icon')}
|
|
disabled={loading}
|
|
>
|
|
<TeamIcon team={{ meta: { icon } }} />
|
|
</button>
|
|
</EmojiPicker>
|
|
<input
|
|
id="team-name"
|
|
class="min-w-0 flex-1 bg-transparent text-sm outline-hidden placeholder:text-base-300 dark:placeholder:text-base-700"
|
|
type="text"
|
|
bind:value={name}
|
|
placeholder={$i18n.t('Enter team name')}
|
|
autocomplete="off"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- A short key like a Jira project key; the server checks the format and that it's unique. -->
|
|
<div class="mt-2">
|
|
<label for="team-identifier" class="mb-1 block text-xs text-base-500"
|
|
>{$i18n.t('Identifier')}</label
|
|
>
|
|
<input
|
|
id="team-identifier"
|
|
class="w-full bg-transparent text-sm uppercase outline-hidden placeholder:text-base-300 placeholder:normal-case dark:placeholder:text-base-700"
|
|
type="text"
|
|
bind:value={identifier}
|
|
placeholder={$i18n.t('A short code for the team, like ENG')}
|
|
maxlength={10}
|
|
autocomplete="off"
|
|
spellcheck="false"
|
|
/>
|
|
</div>
|
|
|
|
<div class="my-2">
|
|
<label for="team-description" class="mb-1 block text-xs text-base-500">
|
|
{$i18n.t('Description')}
|
|
</label>
|
|
<Textarea
|
|
id="team-description"
|
|
ariaLabel={$i18n.t('Description')}
|
|
className="w-full bg-transparent text-sm outline-hidden placeholder:text-base-300 dark:placeholder:text-base-700"
|
|
placeholder={$i18n.t('Enter description')}
|
|
maxSize={200}
|
|
bind:value={description}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
{#if canSetPermissions}
|
|
<!-- The Settings rows: label and control on one line, the description under them, spaced like UserSettingSection. -->
|
|
<div class="mt-1 mb-1 flex flex-col gap-2" class:hidden={selectedTab !== 'permissions'}>
|
|
<UserSettingRow
|
|
label={$i18n.t('settings.team.access.label')}
|
|
description={$i18n.t('settings.team.access.description')}
|
|
let:labelId
|
|
>
|
|
<SettingsSelect
|
|
id={`${labelId}-control`}
|
|
aria-labelledby={labelId}
|
|
bind:value={isPrivate}
|
|
>
|
|
<option value={false}>{$i18n.t('Public')}</option>
|
|
<option value={true}>{$i18n.t('Private')}</option>
|
|
</SettingsSelect>
|
|
</UserSettingRow>
|
|
{#each permissionRows as row (row.key)}
|
|
<UserSettingRow label={row.label} description={row.description} let:labelId>
|
|
<SettingsSelect
|
|
id={`${labelId}-control`}
|
|
aria-labelledby={labelId}
|
|
value={permissions[row.key] ?? 'owner'}
|
|
on:change={(e) =>
|
|
(permissions = {
|
|
...permissions,
|
|
[row.key]: (e.target as HTMLSelectElement).value as TeamRole
|
|
})}
|
|
>
|
|
<option value="member">{$i18n.t('Members')}</option>
|
|
<option value="owner">{$i18n.t('Owners')}</option>
|
|
</SettingsSelect>
|
|
</UserSettingRow>
|
|
{/each}
|
|
<p class="mt-2 text-[0.6875rem] text-base-500 dark:text-base-400">
|
|
{$i18n.t('settings.team.accessAndPermissions.description')}
|
|
</p>
|
|
</div>
|
|
{/if}
|
|
</form>
|
|
</div>
|
|
|
|
<div class="flex shrink-0 justify-end gap-1.5 px-4 pt-3 pb-4 text-sm font-normal">
|
|
<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"
|
|
form="team-form"
|
|
disabled={loading}
|
|
>
|
|
{$i18n.t('Save')}
|
|
{#if loading}
|
|
<div class="shrink-0"><Spinner className="size-3" /></div>
|
|
{/if}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</Modal>
|