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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
// The "Photo backup" section of the Android Sync page, on the Android
// application only (docs/MESHBAY_DESIGN.md §9.12). Loaded on demand: no other
// client has it. Where the photos go is the page's destination section;
// this one chooses which.
import { html, useState, useEffect, useCallback } from './vendor/htm-preact.js';
import { t } from './i18n.js';
import * as platform from './platform.js';
import { formatSize } from './file-utils.js';
import { ask } from './ask.js';
import { subscribe, runPhotoSync } from './photo-sync.js';
function stateLine(st, status) {
switch (st.phase) {
case 'waiting_wifi': return t('photosync.state_waiting_wifi');
case 'connecting': return t('photosync.state_connecting');
case 'sending': return t('photosync.state_sending', { done: st.done, total: st.total });
case 'offline': return t('photosync.state_offline');
case 'interrupted': return t('photosync.state_interrupted');
case 'up_to_date': return t('photosync.state_up_to_date');
default: return status && status.lastCompleted ? t('photosync.state_up_to_date') : '';
}
}
const FAILURES = {
disk_full: 'photosync.fail_disk_full',
root_read_only: 'photosync.fail_read_only',
no_writable_root: 'photosync.fail_read_only',
no_such_root: 'photosync.fail_no_folder',
no_such_directory: 'photosync.fail_no_folder',
root_unavailable: 'photosync.fail_unavailable',
not_a_member: 'photosync.fail_not_member',
not_private: 'photosync.fail_not_private',
permission: 'photosync.fail_permission',
};
export function PhotoSyncSection({ destination }) {
const [status, setStatus] = useState(null);
const [run, setRun] = useState({ phase: 'idle' });
const [editing, setEditing] = useState(false);
const [error, setError] = useState('');
const refresh = useCallback(() => platform.photoSync.status().then(setStatus).catch(() => {}), []);
useEffect(() => { refresh(); }, [refresh]);
// A run that ends changes what the phone says (sent, last backup, a
// failure); read it again then rather than on a timer.
useEffect(() => subscribe((st) => {
setRun(st);
if (st.phase !== 'sending' && st.phase !== 'connecting') refresh();
}), [refresh]);
if (!status) return null;
const cfg = status.config;
if (!destination) {
return html`
<div class="settings-section">
<h3 class="settings-heading">${t('photosync.heading')}</h3>
<p class="settings-hint">${t('photosync.intro')}</p>
<p class="settings-hint">${t('androidsync.dest_needed')}</p>
</div>
`;
}
const backUpNow = async () => {
setError('');
try {
if (!status.unmetered) {
const est = await platform.photoSync.estimate(cfg);
if (est.count === 0) { await runPhotoSync({ force: true, metered: true }); return; }
const yes = await ask(t('photosync.metered_confirm', {
count: est.count, size: formatSize(est.bytes) }));
if (!yes) return;
await runPhotoSync({ force: true, metered: true });
} else {
await runPhotoSync({ force: true });
}
} catch (e) { setError(e.message); }
};
const turnOff = async () => {
setError('');
try { setStatus(await platform.photoSync.configure(null)); } catch (e) { setError(e.message); }
};
return html`
<div class="settings-section">
<h3 class="settings-heading">${t('photosync.heading')}</h3>
${!cfg && !editing && html`
<p class="settings-hint">${t('photosync.intro')}</p>
<button class="admin-btn" onClick=${() => setEditing(true)}>${t('photosync.setup')}</button>
`}
${cfg && !editing && html`
<div class="settings-row">
<span class="settings-label settings-name">
${t('phonesync.summary', { group: destination.groupName, dir: status.dir })}
</span>
</div>
<p class="settings-hint">
${t('photosync.sent_total', { n: status.sent })}
${' · '}
${status.lastCompleted
? t('photosync.last_run', { when: new Date(status.lastCompleted).toLocaleString() })
: t('photosync.never')}
</p>
${status.permission === 'partial' && html`
<p class="settings-hint">${t('photosync.permission_partial')}</p>`}
${cfg.includeVideos && status.videoPermission === 'denied' && html`
<p class="settings-hint">${t('photosync.video_permission_needed')}</p>
<button class="btn-secondary" onClick=${async () => {
try { setStatus(await platform.photoSync.permit(true)); } catch (e) { setError(e.message); }
}}>${t('photosync.permission_allow')}</button>`}
${status.failure
? html`<p class="error-msg">${t(FAILURES[status.failure] || 'photosync.fail_other')}</p>`
: stateLine(run, status) && html`<p class="settings-hint">${stateLine(run, status)}</p>`}
<div class="settings-row" style="gap:8px; justify-content:flex-start; flex-wrap:wrap">
<button class="admin-btn" disabled=${run.phase === 'sending' || run.phase === 'connecting'}
onClick=${backUpNow}>${t('photosync.now')}</button>
<button class="btn-secondary" onClick=${() => setEditing(true)}>${t('photosync.change')}</button>
<button class="btn-secondary" onClick=${turnOff}>${t('photosync.turn_off')}</button>
</div>
`}
${editing && html`
<${Setup} status=${status}
onPermission=${setStatus}
onDone=${(next) => { setEditing(false); if (next) { setStatus(next); runPhotoSync(); } }} />`}
${error && html`<p class="error-msg">${error}</p>`}
</div>
`;
}
function Setup({ status, onPermission, onDone }) {
const cfg = status.config;
const [albums, setAlbums] = useState(null);
const [chosen, setChosen] = useState(() => new Set(cfg ? cfg.albums : []));
const [existing, setExisting] = useState(cfg ? cfg.includeExisting : true);
const [videos, setVideos] = useState(cfg ? !!cfg.includeVideos : false);
const [busy, setBusy] = useState(false);
const [error, setError] = useState('');
const permitted = status.permission !== 'denied';
const videosAllowed = status.videoPermission !== 'denied';
useEffect(() => {
if (!permitted) return;
platform.photoSync.albums(videos && videosAllowed).then((list) => {
setAlbums(list);
// The camera, and only the camera, unless the person chose before:
// screenshots and saved images are where photos nobody meant to share live.
if (!cfg) {
setChosen((prev) => (prev.size ? prev : new Set(list.filter((a) => a.camera).map((a) => a.id))));
}
}).catch((e) => setError(e.message));
}, [permitted, videos, videosAllowed]);
// Ticking videos asks for them then, not before: Android 13+ asks apart.
const toggleVideos = async () => {
const next = !videos;
setVideos(next);
if (next && !videosAllowed) {
try { onPermission(await platform.photoSync.permit(true)); } catch (e) { setError(e.message); }
}
};
const allow = async () => {
setError('');
try { onPermission(await platform.photoSync.permit()); } catch (e) { setError(e.message); }
};
const toggle = (id) => setChosen((prev) => {
const next = new Set(prev);
if (next.has(id)) next.delete(id); else next.add(id);
return next;
});
const start = async () => {
if (!chosen.size) return;
setBusy(true);
setError('');
try {
const settings = { albums: [...chosen], includeExisting: existing, includeVideos: videos };
// Asked when the photos would come from somewhere new in time, or when
// videos start going; never for a change of albums alone, which the
// person just made.
if (!cfg || cfg.includeExisting !== existing || (videos && !cfg.includeVideos)) {
const est = await platform.photoSync.estimate(settings);
const params = {
count: est.count, size: formatSize(est.bytes), dir: status.dir,
videos: est.videos || 0, videoSize: formatSize(est.videoBytes || 0),
};
const key = !(est.count > 0) ? 'photosync.confirm_new'
: est.videos > 0 ? 'photosync.confirm_videos' : 'photosync.confirm';
const yes = await ask(t(key, params));
if (!yes) { setBusy(false); return; }
}
onDone(await platform.photoSync.configure(settings));
} catch (e) {
setError(e.message);
setBusy(false);
}
};
return html`
<div>
${!permitted && html`
<p class="settings-hint">${t('photosync.permission_needed')}</p>
<button class="admin-btn" onClick=${allow}>${t('photosync.permission_allow')}</button>
`}
${status.permission === 'partial' && html`
<p class="settings-hint">${t('photosync.permission_partial')}</p>
<button class="btn-secondary" onClick=${allow}>${t('photosync.permission_allow')}</button>
`}
${permitted && html`
<p class="settings-label" style="margin-top:10px">${t('photosync.albums')}</p>
${(albums || []).map((a) => html`
<label class="settings-choice" key=${a.id}>
<input type="checkbox" checked=${chosen.has(a.id)} onChange=${() => toggle(a.id)} />
<span class="settings-name">
<strong>${a.camera ? t('photosync.album_camera') : a.name}</strong>
<span class="settings-hint">
${videos && a.videos
? t('photosync.album_count_videos',
{ n: a.count, v: a.videos, size: formatSize(a.bytes + a.videoBytes) })
: t('photosync.album_count', { n: a.count, size: formatSize(a.bytes) })}</span>
</span>
</label>
`)}
<label class="settings-choice">
<input type="checkbox" checked=${videos} onChange=${toggleVideos} />
<span class="settings-name">
<strong>${t('photosync.include_videos')}</strong>
<span class="settings-hint">${t('photosync.include_videos_hint')}</span>
</span>
</label>
${videos && !videosAllowed && html`
<p class="settings-hint">${t('photosync.video_permission_needed')}</p>`}
<label class="settings-choice">
<input type="radio" name="photosync-scope" checked=${existing}
onChange=${() => setExisting(true)} />
<span><strong>${t('photosync.scope_existing')}</strong></span>
</label>
<label class="settings-choice">
<input type="radio" name="photosync-scope" checked=${!existing}
onChange=${() => setExisting(false)} />
<span><strong>${t('photosync.scope_new')}</strong></span>
</label>
<div class="settings-row" style="gap:8px; justify-content:flex-start">
<button class="admin-btn" onClick=${start}
disabled=${busy || !chosen.size}>${t('photosync.start')}</button>
<button class="btn-secondary" onClick=${() => onDone(null)}>${t('dialog.cancel')}</button>
</div>
`}
${!permitted && html`
<button class="btn-secondary" onClick=${() => onDone(null)}>${t('dialog.cancel')}</button>`}
${error && html`<p class="error-msg">${error}</p>`}
</div>
`;
}
|