index.tsx 5.4 KB

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