♻️ (builder) Change to features-centric folder structure
This commit is contained in:
committed by
Baptiste Arnaud
parent
3686465a85
commit
643571fe7d
19
apps/builder/src/features/results/hooks/useLogs.ts
Normal file
19
apps/builder/src/features/results/hooks/useLogs.ts
Normal 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,
|
||||
}
|
||||
}
|
||||
64
apps/builder/src/features/results/hooks/useResultsQuery.ts
Normal file
64
apps/builder/src/features/results/hooks/useResultsQuery.ts
Normal 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}`
|
||||
}
|
||||
22
apps/builder/src/features/results/hooks/useStats.ts
Normal file
22
apps/builder/src/features/results/hooks/useStats.ts
Normal 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,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user