From 7e4cd767622e3e1a09e76770ce800f4a541cf0e8 Mon Sep 17 00:00:00 2001 From: Zaka <149817565+ZakaHaceCosas@users.noreply.github.com> Date: Fri, 15 Aug 2025 23:01:18 +0200 Subject: feat: add an input so the user can go to a specific timestamp (#530) * feat: add an input so the user can go to a specific timestamp * fix: use TimeCode type for EditableTimecodeProps * cleanup --------- Co-authored-by: Maze Winther --- apps/web/src/lib/time.ts | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'apps/web/src/lib') diff --git a/apps/web/src/lib/time.ts b/apps/web/src/lib/time.ts index f24c5961..20e2ff6d 100644 --- a/apps/web/src/lib/time.ts +++ b/apps/web/src/lib/time.ts @@ -1,10 +1,14 @@ // Time-related utility functions +import { DEFAULT_FPS } from "@/stores/project-store"; + +export type TimeCode = "MM:SS" | "HH:MM:SS" | "HH:MM:SS:CS" | "HH:MM:SS:FF"; + // Helper function to format time in various formats (MM:SS, HH:MM:SS, HH:MM:SS:CS, HH:MM:SS:FF) export const formatTimeCode = ( timeInSeconds: number, - format: "MM:SS" | "HH:MM:SS" | "HH:MM:SS:CS" | "HH:MM:SS:FF" = "HH:MM:SS:CS", - fps = 30 + format: TimeCode = "HH:MM:SS:CS", + fps = DEFAULT_FPS ): string => { const hours = Math.floor(timeInSeconds / 3600); const minutes = Math.floor((timeInSeconds % 3600) / 60); @@ -26,8 +30,8 @@ export const formatTimeCode = ( export const parseTimeCode = ( timeCode: string, - format: "MM:SS" | "HH:MM:SS" | "HH:MM:SS:CS" | "HH:MM:SS:FF" = "HH:MM:SS:CS", - fps = 30 + format: TimeCode = "HH:MM:SS:CS", + fps = DEFAULT_FPS ): number | null => { if (!timeCode || typeof timeCode !== "string") return null; @@ -116,3 +120,18 @@ export const parseTimeCode = ( return null; }; + +export const guessTimeCodeFormat = (timeCode: string): TimeCode | null => { + if (!timeCode || typeof timeCode !== "string") return null; + + const numbers = timeCode.split(":"); + + if (!numbers.every((n) => !isNaN(Number(n)))) return null; + + if (numbers.length === 2) return "MM:SS"; + if (numbers.length === 3) return "HH:MM:SS"; + // todo: how to tell frames apart from cs? + if (numbers.length === 4) return "HH:MM:SS:FF"; + + return null; +}; -- cgit v1.3.1