refactor: api key modals (#23420)

This commit is contained in:
Daniel Dietzler
2025-10-31 13:58:52 +01:00
committed by GitHub
parent 4abaad548a
commit 3531856d1c
5 changed files with 216 additions and 175 deletions

View File

@@ -1,10 +1,11 @@
<script lang="ts">
import { dateFormats } from '$lib/constants';
import ApiKeyModal from '$lib/modals/ApiKeyModal.svelte';
import ApiKeyCreateModal from '$lib/modals/ApiKeyCreateModal.svelte';
import ApiKeySecretModal from '$lib/modals/ApiKeySecretModal.svelte';
import ApiKeyUpdateModal from '$lib/modals/ApiKeyUpdateModal.svelte';
import { locale } from '$lib/stores/preferences.store';
import { handleError } from '$lib/utils/handle-error';
import { createApiKey, deleteApiKey, getApiKeys, updateApiKey, type ApiKeyResponseDto } from '@immich/sdk';
import { deleteApiKey, getApiKeys, type ApiKeyResponseDto } from '@immich/sdk';
import { Button, IconButton, modalManager, toastManager } from '@immich/ui';
import { mdiPencilOutline, mdiTrashCanOutline } from '@mdi/js';
import { t } from 'svelte-i18n';
@@ -21,49 +22,22 @@
}
const handleCreate = async () => {
const result = await modalManager.show(ApiKeyModal, {
title: $t('new_api_key'),
apiKey: { name: 'API Key', permissions: [] },
submitText: $t('create'),
});
const secret = await modalManager.show(ApiKeyCreateModal);
if (!result) {
if (!secret) {
return;
}
try {
const { secret } = await createApiKey({
apiKeyCreateDto: {
name: result.name,
permissions: result.permissions,
},
});
await modalManager.show(ApiKeySecretModal, { secret });
} catch (error) {
handleError(error, $t('errors.unable_to_create_api_key'));
} finally {
await refreshKeys();
}
await modalManager.show(ApiKeySecretModal, { secret });
await refreshKeys();
};
const handleUpdate = async (key: ApiKeyResponseDto) => {
const result = await modalManager.show(ApiKeyModal, {
title: $t('api_key'),
submitText: $t('save'),
const success = await modalManager.show(ApiKeyUpdateModal, {
apiKey: key,
});
if (!result) {
return;
}
try {
await updateApiKey({ id: key.id, apiKeyUpdateDto: { name: result.name, permissions: result.permissions } });
toastManager.success($t('saved_api_key'));
} catch (error) {
handleError(error, $t('errors.unable_to_save_api_key'));
} finally {
if (success) {
await refreshKeys();
}
};