north-star / main_old / files / src / features / session / session.ts
Branch main_old · default branch · all branches
session.ts
f32f5e95 2026-03-16 Pavel Safronov | 1 | import { generateRoomKey, exportRoomKey, importRoomKey } from '../../core/crypto'; |
| 2 | import { buildRoomHash, parseRoomFromHash } from '../../core/room'; | |
92f24c4e 2026-03-16 Pavel Safronov | 3 | import { renderInviteQrCard } from './qr'; |
32796e53 2026-03-16 Pavel Safronov | 4 | import { PUBLIC_SETTINGS } from '../../public_settings'; |
36d64b37 2026-03-15 Pavel Safronov | 5 | |
| 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 | */ | |
| 11 | export async function initSession({ | |
| 12 | roomCurrent, | |
| 13 | createRoomLink, | |
| 14 | inviteRoomQrList, | |
| 15 | isLocalCommit, | |
| 16 | BASE_ROOM_URL, | |
764d68de 2026-03-16 Pavel Safronov | 17 | CREATE_ROOM_URL, |
36d64b37 2026-03-15 Pavel Safronov | 18 | GITLAB_PAGES_BASE_URL |
b4e7f682 2026-03-16 Pavel Safronov | 19 | }: { |
| 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 Safronov | 25 | CREATE_ROOM_URL: string; |
b4e7f682 2026-03-16 Pavel Safronov | 26 | GITLAB_PAGES_BASE_URL: string; |
36d64b37 2026-03-15 Pavel Safronov | 27 | }) { |
b4e7f682 2026-03-16 Pavel Safronov | 28 | let roomKey: CryptoKey | null = null; |
| 29 | let roomKeyBase64: string | null = null; | |
| 30 | let roomId: string; | |
36d64b37 2026-03-15 Pavel Safronov | 31 | |
| 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 Safronov | 44 | roomId = crypto.randomUUID().replace(/-/g, '').slice(0, PUBLIC_SETTINGS.room.idLength); |
36d64b37 2026-03-15 Pavel Safronov | 45 | 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 Safronov | 51 | if (createRoomLink) createRoomLink.href = CREATE_ROOM_URL; |
36d64b37 2026-03-15 Pavel Safronov | 52 | |
| 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, | |
| 70 | title: 'GitLab Pages URL', | |
| 71 | 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 | } |