-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
34 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import { formatTimeFromSeconds } from "../../util/timeFormatter"; | ||
import { convertTimeFromSeconds } from "../../util/timeFormatter"; | ||
import type { VideoMetadata } from "./type"; | ||
|
||
export function formatUrlFromYouTubeMetadata(metadata: VideoMetadata) { | ||
return `https://www.youtube.com/embed/${metadata.id}?start=${metadata.seconds}`; | ||
} | ||
|
||
export function formatUrlFromTwitchMetadata(metadata: VideoMetadata) { | ||
return `https://player.twitch.tv/?video=${metadata.id}&parent=${window.location.hostname}&time=${formatTimeFromSeconds(metadata.seconds ?? 0)}&autoplay=false`; | ||
const time = convertTimeFromSeconds(metadata.seconds ?? 0); | ||
return `https://player.twitch.tv/?video=${metadata.id}&parent=${window.location.hostname}&time=${time.hours}h${time.minutes}m${time.seconds}s&autoplay=false`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,28 @@ | ||
export const formatTimeFromSeconds = (seconds: number) => { | ||
export const convertTimeFromSeconds = (seconds: number) => { | ||
const hours = Math.floor(seconds / 3600); | ||
const minutes = Math.floor((seconds % 3600) / 60); | ||
const remainingSeconds = seconds % 60; | ||
|
||
return `${hours}h${minutes}m${remainingSeconds}s`; | ||
return { hours, minutes, seconds: remainingSeconds }; | ||
}; | ||
|
||
export const parseTimeToSeconds = (time: string) => { | ||
const timeRegex = /(\d+)h(\d+)m(\d+)s/; | ||
const match = time.match(timeRegex); | ||
export const parseTimeToSeconds = (time: string, regex: RegExp) => { | ||
const match = time.match(regex); | ||
|
||
if (!match) { | ||
if (match?.length !== 4) { | ||
throw new Error("Invalid time format"); | ||
} | ||
|
||
const hours = Number.parseInt(match[1], 10); | ||
const minutes = Number.parseInt(match[2], 10); | ||
const seconds = Number.parseInt(match[3], 10); | ||
|
||
const isContainNaN = | ||
Number.isNaN(hours) || Number.isNaN(minutes) || Number.isNaN(seconds); | ||
const isOvered = minutes >= 60 || seconds >= 60; | ||
if (isContainNaN || isOvered) { | ||
throw new Error("Invalid time format"); | ||
} | ||
|
||
return hours * 3600 + minutes * 60 + seconds; | ||
}; |