open-webui/src/lib/components/admin/Users/Groups/EditGroupModal.svelte

238 lines
6.4 KiB
Svelte
Raw Normal View History

2024-11-13 11:09:46 +00:00
<script lang="ts">
import { toast } from 'svelte-sonner';
import { getContext, onMount } from 'svelte';
const i18n = getContext('i18n');
import Modal from '$lib/components/common/Modal.svelte';
import Plus from '$lib/components/icons/Plus.svelte';
import Minus from '$lib/components/icons/Minus.svelte';
import PencilSolid from '$lib/components/icons/PencilSolid.svelte';
import SensitiveInput from '$lib/components/common/SensitiveInput.svelte';
import Tooltip from '$lib/components/common/Tooltip.svelte';
import Switch from '$lib/components/common/Switch.svelte';
2024-11-14 05:21:50 +00:00
import Display from './Display.svelte';
import Permissions from './Permissions.svelte';
import Users from './Users.svelte';
2024-11-13 11:09:46 +00:00
export let onSubmit: Function = () => {};
export let onDelete: Function = () => {};
export let show = false;
export let edit = false;
2024-11-14 10:20:34 +00:00
export let users = [];
2024-11-13 11:09:46 +00:00
export let group = null;
2024-11-14 10:20:34 +00:00
export let custom = true;
export let tabs = ['display', 'permissions', 'users'];
2024-11-14 05:21:50 +00:00
let selectedTab = 'display';
2024-11-13 11:09:46 +00:00
let name = '';
let description = '';
2024-11-14 05:21:50 +00:00
2024-11-13 11:09:46 +00:00
let permissions = {};
2024-11-14 05:21:50 +00:00
let userIds = [];
2024-11-14 10:20:34 +00:00
let adminIds = [];
2024-11-13 11:09:46 +00:00
let loading = false;
const submitHandler = async () => {
loading = true;
const group = {
name,
description,
2024-11-14 05:21:50 +00:00
permissions,
user_ids: userIds
2024-11-13 11:09:46 +00:00
};
await onSubmit(group);
loading = false;
show = false;
name = '';
permissions = {};
2024-11-14 05:21:50 +00:00
userIds = [];
2024-11-13 11:09:46 +00:00
};
const init = () => {
if (group) {
name = group.name;
description = group.description;
permissions = group?.permissions ?? {};
2024-11-14 05:21:50 +00:00
userIds = group?.user_ids ?? [];
2024-11-13 11:09:46 +00:00
}
};
$: if (show) {
init();
}
onMount(() => {
2024-11-14 10:20:34 +00:00
console.log(tabs);
selectedTab = tabs[0];
2024-11-13 11:09:46 +00:00
init();
});
</script>
2024-11-14 05:21:50 +00:00
<Modal size="sm" bind:show>
2024-11-13 11:09:46 +00:00
<div>
2024-11-14 10:20:34 +00:00
<div class=" flex justify-between dark:text-gray-100 px-5 pt-4 mb-1.5">
2024-11-13 11:09:46 +00:00
<div class=" text-lg font-medium self-center font-primary">
2024-11-14 10:20:34 +00:00
{#if custom}
{#if edit}
{$i18n.t('Edit User Group')}
{:else}
{$i18n.t('Add User Group')}
{/if}
2024-11-13 11:09:46 +00:00
{:else}
2024-11-14 10:20:34 +00:00
{$i18n.t('Edit Default Permissions')}
2024-11-13 11:09:46 +00:00
{/if}
</div>
<button
class="self-center"
on:click={() => {
show = false;
}}
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
class="w-5 h-5"
>
<path
d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z"
/>
</svg>
</button>
</div>
<div class="flex flex-col md:flex-row w-full px-4 pb-4 md:space-x-4 dark:text-gray-200">
<div class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6">
<form
class="flex flex-col w-full"
on:submit={(e) => {
e.preventDefault();
submitHandler();
}}
>
2024-11-14 05:21:50 +00:00
<div
2024-11-14 10:20:34 +00:00
class=" tabs flex flex-row overflow-x-auto gap-2.5 text-sm font-medium border-b border-b-gray-800 scrollbar-hidden"
2024-11-14 05:21:50 +00:00
>
2024-11-14 10:20:34 +00:00
{#if tabs.includes('display')}
<button
class="px-0.5 pb-1.5 min-w-fit flex text-right transition border-b-2 {selectedTab ===
'display'
? ' dark:border-white'
: 'border-transparent text-gray-300 dark:text-gray-600 hover:text-gray-700 dark:hover:text-white'}"
on:click={() => {
selectedTab = 'display';
}}
type="button"
>
{$i18n.t('Display')}
</button>
{/if}
2024-11-14 05:21:50 +00:00
2024-11-14 10:20:34 +00:00
{#if tabs.includes('permissions')}
<button
class="px-0.5 pb-1.5 min-w-fit flex text-right transition border-b-2 {selectedTab ===
'permissions'
? ' dark:border-white'
: 'border-transparent text-gray-300 dark:text-gray-600 hover:text-gray-700 dark:hover:text-white'}"
on:click={() => {
selectedTab = 'permissions';
}}
type="button"
>
{$i18n.t('Permissions')}
</button>
{/if}
2024-11-14 05:21:50 +00:00
2024-11-14 10:20:34 +00:00
{#if tabs.includes('users')}
<button
class="px-0.5 pb-1.5 min-w-fit flex text-right transition border-b-2 {selectedTab ===
'users'
? ' dark:border-white'
: ' border-transparent text-gray-300 dark:text-gray-600 hover:text-gray-700 dark:hover:text-white'}"
on:click={() => {
selectedTab = 'users';
}}
type="button"
>
{$i18n.t('Users')} ({userIds.length})
</button>
{/if}
2024-11-14 05:21:50 +00:00
</div>
2024-11-14 10:20:34 +00:00
<div class="px-1 h-96 lg:max-h-96 overflow-y-auto scrollbar-hidden mt-2.5">
2024-11-14 05:21:50 +00:00
{#if selectedTab == 'display'}
<Display bind:name bind:description />
{:else if selectedTab == 'permissions'}
2024-11-14 10:20:34 +00:00
<Permissions bind:permissions {custom} />
2024-11-14 05:21:50 +00:00
{:else if selectedTab == 'users'}
2024-11-14 10:20:34 +00:00
<Users bind:userIds bind:adminIds {users} />
2024-11-14 05:21:50 +00:00
{/if}
2024-11-13 11:09:46 +00:00
</div>
<div class="flex justify-end pt-3 text-sm font-medium gap-1.5">
{#if edit}
<button
class="px-3.5 py-1.5 text-sm font-medium dark:bg-black dark:hover:bg-gray-900 dark:text-white bg-white text-black hover:bg-gray-100 transition rounded-full flex flex-row space-x-1 items-center"
type="button"
on:click={() => {
onDelete();
show = false;
}}
>
{$i18n.t('Delete')}
</button>
{/if}
<button
class="px-3.5 py-1.5 text-sm font-medium bg-black hover:bg-gray-900 text-white dark:bg-white dark:text-black dark:hover:bg-gray-100 transition rounded-full flex flex-row space-x-1 items-center {loading
? ' cursor-not-allowed'
: ''}"
type="submit"
disabled={loading}
>
{$i18n.t('Save')}
{#if loading}
<div class="ml-2 self-center">
<svg
class=" w-4 h-4"
viewBox="0 0 24 24"
fill="currentColor"
xmlns="http://www.w3.org/2000/svg"
><style>
.spinner_ajPY {
transform-origin: center;
animation: spinner_AtaB 0.75s infinite linear;
}
@keyframes spinner_AtaB {
100% {
transform: rotate(360deg);
}
}
</style><path
d="M12,1A11,11,0,1,0,23,12,11,11,0,0,0,12,1Zm0,19a8,8,0,1,1,8-8A8,8,0,0,1,12,20Z"
opacity=".25"
/><path
d="M10.14,1.16a11,11,0,0,0-9,8.92A1.59,1.59,0,0,0,2.46,12,1.52,1.52,0,0,0,4.11,10.7a8,8,0,0,1,6.66-6.61A1.42,1.42,0,0,0,12,2.69h0A1.57,1.57,0,0,0,10.14,1.16Z"
class="spinner_ajPY"
/></svg
>
</div>
{/if}
</button>
</div>
</form>
</div>
</div>
</div>
</Modal>