Files
bot/apps/builder/src/features/theme/components/SaveThemeModal.tsx
Gabriel Pavão 5fbbe9d86e 🌐 Add theme tab translation keys (#1119)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Implemented internationalization across various components, enabling
dynamic language translation for UI elements.

- **Enhancements**
	- Improved accessibility with translated `aria-label` attributes.
	- Enhanced user interface with translated button texts and labels.

- **Refactor**
	- Streamlined translation handling with the `useTranslate` hook.

- **Bug Fixes**
	- Corrected tooltip label for better clarity.

- **Style**
	- Updated visual elements to reflect changes in language settings.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Baptiste Arnaud <baptiste.arnaud95@gmail.com>
2023-12-29 12:02:14 +01:00

116 lines
3.1 KiB
TypeScript

import { TextInput } from '@/components/inputs'
import { useToast } from '@/hooks/useToast'
import { trpc } from '@/lib/trpc'
import {
Button,
HStack,
Modal,
ModalBody,
ModalCloseButton,
ModalContent,
ModalFooter,
ModalHeader,
ModalOverlay,
} from '@chakra-ui/react'
import { createId } from '@paralleldrive/cuid2'
import { useTranslate } from '@tolgee/react'
import { ThemeTemplate } from '@typebot.io/schemas'
import { FormEvent, useRef, useState } from 'react'
type Props = {
workspaceId: string
isOpen: boolean
onClose: (template?: Pick<ThemeTemplate, 'id' | 'theme'>) => void
selectedTemplate: Pick<ThemeTemplate, 'id' | 'name'> | undefined
theme: ThemeTemplate['theme']
}
export const SaveThemeModal = ({
workspaceId,
isOpen,
onClose,
selectedTemplate,
theme,
}: Props) => {
const { t } = useTranslate()
const { showToast } = useToast()
const [isSaving, setIsSaving] = useState(false)
const inputRef = useRef<HTMLInputElement>(null)
const {
theme: {
listThemeTemplates: { refetch: refetchThemeTemplates },
},
} = trpc.useContext()
const { mutate } = trpc.theme.saveThemeTemplate.useMutation({
onMutate: () => setIsSaving(true),
onSettled: () => setIsSaving(false),
onSuccess: ({ themeTemplate }) => {
refetchThemeTemplates()
onClose(themeTemplate)
},
onError: (error) => {
showToast({
description: error.message,
})
},
})
const updateExistingTemplate = (e: FormEvent) => {
e.preventDefault()
const newName = inputRef.current?.value
if (!newName) return
mutate({
name: newName,
theme,
workspaceId,
themeTemplateId: selectedTemplate?.id ?? createId(),
})
}
const saveNewTemplate = () => {
const newName = inputRef.current?.value
if (!newName) return
mutate({
name: newName,
theme,
workspaceId,
themeTemplateId: createId(),
})
}
return (
<Modal isOpen={isOpen} onClose={onClose} initialFocusRef={inputRef}>
<ModalOverlay />
<ModalContent as="form" onSubmit={updateExistingTemplate}>
<ModalHeader>
{t('theme.sideMenu.template.myTemplates.saveTheme.title')}
</ModalHeader>
<ModalCloseButton />
<ModalBody>
<TextInput
ref={inputRef}
label={t('theme.sideMenu.template.myTemplates.saveTheme.name')}
defaultValue={selectedTemplate?.name}
withVariableButton={false}
placeholder={t(
'theme.sideMenu.template.myTemplates.saveTheme.myTemplate'
)}
isRequired
/>
</ModalBody>
<ModalFooter as={HStack}>
{selectedTemplate?.id && (
<Button isLoading={isSaving} onClick={saveNewTemplate}>
{t('theme.sideMenu.template.myTemplates.saveTheme.saveAsNew')}
</Button>
)}
<Button type="submit" colorScheme="blue" isLoading={isSaving}>
{selectedTemplate?.id ? t('update') : t('save')}
</Button>
</ModalFooter>
</ModalContent>
</Modal>
)
}