index.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 { useDebounce } from 'ahooks'
  7. import { omit } from 'lodash-es'
  8. import dayjs from 'dayjs'
  9. import utc from 'dayjs/plugin/utc'
  10. import timezone from 'dayjs/plugin/timezone'
  11. import { Trans, useTranslation } from 'react-i18next'
  12. import Link from 'next/link'
  13. import List from './list'
  14. import Filter, { TIME_PERIOD_MAPPING } from './filter'
  15. import Pagination from '@/app/components/base/pagination'
  16. import Loading from '@/app/components/base/loading'
  17. import { fetchWorkflowLogs } from '@/service/log'
  18. import { APP_PAGE_LIMIT } from '@/config'
  19. import type { App, AppMode } from '@/types/app'
  20. import { useAppContext } from '@/context/app-context'
  21. dayjs.extend(utc)
  22. dayjs.extend(timezone)
  23. export type ILogsProps = {
  24. appDetail: App
  25. }
  26. export type QueryParam = {
  27. period: string
  28. status?: string
  29. keyword?: string
  30. }
  31. const ThreeDotsIcon = ({ className }: SVGProps<SVGElement>) => {
  32. return <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg" className={className ?? ''}>
  33. <path d="M5 6.5V5M8.93934 7.56066L10 6.5M10.0103 11.5H11.5103" stroke="#374151" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
  34. </svg>
  35. }
  36. const EmptyElement: FC<{ appUrl: string }> = ({ appUrl }) => {
  37. const { t } = useTranslation()
  38. const pathname = usePathname()
  39. const pathSegments = pathname.split('/')
  40. pathSegments.pop()
  41. return <div className='flex items-center justify-center h-full'>
  42. <div className='bg-background-section-burn w-[560px] h-fit box-border px-5 py-4 rounded-2xl'>
  43. <span className='text-text-secondary system-md-semibold'>{t('appLog.table.empty.element.title')}<ThreeDotsIcon className='inline relative -top-3 -left-1.5' /></span>
  44. <div className='mt-2 text-text-tertiary system-sm-regular'>
  45. <Trans
  46. i18nKey="appLog.table.empty.element.content"
  47. components={{ shareLink: <Link href={`${pathSegments.join('/')}/overview`} className='text-util-colors-blue-blue-600' />, testLink: <Link href={appUrl} className='text-util-colors-blue-blue-600' target='_blank' rel='noopener noreferrer' /> }}
  48. />
  49. </div>
  50. </div>
  51. </div>
  52. }
  53. const Logs: FC<ILogsProps> = ({ appDetail }) => {
  54. const { t } = useTranslation()
  55. const { userProfile: { timezone } } = useAppContext()
  56. const [queryParams, setQueryParams] = useState<QueryParam>({ status: 'all', period: '2' })
  57. const [currPage, setCurrPage] = React.useState<number>(0)
  58. const debouncedQueryParams = useDebounce(queryParams, { wait: 500 })
  59. const [limit, setLimit] = React.useState<number>(APP_PAGE_LIMIT)
  60. const query = {
  61. page: currPage + 1,
  62. limit,
  63. ...(debouncedQueryParams.status !== 'all' ? { status: debouncedQueryParams.status } : {}),
  64. ...(debouncedQueryParams.keyword ? { keyword: debouncedQueryParams.keyword } : {}),
  65. ...((debouncedQueryParams.period !== '9')
  66. ? {
  67. created_at__after: dayjs().subtract(TIME_PERIOD_MAPPING[debouncedQueryParams.period].value, 'day').startOf('day').tz(timezone).format('YYYY-MM-DDTHH:mm:ssZ'),
  68. created_at__before: dayjs().endOf('day').tz(timezone).format('YYYY-MM-DDTHH:mm:ssZ'),
  69. }
  70. : {}),
  71. ...omit(debouncedQueryParams, ['period', 'status']),
  72. }
  73. const getWebAppType = (appType: AppMode) => {
  74. if (appType !== 'completion' && appType !== 'workflow')
  75. return 'chat'
  76. return appType
  77. }
  78. const { data: workflowLogs, mutate } = useSWR({
  79. url: `/apps/${appDetail.id}/workflow-app-logs`,
  80. params: query,
  81. }, fetchWorkflowLogs)
  82. const total = workflowLogs?.total
  83. return (
  84. <div className='flex flex-col h-full'>
  85. <h1 className='text-text-primary system-xl-semibold'>{t('appLog.workflowTitle')}</h1>
  86. <p className='text-text-tertiary system-sm-regular'>{t('appLog.workflowSubtitle')}</p>
  87. <div className='flex flex-col py-4 flex-1 max-h-[calc(100%-16px)]'>
  88. <Filter queryParams={queryParams} setQueryParams={setQueryParams} />
  89. {/* workflow log */}
  90. {total === undefined
  91. ? <Loading type='app' />
  92. : total > 0
  93. ? <List logs={workflowLogs} appDetail={appDetail} onRefresh={mutate} />
  94. : <EmptyElement appUrl={`${appDetail.site.app_base_url}/${getWebAppType(appDetail.mode)}/${appDetail.site.access_token}`} />
  95. }
  96. {/* Show Pagination only if the total is more than the limit */}
  97. {(total && total > APP_PAGE_LIMIT)
  98. ? <Pagination
  99. current={currPage}
  100. onChange={setCurrPage}
  101. total={total}
  102. limit={limit}
  103. onLimitChange={setLimit}
  104. />
  105. : null}
  106. </div>
  107. </div>
  108. )
  109. }
  110. export default Logs