♻️ (builder) Change to features-centric folder structure
This commit is contained in:
committed by
Baptiste Arnaud
parent
3686465a85
commit
643571fe7d
32
apps/builder/src/hooks/useAutoSave.ts
Normal file
32
apps/builder/src/hooks/useAutoSave.ts
Normal 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])
|
||||
}
|
37
apps/builder/src/hooks/useLinkedTypebots.ts
Normal file
37
apps/builder/src/hooks/useLinkedTypebots.ts
Normal 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,
|
||||
}
|
||||
}
|
22
apps/builder/src/hooks/useToast.ts
Normal file
22
apps/builder/src/hooks/useToast.ts
Normal 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 }
|
||||
}
|
33
apps/builder/src/hooks/useTypebotQuery.ts
Normal file
33
apps/builder/src/hooks/useTypebotQuery.ts
Normal 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,
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user