2
0
Files
bot/apps/builder/services/results.ts
Baptiste Arnaud 6322402c96 🖐️ Analytics drop off rates
2022-01-03 17:39:59 +01:00

42 lines
926 B
TypeScript

import { Result } from 'bot-engine'
import useSWR from 'swr'
import { fetcher } from './utils'
import { stringify } from 'qs'
import { Answer } from 'db'
export const useResults = ({
lastResultId,
typebotId,
onError,
}: {
lastResultId?: string
typebotId: string
onError: (error: Error) => void
}) => {
const params = stringify({
lastResultId,
})
const { data, error, mutate } = useSWR<
{ results: (Result & { answers: Answer[] })[] },
Error
>(`/api/typebots/${typebotId}/results?${params}`, fetcher)
if (error) onError(error)
return {
results: data?.results,
isLoading: !error && !data,
mutate,
}
}
export const parseDateToReadable = (dateStr: string): string => {
const date = new Date(dateStr)
return (
date.toDateString().split(' ').slice(1, 3).join(' ') +
', ' +
date.toLocaleTimeString([], {
hour: '2-digit',
minute: '2-digit',
})
)
}