summaryrefslogtreecommitdiffhomepage
path: root/yt-music-dl-web/src/features/settings/SettingsView.svelte
blob: 61861296027766d6705df33f64bdfba7e6cd8e79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<!--
	src/features/settings/SettingsView.svelte — thin component.

	Loads settings via the injected SettingsApi (GET /api/settings), routes every
	field edit through the pure `updateField` reducer, and saves via PUT
	/api/settings. A live `previewPath` shows where a file would land under the
	current template. Pure view-model (tokens, isDirty, previewPath) lives in
	logic.ts. No business logic here — only load/save/edit wiring.
-->
<script lang="ts">
	import type { Settings } from "@yt-music/contract";
	import type { SettingsApi } from "../../adapters/settings.js";
	import {
		DEFAULT_PATH_TEMPLATE,
		DEFAULT_PREVIEW_SAMPLE,
		EMPTY_SETTINGS,
		FORMAT_OPTIONS,
		PATH_TEMPLATE_TOKENS,
		isDirty,
		previewPath,
		updateField,
	} from "./logic.js";

	interface Props {
		api: SettingsApi;
	}
	let { api }: Props = $props();

	let original = $state<Settings | null>(null);
	let form = $state<Settings>({ ...EMPTY_SETTINGS });
	let loading = $state(true);
	let error = $state<string | undefined>(undefined);
	let saving = $state(false);
	let savedFlash = $state(false);

	let dirty = $derived(original !== null && isDirty(original, form));
	let canSave = $derived(dirty && !saving);
	let preview = $derived(previewPath(form.pathTemplate, DEFAULT_PREVIEW_SAMPLE));

	$effect(() => {
		const current = api;
		let cancelled = false;
		loading = true;
		error = undefined;
		current
			.fetchSettings()
			.then((s) => {
				if (cancelled) return;
				original = s;
				form = { ...s };
			})
			.catch((err: unknown) => {
				if (cancelled) return;
				error = err instanceof Error ? err.message : String(err);
			})
			.finally(() => {
				if (!cancelled) loading = false;
			});
		return () => {
			cancelled = true;
		};
	});

	// Clear the "Saved ✓" badge as soon as the user edits the form again.
	$effect(() => {
		if (dirty) savedFlash = false;
	});

	function set(field: "outputDir" | "pathTemplate", value: string) {
		form = updateField(form, field, value);
	}
	function setFormat(value: string) {
		form = updateField(form, "format", value);
	}

	async function save() {
		if (!canSave) return;
		saving = true;
		savedFlash = false;
		try {
			const saved = await api.saveSettings(form);
			original = saved;
			form = { ...saved };
			savedFlash = true;
		} catch (err) {
			error = err instanceof Error ? err.message : String(err);
		} finally {
			saving = false;
		}
	}
</script>

<div class="card bg-base-100 shadow">
	<div class="card-body gap-3">
		<div class="flex items-center justify-between">
			<h2 class="card-title text-lg">Settings</h2>
			{#if savedFlash}
				<span class="badge badge-success badge-sm">Saved ✓</span>
			{/if}
		</div>

		{#if loading}
			<div class="flex items-center gap-2 text-base-content/60">
				<span class="loading loading-spinner loading-sm"></span>
				<span>Loading settings…</span>
			</div>
		{:else if error}
			<div class="alert alert-error">
				<span>{error}</span>
			</div>
		{:else}
			<label class="form-control">
				<span class="label-text mb-1 text-xs">Output directory</span>
				<input
					class="input input-bordered w-full"
					placeholder="/path/to/music"
					value={form.outputDir}
					oninput={(e) => set("outputDir", e.currentTarget.value)}
				/>
				<span class="label-text-alt mt-1 text-xs text-base-content/50">
					Where downloaded files are written. Takes effect on next backend restart.
				</span>
			</label>

			<label class="form-control">
				<span class="label-text mb-1 text-xs">Default format</span>
				<select
					class="select select-bordered w-full"
					value={form.format}
					onchange={(e) => setFormat(e.currentTarget.value)}
				>
					{#each FORMAT_OPTIONS as f (f)}
						<option value={f}>{f.toUpperCase()}</option>
					{/each}
				</select>
				<span class="label-text-alt mt-1 text-xs text-base-content/50">
					Used for new downloads. Takes effect on next backend restart.
				</span>
			</label>

			<label class="form-control">
				<span class="label-text mb-1 text-xs">Path template</span>
				<input
					class="input input-bordered w-full font-mono text-sm"
					placeholder={DEFAULT_PATH_TEMPLATE}
					value={form.pathTemplate}
					oninput={(e) => set("pathTemplate", e.currentTarget.value)}
				/>
				<span class="label-text-alt mt-1 text-xs text-base-content/50">
					Where files land in the output dir. Takes effect immediately for organize / rename / downloads.
				</span>
				<div class="mt-1 flex flex-wrap gap-x-3 gap-y-1 text-xs text-base-content/60">
					{#each PATH_TEMPLATE_TOKENS as tok (tok.token)}
						<span>
							<code class="text-primary">{tok.token}</code>
							<span class="text-base-content/40"> — {tok.description}</span>
						</span>
					{/each}
				</div>
			</label>

			<div class="rounded-box bg-base-200 px-3 py-2">
				<div class="text-xs text-base-content/50">Preview (sample track)</div>
				<code class="block break-all font-mono text-sm text-base-content/80">{preview}</code>
			</div>

			<div class="card-actions justify-end">
				<button class="btn btn-primary" disabled={!canSave} onclick={save}>
					{#if saving}<span class="loading loading-spinner loading-xs"></span>{/if}
					Save
				</button>
			</div>
		{/if}
	</div>
</div>