2
0

♻️ (builder) Change to features-centric folder structure

This commit is contained in:
Baptiste Arnaud
2022-11-15 09:35:48 +01:00
committed by Baptiste Arnaud
parent 3686465a85
commit 643571fe7d
683 changed files with 3907 additions and 3643 deletions

View File

@ -0,0 +1,32 @@
import { useEffect } from 'react'
import { useDebounce } from 'use-debounce'
export const useAutoSave = <T>(
{
handler,
item,
debounceTimeout,
}: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
handler: () => Promise<any>
item?: T
debounceTimeout: number
},
dependencies: unknown[]
) => {
const [debouncedItem] = useDebounce(item, debounceTimeout)
useEffect(() => {
const save = () => handler()
document.addEventListener('visibilitychange', save)
return () => {
document.removeEventListener('visibilitychange', save)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, dependencies)
return useEffect(() => {
handler()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [debouncedItem])
}

View File

@ -0,0 +1,37 @@
import { fetcher } from '@/utils/helpers'
import { Typebot } from 'models'
import { stringify } from 'qs'
import useSWR from 'swr'
export const useLinkedTypebots = ({
workspaceId,
typebotId,
typebotIds,
onError,
}: {
workspaceId?: string
typebotId?: string
typebotIds?: string[]
onError: (error: Error) => void
}) => {
const params = stringify({ typebotIds, workspaceId }, { indices: false })
const { data, error, mutate } = useSWR<
{
typebots: Typebot[]
},
Error
>(
workspaceId
? typebotIds?.every((id) => typebotId === id)
? undefined
: `/api/typebots?${params}`
: null,
fetcher
)
if (error) onError(error)
return {
typebots: data?.typebots,
isLoading: !error && !data,
mutate,
}
}

View File

@ -0,0 +1,22 @@
import { useToast as useChakraToast, UseToastOptions } from '@chakra-ui/react'
import { useCallback } from 'react'
export const useToast = () => {
const toast = useChakraToast()
const showToast = useCallback(
({ title, description, status = 'error', ...props }: UseToastOptions) => {
toast({
position: 'top-right',
description,
title,
status,
isClosable: true,
...props,
})
},
[toast]
)
return { showToast }
}

View File

@ -0,0 +1,33 @@
import { fetcher } from '@/utils/helpers'
import { PublicTypebot, Typebot, Webhook } from 'models'
import useSWR from 'swr'
import { env } from 'utils'
export const useTypebotQuery = ({
typebotId,
onError,
}: {
typebotId: string
onError?: (error: Error) => void
}) => {
const { data, error, mutate } = useSWR<
{
typebot: Typebot
webhooks: Webhook[]
publishedTypebot?: PublicTypebot
isReadOnly?: boolean
},
Error
>(`/api/typebots/${typebotId}`, fetcher, {
dedupingInterval: env('E2E_TEST') === 'true' ? 0 : undefined,
})
if (error && onError) onError(error)
return {
typebot: data?.typebot,
webhooks: data?.webhooks,
publishedTypebot: data?.publishedTypebot,
isReadOnly: data?.isReadOnly,
isLoading: !error && !data,
mutate,
}
}