From e64e2353d9661e03cde7d489e48e4b643abd59b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=AB?= Date: Fri, 7 Aug 2026 13:11:27 +0200 Subject: [PATCH] fix(db): replace crypto.randomUUID with custom UUID generator --- src/db/operations.ts | 9 +++++---- src/features/workout-builder/screens/BuilderScreen.tsx | 3 ++- src/utils/uuid.ts | 7 +++++++ 3 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 src/utils/uuid.ts diff --git a/src/db/operations.ts b/src/db/operations.ts index 5b97b7a..7753483 100644 --- a/src/db/operations.ts +++ b/src/db/operations.ts @@ -1,4 +1,5 @@ import { db } from "./client"; +import { generateUUID } from "../utils/uuid"; import { workouts, sets, laps, templates } from "./schema"; import { eq, desc, sql } from "drizzle-orm"; import type { WorkoutSet, LapData } from "@types/workout"; @@ -6,7 +7,7 @@ import type { WorkoutSet, LapData } from "@types/workout"; // ─── Templates ─────────────────────────────────────────────── export async function saveTemplate(name: string, poolLength: number, setsData: WorkoutSet[]) { - const id = crypto.randomUUID(); + const id = generateUUID(); await db.insert(templates).values({ id, name, @@ -37,7 +38,7 @@ export async function saveWorkout( setsData: WorkoutSet[], templateId?: string ) { - const workoutId = crypto.randomUUID(); + const workoutId = generateUUID(); const totalDistance = setsData.reduce((acc, s) => acc + s.distancePerRep * s.repCount, 0); await db.insert(workouts).values({ @@ -53,7 +54,7 @@ export async function saveWorkout( for (let i = 0; i < setsData.length; i++) { const s = setsData[i]; await db.insert(sets).values({ - id: crypto.randomUUID(), + id: generateUUID(), workoutId, position: i + 1, stroke: s.stroke, @@ -112,7 +113,7 @@ export async function saveLap(workoutId: string, lap: LapData) { lap.strokeCount != null ? lap.strokeCount + Math.round(lap.duration) : null; await db.insert(laps).values({ - id: crypto.randomUUID(), + id: generateUUID(), workoutId, setPosition: lap.setPosition, repNumber: lap.repNumber, diff --git a/src/features/workout-builder/screens/BuilderScreen.tsx b/src/features/workout-builder/screens/BuilderScreen.tsx index 39feda8..e052322 100644 --- a/src/features/workout-builder/screens/BuilderScreen.tsx +++ b/src/features/workout-builder/screens/BuilderScreen.tsx @@ -29,7 +29,8 @@ export function BuilderScreen() { const workoutId = await save(poolLength); navigation.navigate("PreSwim", { workoutId }); } catch (e) { - Alert.alert("Error", "Failed to save workout"); + console.error("Failed to save workout:", e); + Alert.alert("Error", `Failed to save workout: ${e}`); } }; diff --git a/src/utils/uuid.ts b/src/utils/uuid.ts new file mode 100644 index 0000000..416cfe7 --- /dev/null +++ b/src/utils/uuid.ts @@ -0,0 +1,7 @@ +export function generateUUID(): string { + return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => { + const r = (Math.random() * 16) | 0; + const v = c === "x" ? r : (r & 0x3) | 0x8; + return v.toString(16); + }); +}