2
0

feat: Add custom domains

This commit is contained in:
Baptiste Arnaud
2022-02-18 14:57:10 +01:00
parent 1c178e01a6
commit f3ecb948a1
18 changed files with 694 additions and 66 deletions

View File

@ -60,6 +60,7 @@ const typebotContext = createContext<
updateOnBothTypebots: (updates: {
publicId?: string
name?: string
customDomain?: string | null
}) => void
publishTypebot: () => void
} & BlocksActions &
@ -144,8 +145,6 @@ export const TypebotContext = ({
debounceTimeout: autoSaveTimeout,
})
const [localPublishedTypebot, setLocalPublishedTypebot] =
useState<PublicTypebot>()
const [isSavingLoading, setIsSavingLoading] = useState(false)
const [isPublishing, setIsPublishing] = useState(false)
@ -177,7 +176,6 @@ export const TypebotContext = ({
return
}
setLocalTypebot({ ...typebot })
if (publishedTypebot) setLocalPublishedTypebot({ ...publishedTypebot })
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isLoading])
@ -206,25 +204,15 @@ export const TypebotContext = ({
const updateLocalTypebot = (updates: UpdateTypebotPayload) =>
localTypebot && setLocalTypebot({ ...localTypebot, ...updates })
const updateLocalPublishedTypebot = (updates: UpdateTypebotPayload) =>
publishedTypebot &&
setLocalPublishedTypebot({
...localPublishedTypebot,
...(updates as PublicTypebot),
})
const publishTypebot = async () => {
if (!localTypebot) return
const publishedTypebotId = generate()
const newLocalTypebot = { ...localTypebot }
if (
localPublishedTypebot &&
isNotDefined(localTypebot.publishedTypebotId)
) {
updateLocalTypebot({ publishedTypebotId: localPublishedTypebot.id })
if (publishedTypebot && isNotDefined(localTypebot.publishedTypebotId)) {
updateLocalTypebot({ publishedTypebotId: publishedTypebot.id })
await saveTypebot()
}
if (!localPublishedTypebot) {
if (!publishedTypebot) {
const newPublicId = parseDefaultPublicId(
localTypebot.name,
localTypebot.id
@ -233,10 +221,10 @@ export const TypebotContext = ({
newLocalTypebot.publicId = newPublicId
await saveTypebot()
}
if (localPublishedTypebot) {
if (publishedTypebot) {
await savePublishedTypebot({
...parseTypebotToPublicTypebot(newLocalTypebot),
id: localPublishedTypebot.id,
id: publishedTypebot.id,
})
} else {
setIsPublishing(true)
@ -244,24 +232,23 @@ export const TypebotContext = ({
...parseTypebotToPublicTypebot(newLocalTypebot),
id: publishedTypebotId,
})
setLocalPublishedTypebot(data)
setIsPublishing(false)
if (error) return toast({ title: error.name, description: error.message })
mutate({ typebot: localTypebot })
mutate({ typebot: localTypebot, publishedTypebot: data })
}
}
const updateOnBothTypebots = async (updates: {
publicId?: string
name?: string
customDomain?: string | null
}) => {
updateLocalTypebot(updates)
await saveTypebot()
if (!localPublishedTypebot) return
updateLocalPublishedTypebot(updates)
if (!publishedTypebot) return
await savePublishedTypebot({
...localPublishedTypebot,
...(updates as PublicTypebot),
...publishedTypebot,
...updates,
})
}
@ -269,7 +256,7 @@ export const TypebotContext = ({
<typebotContext.Provider
value={{
typebot: localTypebot,
publishedTypebot: localPublishedTypebot,
publishedTypebot,
hasUnsavedChanges,
isSavingLoading,
save: saveTypebot,

View File

@ -37,11 +37,16 @@ export const UserContext = ({ children }: { children: ReactNode }) => {
const router = useRouter()
const { data: session, status } = useSession()
const [user, setUser] = useState<User | undefined>()
const { credentials, mutate } = useCredentials({
const toast = useToast({
position: 'top-right',
status: 'error',
})
const { credentials, mutate: mutateCredentials } = useCredentials({
userId: user?.id,
onError: (error) =>
toast({ title: error.name, description: error.message }),
})
const [isSaving, setIsSaving] = useState(false)
const isOAuthProvider = useMemo(
() => (session?.providerType as boolean | undefined) ?? false,
@ -53,11 +58,6 @@ export const UserContext = ({ children }: { children: ReactNode }) => {
[session?.user, user]
)
const toast = useToast({
position: 'top-right',
status: 'error',
})
useEffect(() => {
if (isDefined(user) || isNotDefined(session)) return
setUser(session.user as User)
@ -92,14 +92,14 @@ export const UserContext = ({ children }: { children: ReactNode }) => {
<userContext.Provider
value={{
user,
updateUser,
saveUser,
isSaving,
isLoading: status === 'loading',
hasUnsavedChanges,
isOAuthProvider,
credentials: credentials ?? [],
mutateCredentials: mutate,
mutateCredentials,
updateUser,
saveUser,
}}
>
{children}