From ae7723ddd274ba7741b398462180caf720b049fc Mon Sep 17 00:00:00 2001 From: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Date: Thu, 3 Apr 2025 17:36:37 +0800 Subject: [PATCH] refactor: fix durationInSeconds type (#3399) * chore: fix durationInSeconds type * chore: type --- .../templates/list-item-template.tsx | 11 +--- .../entry-content/components/EntryTitle.tsx | 8 +-- .../modules/renderer/components/TimeStamp.tsx | 7 +-- apps/mobile/src/database/schemas/types.ts | 2 +- .../entry-list/templates/EntryNormalItem.tsx | 8 +-- packages/shared/src/hono.ts | 56 +++++++++---------- packages/utils/src/utils.ts | 6 +- 7 files changed, 42 insertions(+), 56 deletions(-) diff --git a/apps/desktop/src/renderer/src/modules/entry-column/templates/list-item-template.tsx b/apps/desktop/src/renderer/src/modules/entry-column/templates/list-item-template.tsx index 00f5efff4..407a9ebef 100644 --- a/apps/desktop/src/renderer/src/modules/entry-column/templates/list-item-template.tsx +++ b/apps/desktop/src/renderer/src/modules/entry-column/templates/list-item-template.tsx @@ -246,7 +246,7 @@ function AudioCover({ }: { entryId: string src: string - durationInSeconds?: number + durationInSeconds?: number | string feedIcon: React.ReactNode }) { const isMobile = useMobile() @@ -254,13 +254,8 @@ function AudioCover({ playerValue.src === src && playerValue.show ? playerValue.status : false, ) - // durationInSeconds's format like 00:00:00 or 4000 - if (durationInSeconds && Number.isNaN(+durationInSeconds)) { - // @ts-expect-error durationInSeconds is string - durationInSeconds = formatTimeToSeconds(durationInSeconds) - } - - const estimatedMins = durationInSeconds && Math.floor(durationInSeconds / 60) + const seconds = formatTimeToSeconds(durationInSeconds) + const estimatedMins = seconds && Math.floor(seconds / 60) const handleClickPlay = (e: React.MouseEvent) => { if (isMobile) e.stopPropagation() diff --git a/apps/desktop/src/renderer/src/modules/entry-content/components/EntryTitle.tsx b/apps/desktop/src/renderer/src/modules/entry-content/components/EntryTitle.tsx index 395e94a1f..e25922268 100644 --- a/apps/desktop/src/renderer/src/modules/entry-content/components/EntryTitle.tsx +++ b/apps/desktop/src/renderer/src/modules/entry-content/components/EntryTitle.tsx @@ -68,11 +68,9 @@ export const EntryTitle = ({ entryId, compact }: EntryLinkProps) => { return } - let durationInSeconds = audioAttachment.duration_in_seconds - - if (Number.isNaN(+durationInSeconds)) { - // @ts-expect-error durationInSeconds is string - durationInSeconds = formatTimeToSeconds(durationInSeconds) + const durationInSeconds = formatTimeToSeconds(audioAttachment.duration_in_seconds) + if (!durationInSeconds) { + return } return formatEstimatedMins(Math.floor(durationInSeconds / 60)) diff --git a/apps/desktop/src/renderer/src/modules/renderer/components/TimeStamp.tsx b/apps/desktop/src/renderer/src/modules/renderer/components/TimeStamp.tsx index f8618b19e..2e17e2404 100644 --- a/apps/desktop/src/renderer/src/modules/renderer/components/TimeStamp.tsx +++ b/apps/desktop/src/renderer/src/modules/renderer/components/TimeStamp.tsx @@ -10,12 +10,7 @@ import { EntryInfoContext } from "../context" export const TimeStamp = (props: { time: string }) => { const { entryId } = useContext(EntryInfoContext) const entry = useEntry(entryId) - let mediaDuration = entry?.entries.attachments?.[0]?.duration_in_seconds - - if (mediaDuration && Number.isNaN(+mediaDuration)) { - // @ts-expect-error mediaDuration is string - mediaDuration = formatTimeToSeconds(mediaDuration) - } + const mediaDuration = formatTimeToSeconds(entry?.entries.attachments?.[0]?.duration_in_seconds) const src = entry?.entries?.attachments?.[0]?.url if (!src) return {props.time} diff --git a/apps/mobile/src/database/schemas/types.ts b/apps/mobile/src/database/schemas/types.ts index 981ab2499..4745f2106 100644 --- a/apps/mobile/src/database/schemas/types.ts +++ b/apps/mobile/src/database/schemas/types.ts @@ -49,7 +49,7 @@ export type MediaModel = { export type AttachmentsModel = { url: string - duration_in_seconds?: number + duration_in_seconds?: number | string mime_type?: string size_in_bytes?: number title?: string diff --git a/apps/mobile/src/modules/entry-list/templates/EntryNormalItem.tsx b/apps/mobile/src/modules/entry-list/templates/EntryNormalItem.tsx index 3865a691d..d753842a1 100644 --- a/apps/mobile/src/modules/entry-list/templates/EntryNormalItem.tsx +++ b/apps/mobile/src/modules/entry-list/templates/EntryNormalItem.tsx @@ -89,13 +89,7 @@ export function EntryNormalItem({ entryId, extraData }: { entryId: string; extra const isLoading = audioState === "loading" const estimatedMins = useMemo(() => { - let durationInSeconds = audio?.duration_in_seconds - // durationInSeconds's format like 00:00:00 or 4000 - if (durationInSeconds && Number.isNaN(+durationInSeconds)) { - // @ts-expect-error durationInSeconds is string - durationInSeconds = formatTimeToSeconds(durationInSeconds) - } - + const durationInSeconds = formatTimeToSeconds(audio?.duration_in_seconds) return durationInSeconds && Math.floor(durationInSeconds / 60) }, [audio?.duration_in_seconds]) diff --git a/packages/shared/src/hono.ts b/packages/shared/src/hono.ts index 55b61fe5a..3502fadce 100644 --- a/packages/shared/src/hono.ts +++ b/packages/shared/src/hono.ts @@ -1574,7 +1574,7 @@ type MediaModel = { }; type AttachmentsModel = { url: string; - duration_in_seconds?: number; + duration_in_seconds?: number | string; mime_type?: string; size_in_bytes?: number; title?: string; @@ -2027,20 +2027,20 @@ declare const entries: drizzle_orm_pg_core.PgTableWithColumns<{ }>; declare const attachmentsZodSchema: z.ZodNullable; + duration_in_seconds: z.ZodOptional>; mime_type: z.ZodOptional; size_in_bytes: z.ZodOptional; title: z.ZodOptional; }, "strip", z.ZodTypeAny, { url: string; title?: string | undefined; - duration_in_seconds?: number | undefined; + duration_in_seconds?: string | number | undefined; mime_type?: string | undefined; size_in_bytes?: number | undefined; }, { url: string; title?: string | undefined; - duration_in_seconds?: number | undefined; + duration_in_seconds?: string | number | undefined; mime_type?: string | undefined; size_in_bytes?: number | undefined; }>, "many">>>; @@ -2258,20 +2258,20 @@ declare const entriesOpenAPISchema: z.ZodObject, { attachments: z.ZodNullable; + duration_in_seconds: z.ZodOptional>; mime_type: z.ZodOptional; size_in_bytes: z.ZodOptional; title: z.ZodOptional; }, "strip", z.ZodTypeAny, { url: string; title?: string | undefined; - duration_in_seconds?: number | undefined; + duration_in_seconds?: string | number | undefined; mime_type?: string | undefined; size_in_bytes?: number | undefined; }, { url: string; title?: string | undefined; - duration_in_seconds?: number | undefined; + duration_in_seconds?: string | number | undefined; mime_type?: string | undefined; size_in_bytes?: number | undefined; }>, "many">>>; @@ -2350,7 +2350,7 @@ declare const entriesOpenAPISchema: z.ZodObject, { attachments: z.ZodNullable; + duration_in_seconds: z.ZodOptional>; mime_type: z.ZodOptional; size_in_bytes: z.ZodOptional; title: z.ZodOptional; }, "strip", z.ZodTypeAny, { url: string; title?: string | undefined; - duration_in_seconds?: number | undefined; + duration_in_seconds?: string | number | undefined; mime_type?: string | undefined; size_in_bytes?: number | undefined; }, { url: string; title?: string | undefined; - duration_in_seconds?: number | undefined; + duration_in_seconds?: string | number | undefined; mime_type?: string | undefined; size_in_bytes?: number | undefined; }>, "many">>>; @@ -3588,7 +3588,7 @@ declare const inboxesEntriesOpenAPISchema: z.ZodObject, { attachments: z.ZodNullable; + duration_in_seconds: z.ZodOptional>; mime_type: z.ZodOptional; size_in_bytes: z.ZodOptional; title: z.ZodOptional; }, "strip", z.ZodTypeAny, { url: string; title?: string | undefined; - duration_in_seconds?: number | undefined; + duration_in_seconds?: string | number | undefined; mime_type?: string | undefined; size_in_bytes?: number | undefined; }, { url: string; title?: string | undefined; - duration_in_seconds?: number | undefined; + duration_in_seconds?: string | number | undefined; mime_type?: string | undefined; size_in_bytes?: number | undefined; }>, "many">>>; @@ -3892,7 +3892,7 @@ declare const inboxesEntriesInsertOpenAPISchema: z.ZodObject { +export const formatTimeToSeconds = (time?: string | number) => { + if (typeof time === "number" || time === undefined) { + return time + } + const formats = ["h:mm:ss", "mm:ss", "m:ss"] for (const format of formats) {