2021-12-23 09:37:42 +01:00
|
|
|
import React, { useEffect, useState } from 'react'
|
2022-01-24 09:36:28 +01:00
|
|
|
import { Text, HStack } from '@chakra-ui/react'
|
2023-03-03 09:01:11 +01:00
|
|
|
import { AutocompleteInput } from '@/components/inputs/AutocompleteInput'
|
2023-08-28 09:13:53 +02:00
|
|
|
import { env } from '@typebot.io/env'
|
2023-12-29 08:02:14 -03:00
|
|
|
import { useTranslate } from '@tolgee/react'
|
2021-12-23 09:37:42 +01:00
|
|
|
|
|
|
|
type FontSelectorProps = {
|
|
|
|
activeFont?: string
|
|
|
|
onSelectFont: (font: string) => void
|
|
|
|
}
|
|
|
|
|
|
|
|
export const FontSelector = ({
|
|
|
|
activeFont,
|
|
|
|
onSelectFont,
|
|
|
|
}: FontSelectorProps) => {
|
2023-12-29 08:02:14 -03:00
|
|
|
const { t } = useTranslate()
|
2021-12-23 09:37:42 +01:00
|
|
|
const [currentFont, setCurrentFont] = useState(activeFont)
|
|
|
|
const [googleFonts, setGoogleFonts] = useState<string[]>([])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
fetchPopularFonts().then(setGoogleFonts)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const fetchPopularFonts = async () => {
|
2023-08-28 09:13:53 +02:00
|
|
|
if (!env.NEXT_PUBLIC_GOOGLE_API_KEY) return []
|
2021-12-23 09:37:42 +01:00
|
|
|
const response = await fetch(
|
2023-08-28 09:13:53 +02:00
|
|
|
`https://www.googleapis.com/webfonts/v1/webfonts?key=${env.NEXT_PUBLIC_GOOGLE_API_KEY}&sort=popularity`
|
2021-12-23 09:37:42 +01:00
|
|
|
)
|
|
|
|
return (await response.json()).items.map(
|
|
|
|
(item: { family: string }) => item.family
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-22 18:24:57 +01:00
|
|
|
const handleFontSelected = (nextFont: string) => {
|
2023-03-03 09:01:11 +01:00
|
|
|
if (nextFont === currentFont) return
|
2022-01-22 18:24:57 +01:00
|
|
|
setCurrentFont(nextFont)
|
|
|
|
onSelectFont(nextFont)
|
|
|
|
}
|
|
|
|
|
2021-12-23 09:37:42 +01:00
|
|
|
return (
|
2022-01-24 09:36:28 +01:00
|
|
|
<HStack justify="space-between" align="center">
|
2023-12-29 08:02:14 -03:00
|
|
|
<Text>{t('theme.sideMenu.global.font')}</Text>
|
2023-03-03 09:01:11 +01:00
|
|
|
<AutocompleteInput
|
2023-03-28 15:10:06 +02:00
|
|
|
value={activeFont}
|
2021-12-23 09:37:42 +01:00
|
|
|
items={googleFonts}
|
2023-03-03 09:01:11 +01:00
|
|
|
onChange={handleFontSelected}
|
|
|
|
withVariableButton={false}
|
2021-12-23 09:37:42 +01:00
|
|
|
/>
|
2022-01-24 09:36:28 +01:00
|
|
|
</HStack>
|
2021-12-23 09:37:42 +01:00
|
|
|
)
|
|
|
|
}
|