2026-07-10 15:57:19 -04:00
|
|
|
import React, { useCallback, useRef, useState } from 'react';
|
2026-07-02 22:19:22 -04:00
|
|
|
import { Box, Button, config, Icon, Icons, Text } from 'folds';
|
2026-07-10 15:57:19 -04:00
|
|
|
import { QRCodeSVG, QRCodeCanvas } from 'qrcode.react';
|
2026-06-02 19:31:30 -04:00
|
|
|
import { SequenceCard } from '../../../components/sequence-card';
|
|
|
|
|
import { SequenceCardStyle } from '../../room-settings/styles.css';
|
|
|
|
|
import { SettingTile } from '../../../components/setting-tile';
|
|
|
|
|
import { CutoutCard } from '../../../components/cutout-card';
|
|
|
|
|
import { useMatrixClient } from '../../../hooks/useMatrixClient';
|
|
|
|
|
import { useRoom } from '../../../hooks/useRoom';
|
|
|
|
|
import { getMatrixToRoom } from '../../../plugins/matrix-to';
|
2026-07-10 15:57:19 -04:00
|
|
|
import { useSaveFile } from '../../../hooks/useSaveFile';
|
|
|
|
|
|
|
|
|
|
// Filesystem-safe basename from an arbitrary room name.
|
|
|
|
|
function sanitizeFilename(name: string): string {
|
|
|
|
|
const cleaned = name.replace(/[^a-z0-9\-_ ]+/gi, '').trim().replace(/\s+/g, '-');
|
|
|
|
|
return cleaned || 'room';
|
|
|
|
|
}
|
2026-06-02 19:31:30 -04:00
|
|
|
|
|
|
|
|
export function RoomShareInvite() {
|
|
|
|
|
const mx = useMatrixClient();
|
|
|
|
|
const room = useRoom();
|
2026-07-10 15:57:19 -04:00
|
|
|
const saveFile = useSaveFile();
|
2026-06-02 19:31:30 -04:00
|
|
|
const [copied, setCopied] = useState(false);
|
2026-07-10 15:57:19 -04:00
|
|
|
// Offscreen high-resolution canvas used only to export the QR as a PNG; the
|
|
|
|
|
// visible code stays an SVG so it renders crisply at any size/theme.
|
|
|
|
|
const qrCanvasRef = useRef<HTMLCanvasElement>(null);
|
2026-06-02 19:31:30 -04:00
|
|
|
|
|
|
|
|
const domain = mx.getDomain() ?? undefined;
|
|
|
|
|
const inviteUrl = getMatrixToRoom(room.roomId, domain ? [domain] : undefined);
|
|
|
|
|
|
|
|
|
|
const handleCopy = useCallback(() => {
|
|
|
|
|
navigator.clipboard.writeText(inviteUrl).then(() => {
|
|
|
|
|
setCopied(true);
|
|
|
|
|
setTimeout(() => setCopied(false), 2000);
|
|
|
|
|
});
|
|
|
|
|
}, [inviteUrl]);
|
|
|
|
|
|
2026-07-10 15:57:19 -04:00
|
|
|
const handleDownloadQr = useCallback(() => {
|
|
|
|
|
const canvas = qrCanvasRef.current;
|
|
|
|
|
if (!canvas) return;
|
|
|
|
|
canvas.toBlob((blob) => {
|
|
|
|
|
if (blob) saveFile(blob, `${sanitizeFilename(room.name)}-invite-qr.png`);
|
|
|
|
|
}, 'image/png');
|
|
|
|
|
}, [saveFile, room.name]);
|
|
|
|
|
|
2026-06-02 19:31:30 -04:00
|
|
|
return (
|
|
|
|
|
<SequenceCard
|
|
|
|
|
className={SequenceCardStyle}
|
|
|
|
|
variant="SurfaceVariant"
|
|
|
|
|
direction="Column"
|
|
|
|
|
gap="400"
|
|
|
|
|
>
|
|
|
|
|
<SettingTile
|
|
|
|
|
title="Share Room"
|
|
|
|
|
description="Share this invite link so others can join the room."
|
|
|
|
|
/>
|
|
|
|
|
<CutoutCard variant="Surface" style={{ padding: config.space.S300 }}>
|
|
|
|
|
<Box direction="Column" gap="300">
|
|
|
|
|
<Box gap="200" alignItems="Center">
|
|
|
|
|
<Box grow="Yes">
|
|
|
|
|
<Text
|
|
|
|
|
size="T200"
|
|
|
|
|
style={{
|
|
|
|
|
wordBreak: 'break-all',
|
|
|
|
|
userSelect: 'all',
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{inviteUrl}
|
|
|
|
|
</Text>
|
|
|
|
|
</Box>
|
|
|
|
|
<Box shrink="No">
|
|
|
|
|
<Button
|
|
|
|
|
size="300"
|
|
|
|
|
variant={copied ? 'Success' : 'Secondary'}
|
|
|
|
|
fill={copied ? 'Solid' : 'Soft'}
|
|
|
|
|
radii="300"
|
|
|
|
|
onClick={handleCopy}
|
|
|
|
|
before={<Icon size="100" src={copied ? Icons.Check : Icons.Link} />}
|
|
|
|
|
>
|
|
|
|
|
<Text size="B300">{copied ? 'Copied!' : 'Copy Link'}</Text>
|
|
|
|
|
</Button>
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
2026-07-10 15:57:19 -04:00
|
|
|
<Box direction="Column" alignItems="Center" gap="300">
|
2026-07-02 22:19:22 -04:00
|
|
|
{/* Generated locally (qrcode.react) — no third-party service, works
|
|
|
|
|
offline + under strict CSP. White padded quiet-zone so the
|
|
|
|
|
default black-on-white code scans on any theme. */}
|
|
|
|
|
<Box
|
|
|
|
|
style={{
|
|
|
|
|
padding: config.space.S200,
|
|
|
|
|
background: '#ffffff',
|
|
|
|
|
borderRadius: config.radii.R300,
|
|
|
|
|
lineHeight: 0,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<QRCodeSVG value={inviteUrl} size={160} level="M" title="Room invite QR code" />
|
|
|
|
|
</Box>
|
2026-07-10 15:57:19 -04:00
|
|
|
<Button
|
|
|
|
|
size="300"
|
|
|
|
|
variant="Secondary"
|
|
|
|
|
fill="Soft"
|
|
|
|
|
radii="300"
|
|
|
|
|
onClick={handleDownloadQr}
|
|
|
|
|
before={<Icon size="100" src={Icons.Download} />}
|
|
|
|
|
>
|
|
|
|
|
<Text size="B300">Download QR</Text>
|
|
|
|
|
</Button>
|
2026-06-02 19:31:30 -04:00
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
</CutoutCard>
|
2026-07-10 15:57:19 -04:00
|
|
|
{/* Offscreen, high-res (spec 4-module quiet zone) source for PNG export. */}
|
|
|
|
|
<QRCodeCanvas
|
|
|
|
|
ref={qrCanvasRef}
|
|
|
|
|
value={inviteUrl}
|
|
|
|
|
size={1024}
|
|
|
|
|
level="M"
|
|
|
|
|
marginSize={4}
|
|
|
|
|
style={{ display: 'none' }}
|
|
|
|
|
/>
|
2026-06-02 19:31:30 -04:00
|
|
|
</SequenceCard>
|
|
|
|
|
);
|
|
|
|
|
}
|