From 3c2586df51a2088155b5ef108226da289d4e9a8a Mon Sep 17 00:00:00 2001 From: Konv Suu <2583695112@qq.com> Date: Tue, 18 Mar 2025 09:33:27 +0800 Subject: [PATCH] fix: incorrect formatting of time duration (#3148) * feat(desktop): add duration formatting for audio entries * update * update * update * update * update --------- Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com> --- .../templates/list-item-template.tsx | 26 +++++++++++-------- packages/components/src/utils/dayjs.ts | 2 ++ packages/utils/src/utils.ts | 11 ++++++++ 3 files changed, 28 insertions(+), 11 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 67857546a..00f5efff4 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 @@ -1,6 +1,6 @@ import { useMobile } from "@follow/components/hooks/useMobile.js" import { EllipsisHorizontalTextWithTooltip } from "@follow/components/ui/typography/index.js" -import { clsx, cn, formatEstimatedMins, isSafari } from "@follow/utils/utils" +import { clsx, cn, formatEstimatedMins, formatTimeToSeconds, isSafari } from "@follow/utils/utils" import { useMemo } from "react" import { AudioPlayer, useAudioPlayerAtomSelector } from "~/atoms/player" @@ -79,6 +79,10 @@ export function ListItem({ } }, [settingWideMode]) + const audioAttachment = useMemo(() => { + return entry?.entries?.attachments?.find((a) => a.mime_type?.startsWith("audio") && a.url) + }, [entry?.entries?.attachments]) + // NOTE: prevent 0 height element, react virtuoso will not stop render any more if (!entry || !(feed || inbox)) return null @@ -86,10 +90,7 @@ export function ListItem({ const related = feed || inbox - const hasAudio = simple - ? false - : !!entry.entries?.attachments?.[0]?.url && - entry.entries?.attachments?.[0]?.mime_type?.startsWith("audio") + const hasAudio = simple ? false : !!audioAttachment const hasMedia = simple ? false : !!entry.entries?.media?.[0]?.url const marginWidth = 8 * (isMobile ? 1.125 : 1) @@ -186,11 +187,8 @@ export function ListItem({ {hasAudio && ( ) => { if (isMobile) e.stopPropagation() diff --git a/packages/components/src/utils/dayjs.ts b/packages/components/src/utils/dayjs.ts index 353912f07..5310e85b5 100644 --- a/packages/components/src/utils/dayjs.ts +++ b/packages/components/src/utils/dayjs.ts @@ -1,4 +1,5 @@ import dayjs from "dayjs" +import customParseFormat from "dayjs/plugin/customParseFormat" import duration from "dayjs/plugin/duration" import localizedFormat from "dayjs/plugin/localizedFormat" import relativeTime from "dayjs/plugin/relativeTime" @@ -7,4 +8,5 @@ export const initializeDayjs = () => { dayjs.extend(duration) dayjs.extend(relativeTime) dayjs.extend(localizedFormat) + dayjs.extend(customParseFormat) } diff --git a/packages/utils/src/utils.ts b/packages/utils/src/utils.ts index 2257ec78c..1ae52e367 100644 --- a/packages/utils/src/utils.ts +++ b/packages/utils/src/utils.ts @@ -1,5 +1,6 @@ import type { ClassValue } from "clsx" import { clsx } from "clsx" +import dayjs from "dayjs" import { twMerge } from "tailwind-merge" import { parse } from "tldts" @@ -255,6 +256,16 @@ export function transformShortcut(shortcut: string, platform: OS = getOS()): str return shortcut } +// time like 1:30:00 +export const formatTimeToSeconds = (time: string) => { + const date = dayjs(time, "h:mm:ss") + if (!date.isValid()) { + return 0 + } + const totalSeconds = date.hour() * 3600 + date.minute() * 60 + date.second() + return totalSeconds +} + export const formatEstimatedMins = (estimatedMins: number) => { const minutesInHour = 60 const minutesInDay = minutesInHour * 24