fuzzyworld

north-star / files / src / features / session / session.ts

session.ts

Blame: the commit that last changed each line · file · history

f32f5e95 2026-03-16 Pavel Safronov1import { generateRoomKey, exportRoomKey, importRoomKey } from '../../core/crypto';
2import { buildRoomHash, parseRoomFromHash } from '../../core/room';
92f24c4e 2026-03-16 Pavel Safronov3import { renderInviteQrCard } from './qr';
32796e53 2026-03-16 Pavel Safronov4import { PUBLIC_SETTINGS } from '../../public_settings';
36d64b37 2026-03-15 Pavel Safronov5
6/**
7 * Parses an existing room from the URL hash, or generates a new one.
8 * Sets room UI elements and renders invite QR cards.
9 * Returns { roomId, roomKey, roomKeyBase64 }.
10 */
11export async function initSession({
12 roomCurrent,
13 createRoomLink,
14 inviteRoomQrList,
15 isLocalCommit,
16 BASE_ROOM_URL,
764d68de 2026-03-16 Pavel Safronov17 CREATE_ROOM_URL,
36d64b37 2026-03-15 Pavel Safronov18 GITLAB_PAGES_BASE_URL
b4e7f682 2026-03-16 Pavel Safronov19}: {
20 roomCurrent: HTMLElement | null;
21 createRoomLink: HTMLAnchorElement | null;
22 inviteRoomQrList: HTMLElement | null;
23 isLocalCommit: boolean;
24 BASE_ROOM_URL: string;
764d68de 2026-03-16 Pavel Safronov25 CREATE_ROOM_URL: string;
b4e7f682 2026-03-16 Pavel Safronov26 GITLAB_PAGES_BASE_URL: string;
36d64b37 2026-03-15 Pavel Safronov27}) {
b4e7f682 2026-03-16 Pavel Safronov28 let roomKey: CryptoKey | null = null;
29 let roomKeyBase64: string | null = null;
30 let roomId: string;
36d64b37 2026-03-15 Pavel Safronov31
32 const parsedRoom = parseRoomFromHash(location.hash);
33 if (parsedRoom) {
34 roomId = parsedRoom.roomId;
35 if (parsedRoom.roomKeyBase64) {
36 try {
37 roomKey = await importRoomKey(parsedRoom.roomKeyBase64);
38 roomKeyBase64 = parsedRoom.roomKeyBase64;
39 } catch (err) {
40 console.error('Failed to import room key from URL — running without encryption', err);
41 }
42 }
43 } else {
9fda5271 2026-03-16 Pavel Safronov44 roomId = crypto.randomUUID().replace(/-/g, '').slice(0, PUBLIC_SETTINGS.room.idLength);
36d64b37 2026-03-15 Pavel Safronov45 roomKey = await generateRoomKey();
46 roomKeyBase64 = await exportRoomKey(roomKey);
47 location.hash = buildRoomHash(roomId, roomKeyBase64);
48 }
49
50 if (roomCurrent) roomCurrent.textContent = `You are currently in room ${roomId}`;
764d68de 2026-03-16 Pavel Safronov51 if (createRoomLink) createRoomLink.href = CREATE_ROOM_URL;
36d64b37 2026-03-15 Pavel Safronov52
53 if (inviteRoomQrList) {
54 const inviteRoomUrl = `${BASE_ROOM_URL}#${buildRoomHash(roomId, roomKeyBase64)}`;
55 inviteRoomQrList.textContent = '';
56 const isLocalRun = isLocalCommit || ['localhost', '127.0.0.1', '::1'].includes(location.hostname);
57 const roomHash = buildRoomHash(roomId, roomKeyBase64);
58
59 await renderInviteQrCard({
60 container: inviteRoomQrList,
61 title: isLocalRun ? 'This Device URL' : '',
62 url: inviteRoomUrl,
63 statusText: 'Scan to join this room',
64 roomId
65 });
66
67 if (isLocalRun) {
68 await renderInviteQrCard({
69 container: inviteRoomQrList,
b10cf38c 2026-10-27 Pavel Safronov70 title: 'App URL',
36d64b37 2026-03-15 Pavel Safronov71 url: `${GITLAB_PAGES_BASE_URL}#${roomHash}`,
72 statusText: 'Scan to join on hosted app',
73 roomId
74 });
75 }
76 }
77
78 return { roomId, roomKey, roomKeyBase64 };
79}