|
1 | 1 | <script lang="ts"> |
2 | | - import { formatLabel, getComponentFromSchema } from '$lib/utils/workflow'; |
| 2 | + import { formatLabel, getComponentFromSchema, type ComponentConfig } from '$lib/utils/workflow'; |
3 | 3 | import { Field, Input, MultiSelect, Select, Switch, Text, type SelectItem } from '@immich/ui'; |
4 | 4 | import WorkflowPickerField from './WorkflowPickerField.svelte'; |
5 | 5 |
|
|
25 | 25 | config = configKey ? { ...config, [configKey]: { ...actualConfig, ...updates } } : { ...config, ...updates }; |
26 | 26 | }; |
27 | 27 |
|
| 28 | + // Helper to determine default value for a component based on its type |
| 29 | + const getDefaultValue = (component: ComponentConfig): unknown => { |
| 30 | + if (component.defaultValue !== undefined) { |
| 31 | + return component.defaultValue; |
| 32 | + } |
| 33 | +
|
| 34 | + // Initialize with appropriate empty value based on component type |
| 35 | + if (component.type === 'multiselect' || (component.type === 'text' && component.subType === 'people-picker')) { |
| 36 | + return []; |
| 37 | + } |
| 38 | +
|
| 39 | + if (component.type === 'switch') { |
| 40 | + return false; |
| 41 | + } |
| 42 | +
|
| 43 | + return ''; |
| 44 | + }; |
| 45 | +
|
| 46 | + // Derive which keys need initialization (missing from actualConfig) |
| 47 | + const uninitializedKeys = $derived.by(() => { |
| 48 | + if (!components) { |
| 49 | + return []; |
| 50 | + } |
| 51 | +
|
| 52 | + return Object.entries(components) |
| 53 | + .filter(([key]) => actualConfig[key] === undefined) |
| 54 | + .map(([key, component]) => ({ key, component, defaultValue: getDefaultValue(component) })); |
| 55 | + }); |
| 56 | +
|
| 57 | + // Derive the batch updates needed |
| 58 | + const pendingUpdates = $derived.by(() => { |
| 59 | + const updates: Record<string, unknown> = {}; |
| 60 | + for (const { key, defaultValue } of uninitializedKeys) { |
| 61 | + updates[key] = defaultValue; |
| 62 | + } |
| 63 | + return updates; |
| 64 | + }); |
| 65 | +
|
28 | 66 | let selectValue = $state<SelectItem>(); |
29 | 67 | let switchValue = $state<boolean>(false); |
30 | 68 | let multiSelectValue = $state<SelectItem[]>([]); |
31 | 69 |
|
| 70 | + // Initialize config namespace if needed |
32 | 71 | $effect(() => { |
33 | | - // Initialize config for actions/filters with empty schemas |
34 | 72 | if (configKey && !config[configKey]) { |
35 | 73 | config = { ...config, [configKey]: {} }; |
36 | 74 | } |
| 75 | + }); |
| 76 | +
|
| 77 | + // Apply pending config updates |
| 78 | + $effect(() => { |
| 79 | + if (Object.keys(pendingUpdates).length > 0) { |
| 80 | + updateConfigBatch(pendingUpdates); |
| 81 | + } |
| 82 | + }); |
| 83 | +
|
| 84 | + // Sync UI state for components with default values |
| 85 | + $effect(() => { |
| 86 | + for (const { component } of uninitializedKeys) { |
| 87 | + if (component.defaultValue === undefined) { |
| 88 | + continue; |
| 89 | + } |
37 | 90 |
|
38 | | - if (components) { |
39 | | - const updates: Record<string, unknown> = {}; |
40 | | -
|
41 | | - for (const [key, component] of Object.entries(components)) { |
42 | | - // Only initialize if the key doesn't exist in config yet |
43 | | - if (actualConfig[key] === undefined) { |
44 | | - // Use default value if available, otherwise use appropriate empty value based on type |
45 | | - const hasDefault = component.defaultValue !== undefined; |
46 | | -
|
47 | | - if (hasDefault) { |
48 | | - updates[key] = component.defaultValue; |
49 | | - } else { |
50 | | - // Initialize with appropriate empty value based on component type |
51 | | - if ( |
52 | | - component.type === 'multiselect' || |
53 | | - (component.type === 'text' && component.subType === 'people-picker') |
54 | | - ) { |
55 | | - updates[key] = []; |
56 | | - } else if (component.type === 'switch') { |
57 | | - updates[key] = false; |
58 | | - } else { |
59 | | - updates[key] = ''; |
60 | | - } |
61 | | - } |
62 | | -
|
63 | | - // Update UI state for components with default values |
64 | | - if (hasDefault) { |
65 | | - if (component.type === 'select') { |
66 | | - selectValue = { |
67 | | - label: formatLabel(String(component.defaultValue)), |
68 | | - value: String(component.defaultValue), |
69 | | - }; |
70 | | - } |
71 | | -
|
72 | | - if (component.type === 'switch') { |
73 | | - switchValue = Boolean(component.defaultValue); |
74 | | - } |
75 | | - } |
76 | | - } |
| 91 | + if (component.type === 'select') { |
| 92 | + selectValue = { |
| 93 | + label: formatLabel(String(component.defaultValue)), |
| 94 | + value: String(component.defaultValue), |
| 95 | + }; |
77 | 96 | } |
78 | 97 |
|
79 | | - if (Object.keys(updates).length > 0) { |
80 | | - updateConfigBatch(updates); |
| 98 | + if (component.type === 'switch') { |
| 99 | + switchValue = Boolean(component.defaultValue); |
81 | 100 | } |
82 | 101 | } |
83 | 102 | }); |
|
0 commit comments