Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

Toast: an optional action (Undo) that stays 5 s and holds still under a finger

Duarte Santosf93e3f28a036
diff --git a/frontend/src/components/Icon.jsx b/frontend/src/components/Icon.jsx
index 33d17d86a..75ba4ab81 100644
--- a/frontend/src/components/Icon.jsx
+++ b/frontend/src/components/Icon.jsx
@@ -79,6 +79,8 @@ const P = {
pause: <path d="M9.4 5.8v12.4M14.6 5.8v12.4" />,
skipForward: <><path d="M6.4 5.8 15.2 12l-8.8 6.2Z" /><path d="M18.2 5.8v12.4" /></>,
reset: <><path d="M4.4 12a7.6 7.6 0 1 0 2.3-5.4" /><path d="M4 4.4v4.4h4.4" /></>,
+ // undo: an arrow curling back to the start
+ undo: <><path d="M8.6 14.4 4.2 10l4.4-4.4" /><path d="M4.6 10h9.6a5.4 5.4 0 0 1 0 10.8h-3" /></>,
bell: <><path d="M6.6 10.4a5.4 5.4 0 0 1 10.8 0c0 4 1.4 5.6 1.4 5.6H5.2s1.4-1.6 1.4-5.6Z" /><path d="M10.1 19.2a2.1 2.1 0 0 0 3.8 0" /></>,
bellSlash: <><path d="M8.1 6.6a5.4 5.4 0 0 1 9.3 3.8c0 4 1.4 5.6 1.4 5.6H9.4M6.6 16H5.2s1.4-1.6 1.4-5.6v-.6" /><path d="M10.1 19.2a2.1 2.1 0 0 0 3.8 0M4 3.6l16 16.8" /></>,
chevronRight: <path d="m9.6 5.6 6.6 6.4-6.6 6.4" />,
diff --git a/frontend/src/components/Toast.jsx b/frontend/src/components/Toast.jsx
index cb17095c4..d20bc8dd4 100644
--- a/frontend/src/components/Toast.jsx
+++ b/frontend/src/components/Toast.jsx
@@ -1,5 +1,20 @@
import { useUI } from '../store/useUI.js'
+import Icon from './Icon.jsx'
+
+// The one toast. Mostly text; with an action (the Undo after a set is removed) it carries a button
+// at its end, stays longer and holds still while a finger or the mouse rests on it.
export default function Toast() {
const msg = useUI(s => s.toastMsg)
- return <div id="toast" className={msg ? 'show' : ''}>{msg}</div>
+ const action = useUI(s => s.toastAction)
+ const { pauseToast, resumeToast, runToastAction } = useUI.getState()
+ const live = !!(msg && action)
+ return <div id="toast" role="status" className={(msg ? 'show' : '') + (live ? ' has-action' : '')}
+ onPointerEnter={live ? pauseToast : undefined} onPointerDown={live ? pauseToast : undefined}
+ onPointerLeave={live ? resumeToast : undefined} onPointerCancel={live ? resumeToast : undefined}
+ onPointerUp={live ? (e => { if (e.pointerType !== 'mouse') resumeToast() }) : undefined}>
+ <span className="toast-msg">{msg}</span>
+ {live && <button type="button" className="toast-act" key={action.id} onClick={runToastAction}>
+ <Icon name="undo" />{action.label}
+ </button>}
+ </div>
}
diff --git a/frontend/src/index.css b/frontend/src/index.css
index 518849c7a..a04ac677e 100644
--- a/frontend/src/index.css
+++ b/frontend/src/index.css
@@ -1602,6 +1602,12 @@ body.no-tabbar #timer.working{box-shadow:inset 0 var(--hair) 0 var(--acc),0 -10p
width:max-content;max-width:88vw;text-align:center;
}
#toast.show{opacity:1;transform:translateX(-50%) translateY(0)}
+/* With an action (Undo): the message, then the button at the end, and the toast takes taps. */
+#toast.has-action{pointer-events:auto;display:flex;align-items:center;gap:6px;padding:4px 6px 4px 18px;text-align:start}
+#toast.has-action .toast-msg{flex:1;min-width:0;padding:7px 0}
+#toast .toast-act{flex:none;display:inline-flex;align-items:center;gap:5px;color:var(--acc);font-weight:700;padding:8px 12px;border-radius:14px;min-height:36px}
+#toast .toast-act:active{background:color-mix(in srgb,var(--label) 10%,transparent)}
+#toast .toast-act .icn{font-size:16px}
body.no-tabbar #toast{bottom:calc(24px + var(--sab))}
body.no-tabbar.resting #toast{bottom:calc(100px + var(--sab))}
diff --git a/frontend/src/store/useUI.js b/frontend/src/store/useUI.js
index f303ff252..5a9c9584a 100644
--- a/frontend/src/store/useUI.js
+++ b/frontend/src/store/useUI.js
@@ -75,6 +75,18 @@ const maybeRestNotification = async () => {
}
let toastTm = null
+const TOAST_MS = 2200
+const TOAST_ACTION_MS = 5000
+let toastLeft = 0
+let toastT0 = 0
+let toastPaused = false
+let toastSeq = 0
+const runToast = set => {
+ toastPaused = false
+ toastT0 = Date.now()
+ clearTimeout(toastTm)
+ toastTm = setTimeout(() => set({ toastMsg: '', toastAction: null }), toastLeft)
+}
let timerInt = null
let timerTick = null
let workInt = null
@@ -135,6 +147,7 @@ const runRest = (set, get) => {
export const useUI = create((set, get) => ({
sheets: [], // { id, render:(close)=>JSX, kind:'sheet'|'center', locked }
toastMsg: '',
+ toastAction: null, // { label, run, id } while the toast offers an action (Undo)
timer: null, // rest countdown between sets — { left, total, endsAt, forIdx, ready?, paused?, kind? }
// kind: 'switch' for the short pause between the two sides of a timed set
// forIdx: index of the active entry whose set started the rest (undefined when unknown)
@@ -156,10 +169,35 @@ export const useUI = create((set, get) => ({
closeSheet(id) { set(s => ({ sheets: s.sheets.filter(x => x.id !== id) })) },
closeAll() { set({ sheets: [] }) },
- toast(msg) {
- set({ toastMsg: msg })
+ // A toast is 2.2 s of text. With an action (`{ action: 'Undo', onAction }`) it stays 5 s, shows
+ // the action as a button at its end and holds still while a finger rests on it (pauseToast). A
+ // new toast replaces the one showing, action and all: the old Undo is gone, what it would have
+ // undone stays done. The action runs once at most, and the toast goes with it.
+ toast(msg, { action, onAction, ms } = {}) {
+ const withAction = !!(action && typeof onAction === 'function')
+ clearTimeout(toastTm)
+ toastLeft = ms || (withAction ? TOAST_ACTION_MS : TOAST_MS)
+ toastSeq += 1
+ set({ toastMsg: msg, toastAction: withAction ? { label: action, run: onAction, id: toastSeq } : null })
+ runToast(set)
+ },
+ runToastAction() {
+ const act = get().toastAction
+ if (!act) return
clearTimeout(toastTm)
- toastTm = setTimeout(() => set({ toastMsg: '' }), 2200)
+ set({ toastMsg: '', toastAction: null })
+ act.run()
+ },
+ pauseToast() {
+ if (!get().toastAction || toastPaused) return
+ toastPaused = true
+ clearTimeout(toastTm)
+ toastLeft = Math.max(400, toastLeft - (Date.now() - toastT0))
+ },
+ resumeToast() {
+ if (!toastPaused) return
+ toastPaused = false
+ if (get().toastMsg) runToast(set)
},
startRest(sec, forIdx, { kind } = {}) {
diff --git a/frontend/src/store/useUI.toast.test.js b/frontend/src/store/useUI.toast.test.js
new file mode 100644
index 000000000..92924f1be
--- /dev/null
+++ b/frontend/src/store/useUI.toast.test.js
@@ -0,0 +1,62 @@
+// @vitest-environment happy-dom
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { useUI } from './useUI.js'
+
+beforeEach(() => { vi.useFakeTimers(); useUI.setState({ toastMsg: '', toastAction: null }) })
+afterEach(() => { vi.clearAllTimers(); vi.useRealTimers() })
+
+describe('toast with an action', () => {
+ it('a plain toast is text only and goes after 2.2 s', () => {
+ useUI.getState().toast('Saved')
+ expect(useUI.getState().toastMsg).toBe('Saved')
+ expect(useUI.getState().toastAction).toBeNull()
+ vi.advanceTimersByTime(2199)
+ expect(useUI.getState().toastMsg).toBe('Saved')
+ vi.advanceTimersByTime(2)
+ expect(useUI.getState().toastMsg).toBe('')
+ })
+
+ it('stays 5 s with an action, and the action fires once', () => {
+ const onAction = vi.fn()
+ useUI.getState().toast('Set 2 gone.', { action: 'Undo', onAction })
+ expect(useUI.getState().toastAction.label).toBe('Undo')
+ vi.advanceTimersByTime(4900)
+ expect(useUI.getState().toastMsg).toBe('Set 2 gone.')
+ useUI.getState().runToastAction()
+ useUI.getState().runToastAction()
+ expect(onAction).toHaveBeenCalledTimes(1)
+ expect(useUI.getState().toastMsg).toBe('')
+ })
+
+ it('times out after 5 s without running the action', () => {
+ const onAction = vi.fn()
+ useUI.getState().toast('Set 2 gone.', { action: 'Undo', onAction })
+ vi.advanceTimersByTime(5001)
+ expect(useUI.getState().toastMsg).toBe('')
+ expect(useUI.getState().toastAction).toBeNull()
+ useUI.getState().runToastAction()
+ expect(onAction).not.toHaveBeenCalled()
+ })
+
+ it('a new toast replaces the old one and its action', () => {
+ const first = vi.fn()
+ useUI.getState().toast('Set 2 gone.', { action: 'Undo', onAction: first })
+ useUI.getState().toast('Copied')
+ expect(useUI.getState().toastAction).toBeNull()
+ useUI.getState().runToastAction()
+ expect(first).not.toHaveBeenCalled()
+ })
+
+ it('holds still while touched and carries on afterwards', () => {
+ useUI.getState().toast('Set 2 gone.', { action: 'Undo', onAction: () => {} })
+ vi.advanceTimersByTime(3000)
+ useUI.getState().pauseToast()
+ vi.advanceTimersByTime(20000)
+ expect(useUI.getState().toastMsg).toBe('Set 2 gone.')
+ useUI.getState().resumeToast()
+ vi.advanceTimersByTime(1900)
+ expect(useUI.getState().toastMsg).toBe('Set 2 gone.')
+ vi.advanceTimersByTime(200)
+ expect(useUI.getState().toastMsg).toBe('')
+ })
+})