205 lines
12 KiB
Vue
205 lines
12 KiB
Vue
<script setup lang="ts">
|
||
import { computed, nextTick, onBeforeUnmount, onMounted, ref } from 'vue';
|
||
import ConnectionWorkspace from './components/ConnectionWorkspace.vue';
|
||
import AppDialog from './components/AppDialog.vue';
|
||
import EditableName from './components/EditableName.vue';
|
||
import InvitationDialog from './components/InvitationDialog.vue';
|
||
import { addConnection, CONNECTION_LIMIT, emptyConnection, loadConnections, removeConnection, saveConnection, updateConnection, type ConnectionActivity, type ConnectionUpdate, type SavedConnection } from './lib/connections.ts';
|
||
import { takeFragment } from './lib/invitations.ts';
|
||
import { asError } from './lib/validation.ts';
|
||
|
||
// A code from the link this page was opened with, already removed from the URL.
|
||
const props = defineProps<{ initialCode?: string }>();
|
||
|
||
const logoUrl = `${import.meta.env.BASE_URL}HellNet_alt.png`;
|
||
const giteaIconUrl = `${import.meta.env.BASE_URL}gitea.svg`;
|
||
const connections = ref<SavedConnection[]>([]), selected = ref('');
|
||
const states = ref<Record<string, 'ready' | 'connecting' | 'connected'>>({});
|
||
const current = computed(() => connections.value.find(item => item.id === selected.value));
|
||
const sidebarOpen = ref(false), adding = ref(false);
|
||
const confirmingRemoval = ref(false), removing = ref(false);
|
||
const error = ref(''), removalError = ref('');
|
||
const heading = ref<HTMLElement | null>(null);
|
||
// An opened link awaiting review, and an accepted offer on its way to a slot.
|
||
const invitation = ref<{ code: string } | null>(null);
|
||
const handoff = ref<{ connection: string; code: string } | null>(null);
|
||
const canAdd = computed(() => !adding.value && !removing.value && connections.value.length < CONNECTION_LIMIT);
|
||
let persistent = true, pending = 0, writes = Promise.resolve(), refreshRequested = false;
|
||
let updates: BroadcastChannel | undefined;
|
||
function select(id: string) {
|
||
selected.value = id; confirmingRemoval.value = false; sidebarOpen.value = false;
|
||
void nextTick(() => heading.value?.focus());
|
||
}
|
||
function applyRecords(records: SavedConnection[]) {
|
||
connections.value = records;
|
||
for (const id of Object.keys(states.value)) if (!records.some(item => item.id === id)) delete states.value[id];
|
||
if (!records.some(item => item.id === selected.value)) select(records[0]!.id);
|
||
}
|
||
function setStatus(id: string, state: 'ready' | 'connecting' | 'connected') {
|
||
if (connections.value.some(item => item.id === id)) states.value[id] = state;
|
||
}
|
||
function statusLabel(id: string) { return states.value[id] === 'connected' ? 'Connected' : states.value[id] === 'connecting' ? 'Connecting' : 'Ready to connect'; }
|
||
async function refresh() {
|
||
if (!persistent) return;
|
||
if (pending || removing.value) { refreshRequested = true; return; }
|
||
refreshRequested = false;
|
||
try { const records = await loadConnections(); if (!pending && !removing.value) applyRecords(records); else refreshRequested = true; }
|
||
catch (err) { error.value = asError(err).message; }
|
||
}
|
||
onMounted(async () => {
|
||
try { connections.value = await loadConnections(); }
|
||
catch (err) {
|
||
persistent = false; connections.value = [emptyConnection()];
|
||
error.value = `${asError(err).message} Connections and activity will only last for this tab.`;
|
||
}
|
||
selected.value = connections.value[0]!.id;
|
||
if (typeof BroadcastChannel !== 'undefined') {
|
||
updates = new BroadcastChannel('hellcon-connections');
|
||
updates.onmessage = () => { void refresh(); };
|
||
}
|
||
window.addEventListener('focus', refresh);
|
||
window.addEventListener('hashchange', openLink);
|
||
if (props.initialCode) invitation.value = { code: props.initialCode };
|
||
});
|
||
onBeforeUnmount(() => { updates?.close(); window.removeEventListener('focus', refresh); window.removeEventListener('hashchange', openLink); });
|
||
// A link opened in an existing tab only changes the fragment.
|
||
function openLink() {
|
||
const code = takeFragment();
|
||
if (code) invitation.value = { code };
|
||
}
|
||
async function add() {
|
||
if (adding.value || removing.value || connections.value.length >= CONNECTION_LIMIT) return;
|
||
adding.value = true;
|
||
try {
|
||
await writes;
|
||
if (persistent) connections.value = await addConnection();
|
||
else connections.value.push(emptyConnection(Math.max(...connections.value.map(item => item.number)) + 1));
|
||
const id = connections.value.at(-1)!.id;
|
||
select(id); updates?.postMessage('changed');
|
||
return id;
|
||
} catch (err) { error.value = asError(err).message; }
|
||
finally { adding.value = false; }
|
||
}
|
||
async function openInvitation(target: string, code: string) {
|
||
const id = target === 'new' ? await add() : target;
|
||
if (!id || !connections.value.some(item => item.id === id)) return;
|
||
if ((states.value[id] ?? 'ready') !== 'ready') { error.value = 'That connection is in use. Open the link again and choose another connection.'; return; }
|
||
select(id); handoff.value = { connection: id, code };
|
||
}
|
||
function update(id: string, change: ConnectionUpdate) {
|
||
const index = connections.value.findIndex(item => item.id === id);
|
||
if (index < 0) return;
|
||
try { connections.value[index] = updateConnection(connections.value[index]!, change); }
|
||
catch (err) { error.value = asError(err).message; return; }
|
||
if (!persistent) return;
|
||
pending++;
|
||
writes = writes.then(async () => {
|
||
try {
|
||
const records = await saveConnection(id, change);
|
||
if (pending === 1) applyRecords(records);
|
||
updates?.postMessage('changed');
|
||
} catch (err) {
|
||
// Keep this tab's activity usable without pretending it reached storage.
|
||
error.value = `${asError(err).message} Your latest changes may be lost when this tab closes.`;
|
||
refreshRequested = true;
|
||
} finally { pending--; if (!pending && refreshRequested) void refresh(); }
|
||
});
|
||
}
|
||
function record(id: string, activity: ConnectionActivity) { update(id, { activity }); }
|
||
// Connection setup re-reads the record so trust forgotten in another tab is
|
||
// never reused from this tab's copy. A failed read stops setup.
|
||
async function trust(id: string) {
|
||
await writes;
|
||
const records = persistent ? await loadConnections() : connections.value;
|
||
const record = records.find(item => item.id === id);
|
||
if (!record) throw new Error('This connection is no longer saved. Reload the page.');
|
||
if (persistent && !pending && !removing.value) applyRecords(records);
|
||
return record;
|
||
}
|
||
async function remove() {
|
||
if (!current.value || removing.value || adding.value) return;
|
||
const id = current.value.id;
|
||
removing.value = true; removalError.value = '';
|
||
try {
|
||
await writes;
|
||
const records = persistent ? await removeConnection(id) : connections.value.filter(item => item.id !== id);
|
||
applyRecords(records.length ? records : [emptyConnection()]);
|
||
confirmingRemoval.value = false;
|
||
updates?.postMessage('changed');
|
||
} catch (err) { removalError.value = asError(err).message; }
|
||
finally { removing.value = false; if (refreshRequested) void refresh(); }
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<a class="skip-link" href="#main-content">Skip to main content</a>
|
||
<div class="app-shell" :class="{ 'sidebar-open': sidebarOpen }">
|
||
<aside id="connections-sidebar" class="sidebar" aria-label="Connections sidebar">
|
||
<header class="brand">
|
||
<img :src="logoUrl" width="38" height="38" alt="" />
|
||
<div><span class="brand-name">HellCon</span><p>Direct file sharing</p></div>
|
||
</header>
|
||
<div class="sidebar-section-heading"><h2>Connections</h2><span>{{ connections.length }}</span></div>
|
||
<nav class="connection-nav" aria-label="Connections">
|
||
<button v-for="item in connections" :key="item.id" class="connection-link" :class="{ selected: selected === item.id }"
|
||
:aria-current="selected === item.id ? 'page' : undefined" @click="select(item.id)">
|
||
<span class="connection-avatar" aria-hidden="true">{{ item.name.slice(0, 1).toUpperCase() }}</span>
|
||
<span class="connection-label"><span class="connection-name">{{ item.name }}</span><span class="connection-preview">{{ item.history.at(-1)?.name || (item.peerKey ? 'Remembered device' : 'No activity yet') }}</span></span>
|
||
<span class="connection-dot" :class="states[item.id] || 'ready'" :aria-label="statusLabel(item.id)"></span>
|
||
</button>
|
||
</nav>
|
||
<button class="add-connection" :disabled="adding || removing || !connections.length || connections.length >= CONNECTION_LIMIT" @click="add"><span aria-hidden="true">+</span> Add connection</button>
|
||
<footer class="sidebar-footer">
|
||
<span class="sidebar-tag">Browser to browser</span>
|
||
<p>No accounts. Your connections stay on this device.</p>
|
||
<details class="privacy-details">
|
||
<summary>About privacy & connections</summary>
|
||
<p>Files travel over encrypted WebRTC data channels. Compare full fingerprints on first pairing, or let a connection authenticate its remembered device. Reconnect by exchanging fresh codes.</p>
|
||
<p>Trust your browser and the app’s host: its code can read selected files and use your device identity. HTTPS protects delivery. A TURN relay is used only after opt-in.</p>
|
||
<p>Clearing site data removes saved connections, identities, and history.</p>
|
||
</details>
|
||
<a class="source-link" href="https://gitea.pagequit.net/Schluffe/HellCon" target="_blank" rel="noopener noreferrer" aria-label="View HellCon source code on Gitea">
|
||
<img :src="giteaIconUrl" width="20" height="20" alt="" />
|
||
<span>Source code</span>
|
||
</a>
|
||
</footer>
|
||
</aside>
|
||
<main id="main-content" class="main-pane" tabindex="-1">
|
||
<header class="connection-header">
|
||
<button class="sidebar-toggle" :aria-expanded="sidebarOpen" aria-controls="connections-sidebar" @click="sidebarOpen = !sidebarOpen">{{ sidebarOpen ? 'Hide connections' : 'Show connections' }}</button>
|
||
<template v-if="current">
|
||
<div class="connection-title">
|
||
<p class="eyebrow">Connection</p>
|
||
<div class="title-line">
|
||
<h1 ref="heading" class="sr-only" tabindex="-1">{{ current.name }}</h1>
|
||
<EditableName :key="current.id" :name="current.name" :disabled="removing" @rename="update(current!.id, { name: $event })" />
|
||
<button class="header-action" :disabled="adding || removing" @click="removalError = ''; confirmingRemoval = true"><span class="header-action-icon" aria-hidden="true">✕</span> Remove</button>
|
||
</div>
|
||
</div>
|
||
<span class="session-badge" :class="states[current.id] || 'ready'"><span class="connection-dot" :class="states[current.id] || 'ready'" aria-hidden="true"></span>{{ statusLabel(current.id) }}</span>
|
||
</template>
|
||
</header>
|
||
<div class="workspace-scroll">
|
||
<p v-if="error" class="storage-error" role="alert">{{ error }}</p>
|
||
<template v-for="item in connections" :key="item.id">
|
||
<KeepAlive>
|
||
<ConnectionWorkspace v-if="selected === item.id" :connection="item" :load-trust="trust" :invitation="handoff?.connection === item.id ? handoff : null"
|
||
@status="setStatus" @activity="record" @update="update" @invitation-taken="handoff = null" />
|
||
</KeepAlive>
|
||
</template>
|
||
<p v-if="!current" class="status">Opening your connections…</p>
|
||
</div>
|
||
</main>
|
||
</div>
|
||
<AppDialog v-model="confirmingRemoval" :title="`Remove ${current?.name || 'connection'}?`" :busy="removing">
|
||
<p>This ends its session and deletes its saved activity and remembered device. Saved files and your device identity are kept.</p>
|
||
<p v-if="connections.length === 1">A fresh Con 1 will replace your last connection.</p>
|
||
<p v-if="removalError" class="error" role="alert">{{ removalError }}</p>
|
||
<template #actions="{ close }">
|
||
<button :disabled="removing" autofocus @click="close">Keep connection</button>
|
||
<button class="danger" :disabled="removing" @click="remove">{{ removing ? 'Removing…' : 'Remove connection' }}</button>
|
||
</template>
|
||
</AppDialog>
|
||
<InvitationDialog v-if="current" v-model="invitation" :connections="connections" :states="states" :current="current.id" :can-add="canAdd" @open="openInvitation" />
|
||
</template>
|