mirror of
https://github.com/open-webui/open-webui.git
synced 2026-12-01 17:32:52 -05:00
refac
This commit is contained in:
1 parent
db1ca1afab
commit
b3ce7de8d3
12 files changed
+199
-7
No files matched your search
@@ -1,6 +1,6 @@
|
||||
<script lang="ts">
|
||||
import { getContext } from 'svelte';
|
||||
const i18n = getContext('i18n');
|
||||
const i18n = getContext<typeof import('$lib/i18n').default>('i18n');
|
||||
|
||||
import Modal from '$lib/components/common/Modal.svelte';
|
||||
import AccessControl from './AccessControl.svelte';
|
||||
@@ -14,6 +14,7 @@
|
||||
};
|
||||
|
||||
export let show = false;
|
||||
export let disabled = false;
|
||||
export let accessGrants: AccessGrant[] = [];
|
||||
export let accessControl: any = undefined;
|
||||
export let accessRoles = ['read'];
|
||||
@@ -24,7 +25,7 @@
|
||||
export let shareUsers = true;
|
||||
export let allowGroups = true;
|
||||
|
||||
export let onChange = () => {};
|
||||
export let onChange: (grants: AccessGrant[]) => void | Promise<void> = () => {};
|
||||
</script>
|
||||
|
||||
<Modal size="sm" bind:show>
|
||||
@@ -43,7 +44,7 @@
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="w-full px-4 pb-3 dark:text-white">
|
||||
<div class="w-full px-4 pb-3 dark:text-white" inert={disabled} aria-busy={disabled}>
|
||||
<AccessControl
|
||||
bind:accessGrants
|
||||
bind:accessControl
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
<script lang="ts">
|
||||
import { getContext } from 'svelte';
|
||||
import { toast } from 'svelte-sonner';
|
||||
import { user } from '$lib/stores';
|
||||
import { getModelById, updateModelAccessGrants } from '$lib/apis/models';
|
||||
import { getKnowledgeById, updateKnowledgeAccessGrants } from '$lib/apis/knowledge';
|
||||
import { getPromptById, updatePromptAccessGrants } from '$lib/apis/prompts';
|
||||
import { getSkillById, updateSkillAccessGrants } from '$lib/apis/skills';
|
||||
import { getToolById, updateToolAccessGrants } from '$lib/apis/tools';
|
||||
import AccessControlModal from './AccessControlModal.svelte';
|
||||
|
||||
export let resourceType: 'models' | 'knowledge' | 'prompts' | 'skills' | 'tools';
|
||||
export let onUpdated: () => void | Promise<void> = () => {};
|
||||
const i18n = getContext<any>('i18n');
|
||||
const loaders = {
|
||||
models: getModelById,
|
||||
knowledge: getKnowledgeById,
|
||||
prompts: getPromptById,
|
||||
skills: getSkillById,
|
||||
tools: getToolById
|
||||
};
|
||||
const savers = {
|
||||
knowledge: updateKnowledgeAccessGrants,
|
||||
prompts: updatePromptAccessGrants,
|
||||
skills: updateSkillAccessGrants,
|
||||
tools: updateToolAccessGrants
|
||||
};
|
||||
let show = false;
|
||||
let saving = false;
|
||||
let resource: any = null;
|
||||
let accessGrants: any[] = [];
|
||||
let savedGrants: any[] = [];
|
||||
let requestId = 0;
|
||||
const reportError = (error: any) => toast.error(`${error?.detail ?? error}`);
|
||||
|
||||
export const open = async (id: string) => {
|
||||
const request = ++requestId;
|
||||
show = false;
|
||||
try {
|
||||
const result = await loaders[resourceType](localStorage.token, id);
|
||||
if (request !== requestId) return;
|
||||
resource = result;
|
||||
accessGrants = structuredClone(result.access_grants ?? []);
|
||||
savedGrants = structuredClone(accessGrants);
|
||||
saving = false;
|
||||
show = true;
|
||||
} catch (error) {
|
||||
if (request === requestId) reportError(error);
|
||||
}
|
||||
};
|
||||
|
||||
const save = async (grants: any[]) => {
|
||||
if (saving || !resource) return;
|
||||
const request = requestId;
|
||||
saving = true;
|
||||
try {
|
||||
const updated =
|
||||
resourceType === 'models'
|
||||
? await updateModelAccessGrants(localStorage.token, resource.id, resource.name, grants)
|
||||
: await savers[resourceType](localStorage.token, resource.id, grants);
|
||||
if (request === requestId) {
|
||||
accessGrants = structuredClone(updated.access_grants ?? []);
|
||||
savedGrants = structuredClone(accessGrants);
|
||||
}
|
||||
toast.success($i18n.t('Saved'));
|
||||
} catch (error) {
|
||||
if (request === requestId) accessGrants = structuredClone(savedGrants);
|
||||
reportError(error);
|
||||
return;
|
||||
} finally {
|
||||
if (request === requestId) saving = false;
|
||||
}
|
||||
try {
|
||||
await onUpdated();
|
||||
} catch (error) {
|
||||
reportError(error);
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<AccessControlModal
|
||||
bind:show
|
||||
bind:accessGrants
|
||||
disabled={saving}
|
||||
accessRoles={resourceType === 'models' && !resource?.base_model_id ? ['read'] : ['read', 'write']}
|
||||
share={$user?.permissions?.sharing?.[resourceType] || $user?.role === 'admin'}
|
||||
sharePublic={$user?.permissions?.sharing?.[`public_${resourceType}`] || $user?.role === 'admin'}
|
||||
shareUsers={($user?.permissions?.access_grants?.allow_users ?? true) || $user?.role === 'admin'}
|
||||
allowGroups={($user?.permissions?.access_grants?.allow_groups ?? true) || $user?.role === 'admin'}
|
||||
onChange={save}
|
||||
/>
|
||||
Reference in new issue
Block a user