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.

feat: add copy routine button to RoutineEdit view

Sebastian G19f09dcccc5e
diff --git a/frontend/src/lib/routines.js b/frontend/src/lib/routines.js
new file mode 100644
index 000000000..7613b302b
--- /dev/null
+++ b/frontend/src/lib/routines.js
@@ -0,0 +1,12 @@
+import { uid } from './format.js'
+
+/**
+ * Create a deep copy of a routine with a new id and a "(Copy)" suffix.
+ * All exercises and their configuration are preserved independently.
+ */
+export function copyRoutine(routine, suffix = 'Copy') {
+ const copy = structuredClone(routine)
+ copy.id = uid()
+ copy.name = routine.name + ' (' + suffix + ')'
+ return copy
+}
diff --git a/frontend/src/lib/routines.test.js b/frontend/src/lib/routines.test.js
new file mode 100644
index 000000000..8c6912bfc
--- /dev/null
+++ b/frontend/src/lib/routines.test.js
@@ -0,0 +1,79 @@
+import { describe, it, expect } from 'vitest'
+import { copyRoutine } from './routines.js'
+
+const routine = {
+ id: 'r1',
+ name: 'Push Day',
+ emoji: 'arm',
+ prog: 'linear',
+ ex: [
+ { id: 'ex1', sets: 3, reps: 10, weight: 60, sg: 'sg1' },
+ { id: 'ex2', sets: 4, reps: 8, weight: 80, sg: 'sg1' },
+ { id: 'ex3', sets: 3, reps: 12, weight: 20 }
+ ]
+}
+
+describe('copyRoutine', () => {
+ it('returns a new routine with a different id', () => {
+ const copy = copyRoutine(routine)
+ expect(copy.id).not.toBe(routine.id)
+ expect(copy.id).toBeTruthy()
+ })
+
+ it('appends "(Copy)" to the name by default', () => {
+ const copy = copyRoutine(routine)
+ expect(copy.name).toBe('Push Day (Copy)')
+ })
+
+ it('uses a custom suffix when provided', () => {
+ const copy = copyRoutine(routine, 'Kopie')
+ expect(copy.name).toBe('Push Day (Kopie)')
+ })
+
+ it('deep-copies all exercises with their configuration', () => {
+ const copy = copyRoutine(routine)
+ expect(copy.ex).toHaveLength(3)
+ expect(copy.ex[0]).toEqual(routine.ex[0])
+ expect(copy.ex[1]).toEqual(routine.ex[1])
+ expect(copy.ex[2]).toEqual(routine.ex[2])
+ })
+
+ it('preserves exercise ids (references to the global library)', () => {
+ const copy = copyRoutine(routine)
+ expect(copy.ex[0].id).toBe('ex1')
+ expect(copy.ex[1].id).toBe('ex2')
+ expect(copy.ex[2].id).toBe('ex3')
+ })
+
+ it('preserves superset groupings', () => {
+ const copy = copyRoutine(routine)
+ expect(copy.ex[0].sg).toBe('sg1')
+ expect(copy.ex[1].sg).toBe('sg1')
+ expect(copy.ex[2].sg).toBeUndefined()
+ })
+
+ it('preserves the emoji and progression settings', () => {
+ const copy = copyRoutine(routine)
+ expect(copy.emoji).toBe('arm')
+ expect(copy.prog).toBe('linear')
+ })
+
+ it('produces an independent copy — mutating it does not affect the original', () => {
+ const copy = copyRoutine(routine)
+ copy.name = 'Modified'
+ copy.ex[0].weight = 999
+ copy.ex.push({ id: 'ex4', sets: 5, reps: 5 })
+
+ expect(routine.name).toBe('Push Day')
+ expect(routine.ex[0].weight).toBe(60)
+ expect(routine.ex).toHaveLength(3)
+ })
+
+ it('handles a routine with no exercises', () => {
+ const empty = { id: 'r2', name: 'Empty', emoji: 'dumbbell', ex: [] }
+ const copy = copyRoutine(empty)
+ expect(copy.ex).toEqual([])
+ expect(copy.name).toBe('Empty (Copy)')
+ expect(copy.id).not.toBe('r2')
+ })
+})
diff --git a/frontend/src/views/RoutineEdit.jsx b/frontend/src/views/RoutineEdit.jsx
index 701b06249..bedef9628 100644
--- a/frontend/src/views/RoutineEdit.jsx
+++ b/frontend/src/views/RoutineEdit.jsx
@@ -11,6 +11,7 @@ import { glyphPicker, exercisePicker, exConfigSheet, confirmSheet } from '../she
import Icon from '../components/Icon.jsx'
import { glyphOf } from '../lib/glyphs.js'
import { Button, Row, SelectRow, Switch } from '../components/ui.jsx'
+import { copyRoutine } from '../lib/routines.js'
import { POLICIES_FOR, POLICY_NAME, POLICY_DESC } from '../lib/progression.js'
import BodyMap from '../components/BodyMap.jsx'
import { loadOfRoutine, rankOf, MUSCLE_NAME } from '../lib/muscles.js'
@@ -425,6 +426,12 @@ export default function RoutineEdit() {
<div className="small dim row" style={{ margin: '10px 2px', gap: 5 }}><Icon name="link" style={{ fontSize: 13 }} />{t('Tap the link button on an exercise to superset it with the one above — you’ll do them back-to-back.')}</div>
<Button variant="primary" onClick={() => exercisePicker(ex => exConfigSheet(ex, null, cfg => edit(x => { x.push({ id: ex.id, ...cfg }) }), null, r))} icon="plus">{t('Add exercise')}</Button>
<div style={{ height: 10 }} />
+ <Button onClick={() => {
+ const copy = copyRoutine(r, t('Copy'))
+ update(s => { s.routines.push(copy) })
+ nav('/plan/r/' + copy.id)
+ }}>{t('Copy routine')}</Button>
+ <div style={{ height: 10 }} />
<Button variant="danger" onClick={() => confirmSheet({
title: t('Delete routine?'), message: t('“{0}” and its exercises will be removed.', r.name), confirmText: t('Delete'), danger: true,
onConfirm: () => {