164 lines
7.8 KiB
Svelte
164 lines
7.8 KiB
Svelte
<!-- This Source Code Form is subject to the terms of the Mozilla Public
|
|
- License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
- file, You can obtain one at https://mozilla.org/MPL/2.0/. -->
|
|
<script lang="ts">
|
|
// Handoff §8.2: mint on open (click 2), Copy link (click 3). Changing an option re-mints with
|
|
// `replaces`, so one link from this dialog is valid at a time; closing without copying stops it.
|
|
// Links off: "Turn on share links" applies the change, then Copy link (4 clicks: open
|
|
// question Q1 is not decided, so this is the fallback path the design allows).
|
|
import { onDestroy, onMount } from "svelte";
|
|
import { Banner, Button, Dialog, Icon, Segmented } from "@ianitor/ui";
|
|
import ApplyProgress from "./ApplyProgress.svelte";
|
|
import { ApiError, call, del, newKey, patch, sentence } from "../lib/api";
|
|
import { app } from "../lib/app.svelte";
|
|
import { follow } from "../lib/applies";
|
|
import { longDate } from "../lib/format";
|
|
import type { ApplyView, MintedLink } from "../lib/types";
|
|
|
|
let { hostname, onclose }: { hostname: string; onclose: () => void } = $props();
|
|
|
|
// Known before the Dialog mounts, so its focus lands on Copy link (§2.9), not the title.
|
|
// svelte-ignore state_referenced_locally
|
|
let on = $state(!!app.overview?.hosts.find((h) => h.hostname === hostname)?.share_links);
|
|
let link = $state<MintedLink | null>(null);
|
|
let minting = $state(false);
|
|
let copied = $state(false);
|
|
let copiedBefore = $state(false);
|
|
let refused = $state(false);
|
|
let problem = $state<string | null>(null);
|
|
let fieldErr = $state<{ field: string; message: string } | null>(null);
|
|
let days = $state<"1" | "7" | "30">("7");
|
|
let prefix = $state("/");
|
|
let showPassword = $state(false);
|
|
let password = $state("");
|
|
let usingPassword = $state(false);
|
|
let turning = $state<ApplyView | null>(null);
|
|
let stopFollow: (() => void) | undefined;
|
|
let secretBox: HTMLTextAreaElement | undefined = $state();
|
|
|
|
onMount(() => {
|
|
if (on) void mint();
|
|
});
|
|
onDestroy(() => stopFollow?.());
|
|
|
|
async function mint() {
|
|
minting = true;
|
|
problem = null;
|
|
fieldErr = null;
|
|
try {
|
|
const body: Record<string, unknown> = { path_prefix: prefix || "/", expires_in_s: Number(days) * 86400 };
|
|
if (usingPassword && password) body.password = password;
|
|
if (link) body.replaces = link.id;
|
|
const a = await call<MintedLink>("POST", `/hosts/${encodeURIComponent(hostname)}/share-links`, body, { idem: newKey() });
|
|
if (link && copied) copiedBefore = true;
|
|
link = a.data;
|
|
copied = false;
|
|
} catch (e) {
|
|
if (e instanceof ApiError && e.body.field) {
|
|
fieldErr = { field: e.body.field, message: e.body.message };
|
|
if (e.body.field === "password") usingPassword = false;
|
|
}
|
|
else problem = sentence(e);
|
|
} finally {
|
|
minting = false;
|
|
}
|
|
}
|
|
|
|
async function copy() {
|
|
if (!link) return;
|
|
try {
|
|
await navigator.clipboard.writeText(link.url);
|
|
copied = true;
|
|
refused = false;
|
|
} catch {
|
|
refused = true;
|
|
secretBox?.select();
|
|
}
|
|
}
|
|
|
|
async function turnOn() {
|
|
try {
|
|
const a = await patch<{ apply: ApplyView }>(`/hosts/${encodeURIComponent(hostname)}`, { share_links: true, base_rev: app.overview?.rev ?? null });
|
|
turning = a.apply;
|
|
stopFollow = follow(a.apply.id, async (x) => {
|
|
turning = x;
|
|
if (x.state === "kept") {
|
|
on = true;
|
|
await mint();
|
|
}
|
|
});
|
|
} catch (e) {
|
|
problem = sentence(e);
|
|
}
|
|
}
|
|
|
|
function close() {
|
|
// A link nobody copied does not stay valid.
|
|
if (link && !copied) void del(`/share-links/${encodeURIComponent(link.id)}`, { keepalive: true }).catch(() => {});
|
|
onclose();
|
|
}
|
|
|
|
const underCaddy = $derived(app.overview?.engine.running === "caddy");
|
|
</script>
|
|
|
|
<Dialog title="Share {hostname}" onclose={close}>
|
|
{#if !on}
|
|
{#if turning}
|
|
<ApplyProgress apply={turning} />
|
|
{#if turning.state === "reverted" || turning.state === "failed"}
|
|
<Banner tone="notice" live="status">{#snippet title()}Share links were not turned on. {turning?.reason?.message}{/snippet}</Banner>
|
|
{/if}
|
|
{:else}
|
|
<p><strong>Share links are off for {hostname}.</strong></p>
|
|
<p class="ia-text2">Turning them on is a change to the host, checked like any other.</p>
|
|
{#if problem}<p class="domi-error" role="alert"><Icon name="ring" />{problem}</p>{/if}
|
|
<div><Button variant="outline" onclick={turnOn} disabled={!!app.overview?.paused}>Turn on share links</Button></div>
|
|
{/if}
|
|
{:else}
|
|
<p class="ia-text2">Anyone with this link can open the site without signing in, until it expires.</p>
|
|
{#if underCaddy}
|
|
<Banner tone="notice">{#snippet title()}Caddy is serving right now, so share links do not work: visitors with this link get the normal sign-in. The link works after switching back to Pingora, until it expires.{/snippet}</Banner>
|
|
{/if}
|
|
{#if link?.warning}<p class="ia-hint">{link.warning.message}</p>{/if}
|
|
{#if problem}<Banner tone="notice" live="status">{#snippet title()}{problem}{/snippet}</Banner>{/if}
|
|
{#if link}
|
|
<textarea bind:this={secretBox} class="ia-secret" readonly value={link.url} aria-label="Share link" onfocus={(e) => (e.currentTarget as HTMLTextAreaElement).select()}></textarea>
|
|
{:else if minting}
|
|
<p class="ia-muted">Making the link…</p>
|
|
{/if}
|
|
{#if copied}
|
|
<p class="ia-status" data-s="kept" role="status"><Icon name="check" /><span>Copied</span></p>
|
|
<p class="ia-hint">Expires on {longDate(link?.expires_at)}. To stop it sooner: Settings, Share links, Stop this link.</p>
|
|
{:else}
|
|
<Button variant="primary" full icon="copy" onclick={copy} disabled={!link || minting} data-autofocus>Copy link</Button>
|
|
{/if}
|
|
{#if refused}<p class="ia-hint" role="alert">Your browser did not allow copying. Press Ctrl+C to copy the selected link.</p>{/if}
|
|
{#if copiedBefore && !copied}<p class="ia-hint">The link you copied before is stopped. Copy this one.</p>{/if}
|
|
|
|
<Segmented legend="Expires after" name="expiry" bind:value={days} onchange={() => mint()}
|
|
options={[{ value: "1", label: "1 day" }, { value: "7", label: "7 days" }, { value: "30", label: "30 days" }]} />
|
|
<div class="domi-field">
|
|
<label class="domi-field-label" for="share-prefix">Only addresses starting with</label>
|
|
<input id="share-prefix" class="domi-input" bind:value={prefix} onblur={() => mint()} onkeydown={(e) => { if (e.key === "Enter") { e.preventDefault(); void mint(); } }}
|
|
aria-invalid={fieldErr?.field === "path_prefix" ? "true" : undefined} />
|
|
{#if fieldErr?.field === "path_prefix"}<p class="domi-error"><Icon name="ring" />{fieldErr.message}</p>{/if}
|
|
<p class="ia-hint">/ shares the whole site. Try /share/summer-2026 for one album.</p>
|
|
</div>
|
|
{#if usingPassword}
|
|
<p class="ia-hint">This link asks for the password.</p>
|
|
<div><button type="button" class="domi-btn link" onclick={() => { usingPassword = false; password = ""; void mint(); }}>Remove the password</button></div>
|
|
{:else if showPassword}
|
|
<div class="domi-field">
|
|
<label class="domi-field-label" for="share-password">Password</label>
|
|
<input id="share-password" class="domi-input" type="password" autocomplete="new-password" bind:value={password}
|
|
aria-invalid={fieldErr?.field === "password" ? "true" : undefined} />
|
|
{#if fieldErr?.field === "password"}<p class="domi-error"><Icon name="ring" />{fieldErr.message}</p>{/if}
|
|
<p class="ia-hint">At least 8 characters. Send it to them separately.</p>
|
|
<div><Button onclick={() => { usingPassword = true; void mint(); }}>Use this password</Button></div>
|
|
</div>
|
|
{:else}
|
|
<div><button type="button" class="domi-btn link" onclick={() => (showPassword = true)}>Add a password</button></div>
|
|
{/if}
|
|
{/if}
|
|
</Dialog>
|