~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/knotless

git that lives in your Atmosphere account

Forked from chadtmiller.com/knotless

0
0
50 lines · 1.6 KB Raw History
<script lang="ts">
	import type * as awaitDelegation from '#lib/lexicons/dev/knotless/awaitDelegation.ts';
	import type * as delegateWrites from '#lib/lexicons/dev/knotless/delegateWrites.ts';
	import { call } from '#lib/data.ts';
	/**
	 * The button that delegates the account's writes to this server, as
	 * `knotless account delegate` does: it shows where to approve the grant, waits
	 * until it is, and then calls `done`. `primary` where it is what the
	 * view is for.
	 */
	let { done, primary = false }: { done: () => void; primary?: boolean } = $props();
	let approve = $state('');
	let delegating = $state(false);
	let problem = $state('');
	async function delegate() {
		delegating = true;
		problem = '';
		try {
			const started = await call<typeof delegateWrites.main>('dev.knotless.delegateWrites', {});
			approve = started.url;
			window.open(started.url, '_blank', 'noopener');
			let finished = false;
			while (!finished)
				finished = (
					await call<typeof awaitDelegation.main>('dev.knotless.awaitDelegation', {
						state: started.state
					})
				).done;
			done();
		} catch (err) {
			problem = (err as Error).message;
		} finally {
			delegating = false;
			approve = '';
		}
	}
</script>

<div class="actions">
	<button
		class={['btn', primary && 'primary']}
		type="button"
		disabled={delegating}
		aria-busy={delegating}
		onclick={delegate}
		>{delegating ? 'Waiting for approval…' : 'Delegate writes to this server'}</button
	>
	{#if approve}<a href={approve} target="_blank" rel="noopener">Approve at your PDS</a>{/if}
</div>
{#if problem}<p class="problem" role="alert">{problem}</p>{/if}