clove/pkviewer
web/src/lib/components/manage/ClaimFlow.svelte
back to web/src/lib/components/manage
<!-- web/src/lib/components/manage/ClaimFlow.svelte -->
<script lang="ts">
import type { DiscoveredSystem } from "$lib/types.ts";
import {
claimViaDiscord,
discoverSystems,
startChallenge,
verifyChallenge,
} from "$lib/manage.remote.ts";
import Icon from "../Icon.svelte";
// Two routes, in order of how little they ask. The token option is deliberately absent.
let {
initialSystems,
initialError,
}: {
initialSystems: DiscoveredSystem[];
initialError: string | null;
} = $props();
// svelte-ignore state_referenced_locally
let systems = $state(initialSystems);
// svelte-ignore state_referenced_locally
let error = $state<string | null>(initialError);
let busy = $state<string | null>(null);
let manualRef = $state("");
let challenge = $state<{ id: string; nonce: string; hid: string } | null>(null);
async function run<T>(key: string, fn: () => Promise<T>): Promise<T> {
busy = key;
error = null;
try {
return await fn();
} finally {
busy = null;
}
}
async function claim(ref: string) {
const outcome = await run(`claim:${ref}`, () => claimViaDiscord(ref));
if (!outcome.ok) error = outcome.error;
else window.location.href = `/manage/${outcome.systemId}`;
}
async function recheck() {
const outcome = await run("discover", () => discoverSystems());
if (outcome.ok) {
systems = outcome.systems;
if (outcome.systems.length === 0) {
error = "PluralKit does not have a system linked to this Discord account.";
}
} else error = outcome.error;
}
async function begin() {
const ref = manualRef.trim();
if (!ref) return;
const outcome = await run("challenge", () => startChallenge(ref));
if (!outcome.ok) error = outcome.error;
else challenge = { id: outcome.challengeId, nonce: outcome.nonce, hid: outcome.systemHid };
}
async function check() {
if (!challenge) return;
const id = challenge.id;
const outcome = await run("verify", () => verifyChallenge(id));
if (!outcome.ok) error = outcome.error;
else window.location.href = `/manage/${outcome.systemId}`;
}
</script>
{#if error}
<p class="mg-note" data-tone="warn" role="alert">
<Icon name="exclamation-triangle" />
<span>{error}</span>
</p>
{/if}
<section class="mg-panel">
<h2>Systems linked to your Discord account</h2>
<p class="hint">
PluralKit already knows which system your Discord account belongs to.
If yours is here, that is all the confirmation pkviewer needs.
</p>
{#if systems.length > 0}
<ul class="mg-list">
{#each systems as system (system.uuid)}
<li class="mg-card">
<Icon name="person-circle" class="mg-thumb" />
<span class="grow">
<span class="title">{system.name ?? system.hid}</span>
<span class="sub">PluralKit ID {system.hid}</span>
</span>
<button
type="button"
class="primary"
disabled={busy !== null}
onclick={() => claim(system.hid)}
>
{busy === `claim:${system.hid}` ? "Claiming…" : "Claim this system"}
</button>
</li>
{/each}
</ul>
{:else}
<p class="desc">
Nothing found for this Discord account. Either it is not linked to a
PluralKit system, or you are signing in from a different account than
the one linked to it.
</p>
{/if}
<p style="margin-top: 14px; margin-bottom: 0">
<button type="button" class="ghost" disabled={busy !== null} onclick={recheck}>
<Icon name="arrow-repeat" />
{busy === "discover" ? "Checking…" : "Check again"}
</button>
</p>
</section>
<section class="mg-panel">
<h2>Or confirm with a short code</h2>
<p class="hint">
Use this if your Discord account is not linked to the system, or you
are claiming from a different account. It needs nothing from PluralKit
except a moment's edit to your system description.
</p>
{#if !challenge}
<div class="mg-field">
<label for="claim-ref">PluralKit system ID</label>
<input
id="claim-ref"
type="text"
bind:value={manualRef}
spellcheck="false"
autocapitalize="none"
autocomplete="off"
placeholder="abcdef"
aria-describedby="claim-ref-desc"
/>
<span class="desc" id="claim-ref-desc">
The 5 or 6 character ID PluralKit shows, for example from
<code>pk;system</code>.
</span>
</div>
<button
type="button"
disabled={busy !== null || manualRef.trim().length === 0}
onclick={begin}
>
{busy === "challenge" ? "Getting a code…" : "Get a code"}
</button>
{:else}
<ol class="mg-steps">
<li>
Add this code anywhere in the description of system
<strong>{challenge.hid}</strong>:
<code class="mg-code">{challenge.nonce}</code>
</li>
<li>Save it in PluralKit.</li>
<li>Come back and check below. You can remove the code afterwards.</li>
</ol>
<p class="desc">
The code is only valid for a short while. If it expires, start
again for a new one.
</p>
<button type="button" class="primary" disabled={busy !== null} onclick={check}>
<Icon name="check2-circle" />
{busy === "verify" ? "Checking…" : "I have added the code"}
</button>
<button
type="button"
class="ghost"
disabled={busy !== null}
onclick={() => (challenge = null)}
>
Start over
</button>
{/if}
</section>
<p class="mg-note">
<Icon name="clipboard-check" />
<span>
pkviewer never asks for a PluralKit token in order to claim a system.
Both options above work without one.
</span>
</p>