import { DEVICE_NAME } from '@/constants/config'; import { SIGNAL_CLI_DATA } from '@/constants/paths'; import { isImapConnected } from '@/modules/protonmail'; import { checkSignalCli, finishLink, generateLinkQR, hasLinkUri, hasValidAccount, initSignal, restartDaemon, unlinkDevice, } from '@/modules/signal'; import { getAllMappings } from '@/modules/store'; export const handleHealthFragment = async () => { const signalOk = await checkSignalCli(); const linked = signalOk && (await hasValidAccount()); const imap = isImapConnected(); const html = `
Signal: ${signalOk ? 'Connected' : 'Disconnected'}
Account: ${linked ? 'Linked' : 'Unlinked'}
IMAP: ${imap ? 'Connected' : 'Disconnected'}
`; return new Response(html, { headers: { 'content-type': 'text/html' }, }); }; export const handleSignalInfoFragment = async () => { const linked = await hasValidAccount(); const html = linked ? `
Unlink and remove device
  1. Open Signal app → Settings → Linked Devices
  2. Find "${DEVICE_NAME}" and tap it
  3. Tap "Unlink Device"
` : ``; return new Response(html, { headers: { 'content-type': 'text/html' }, }); }; export const handleEndpointsFragment = async () => { const endpoints = getAllMappings(); if (endpoints.length === 0) { return new Response('

No endpoints registered

', { headers: { 'content-type': 'text/html' }, }); } const html = ` `; return new Response(html, { headers: { 'content-type': 'text/html' }, }); }; export const handleQRSection = async () => { const html = `

Scan this QR code with your Signal app:

Settings → Linked Devices → Link New Device

Generating QR code...
`; return new Response(html, { headers: { 'content-type': 'text/html' }, }); }; export const handleQRImage = async () => { const linked = await hasValidAccount(); if (!linked && (await Bun.file(SIGNAL_CLI_DATA).exists())) { await unlinkDevice(); await restartDaemon(); } const qrDataUrl = await generateLinkQR(); const html = `QR Code`; return new Response(html, { headers: { 'content-type': 'text/html' }, }); }; export const handleLinkStatusCheck = async () => { let linked = await hasValidAccount(); if (!linked && hasLinkUri()) { try { await finishLink(); const result = await initSignal(); linked = result.linked; } catch (error) { console.error('Failed to finish link:', error); } } if (linked) { return new Response('', { headers: { 'content-type': 'text/html', 'HX-Refresh': 'true', }, }); } return new Response('', { headers: { 'content-type': 'text/html' }, }); };