|
2 | 2 | import WorkflowPickerItemCard from '$lib/components/workflows/WorkflowPickerItemCard.svelte'; |
3 | 3 | import AlbumPickerModal from '$lib/modals/AlbumPickerModal.svelte'; |
4 | 4 | import PeoplePickerModal from '$lib/modals/PeoplePickerModal.svelte'; |
| 5 | + import { fetchPickerMetadata, type PickerMetadata } from '$lib/services/workflow.service'; |
5 | 6 | import type { ComponentConfig } from '$lib/utils/workflow'; |
6 | | - import { getAlbumInfo, getPerson, type AlbumResponseDto, type PersonResponseDto } from '@immich/sdk'; |
| 7 | + import type { AlbumResponseDto, PersonResponseDto } from '@immich/sdk'; |
7 | 8 | import { Button, Field, modalManager } from '@immich/ui'; |
8 | 9 | import { mdiPlus } from '@mdi/js'; |
9 | 10 | import { t } from 'svelte-i18n'; |
|
22 | 23 | const isAlbum = $derived(subType === 'album-picker'); |
23 | 24 | const multiple = $derived(component.type === 'multiselect' || Array.isArray(value)); |
24 | 25 |
|
25 | | - let pickerMetadata = $state<AlbumResponseDto | PersonResponseDto | AlbumResponseDto[] | PersonResponseDto[]>(); |
| 26 | + let pickerMetadata = $state<PickerMetadata | undefined>(); |
26 | 27 |
|
27 | 28 | $effect(() => { |
28 | 29 | if (!value) { |
29 | 30 | pickerMetadata = undefined; |
30 | 31 | return; |
31 | 32 | } |
32 | 33 |
|
33 | | - void fetchMetadata(); |
34 | | - }); |
35 | | -
|
36 | | - const fetchMetadata = async () => { |
37 | | - if (!value || pickerMetadata) { |
38 | | - return; |
| 34 | + if (!pickerMetadata) { |
| 35 | + void loadMetadata(); |
39 | 36 | } |
| 37 | + }); |
40 | 38 |
|
41 | | - try { |
42 | | - if (Array.isArray(value) && value.length > 0) { |
43 | | - // Multiple selection |
44 | | - pickerMetadata = await (isAlbum |
45 | | - ? Promise.all(value.map((id) => getAlbumInfo({ id }))) |
46 | | - : Promise.all(value.map((id) => getPerson({ id })))); |
47 | | - } else if (typeof value === 'string' && value) { |
48 | | - // Single selection |
49 | | - pickerMetadata = await (isAlbum ? getAlbumInfo({ id: value }) : getPerson({ id: value })); |
50 | | - } |
51 | | - } catch (error) { |
52 | | - console.error(`Failed to fetch metadata for ${configKey}:`, error); |
53 | | - } |
| 39 | + const loadMetadata = async () => { |
| 40 | + pickerMetadata = await fetchPickerMetadata(value, subType); |
54 | 41 | }; |
55 | 42 |
|
56 | 43 | const handlePicker = async () => { |
57 | 44 | if (isAlbum) { |
58 | 45 | const albums = await modalManager.show(AlbumPickerModal, { shared: false }); |
59 | 46 | if (albums && albums.length > 0) { |
60 | | - const newValue = multiple ? albums.map((a) => a.id) : albums[0].id; |
| 47 | + const newValue = multiple ? albums.map((album) => album.id) : albums[0].id; |
61 | 48 | onchange(newValue); |
62 | 49 | pickerMetadata = multiple ? albums : albums[0]; |
63 | 50 | } |
|
66 | 53 | const excludedIds = multiple ? currentIds : []; |
67 | 54 | const people = await modalManager.show(PeoplePickerModal, { multiple, excludedIds }); |
68 | 55 | if (people && people.length > 0) { |
69 | | - const newValue = multiple ? people.map((p) => p.id) : people[0].id; |
| 56 | + const newValue = multiple ? people.map((person) => person.id) : people[0].id; |
70 | 57 | onchange(newValue); |
71 | 58 | pickerMetadata = multiple ? people : people[0]; |
72 | 59 | } |
|
0 commit comments