♻️ (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,19 @@
import { fetcher } from '@/utils/helpers'
import { Log } from 'db'
import useSWR from 'swr'
export const useLogs = (
typebotId: string,
resultId?: string,
onError?: (e: Error) => void
) => {
const { data, error } = useSWR<{ logs: Log[] }>(
resultId ? `/api/typebots/${typebotId}/results/${resultId}/logs` : null,
fetcher
)
if (error && onError) onError(error)
return {
logs: data?.logs,
isLoading: !error && !data,
}
}

View File

@@ -0,0 +1,64 @@
import { fetcher } from '@/utils/helpers'
import { ResultWithAnswers } from 'models'
import { env } from 'utils'
import useSWRInfinite from 'swr/infinite'
const paginationLimit = 50
export const useResultsQuery = ({
workspaceId,
typebotId,
onError,
}: {
workspaceId: string
typebotId: string
onError?: (error: Error) => void
}) => {
const { data, error, mutate, setSize, size, isValidating } = useSWRInfinite<
{ results: ResultWithAnswers[] },
Error
>(
(
pageIndex: number,
previousPageData: {
results: ResultWithAnswers[]
}
) => getKey(workspaceId, typebotId, pageIndex, previousPageData),
fetcher,
{
revalidateAll: true,
dedupingInterval: env('E2E_TEST') === 'true' ? 0 : undefined,
}
)
if (error && onError) onError(error)
return {
data,
isLoading: !error && !data,
mutate,
setSize,
size,
hasMore:
isValidating ||
(data &&
data.length > 0 &&
data[data.length - 1].results.length > 0 &&
data.length === paginationLimit),
}
}
const getKey = (
workspaceId: string,
typebotId: string,
pageIndex: number,
previousPageData: {
results: ResultWithAnswers[]
}
) => {
if (previousPageData && previousPageData.results.length === 0) return null
if (pageIndex === 0)
return `/api/typebots/${typebotId}/results?limit=50&workspaceId=${workspaceId}`
return `/api/typebots/${typebotId}/results?lastResultId=${
previousPageData.results[previousPageData.results.length - 1].id
}&limit=${paginationLimit}&workspaceId=${workspaceId}`
}

View File

@@ -0,0 +1,22 @@
import { Stats } from 'models'
import { fetcher } from '@/utils/helpers'
import useSWR from 'swr'
export const useStats = ({
typebotId,
onError,
}: {
typebotId?: string
onError: (error: Error) => void
}) => {
const { data, error, mutate } = useSWR<{ stats: Stats }, Error>(
typebotId ? `/api/typebots/${typebotId}/results/stats` : null,
fetcher
)
if (error) onError(error)
return {
stats: data?.stats,
isLoading: !error && !data,
mutate,
}
}