index.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. 'use client'
  2. import type { FC, SVGProps } from 'react'
  3. import React, { useState } from 'react'
  4. import useSWR from 'swr'
  5. import { usePathname } from 'next/navigation'
  6. import { Pagination } from 'react-headless-pagination'
  7. import { omit } from 'lodash-es'
  8. import dayjs from 'dayjs'
  9. import { ArrowLeftIcon, ArrowRightIcon } from '@heroicons/react/24/outline'
  10. import { Trans, useTranslation } from 'react-i18next'
  11. import Link from 'next/link'
  12. import List from './list'
  13. import Filter from './filter'
  14. import s from './style.module.css'
  15. import Loading from '@/app/components/base/loading'
  16. import { fetchChatConversations, fetchCompletionConversations } from '@/service/log'
  17. import { fetchAppDetail } from '@/service/apps'
  18. import { APP_PAGE_LIMIT } from '@/config'
  19. export type ILogsProps = {
  20. appId: string
  21. }
  22. export type QueryParam = {
  23. period?: number | string
  24. annotation_status?: string
  25. keyword?: string
  26. }
  27. const ThreeDotsIcon = ({ className }: SVGProps<SVGElement>) => {
  28. return <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg" className={className ?? ''}>
  29. <path d="M5 6.5V5M8.93934 7.56066L10 6.5M10.0103 11.5H11.5103" stroke="#374151" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
  30. </svg>
  31. }
  32. const EmptyElement: FC<{ appUrl: string }> = ({ appUrl }) => {
  33. const { t } = useTranslation()
  34. const pathname = usePathname()
  35. const pathSegments = pathname.split('/')
  36. pathSegments.pop()
  37. return <div className='flex items-center justify-center h-full'>
  38. <div className='bg-gray-50 w-[560px] h-fit box-border px-5 py-4 rounded-2xl'>
  39. <span className='text-gray-700 font-semibold'>{t('appLog.table.empty.element.title')}<ThreeDotsIcon className='inline relative -top-3 -left-1.5' /></span>
  40. <div className='mt-2 text-gray-500 text-sm font-normal'>
  41. <Trans
  42. i18nKey="appLog.table.empty.element.content"
  43. components={{ shareLink: <Link href={`${pathSegments.join('/')}/overview`} className='text-primary-600' />, testLink: <Link href={appUrl} className='text-primary-600' target='_blank' rel='noopener noreferrer' /> }}
  44. />
  45. </div>
  46. </div>
  47. </div>
  48. }
  49. const Logs: FC<ILogsProps> = ({ appId }) => {
  50. const { t } = useTranslation()
  51. const [queryParams, setQueryParams] = useState<QueryParam>({ period: 7, annotation_status: 'all' })
  52. const [currPage, setCurrPage] = React.useState<number>(0)
  53. const query = {
  54. page: currPage + 1,
  55. limit: APP_PAGE_LIMIT,
  56. ...(queryParams.period !== 'all'
  57. ? {
  58. start: dayjs().subtract(queryParams.period as number, 'day').startOf('day').format('YYYY-MM-DD HH:mm'),
  59. end: dayjs().format('YYYY-MM-DD HH:mm'),
  60. }
  61. : {}),
  62. ...omit(queryParams, ['period']),
  63. }
  64. // Get the app type first
  65. const { data: appDetail } = useSWR({ url: '/apps', id: appId }, fetchAppDetail)
  66. const isChatMode = appDetail?.mode === 'chat'
  67. // When the details are obtained, proceed to the next request
  68. const { data: chatConversations, mutate: mutateChatList } = useSWR(() => isChatMode
  69. ? {
  70. url: `/apps/${appId}/chat-conversations`,
  71. params: query,
  72. }
  73. : null, fetchChatConversations)
  74. const { data: completionConversations, mutate: mutateCompletionList } = useSWR(() => !isChatMode
  75. ? {
  76. url: `/apps/${appId}/completion-conversations`,
  77. params: query,
  78. }
  79. : null, fetchCompletionConversations)
  80. const total = isChatMode ? chatConversations?.total : completionConversations?.total
  81. return (
  82. <div className='flex flex-col h-full'>
  83. <p className='flex text-sm font-normal text-gray-500'>{t('appLog.description')}</p>
  84. <div className='flex flex-col py-4 flex-1'>
  85. <Filter appId={appId} queryParams={queryParams} setQueryParams={setQueryParams} />
  86. {total === undefined
  87. ? <Loading type='app' />
  88. : total > 0
  89. ? <List logs={isChatMode ? chatConversations : completionConversations} appDetail={appDetail} onRefresh={isChatMode ? mutateChatList : mutateCompletionList} />
  90. : <EmptyElement appUrl={`${appDetail?.site.app_base_url}/${appDetail?.mode}/${appDetail?.site.access_token}`} />
  91. }
  92. {/* Show Pagination only if the total is more than the limit */}
  93. {(total && total > APP_PAGE_LIMIT)
  94. ? <Pagination
  95. className="flex items-center w-full h-10 text-sm select-none mt-8"
  96. currentPage={currPage}
  97. edgePageCount={2}
  98. middlePagesSiblingCount={1}
  99. setCurrentPage={setCurrPage}
  100. totalPages={Math.ceil(total / APP_PAGE_LIMIT)}
  101. truncableClassName="w-8 px-0.5 text-center"
  102. truncableText="..."
  103. >
  104. <Pagination.PrevButton
  105. disabled={currPage === 0}
  106. className={`flex items-center mr-2 text-gray-500 focus:outline-none ${currPage === 0 ? 'cursor-not-allowed opacity-50' : 'cursor-pointer hover:text-gray-600 dark:hover:text-gray-200'}`} >
  107. <ArrowLeftIcon className="mr-3 h-3 w-3" />
  108. {t('appLog.table.pagination.previous')}
  109. </Pagination.PrevButton>
  110. <div className={`flex items-center justify-center flex-grow ${s.pagination}`}>
  111. <Pagination.PageButton
  112. activeClassName="bg-primary-50 dark:bg-opacity-0 text-primary-600 dark:text-white"
  113. className="flex items-center justify-center h-8 w-8 rounded-full cursor-pointer"
  114. inactiveClassName="text-gray-500"
  115. />
  116. </div>
  117. <Pagination.NextButton
  118. disabled={currPage === Math.ceil(total / APP_PAGE_LIMIT) - 1}
  119. className={`flex items-center mr-2 text-gray-500 focus:outline-none ${currPage === Math.ceil(total / APP_PAGE_LIMIT) - 1 ? 'cursor-not-allowed opacity-50' : 'cursor-pointer hover:text-gray-600 dark:hover:text-gray-200'}`} >
  120. {t('appLog.table.pagination.next')}
  121. <ArrowRightIcon className="ml-3 h-3 w-3" />
  122. </Pagination.NextButton>
  123. </Pagination>
  124. : null}
  125. </div>
  126. </div>
  127. )
  128. }
  129. export default Logs