immich/web/src/lib/components/user-settings-page/user-api-key-list.svelte

159 lines
5 KiB
Svelte
Raw Normal View History

<script lang="ts">
import CircleIconButton from '$lib/components/elements/buttons/circle-icon-button.svelte';
import { dialogController } from '$lib/components/shared-components/dialog/dialog';
2025-05-07 23:58:46 +02:00
import { modalManager } from '$lib/managers/modal-manager.svelte';
import ApiKeyModal from '$lib/modals/ApiKeyModal.svelte';
import ApiKeySecretModal from '$lib/modals/ApiKeySecretModal.svelte';
import { locale } from '$lib/stores/preferences.store';
import {
createApiKey,
deleteApiKey,
getApiKeys,
Permission,
updateApiKey,
type ApiKeyResponseDto,
} from '@immich/sdk';
import { Button } from '@immich/ui';
import { mdiPencilOutline, mdiTrashCanOutline } from '@mdi/js';
import { t } from 'svelte-i18n';
import { fade } from 'svelte/transition';
import { handleError } from '../../utils/handle-error';
import { notificationController, NotificationType } from '../shared-components/notification/notification';
interface Props {
keys: ApiKeyResponseDto[];
}
let { keys = $bindable() }: Props = $props();
const format: Intl.DateTimeFormatOptions = {
month: 'short',
day: 'numeric',
year: 'numeric',
};
async function refreshKeys() {
keys = await getApiKeys();
}
2025-05-07 23:58:46 +02:00
const handleCreate = async () => {
2025-05-08 00:08:19 +02:00
const result = await modalManager.show(ApiKeyModal, {
2025-05-07 23:58:46 +02:00
title: $t('new_api_key'),
apiKey: { name: 'API Key' },
submitText: $t('create'),
});
if (!result) {
return;
}
try {
2025-05-07 23:58:46 +02:00
const { secret } = await createApiKey({
apiKeyCreateDto: {
2025-05-07 23:58:46 +02:00
name: result.name,
permissions: [Permission.All],
},
});
2025-05-07 23:58:46 +02:00
2025-05-08 00:08:19 +02:00
await modalManager.show(ApiKeySecretModal, { secret });
} catch (error) {
handleError(error, $t('errors.unable_to_create_api_key'));
} finally {
await refreshKeys();
}
};
2025-05-07 23:58:46 +02:00
const handleUpdate = async (key: ApiKeyResponseDto) => {
2025-05-08 00:08:19 +02:00
const result = await modalManager.show(ApiKeyModal, {
2025-05-07 23:58:46 +02:00
title: $t('api_key'),
submitText: $t('save'),
apiKey: key,
});
if (!result) {
return;
}
try {
2025-05-07 23:58:46 +02:00
await updateApiKey({ id: key.id, apiKeyUpdateDto: { name: result.name } });
notificationController.show({
message: $t('saved_api_key'),
type: NotificationType.Info,
});
} catch (error) {
handleError(error, $t('errors.unable_to_save_api_key'));
} finally {
await refreshKeys();
}
};
const handleDelete = async (key: ApiKeyResponseDto) => {
const isConfirmed = await dialogController.show({ prompt: $t('delete_api_key_prompt') });
if (!isConfirmed) {
return;
}
try {
await deleteApiKey({ id: key.id });
notificationController.show({
message: $t('removed_api_key', { values: { name: key.name } }),
type: NotificationType.Info,
});
} catch (error) {
handleError(error, $t('errors.unable_to_remove_api_key'));
} finally {
await refreshKeys();
}
};
</script>
<section class="my-4">
<div class="flex flex-col gap-2" in:fade={{ duration: 500 }}>
<div class="mb-2 flex justify-end">
2025-05-07 23:58:46 +02:00
<Button shape="round" size="small" onclick={() => handleCreate()}>{$t('new_api_key')}</Button>
</div>
{#if keys.length > 0}
2025-04-28 09:53:53 -04:00
<table class="w-full text-start">
<thead
class="mb-4 flex h-12 w-full rounded-md border bg-gray-50 text-immich-primary dark:border-immich-dark-gray dark:bg-immich-dark-gray dark:text-immich-dark-primary"
>
<tr class="flex w-full place-items-center">
feat(web): translations (#9854) * First test * Added translation using Weblate (French) * Translated using Weblate (German) Currently translated at 100.0% (4 of 4 strings) Translation: immich/web Translate-URL: http://familie-mach.net/projects/immich/web/de/ * Translated using Weblate (French) Currently translated at 100.0% (4 of 4 strings) Translation: immich/web Translate-URL: http://familie-mach.net/projects/immich/web/fr/ * Further testing * Further testing * Translated using Weblate (German) Currently translated at 100.0% (18 of 18 strings) Translation: immich/web Translate-URL: http://familie-mach.net/projects/immich/web/de/ * Further work * Update string file. * More strings * Automatically changed strings * Add automatically translated german file for testing purposes * Fix merge-face-selector component * Make server stats strings uppercase * Fix uppercase string * Fix some strings in jobs-panel * Fix lower and uppercase strings. Add a few additional string. Fix a few unnecessary replacements * Update german test translations * Fix typo in locales file * Change string keys * Extract more strings * Extract and replace some more strings * Update testtranslationfile * Change translation keys * Fix rebase errors * Fix one more rebase error * Remove german translation file * Co-authored-by: Daniel Dietzler <danieldietzler@users.noreply.github.com> * chore: clean up translations * chore: add new line * fix formatting * chore: fixes * fix: loading and tests --------- Co-authored-by: root <root@Blacki> Co-authored-by: admin <admin@example.com> Co-authored-by: Jason Rasmussen <jrasm91@gmail.com> Co-authored-by: Daniel Dietzler <mail@ddietzler.dev>
2024-06-04 21:53:00 +02:00
<th class="w-1/3 text-center text-sm font-medium">{$t('name')}</th>
<th class="w-1/3 text-center text-sm font-medium">{$t('created')}</th>
<th class="w-1/3 text-center text-sm font-medium">{$t('action')}</th>
</tr>
</thead>
<tbody class="block w-full overflow-y-auto rounded-md border dark:border-immich-dark-gray">
{#each keys as key (key.id)}
<tr
class="flex h-[80px] w-full place-items-center text-center dark:text-immich-dark-fg even:bg-subtle/20 odd:bg-subtle/80"
>
<td class="w-1/3 text-ellipsis px-4 text-sm">{key.name}</td>
<td class="w-1/3 text-ellipsis px-4 text-sm"
>{new Date(key.createdAt).toLocaleDateString($locale, format)}
</td>
<td class="flex flex-row flex-wrap justify-center gap-x-2 gap-y-1 w-1/3">
<CircleIconButton
color="primary"
icon={mdiPencilOutline}
title={$t('edit_key')}
size="16"
2025-05-07 23:58:46 +02:00
onclick={() => handleUpdate(key)}
/>
<CircleIconButton
color="primary"
icon={mdiTrashCanOutline}
title={$t('delete_key')}
size="16"
onclick={() => handleDelete(key)}
/>
</td>
</tr>
{/each}
</tbody>
</table>
{/if}
</div>
</section>