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

johnandersen777.bsky.social/knotless

git that lives in your Atmosphere account

Forked from chadtmiller.com/knotless

0
0
72 lines · 1.9 KB Raw History
<script lang="ts">
	import type { AtUriString } from '@atproto/lex';
	import type { LineAt } from '#lib/organisms/DiffFile.svelte';
	import MarkdownField from '#lib/molecules/MarkdownField.svelte';
	/**
	 * Writing a comment on lines of a pull's diff, `from` to `at`: sent
	 * alone at once, or kept with a review being written, as `send` is
	 * told; `reviewing` once there is one. `send` answers why it failed, or
	 * nothing; `cancel` closes it.
	 */
	let {
		at,
		from,
		reviewing,
		repo,
		send,
		cancel
	}: {
		at: LineAt;
		from?: number;
		reviewing: boolean;
		repo?: AtUriString;
		send: (body: string, review: boolean) => Promise<string | undefined>;
		cancel: () => void;
	} = $props();
	let body = $state('');
	// Which is being sent: 'single' or 'review'.
	let busy = $state('');
	let problem = $state('');
	const range = $derived(
		from && from !== at.line ? `lines ${from} to ${at.line}` : `line ${at.line}`
	);
	async function go(review: boolean) {
		busy = review ? 'review' : 'single';
		problem = (await send(body, review)) ?? '';
		busy = '';
	}
</script>

<form
	class="box composer line-composer"
	onsubmit={(e) => {
		e.preventDefault();
		go(true);
	}}
>
	<p class="mute">{`Commenting on ${range}${at.side === 'old' ? ', as it was' : ''}`}</p>
	<MarkdownField
		label="Comment on {range}"
		placeholder="Leave a comment"
		rows={3}
		{repo}
		bind:value={body}
	/>
	{#if problem}<p class="problem" role="alert">{problem}</p>{/if}
	<div class="composer-end">
		<button class="btn" type="button" onclick={cancel}>Cancel</button>
		{#if !reviewing}<button
				class="btn"
				type="button"
				disabled={!!busy || !body.trim()}
				aria-busy={busy === 'single'}
				onclick={() => go(false)}>Add single comment</button
			>{/if}
		<button
			class="btn primary"
			type="submit"
			disabled={!!busy || !body.trim()}
			aria-busy={busy === 'review'}>{reviewing ? 'Add review comment' : 'Start a review'}</button
		>
	</div>
</form>