Show how a session is doing on the progress bar
Each side's bar now shows the session's phase by color and movement, with a short line below saying what is going on: a sweep while connecting, a pulse while someone has to act, a slow pink sweep while retrying after a failure (with the reason and the attempt), then the transfer, done, stopped or error. A receiver whose code no tracker knows says so right away instead of after 6 minutes, and a connection that fails though both sides have a public address names a blocking network as the likely reason. The log marks where each session starts. The send card names the session's file once another one is picked, and a blocked clipboard is shown inline with the text selected for copying by hand. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
02668de602
commit
f7e8999439
4 files changed
+348
-67
No files matched your search
+81
-28
@@ -9,6 +9,7 @@ import {
|
||||
DEFAULT_TRACKERS,
|
||||
type Events,
|
||||
type Incoming,
|
||||
type Phase,
|
||||
type ReceiveEvents,
|
||||
receiveViaTracker,
|
||||
sendViaTracker,
|
||||
@@ -83,6 +84,8 @@ function onResetServers() {
|
||||
}
|
||||
|
||||
const file = ref<File>();
|
||||
// The file of the send session, the user may have picked another one since.
|
||||
const shared = ref<File>();
|
||||
const offer = ref('');
|
||||
const answerInput = ref('');
|
||||
const sendCode = ref('');
|
||||
@@ -100,6 +103,9 @@ const confirmed = ref(false);
|
||||
const log = ref<string[]>([]);
|
||||
// Of the session, shown in the card of its side.
|
||||
const error = ref('');
|
||||
const status = ref<{ phase: Phase; text: string }>();
|
||||
// The bar's look, an error colors whatever it showed.
|
||||
const phase = computed(() => (error.value ? 'error' : status.value?.phase));
|
||||
|
||||
// Shared by both sides, each adds its own progress.
|
||||
const events: Omit<Events, 'progress'> = {
|
||||
@@ -108,6 +114,7 @@ const events: Omit<Events, 'progress'> = {
|
||||
events.log(message);
|
||||
error.value = message;
|
||||
},
|
||||
status: (phase, text) => (status.value = { phase, text }),
|
||||
check: (result) => {
|
||||
check.value = result;
|
||||
confirmed.value = false;
|
||||
@@ -134,24 +141,43 @@ const receiveEvents: ReceiveEvents = {
|
||||
|
||||
const percent = ({ read, size }: Progress) => (size ? Math.floor((read / size) * 100) : 100);
|
||||
|
||||
// Undefined makes the bar indeterminate: until the transfer starts it only shows how the session is
|
||||
// doing. After it, or an error before it, the bar is full.
|
||||
function barValue(progress?: Progress) {
|
||||
if (progress) return percent(progress);
|
||||
return ['start', 'wait', 'retry'].includes(phase.value!) ? undefined : 100;
|
||||
}
|
||||
|
||||
function onConfirm() {
|
||||
confirmed.value = true;
|
||||
check.value?.confirm();
|
||||
}
|
||||
|
||||
// The text copied last, its button says so for a moment.
|
||||
const copied = ref('');
|
||||
// The text copied last and whether that worked. Its button says so, after a success for a moment.
|
||||
const copied = ref<{ text: string; ok: boolean }>();
|
||||
let copiedTimer: number | undefined;
|
||||
|
||||
async function onCopy(text: string) {
|
||||
const copyLabel = (text: string) => (copied.value?.text !== text ? 'Copy' : copied.value.ok ? 'Copied' : 'Copy failed');
|
||||
const copyFailed = (text: string) => copied.value?.text === text && !copied.value.ok;
|
||||
|
||||
// Copies text, shown in the element with id. If the browser blocks that, the text gets selected, so
|
||||
// the user can copy it themselves.
|
||||
async function onCopy(text: string, id: string) {
|
||||
clearTimeout(copiedTimer);
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
} catch {
|
||||
return events.log('copy failed, the browser blocked the clipboard');
|
||||
events.log('copy failed, the browser blocked the clipboard');
|
||||
copied.value = { text, ok: false };
|
||||
const element = document.getElementById(id);
|
||||
if (element instanceof HTMLTextAreaElement) {
|
||||
element.focus();
|
||||
element.select();
|
||||
} else if (element) getSelection()?.selectAllChildren(element);
|
||||
return;
|
||||
}
|
||||
copied.value = text;
|
||||
clearTimeout(copiedTimer);
|
||||
copiedTimer = window.setTimeout(() => (copied.value = ''), 2000);
|
||||
copied.value = { text, ok: true };
|
||||
copiedTimer = window.setTimeout(() => (copied.value = undefined), 2000);
|
||||
}
|
||||
|
||||
let acceptAnswer: ((answer: string) => Promise<void>) | undefined;
|
||||
@@ -161,11 +187,14 @@ let acceptAnswer: ((answer: string) => Promise<void>) | undefined;
|
||||
let session: AbortController | undefined;
|
||||
const side = ref<'send' | 'receive'>();
|
||||
|
||||
function begin(on: 'send' | 'receive') {
|
||||
// what goes into the log, so the sessions in it can be told apart.
|
||||
function begin(on: 'send' | 'receive', what: string) {
|
||||
session?.abort();
|
||||
session = new AbortController();
|
||||
side.value = on;
|
||||
events.log(`--- ${what} ---`);
|
||||
error.value = '';
|
||||
status.value = undefined;
|
||||
check.value = undefined;
|
||||
confirmed.value = false;
|
||||
sent.value = undefined;
|
||||
@@ -213,7 +242,8 @@ function formatSize(bytes: number) {
|
||||
}
|
||||
|
||||
async function onCreateOffer() {
|
||||
const signal = begin('send');
|
||||
const signal = begin('send', `manual offer for ${file.value!.name}`);
|
||||
shared.value = file.value;
|
||||
const result = await attempt(signal, createOffer(file.value!, sendEvents, signal));
|
||||
if (!result) return;
|
||||
offer.value = result.offer;
|
||||
@@ -227,13 +257,14 @@ function onAcceptAnswer() {
|
||||
}
|
||||
|
||||
async function onCreateAnswer() {
|
||||
const signal = begin('receive');
|
||||
const signal = begin('receive', 'manual answer');
|
||||
const result = await attempt(signal, answerOffer(offerInput.value, receiveEvents, signal));
|
||||
if (result) answer.value = result;
|
||||
}
|
||||
|
||||
async function onShare() {
|
||||
const signal = begin('send');
|
||||
const signal = begin('send', `share ${file.value!.name} via tracker`);
|
||||
shared.value = file.value;
|
||||
const code = await attempt(signal, sendViaTracker(file.value!, sendEvents, signal));
|
||||
if (!code) return;
|
||||
sendCode.value = code;
|
||||
@@ -246,7 +277,7 @@ async function onShare() {
|
||||
const codeGone = computed(() => sendExpired.value || !!sent.value || !!error.value);
|
||||
|
||||
function onReceive() {
|
||||
const signal = begin('receive');
|
||||
const signal = begin('receive', 'receive via tracker');
|
||||
attempt(signal, receiveViaTracker(receiveCode.value, receiveEvents, signal));
|
||||
}
|
||||
</script>
|
||||
@@ -301,11 +332,15 @@ function onReceive() {
|
||||
</template>
|
||||
<template v-else>Drop a file or click to choose</template>
|
||||
</label>
|
||||
<p v-if="sent">
|
||||
<progress id="send-progress" max="100" :value="percent(sent)" aria-label="Sent"></progress>
|
||||
{{ percent(sent) }} %
|
||||
</p>
|
||||
<p v-if="error && side === 'send'" id="send-error" role="alert">{{ error }}</p>
|
||||
<div v-if="side === 'send' && phase" :data-phase="phase">
|
||||
<p v-if="shared !== file" id="send-file">{{ shared!.name }}</p>
|
||||
<progress id="send-progress" max="100" :value="barValue(sent)" aria-label="Sent"></progress>
|
||||
<p v-if="error" id="send-error" role="alert">{{ error }}</p>
|
||||
<p v-else role="status">
|
||||
<span id="send-status">{{ status!.text }}</span>
|
||||
<span v-if="sent">{{ percent(sent) }} %</span>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<section>
|
||||
<h3>Tracker</h3>
|
||||
@@ -317,10 +352,19 @@ function onReceive() {
|
||||
<label v-else for="send-code">Code, give this to the receiver, valid until {{ sendUntil }}:</label>
|
||||
<div role="group">
|
||||
<output id="send-code" :data-expired="codeGone || undefined">{{ sendCode }}</output>
|
||||
<button id="copy-send-code" type="button" data-copy :disabled="codeGone" @click="onCopy(sendCode)">
|
||||
{{ copied === sendCode ? 'Copied' : 'Copy' }}
|
||||
<button
|
||||
id="copy-send-code"
|
||||
type="button"
|
||||
data-copy
|
||||
:disabled="codeGone"
|
||||
@click="onCopy(sendCode, 'send-code')"
|
||||
>
|
||||
{{ copyLabel(sendCode) }}
|
||||
</button>
|
||||
</div>
|
||||
<p v-if="copyFailed(sendCode)" role="alert">
|
||||
The browser blocked copying. The code is selected, copy it yourself.
|
||||
</p>
|
||||
</template>
|
||||
</section>
|
||||
|
||||
@@ -331,9 +375,12 @@ function onReceive() {
|
||||
<template v-if="offer">
|
||||
<label for="offer">Offer, send this to the receiver:</label>
|
||||
<textarea id="offer" readonly :value="offer"></textarea>
|
||||
<button id="copy-offer" type="button" data-copy @click="onCopy(offer)">
|
||||
{{ copied === offer ? 'Copied' : 'Copy' }}
|
||||
<button id="copy-offer" type="button" data-copy @click="onCopy(offer, 'offer')">
|
||||
{{ copyLabel(offer) }}
|
||||
</button>
|
||||
<p v-if="copyFailed(offer)" role="alert">
|
||||
The browser blocked copying. The offer is selected, copy it yourself.
|
||||
</p>
|
||||
<label for="answer-input">Answer from the receiver:</label>
|
||||
<textarea id="answer-input" v-model="answerInput"></textarea>
|
||||
<button id="accept-answer" type="button" :disabled="!answerInput.trim()" @click="onAcceptAnswer">
|
||||
@@ -354,11 +401,14 @@ function onReceive() {
|
||||
</template>
|
||||
<template v-else>The sender's file shows up here after the security check</template>
|
||||
</div>
|
||||
<p v-if="received">
|
||||
<progress id="receive-progress" max="100" :value="percent(received)" aria-label="Received"></progress>
|
||||
{{ percent(received) }} %
|
||||
</p>
|
||||
<p v-if="error && side === 'receive'" id="receive-error" role="alert">{{ error }}</p>
|
||||
<div v-if="side === 'receive' && phase" :data-phase="phase">
|
||||
<progress id="receive-progress" max="100" :value="barValue(received)" aria-label="Received"></progress>
|
||||
<p v-if="error" id="receive-error" role="alert">{{ error }}</p>
|
||||
<p v-else role="status">
|
||||
<span id="receive-status">{{ status!.text }}</span>
|
||||
<span v-if="received">{{ percent(received) }} %</span>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<section>
|
||||
<h3>Tracker</h3>
|
||||
@@ -394,9 +444,12 @@ function onReceive() {
|
||||
<template v-if="answer">
|
||||
<label for="answer">Answer, send this back to the sender:</label>
|
||||
<textarea id="answer" readonly :value="answer"></textarea>
|
||||
<button id="copy-answer" type="button" data-copy @click="onCopy(answer)">
|
||||
{{ copied === answer ? 'Copied' : 'Copy' }}
|
||||
<button id="copy-answer" type="button" data-copy @click="onCopy(answer, 'answer')">
|
||||
{{ copyLabel(answer) }}
|
||||
</button>
|
||||
<p v-if="copyFailed(answer)" role="alert">
|
||||
The browser blocked copying. The answer is selected, copy it yourself.
|
||||
</p>
|
||||
</template>
|
||||
</section>
|
||||
</details>
|
||||
|
||||
Reference in new issue
Block a user