-
Notifications
You must be signed in to change notification settings - Fork 50
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
4 changed files
with
139 additions
and
36 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
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
98 changes: 98 additions & 0 deletions
98
src/components/mindset/components/Sidebar/Transcript/Viewer/index.tsx
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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import clsx from 'clsx' | ||
import { useEffect, useState } from 'react' | ||
import styled from 'styled-components' | ||
import { usePlayerStore } from '~/stores/usePlayerStore' | ||
import { colors } from '~/utils' | ||
|
||
type Props = { | ||
transcriptString: string | ||
} | ||
|
||
type Word = { | ||
word: string | ||
start: number | ||
end: number | ||
probability: number | ||
} | ||
|
||
type TranscriptData = { | ||
id: number | ||
seek: number | ||
start: number | ||
end: number | ||
text: string | ||
tokens: number[] | ||
temperature: number | ||
avg_logprob: number | ||
compression_ratio: number | ||
no_speech_prob: number | ||
words: Word[] | ||
} | ||
|
||
export const Viewer = ({ transcriptString }: Props) => { | ||
const [currentTime, setCurrentTime] = useState(0) | ||
const { playerRef } = usePlayerStore((s) => s) | ||
const cleaned = transcriptString.replace(/^["']|["']$/g, '') | ||
|
||
const transcriptData: TranscriptData[] = JSON.parse(cleaned) | ||
|
||
useEffect(() => { | ||
const interval = setInterval(() => { | ||
if (playerRef && setCurrentTime) { | ||
const time = playerRef.getCurrentTime() | ||
|
||
setCurrentTime(time) | ||
} | ||
}, 100) | ||
|
||
return () => clearInterval(interval) | ||
}, [playerRef, setCurrentTime]) | ||
|
||
return ( | ||
<Wrapper> | ||
{transcriptData.map((i) => { | ||
const start = i.start.toFixed(2) | ||
const end = i.end.toFixed(2) | ||
|
||
return ( | ||
<Paragraph key={i.id}> | ||
<Start> | ||
{start}:{end} | ||
</Start> | ||
<Text>{i.text}</Text> | ||
{i.words.map((word) => { | ||
const isActive = word.start < currentTime && currentTime < word.end | ||
|
||
return ( | ||
<Word key={`${word.start}`} className={clsx({ active: isActive })}> | ||
{word.word} | ||
</Word> | ||
) | ||
})} | ||
</Paragraph> | ||
) | ||
})} | ||
</Wrapper> | ||
) | ||
} | ||
|
||
const Paragraph = styled.div` | ||
font-size: 14px; | ||
word-break: break-word; | ||
` | ||
|
||
const Wrapper = styled.div` | ||
width: 100%; | ||
` | ||
|
||
const Start = styled.span`` | ||
|
||
const Text = styled.span`` | ||
|
||
const Word = styled.span` | ||
margin: 0 2px; | ||
&.active { | ||
background: ${colors.lightBlue300}; | ||
} | ||
` |
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