import { StrictMode, useEffect, useState } from 'react';
import { createRoot } from 'react-dom/client';
import { api, SignedOut, type CanvasData } from './api';
import { Canvas } from './Canvas';
import { JoinWithApp } from './MacApp';
import './styles.css';
/** Sign in with a code from one of your devices (no passwords, §8). */
function SignIn({ onDone }: { onDone: () => void }) {
const expired = new URLSearchParams(location.search).has('expired');
const [code, setCode] = useState('');
const [error, setError] = useState(expired ? 'That sign-in link was used or expired. Run `lunesync open` again, or use a code.' : '');
const submit = async (e: React.FormEvent) => {
e.preventDefault();
try {
await api.signin(code);
history.replaceState(null, '', '/');
onDone();
} catch (err) {
setError((err as Error).message);
}
};
return (
);
}
/** What an invite link shows in a browser: get the Mac app and join from it, or join a server from the terminal. */
function Join() {
const link = location.href;
return (
Lune · invite
You're invited to a team
Joining from a server or the terminal instead
lunesync join {link} --name "My server"
);
}
function App() {
const [data, setData] = useState(null);
const [state, setState] = useState<'loading' | 'signin' | 'ready' | 'error'>('loading');
const [error, setError] = useState('');
const load = async () => {
try {
setData(await api.canvas());
setState('ready');
} catch (e) {
if (e instanceof SignedOut) setState('signin');
else {
setError((e as Error).message);
setState('error');
}
}
};
useEffect(() => {
if (!location.pathname.startsWith('/join/')) load();
}, []);
if (location.pathname.startsWith('/join/')) return ;
if (state === 'signin') return ;
if (state === 'error') return