// 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`