clove/pkviewer
web/src/lib/components/manage/SocialLinksEditor.svelte
back to web/src/lib/components/manage
<!-- web/src/lib/components/manage/SocialLinksEditor.svelte -->
<script lang="ts">
import {
MAX_SOCIAL_LINKS,
SOCIAL_PLATFORMS,
SOCIAL_URL_MESSAGES,
validateSocialUrl,
} from "@pkviewer/shared";
import { useUnsavedGuard } from "$lib/unsavedGuard.svelte.ts";
import type { ActionResult, EditableLink } from "$lib/types.ts";
import Icon from "../Icon.svelte";
// Links are data, never visited: no preview fetch, no favicon lookup (SSRF).
// The whole ordered list saves at once, so reorder, edit and delete cannot interleave.
let {
initialLinks,
onSave,
ownerLabel,
}: {
initialLinks: EditableLink[];
onSave: (links: EditableLink[]) => Promise<ActionResult>;
ownerLabel: string;
} = $props();
// svelte-ignore state_referenced_locally
let links = $state<EditableLink[]>(initialLinks.map((link) => ({ ...link })));
// svelte-ignore state_referenced_locally
let saved = $state<EditableLink[]>(initialLinks.map((link) => ({ ...link })));
let status = $state<"idle" | "saving" | "ok" | "error">("idle");
let message = $state<string | null>(null);
const dirty = $derived(JSON.stringify(links) !== JSON.stringify(saved));
useUnsavedGuard(() => dirty);
// A courtesy so the problem shows beside the field; the server revalidates regardless
const errors = $derived(
links.map((link) => {
if (link.url.trim().length === 0) return null;
const check = validateSocialUrl(link.url);
return check.ok ? null : SOCIAL_URL_MESSAGES[check.reason];
}),
);
const hasErrors = $derived(errors.some((entry) => entry !== null));
function move(index: number, delta: number) {
const target = index + delta;
if (target < 0 || target >= links.length) return;
const [item] = links.splice(index, 1);
links.splice(target, 0, item!);
status = "idle";
}
async function save() {
status = "saving";
const usable = $state.snapshot(links).filter((link) => link.url.trim().length > 0);
const outcome = await onSave(usable);
if (outcome.ok) {
links = usable.map((link) => ({ ...link }));
saved = usable.map((link) => ({ ...link }));
status = "ok";
message = "Saved.";
} else {
status = "error";
message = outcome.error ?? "Could not save. Please try again.";
}
}
</script>
<section class="mg-panel">
<h2>Links on {ownerLabel}</h2>
<p class="hint">
Shown as links on the public page. pkviewer never visits them, so
nothing is loaded from these addresses on your visitors' behalf.
</p>
{#if links.length === 0}
<p class="desc">No links yet.</p>
{:else}
<ul class="mg-list">
{#each links as link, index (index)}
{@const platform = SOCIAL_PLATFORMS.find((entry) => entry.id === link.platform)}
{@const errorId = `link-${index}-error`}
{@const name = link.label || platform?.label || "link"}
<li class="mg-panel" style="box-shadow: none">
<div class="mg-grid">
<div class="mg-field">
<label for="link-{index}-platform">Type</label>
<select
id="link-{index}-platform"
bind:value={link.platform}
onchange={() => (status = "idle")}
>
{#each SOCIAL_PLATFORMS as option (option.id)}
<option value={option.id}>{option.label}</option>
{/each}
</select>
</div>
<div class="mg-field">
<label for="link-{index}-label">Label (optional)</label>
<input
id="link-{index}-label"
type="text"
bind:value={link.label}
maxlength="60"
placeholder={platform?.label ?? ""}
oninput={() => (status = "idle")}
/>
<span class="desc">What the link says. Defaults to the type.</span>
</div>
<div class="mg-field">
<label for="link-{index}-url">Address</label>
<input
id="link-{index}-url"
type="url"
bind:value={link.url}
inputmode="url"
spellcheck="false"
placeholder={platform?.placeholder ?? "https://"}
aria-invalid={errors[index] ? "true" : undefined}
aria-describedby={errors[index] ? errorId : undefined}
oninput={() => (status = "idle")}
/>
{#if errors[index]}
<span class="err" id={errorId}>{errors[index]}</span>
{/if}
</div>
</div>
<div style="display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap">
<button
type="button"
class="icon"
onclick={() => move(index, -1)}
disabled={index === 0}
aria-label="Move {name} up"
>
<Icon name="arrow-up" />
</button>
<button
type="button"
class="icon"
onclick={() => move(index, 1)}
disabled={index === links.length - 1}
aria-label="Move {name} down"
>
<Icon name="arrow-down" />
</button>
<span style="flex: 1"></span>
<button
type="button"
class="danger"
onclick={() => {
links.splice(index, 1);
status = "idle";
}}
>
<Icon name="trash" /> Remove
</button>
</div>
</li>
{/each}
</ul>
{/if}
<p style="margin-top: 14px; margin-bottom: 0">
<button
type="button"
onclick={() => {
links.push({ platform: "website", label: "", url: "" });
status = "idle";
}}
disabled={links.length >= MAX_SOCIAL_LINKS}
>
<Icon name="plus" /> Add a link
</button>
{#if links.length >= MAX_SOCIAL_LINKS}
<span class="desc"> Maximum of {MAX_SOCIAL_LINKS} links.</span>
{/if}
</p>
</section>
<div class="mg-savebar">
<span
class="mg-status"
data-tone={status === "ok" ? "ok" : status === "error" || hasErrors ? "error" : dirty ? "dirty" : undefined}
role="status"
aria-live="polite"
>
{#if status === "error" || hasErrors}<Icon name="exclamation-triangle" />{/if}
{#if status === "ok"}<Icon name="check2" />{/if}
{hasErrors
? "Fix the highlighted addresses before saving"
: (message ?? (dirty ? "Unsaved changes" : "No changes"))}
</span>
<span class="spacer"></span>
<button
type="button"
class="ghost"
onclick={() => {
links = saved.map((link) => ({ ...link }));
status = "idle";
message = null;
}}
disabled={!dirty}
>
Discard changes
</button>
<button
type="button"
class="primary"
onclick={save}
disabled={!dirty || hasErrors || status === "saving"}
>
{status === "saving" ? "Saving…" : "Save links"}
</button>
</div>