Files
fnx_web/svelte/src/user_home/remote_sync/RemoteSync.svelte
T

263 lines
7.2 KiB
Svelte
Raw Normal View History

2026-11-09 18:48:27 +01:00
<script lang="ts">
import { onMount } from "svelte";
import { loading_run } from "lib/Loading";
import { check_response, dict_to_form, get_endpoint } from "lib/NovaAPI";
import ProgressBar from "util/ProgressBar.svelte";
import { formatDataVolume, formatDate, formatDuration } from "util/Formatting";
import { user } from "lib/UserStore";
import RemoteSyncForm from "./RemoteSyncForm.svelte";
import { edit_remote_sync, form, secret_fields, service_of, type RemoteSync } from "./RemoteSyncLib.svelte";
2026-11-09 18:48:27 +01:00
let syncs: RemoteSync[] = $state([])
2026-11-09 18:48:27 +01:00
// Polls every second while a sync runs, the server saves the
2026-11-09 18:48:27 +01:00
// progress that often
let mounted = true
let timeout: ReturnType<typeof setTimeout>
const load = async () => {
clearTimeout(timeout)
try {
syncs = await check_response(await fetch(get_endpoint() + "/user/remote_sync"))
2026-11-09 18:48:27 +01:00
} catch (err) {
console.error("Failed to load remote syncs:", err)
2026-11-09 18:48:27 +01:00
}
if (mounted && syncs.some(t => t.status === "running")) {
2026-11-09 18:48:27 +01:00
timeout = setTimeout(load, 1000)
}
}
onMount(() => {
load()
return () => {
mounted = false
clearTimeout(timeout)
}
})
const edit = (t: RemoteSync) => {
edit_remote_sync(t)
document.getElementById("remote_sync_form")?.scrollIntoView({ behavior: "smooth" })
2026-11-09 18:48:27 +01:00
}
const restart = async (t: RemoteSync, body = {}) => {
2026-11-09 18:48:27 +01:00
try {
await loading_run(async () => check_response(
await fetch(get_endpoint() + "/user/remote_sync/" + t.id + "/restart", {
2026-11-09 18:48:27 +01:00
method: "POST",
body: dict_to_form(body),
})
))
} catch (err) {
alert("Failed to restart the remote sync: " + (err.message ?? err))
2026-11-09 18:48:27 +01:00
return
}
asking = ""
load()
}
// A sync without remembered credentials asks for them again. What the form
2026-11-09 18:48:27 +01:00
// still has for that service is filled in
let asking = $state("")
let secrets: { [name: string]: string } = $state({})
let remember_again = $state(false)
const ask = (t: RemoteSync) => {
2026-11-09 18:48:27 +01:00
const service = service_of(t)
secrets = Object.fromEntries(secret_fields(service).map(f =>
[f.name, (form.options[service][f.name] as string) ?? ""]
))
remember_again = false
asking = t.id
}
const remove = async (t: RemoteSync) => {
2026-11-09 18:48:27 +01:00
try {
await loading_run(async () => check_response(
await fetch(get_endpoint() + "/user/remote_sync/" + t.id, { method: "DELETE" })
2026-11-09 18:48:27 +01:00
))
} catch (err) {
alert("Failed to remove the remote sync: " + (err.message ?? err))
2026-11-09 18:48:27 +01:00
return
}
load()
}
</script>
<section>
<h2>Remote sync</h2>
2026-11-09 18:48:27 +01:00
<p>
Copy files between Nova and another storage service. The sync runs
on our servers, so you can close this page while it runs. A sync
2026-11-09 18:48:27 +01:00
which stopped can be started again, files which are already there are
skipped.
</p>
{#if $user !== null && $user.subscription.id === ""}
<p>
Remote sync is available on paid plans.
2026-11-09 18:48:27 +01:00
<a class="button button_highlight" href="/user/subscription/manage">
<i class="icon i_shopping_cart"></i> Upgrade
2026-11-09 18:48:27 +01:00
</a>
</p>
{:else}
<RemoteSyncForm onsaved={load}/>
2026-11-09 18:48:27 +01:00
{/if}
{#each syncs as t (t.id)}
2026-11-09 18:48:27 +01:00
{@const name = t.directory_path || t.directory}
{@const service = service_of(t)}
<div class="remote_sync" class:running={t.status === "running"} class:editing={form.editing === t.id}>
2026-11-09 18:48:27 +01:00
<div class="row">
<span class="title">
<i class="icon {t.direction === 'import' ? 'i_download' : 'i_upload'}"></i>
2026-11-09 18:48:27 +01:00
<span class="badge">{t.sync ? "Sync" : "Copy"}</span>
{t.direction === "import" ? t.remote + " → " + name : name + " → " + t.remote}
</span>
<div class="toolbar_spacer"></div>
{#if t.status === "running"}
<button onclick={() => remove(t)}>
<i class="icon i_cancel"></i> Abort
2026-11-09 18:48:27 +01:00
</button>
{:else}
{#if service !== ""}
<button onclick={() => edit(t)}>
<i class="icon i_edit"></i> Edit
2026-11-09 18:48:27 +01:00
</button>
{/if}
<button onclick={() => t.remember || service === "" ? restart(t) : ask(t)}>
<i class="icon i_restart_alt"></i> Restart
2026-11-09 18:48:27 +01:00
</button>
<button class="button_red" onclick={() => remove(t)}>
<i class="icon i_delete"></i> Remove
2026-11-09 18:48:27 +01:00
</button>
{/if}
</div>
{#if t.stats}
<ProgressBar total={t.stats.totalBytes} used={t.stats.bytes} speed={1000} animation="linear"/>
<div class="details">
<div>
<span>Data copied</span>
{formatDataVolume(t.stats.bytes, 3)} of {formatDataVolume(t.stats.totalBytes, 3)}
</div>
<div>
<span>Files copied</span>
{t.stats.transfers} of {t.stats.totalTransfers}
</div>
<div>
<span>Files checked</span>
{t.stats.checks} of {t.stats.totalChecks}
</div>
<div>
2026-11-12 15:58:21 +01:00
<span>Transfer rate</span>
2026-11-09 18:48:27 +01:00
{formatDataVolume(t.stats.speed, 3)}/s
</div>
<div>
<span>Time left</span>
<!-- Below a minute the second unit would be milliseconds -->
{t.status === "running" && t.stats.eta !== null
? formatDuration(t.stats.eta * 1000, t.stats.eta < 60 ? 1 : 2) : "-"}
</div>
</div>
{/if}
{#if t.error}
<div class="highlight_red">{t.error}</div>
{/if}
{#if asking === t.id}
<form class="form" onsubmit={e => { e.preventDefault(); restart(t, { ...secrets, remember: remember_again }) }}>
<!-- Keeps Firefox from filling in the account's login, see RemoteSyncForm -->
<input type="text" autocomplete="username" value={"RemoteSync " + t.remote} style="display: none;"/>
2026-11-09 18:48:27 +01:00
{#each secret_fields(service) as f (f.name)}
<label for={"restart_" + f.name}>{f.label}</label>
{#if f.type === "textarea"}
<textarea id={"restart_" + f.name} bind:value={secrets[f.name]}></textarea>
{:else if f.type === "password"}
<input id={"restart_" + f.name} type="password" autocomplete="off" bind:value={secrets[f.name]}/>
{:else}
<input id={"restart_" + f.name} type="text" autocomplete="off" bind:value={secrets[f.name]}/>
{/if}
{/each}
<label>
<input type="checkbox" bind:checked={remember_again}/>
Remember the credentials
</label>
<div class="row">
<button type="button" onclick={() => asking = ""}>Cancel</button>
<button class="button_highlight" type="submit">
<i class="icon i_restart_alt"></i> Restart
2026-11-09 18:48:27 +01:00
</button>
</div>
</form>
{/if}
<div class="details">
<div>
<span>Status</span>
{t.status}
</div>
<div>
<span>Started</span>
{t.started ? formatDate(t.started, true, true, false) : "-"}
</div>
<div>
<span>Credentials</span>
{t.remember ? "Remembered" : "Not saved"}
</div>
</div>
</div>
{/each}
</section>
<style>
/* The cards of the API keys page */
.remote_sync {
2026-11-09 18:48:27 +01:00
margin: 0.8em 0;
padding: 0.4em 0.6em;
border-radius: 8px;
border-left: 3px solid var(--separator);
background: var(--card_color);
}
.remote_sync.running,
.remote_sync.editing {
2026-11-09 18:48:27 +01:00
border-left-color: var(--accent_color);
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 0.2em 0.5em;
margin: 0.3em 0;
}
.toolbar_spacer {
flex: 1 1 auto;
}
.title {
display: inline-flex;
align-items: center;
gap: 0.3em;
font-size: 1.1em;
overflow-wrap: anywhere;
}
.badge {
padding: 0 0.6em;
border-radius: 1em;
color: var(--link_color);
background: var(--accent_shade);
}
/* Each detail is a label with its value below it */
.details {
display: flex;
flex-wrap: wrap;
gap: 0.2em 1.5em;
font-size: 0.9em;
}
.details > div {
display: flex;
flex-direction: column;
}
.details span {
opacity: 0.6;
}
</style>