-
Notifications
You must be signed in to change notification settings - Fork 2
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
122 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { FC, useId, useMemo } from 'react'; | ||
import useSWR from 'swr'; | ||
import * as styles from '../../scss/modules/TtsHealth.module.scss'; | ||
|
||
export const ELEVEN_LABS_SUBSCRIPTION_ENDPOINT = 'https://api.elevenlabs.io/v1/user/subscription'; | ||
|
||
export interface TtsHealthProps { | ||
token: string; | ||
} | ||
|
||
export const TtsHealth: FC<TtsHealthProps> = ({ token }) => { | ||
const pf = useMemo(() => new Intl.NumberFormat('en-US', { | ||
style: 'percent', | ||
minimumFractionDigits: 0, | ||
maximumFractionDigits: 0 | ||
}), []); | ||
const nf = useMemo(() => new Intl.NumberFormat('en-US'), []); | ||
const progressBarId = useId(); | ||
|
||
const { data: subscriptionData } = useSWR(ELEVEN_LABS_SUBSCRIPTION_ENDPOINT, (key: string) => fetch(key, { | ||
method: 'GET', | ||
headers: { | ||
'xi-api-key': token, | ||
}, | ||
}).then(r => r.json()), { | ||
refreshInterval: 60e3, | ||
revalidateOnFocus: false, | ||
keepPreviousData: true, | ||
}); | ||
|
||
const limits = useMemo(() => { | ||
let maxChars = 0; | ||
let usedChars = 0; | ||
if (typeof subscriptionData === 'object' && subscriptionData !== null) { | ||
if ('character_limit' in subscriptionData && typeof subscriptionData.character_limit === 'number') { | ||
maxChars = subscriptionData.character_limit; | ||
} | ||
if ('character_count' in subscriptionData && typeof subscriptionData.character_count === 'number') { | ||
usedChars = subscriptionData.character_count; | ||
} | ||
} | ||
return { maxChars, usedChars }; | ||
}, [subscriptionData]); | ||
|
||
if (limits.maxChars === 0) return null; | ||
|
||
const charsAvailable = limits.maxChars - limits.usedChars; | ||
|
||
return <div className={styles['tts-health']}> | ||
<label htmlFor={progressBarId} className={styles['tts-label']}> | ||
<div className={styles['tts-name']}>❤️ TTS Health</div> | ||
{limits.maxChars > 0 && | ||
<div className={styles['tts-percent']}>{nf.format(charsAvailable)} / {nf.format(limits.maxChars)} • {pf.format(1 - limits.usedChars / limits.maxChars)}</div>} | ||
</label> | ||
<progress | ||
id={progressBarId} | ||
max={limits.maxChars} | ||
value={charsAvailable} | ||
/> | ||
</div>; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
.tts-health { | ||
position: absolute; | ||
top: 0; | ||
left: 1rem; | ||
right: 0; | ||
z-index: 10; | ||
box-sizing: border-box; | ||
color: rgba(255, 0, 0, .75); | ||
background-color: rgb(40, 0, 0); | ||
padding: .5em; | ||
border-radius: .4em; | ||
font-size: .6em; | ||
width: calc(100% - 2em); | ||
|
||
.tts-label { | ||
display: flex; | ||
flex-flow: row nowrap; | ||
justify-content: space-between; | ||
|
||
.tts-name { | ||
font-weight: bold; | ||
} | ||
|
||
.tts-percent { | ||
text-align: right; | ||
} | ||
} | ||
|
||
progress { | ||
height: .2em; | ||
border-radius: .2em; | ||
background-color: #202020; | ||
width: 100%; | ||
border: .125em solid #202020; | ||
display: block; | ||
|
||
|
||
&::-moz-progress-bar, &::-webkit-progress-bar { | ||
background-color: currentColor; | ||
border-radius: inherit; | ||
} | ||
} | ||
} |