Files
kuentagym/frontend/src/App.jsx
T
dsantillan 87bfd6d4c4 Rebrand to KuentaGYM, and start a new profile in pounds and Spanish
The app is now KuentaGYM everywhere a user can see it, and a fresh profile no
longer starts on upstream's English/kg defaults. Both are this fork's decisions,
so both are pinned by tests rather than left to a find-and-replace.

Rename (1,270 references, 149 files)

- Visible surfaces: the Login/Home/mobile-onboarding wordmark, the Settings
  version line, the printed plan, the PWA title + manifest, and the 17 branded
  strings across all 14 locale packs.
- Infrastructure: repo links, the container images and the release endpoints now
  name git.dploy.lol/dsantillan/kuentagym; .gitea/workflows/docker-publish.yml
  publishes to this forge's registry instead of gitea.com.
- The Android/iOS application id stays ch.duartesantos.opengym on purpose:
  changing it would make the mobile app a different app that could never update
  an installed copy.

Three things the mechanical pass would have broken, fixed here

- The update check asked gitlab.com for upstream's releases and would have
  installed upstream's APK, putting the old name back on the phone. It now reads
  this fork's Gitea releases API (the assets shape differs), with the tests
  rewritten to the payload Gitea actually returns.
- Renaming the mobile state/pairing files (opengym-state.json,
  opengym-remote.json) would lose the on-device copy of a history whose WebView
  storage may have been evicted, and silently unpair the device. Both are read
  under their old names and written forward under the new ones.
- Renaming the exported plan-file marker (openGym_plan) rejected every plan
  shared before the rename as "not a plan file". Both markers are accepted.

Defaults: pounds and Spanish

- DEF.unit = 'lb', DEF.lang = 'es', plus every fallback that independently
  assumed en/kg (the <html lang> attribute, the Settings pickers, the Store, the
  MCP, the Coach payload).
- Server-generated push copy existed only in English and pt-BR, so a Spanish
  profile would have been notified in English; Spanish added.
- The demo seed states unit: 'kg' — its seeded numbers are kilograms, and
  inheriting the new default would have introduced an 82.4 lb lifter benching 60.
- The module-level language in i18n-core stays English: it is the only language
  whose strings need no pack, so `t()`, dates, numbers and lists agree before a
  pack loads. Starting there on 'es' formats a Spanish-shaped UI with English
  strings.

Also
- api/package.json's test glob missed push-messages.test.js and
  verify-error.test.js, which sat beside the modules they cover and never ran.
- README, docs/, website/ and the issue templates no longer describe upstream's
  GitLab/GitHub pipeline, Discord, sponsorship or public demo as if they were
  this fork's; upstream history and attribution are preserved (NOTICE.md).
2026-09-21 14:29:54 -04:00

199 lines
9.9 KiB
React

import { useEffect, useLayoutEffect, useRef } from 'react'
import { HashRouter, Routes, Route, Navigate, useNavigate, useLocation, useNavigationType } from 'react-router-dom'
import { useStore } from './store/useStore.js'
import { useUI } from './store/useUI.js'
import { bindUI } from './components/ui.jsx'
import { ACCENTS, setWeightDecimals } from './lib/format.js'
import { setLang, useLang } from './lib/i18n.js'
import { setPlayOnSilent } from './lib/sound.js'
import { setNav } from './lib/nav.js'
import { initBackButton } from './lib/back.js'
import { useWakeLock } from './lib/wakelock.js'
import { installViewportGuard } from './lib/viewport-guard.js'
import { installChipDrag } from './lib/hchips.js'
import { syncPushSubscription } from './lib/push.js'
import { MOBILE } from './lib/mobile.js'
import { startFlow } from './sheets.jsx'
import Icon from './components/Icon.jsx'
import TabBar from './components/TabBar.jsx'
import ErrorBoundary from './components/ErrorBoundary.jsx'
import Modals from './components/Modals.jsx'
import Toast from './components/Toast.jsx'
import SyncBanner from './components/SyncBanner.jsx'
import RestTimer from './components/RestTimer.jsx'
import TimerFlash from './components/TimerFlash.jsx'
import Login from './views/Login.jsx'
import MobileOnboarding from './views/MobileOnboarding.jsx'
import Home from './views/Home.jsx'
import CheckIn from './views/CheckIn.jsx'
import Plan from './views/Plan.jsx'
import RoutineEdit from './views/RoutineEdit.jsx'
import Workout from './views/Workout.jsx'
import Stats from './views/Stats.jsx'
import History from './views/History.jsx'
import Library from './views/Library.jsx'
import Muscles from './views/Muscles.jsx'
import Settings from './views/Settings.jsx'
import Admin from './views/Admin.jsx'
import CoachChat from './views/CoachChat.jsx'
import CoachIntake from './views/CoachIntake.jsx'
import CoachSetup from './views/CoachSetup.jsx'
// last known scrollY per route, so back-navigation can put the page where it was
const scrollPositions = new Map()
bindUI(useUI) // lets the shared controls open sheets without importing the store at module scope
// theme === 'system' follows the OS/browser preference instead of a fixed choice.
const resolveTheme = theme => theme === 'light' || theme === 'dark'
? theme
: (window.matchMedia?.('(prefers-color-scheme: dark)').matches ? 'dark' : 'light')
function applyPrefs(theme, accent) {
const de = document.documentElement
de.dataset.theme = resolveTheme(theme)
de.dataset.accent = ACCENTS[accent] ? accent : 'lime'
const meta = document.querySelector('meta[name="theme-color"]')
if (meta) meta.content = de.dataset.theme === 'light' ? '#f2f2f7' : '#000000'
}
function Shell() {
const navigate = useNavigate()
const loc = useLocation()
const navType = useNavigationType()
const { S, user, ready } = useStore()
// iOS: whether timer sounds get past the ring/silent switch (Settings → Sounds). Page-level,
// so it is applied here on load and on change rather than at each beep.
useEffect(() => { setPlayOnSilent(!!S.soundOnSilent) }, [S.soundOnSilent])
const isGuest = useStore(s => s.isGuest())
const needsMobileOnboarding = useStore(s => s.needsMobileOnboarding)
const langV = useLang() // re-renders the whole shell when the language (pack) changes
useEffect(() => { setNav(navigate) }, [navigate])
useEffect(() => { applyPrefs(S.theme, S.accent) }, [S.theme, S.accent])
// 'system' needs to react live if the OS theme flips while the app is open, not just on
// the next mount — a fixed 'dark'/'light' choice never re-fires this since matchMedia
// isn't consulted for those.
useEffect(() => {
if (S.theme !== 'system' || !window.matchMedia) return
const mql = window.matchMedia('(prefers-color-scheme: dark)')
const onChange = () => applyPrefs(S.theme, S.accent)
mql.addEventListener('change', onChange)
return () => mql.removeEventListener('change', onChange)
}, [S.theme, S.accent])
// 'es' is this fork's default language (see DEF in store/useStore.js); the fallback here only
// applies to a state that predates the setting entirely.
useEffect(() => { setLang(S.lang || 'es') }, [S.lang])
// Same shape as the language: a module-level display setting, pushed when it changes (#139).
useEffect(() => { setWeightDecimals(S.wdec) }, [S.wdec])
// `lang` on <html> drives hyphenation, font fallback and what a screen reader announces, so it
// has to agree with the language actually rendered — including this fork's default.
useEffect(() => { document.documentElement.lang = S.lang || 'es' }, [langV, S.lang])
// Forward navigation starts at the top; going back lands where you left off.
// The position is recorded from scroll events rather than read at route
// change, because by then a shorter page may already have clamped it.
const pathRef = useRef(null)
// iOS leaves the page displaced after the keyboard goes away (see lib/viewport-guard.js).
useEffect(() => installViewportGuard(), [])
// Click-drag a horizontal chip strip to scroll it sideways (lib/hchips.js) — on a desktop
// browser there's otherwise no way to reach the filters past the edge.
useEffect(() => installChipDrag(), [])
// Once per signed-in boot, hand the server this browser's push subscription again (see
// lib/push.js): a subscription the instance lost is back before the next reminder is due,
// with nobody having to visit Settings. Web only — the APK has no service worker.
useEffect(() => {
if (MOBILE || !user || !ready) return
syncPushSubscription().catch(() => {})
}, [user?.id, ready])
useEffect(() => {
const onScroll = () => {
// Modals pins the body while a sheet is open; scrollY is 0 then, not a position.
if (document.body.style.position === 'fixed') return
scrollPositions.set(pathRef.current, window.scrollY)
}
window.addEventListener('scroll', onScroll, { passive: true })
return () => window.removeEventListener('scroll', onScroll)
}, [])
useLayoutEffect(() => {
const samePath = pathRef.current === loc.pathname
pathRef.current = loc.pathname
if (navType !== 'POP') { window.scrollTo(0, 0); return }
// A POP that stays on the route we are on is not a back-navigation: it is the history
// entry a sheet pushed (Modals.jsx, #63) being unwound as the sheet closes. Nothing new
// mounted, Modals puts the page back where it was itself, and a view that scrolled on
// purpose because the sheet closed — the workout list going to the current exercise after
// ⋯ → Layout → List (#224) — must not be dragged back to a position recorded before that
// scroll's event had even been dispatched.
if (samePath) return
const y = scrollPositions.get(loc.pathname) || 0
// the restored view needs a layout pass before it is tall enough to scroll to y
const frame = window.requestAnimationFrame(() => window.scrollTo(0, y))
return () => window.cancelAnimationFrame(frame)
}, [loc.pathname, navType])
// bound to the workout, not to the route — checking Stats mid-session keeps the screen on
useWakeLock(!!S.active && S.keepAwake !== false)
const authed = user || isGuest
if (!ready && !authed) return (
<div id="app">
<div style={{ paddingTop: '44vh', display: 'flex', justifyContent: 'center', fontSize: 34, color: 'var(--label-3)' }}>
<Icon name="dumbbell" />
</div>
</div>
)
return (
<>
{/* keyed on the route: a view that throws is contained, and switching tabs
re-mounts the boundary, so the tab bar is always a way out */}
<div id="app" className="vfade" key={loc.pathname}>
<ErrorBoundary>
{authed && !needsMobileOnboarding && <SyncBanner />}
{!authed ? <Login /> : needsMobileOnboarding ? <MobileOnboarding /> : (
<Routes>
<Route path="/home" element={<Home />} />
{/* Gym check-in — switched off in Settings, the route falls through to the
catch-all redirect below. */}
{S.checkIn !== false && <Route path="/checkin" element={<CheckIn />} />}
<Route path="/plan" element={<Plan />} />
<Route path="/plan/r/:id" element={<RoutineEdit />} />
<Route path="/workout" element={<Workout />} />
<Route path="/stats" element={<Stats />} />
<Route path="/history" element={<History />} />
<Route path="/library" element={<Library />} />
<Route path="/muscles" element={<Muscles />} />
<Route path="/settings" element={<Settings />} />
{/* The Coach screens gate themselves on the instance config; the routes exist
unconditionally so a deep link from a notification lands somewhere sane
rather than on the catch-all. */}
<Route path="/coach" element={<CoachChat />} />
<Route path="/coach/intake" element={<CoachIntake />} />
<Route path="/coach/proposal" element={<Navigate to="/coach" replace />} />
<Route path="/coach/setup" element={<CoachSetup />} />
<Route path="/admin" element={user?.admin ? <Admin /> : <Navigate to="/home" replace />} />
<Route path="*" element={<Navigate to="/home" replace />} />
</Routes>
)}
</ErrorBoundary>
</div>
{/* The chat owns the bottom of the screen: its composer sits where the tabs would be. */}
{loc.pathname !== '/coach' && <TabBar onStart={startFlow} />}
<RestTimer />
<Modals />
<Toast />
<TimerFlash />
</>
)
}
export default function App() {
const boot = useStore(s => s.boot)
useEffect(() => { boot() }, [boot])
// Android system back — sheet, then page, then press-again-to-exit (see lib/back.js)
useEffect(() => {
let stop = null, gone = false
initBackButton().then(fn => { if (gone) fn(); else stop = fn })
return () => { gone = true; stop?.() }
}, [])
return <HashRouter><Shell /></HashRouter>
}