Files
sfhorizont/wasm/frontend/src/screens/Setup.jsx
T

140 lines
5.7 KiB
React
Raw Normal View History

2026-10-24 13:20:55 +02:00
import { useCallback, useEffect, useState } from 'react'
import { IconBack, IconPin, IconCompass, IconChevronRight } from '../components/Icons'
import { latText, lonText } from '../lib/format'
import { startCamera, startOrientation } from '../lib/camera'
import { errText } from '../lib/wasm'
import { t } from '../i18n'
function useLocation() {
const [state, setState] = useState(() => (navigator.geolocation
? { status: 'locating', loc: null, error: '' }
: { status: 'error', loc: null, error: t('setup.noGeo') }))
const locate = useCallback(() => {
if (!navigator.geolocation) return
navigator.geolocation.getCurrentPosition(
(p) => setState({
status: 'ok',
error: '',
loc: {
latDeg: p.coords.latitude,
lonDeg: p.coords.longitude,
elevM: p.coords.altitude,
accuracyM: p.coords.accuracy,
},
}),
() => setState({ status: 'error', loc: null, error: t('setup.geoUnavailable') }),
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 60000 },
)
}, [])
useEffect(() => { locate() }, [locate])
const refresh = () => {
if (!navigator.geolocation) return
setState((s) => ({ ...s, status: 'locating', error: '' }))
locate()
}
return [state, refresh]
}
export default function Setup({ suggestedName, onBack, onStart }) {
const [name, setName] = useState(suggestedName)
const [location, refresh] = useLocation()
const [starting, setStarting] = useState(false)
const [error, setError] = useState('')
const start = async () => {
setError('')
setStarting(true)
// Sensors first and synchronously from the tap: iOS only asks inside a gesture.
const ori = startOrientation()
try {
await ori
} catch (e) {
setStarting(false)
setError(t('setup.sensorsError', { error: errText(e) }))
return
}
try {
await startCamera()
} catch (e) {
setStarting(false)
setError(t('setup.cameraError', { error: errText(e) }))
return
}
onStart({ name: name.trim() || suggestedName, location: location.loc })
}
const loc = location.loc
return (
<div className="screen">
<div className="screen-body">
<div className="title-row">
<button className="icon-btn back" aria-label={t('common.back')} onClick={onBack}><IconBack className="flip-rtl" /></button>
<h1>{t('setup.title')}</h1>
</div>
<div style={{ marginTop: 16, display: 'flex', flexDirection: 'column', gap: 22 }}>
<div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
<label htmlFor="site-name" className="overline">{t('setup.siteName')}</label>
<input id="site-name" className="text-input" value={name} maxLength={60}
onChange={(e) => setName(e.target.value)} autoComplete="off" dir="auto" />
<div className="row-card">
<IconPin style={{ color: 'var(--info)', flex: 'none' }} />
<div className="grow">
{loc ? (
<>
<span className="mono" dir="ltr" style={{ fontSize: 13 }}>{latText(loc.latDeg)} · {lonText(loc.lonDeg)}</span>
<span className="small">
{loc.elevM != null ? t('setup.elevation', { m: Math.round(loc.elevM) }) : t('setup.altitudeUnknown')} · {t('setup.gps', { m: Math.round(loc.accuracyM) })}
</span>
</>
) : (
<>
<span className="strong">{location.status === 'locating' ? t('setup.locating') : t('common.noLocation')}</span>
<span className="small">{location.error || t('setup.locationHint')}</span>
</>
)}
</div>
<button className="btn-small" onClick={refresh} disabled={location.status === 'locating'}>{t('setup.refresh')}</button>
</div>
<div className="row-card">
<IconCompass style={{ color: 'var(--info)', flex: 'none' }} />
<div className="grow">
<span className="strong">{t('setup.northTitle')}</span>
<span className="small">{t('setup.northHint')}</span>
</div>
</div>
</div>
<div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
<span className="overline">{t('setup.howTo')}</span>
<svg viewBox="0 0 350 120" width="100%" role="img" style={{ display: 'block' }}
aria-label={t('setup.illustration')}>
<ellipse cx="175" cy="66" rx="150" ry="34" fill="none" stroke="#263042" strokeWidth="22" />
<path d="M25 66 A150 34 0 0 0 325 66" fill="none" stroke="#F5B544" strokeOpacity="0.35" strokeWidth="22" />
<path d="M296 44 l14 8 -16 4" fill="none" stroke="#F5B544" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
<rect x="163" y="40" width="24" height="44" rx="5" fill="#0A0D13" stroke="#E8ECF2" strokeWidth="2" />
<text x="338" y="20" textAnchor="end" fontFamily="IBM Plex Mono, ui-monospace, monospace" fontSize="10" fill="#93A0B4">{t('setup.skyNotNeeded')}</text>
</svg>
<ol className="how-list">
<li>{t('setup.step1')}</li>
<li>{t('setup.step2')}</li>
<li>{t('setup.step3')}</li>
</ol>
</div>
{error && <div className="notice error">{error}</div>}
</div>
</div>
<div className="screen-actions">
<button className="btn btn-primary" onClick={start} disabled={starting}>
{starting ? t('common.startingCamera') : t('setup.start')}
{!starting && <IconChevronRight className="flip-rtl" />}
</button>
</div>
</div>
)
}