'use client' import type { FC, SVGProps } from 'react' import React, { useState } from 'react' import useSWR from 'swr' import { usePathname } from 'next/navigation' import { Pagination } from 'react-headless-pagination' import { useDebounce } from 'ahooks' import { omit } from 'lodash-es' import dayjs from 'dayjs' import { ArrowLeftIcon, ArrowRightIcon } from '@heroicons/react/24/outline' import { Trans, useTranslation } from 'react-i18next' import Link from 'next/link' import List from './list' import Filter from './filter' import s from './style.module.css' import Loading from '@/app/components/base/loading' import { fetchChatConversations, fetchCompletionConversations } from '@/service/log' import { APP_PAGE_LIMIT } from '@/config' import type { App, AppMode } from '@/types/app' export type ILogsProps = { appDetail: App } export type QueryParam = { period?: number | string annotation_status?: string keyword?: string sort_by?: string } const ThreeDotsIcon = ({ className }: SVGProps) => { return } const EmptyElement: FC<{ appUrl: string }> = ({ appUrl }) => { const { t } = useTranslation() const pathname = usePathname() const pathSegments = pathname.split('/') pathSegments.pop() return
{t('appLog.table.empty.element.title')}
, testLink: }} />
} const Logs: FC = ({ appDetail }) => { const { t } = useTranslation() const [queryParams, setQueryParams] = useState({ period: 7, annotation_status: 'all', sort_by: '-created_at', }) const [currPage, setCurrPage] = React.useState(0) const debouncedQueryParams = useDebounce(queryParams, { wait: 500 }) // Get the app type first const isChatMode = appDetail.mode !== 'completion' const query = { page: currPage + 1, limit: APP_PAGE_LIMIT, ...(debouncedQueryParams.period !== 'all' ? { start: dayjs().subtract(debouncedQueryParams.period as number, 'day').startOf('day').format('YYYY-MM-DD HH:mm'), end: dayjs().endOf('day').format('YYYY-MM-DD HH:mm'), } : {}), ...(isChatMode ? { sort_by: debouncedQueryParams.sort_by } : {}), ...omit(debouncedQueryParams, ['period']), } const getWebAppType = (appType: AppMode) => { if (appType !== 'completion' && appType !== 'workflow') return 'chat' return appType } // When the details are obtained, proceed to the next request const { data: chatConversations, mutate: mutateChatList } = useSWR(() => isChatMode ? { url: `/apps/${appDetail.id}/chat-conversations`, params: query, } : null, fetchChatConversations) const { data: completionConversations, mutate: mutateCompletionList } = useSWR(() => !isChatMode ? { url: `/apps/${appDetail.id}/completion-conversations`, params: query, } : null, fetchCompletionConversations) const total = isChatMode ? chatConversations?.total : completionConversations?.total return (

{t('appLog.description')}

{total === undefined ? : total > 0 ? : } {/* Show Pagination only if the total is more than the limit */} {(total && total > APP_PAGE_LIMIT) ? {t('appLog.table.pagination.previous')}
{t('appLog.table.pagination.next')}
: null}
) } export default Logs