|
| 1 | +<script lang="ts"> |
| 2 | + import { type AlbumResponseDto, getAllAlbums } from '@immich/sdk'; |
| 3 | + import { onMount } from 'svelte'; |
| 4 | + import AlbumListItem from '../../asset-viewer/album-list-item.svelte'; |
| 5 | + import NewAlbumListItem from './new-album-list-item.svelte'; |
| 6 | + import FullScreenModal from '$lib/components/shared-components/full-screen-modal.svelte'; |
| 7 | + import { initInput } from '$lib/actions/focus'; |
| 8 | + import { t } from 'svelte-i18n'; |
| 9 | + import { albumViewSettings } from '$lib/stores/preferences.store'; |
| 10 | + import { |
| 11 | + AlbumModalRowConverter, |
| 12 | + AlbumModalRowType, |
| 13 | + isSelectableRowType, |
| 14 | + } from '$lib/components/shared-components/album-selection/album-selection-utils'; |
| 15 | +
|
| 16 | + let albums: AlbumResponseDto[] = $state([]); |
| 17 | + let recentAlbums: AlbumResponseDto[] = $state([]); |
| 18 | + let loading = $state(true); |
| 19 | + let search = $state(''); |
| 20 | + let selectedRowIndex: number = $state(-1); |
| 21 | +
|
| 22 | + interface Props { |
| 23 | + onNewAlbum: (search: string) => void; |
| 24 | + onAlbumClick: (album: AlbumResponseDto) => void; |
| 25 | + shared: boolean; |
| 26 | + onClose: () => void; |
| 27 | + } |
| 28 | +
|
| 29 | + let { onNewAlbum, onAlbumClick, shared, onClose }: Props = $props(); |
| 30 | +
|
| 31 | + onMount(async () => { |
| 32 | + albums = await getAllAlbums({ shared: shared || undefined }); |
| 33 | + recentAlbums = albums.sort((a, b) => (new Date(a.createdAt) > new Date(b.createdAt) ? -1 : 1)).slice(0, 3); |
| 34 | + loading = false; |
| 35 | + }); |
| 36 | +
|
| 37 | + const rowConverter = new AlbumModalRowConverter(shared, $albumViewSettings.sortBy, $albumViewSettings.sortOrder); |
| 38 | + const albumModalRows = $derived(rowConverter.toModalRows(search, recentAlbums, albums, selectedRowIndex)); |
| 39 | + const selectableRowCount = $derived(albumModalRows.filter((row) => isSelectableRowType(row.type)).length); |
| 40 | +
|
| 41 | + const onkeydown = (e: KeyboardEvent) => { |
| 42 | + switch (e.key) { |
| 43 | + case 'ArrowUp': { |
| 44 | + e.preventDefault(); |
| 45 | + if (selectedRowIndex > 0) { |
| 46 | + selectedRowIndex--; |
| 47 | + } else { |
| 48 | + selectedRowIndex = selectableRowCount - 1; |
| 49 | + } |
| 50 | + break; |
| 51 | + } |
| 52 | + case 'ArrowDown': { |
| 53 | + e.preventDefault(); |
| 54 | + if (selectedRowIndex < selectableRowCount - 1) { |
| 55 | + selectedRowIndex++; |
| 56 | + } else { |
| 57 | + selectedRowIndex = 0; |
| 58 | + } |
| 59 | + break; |
| 60 | + } |
| 61 | + case 'Enter': { |
| 62 | + e.preventDefault(); |
| 63 | + const selectedRow = albumModalRows.find((row) => row.selected); |
| 64 | + if (selectedRow) { |
| 65 | + if (selectedRow.type === AlbumModalRowType.NEW_ALBUM) { |
| 66 | + onNewAlbum(search); |
| 67 | + } else if (selectedRow.type === AlbumModalRowType.ALBUM_ITEM && selectedRow.album) { |
| 68 | + onAlbumClick(selectedRow.album); |
| 69 | + } |
| 70 | + selectedRowIndex = -1; |
| 71 | + } |
| 72 | + break; |
| 73 | + } |
| 74 | + default: { |
| 75 | + selectedRowIndex = -1; |
| 76 | + } |
| 77 | + } |
| 78 | + }; |
| 79 | +
|
| 80 | + const handleAlbumClick = (album: AlbumResponseDto) => () => onAlbumClick(album); |
| 81 | +</script> |
| 82 | + |
| 83 | +<FullScreenModal title={shared ? $t('add_to_shared_album') : $t('add_to_album')} {onClose}> |
| 84 | + <div class="mb-2 flex max-h-[400px] flex-col"> |
| 85 | + {#if loading} |
| 86 | + {#each { length: 3 } as _} |
| 87 | + <div class="flex animate-pulse gap-4 px-6 py-2"> |
| 88 | + <div class="h-12 w-12 rounded-xl bg-slate-200"></div> |
| 89 | + <div class="flex flex-col items-start justify-center gap-2"> |
| 90 | + <span class="h-4 w-36 animate-pulse bg-slate-200"></span> |
| 91 | + <div class="flex animate-pulse gap-1"> |
| 92 | + <span class="h-3 w-8 bg-slate-200"></span> |
| 93 | + <span class="h-3 w-20 bg-slate-200"></span> |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + {/each} |
| 98 | + {:else} |
| 99 | + <input |
| 100 | + class="border-b-4 border-immich-bg bg-immich-bg px-6 py-2 text-2xl focus:border-immich-primary dark:border-immich-dark-gray dark:bg-immich-dark-gray dark:focus:border-immich-dark-primary" |
| 101 | + placeholder={$t('search')} |
| 102 | + {onkeydown} |
| 103 | + bind:value={search} |
| 104 | + use:initInput |
| 105 | + /> |
| 106 | + <div class="immich-scrollbar overflow-y-auto"> |
| 107 | + {#each albumModalRows as row} |
| 108 | + {#if row.type === AlbumModalRowType.NEW_ALBUM} |
| 109 | + <NewAlbumListItem selected={row.selected || false} {onNewAlbum} searchQuery={search} /> |
| 110 | + {:else if row.type === AlbumModalRowType.SECTION} |
| 111 | + <p class="px-5 py-3 text-xs">{row.text}</p> |
| 112 | + {:else if row.type === AlbumModalRowType.MESSAGE} |
| 113 | + <p class="px-5 py-1 text-sm">{row.text}</p> |
| 114 | + {:else if row.type === AlbumModalRowType.ALBUM_ITEM && row.album} |
| 115 | + <AlbumListItem |
| 116 | + album={row.album} |
| 117 | + selected={row.selected || false} |
| 118 | + searchQuery={search} |
| 119 | + onAlbumClick={handleAlbumClick(row.album)} |
| 120 | + /> |
| 121 | + {/if} |
| 122 | + {/each} |
| 123 | + </div> |
| 124 | + {/if} |
| 125 | + </div> |
| 126 | +</FullScreenModal> |
0 commit comments