CommandsHistory.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * CommandsHistory
  3. * @author: oldj
  4. * @homepage: https://oldj.net
  5. */
  6. import {
  7. Alert,
  8. AlertDescription,
  9. AlertIcon,
  10. AlertTitle,
  11. Box,
  12. Button,
  13. Center,
  14. HStack,
  15. IconButton,
  16. Spacer,
  17. VStack,
  18. } from '@chakra-ui/react'
  19. import { actions } from '@renderer/core/agent'
  20. import { ICommandRunResult } from '@common/data'
  21. import useI18n from '@renderer/models/useI18n'
  22. import dayjs from 'dayjs'
  23. import React, { useEffect, useState } from 'react'
  24. import { BiTrash } from 'react-icons/bi'
  25. interface Props {
  26. is_show: boolean
  27. }
  28. const CommandsHistory = (props: Props) => {
  29. const { is_show } = props
  30. const [list, setList] = useState<ICommandRunResult[]>([])
  31. const { lang } = useI18n()
  32. const loadData = async () => {
  33. let data = await actions.cmdGetHistoryList()
  34. data = data.reverse()
  35. setList(data)
  36. }
  37. const deleteOneRecord = async (_id: string) => {
  38. await actions.cmdDeleteHistory(_id)
  39. setList(list.filter((i) => i._id !== _id))
  40. }
  41. const clearAll = async () => {
  42. await actions.cmdClearHistory()
  43. setList([])
  44. }
  45. useEffect(() => {
  46. if (is_show) {
  47. loadData()
  48. }
  49. }, [is_show])
  50. if (!is_show) {
  51. return null
  52. }
  53. if (list.length === 0) {
  54. return <Center h="100px">{lang.no_record}</Center>
  55. }
  56. return (
  57. <VStack w="100%">
  58. {list.map((item, idx) => {
  59. return (
  60. <Alert
  61. key={idx}
  62. status={item.success ? 'success' : 'error'}
  63. w="100%"
  64. // alignItems="top"
  65. >
  66. <AlertIcon />
  67. <Box flex="1">
  68. <AlertTitle>
  69. <HStack>
  70. <span>#{item._id}</span>
  71. <span style={{ fontWeight: 'normal' }}>
  72. {dayjs(item.add_time_ms).format('YYYY-MM-DD HH:mm:ss')}
  73. </span>
  74. <Spacer />
  75. <IconButton
  76. aria-label="delete"
  77. icon={<BiTrash />}
  78. size="sm"
  79. variant="ghost"
  80. onClick={() => item._id && deleteOneRecord(item._id)}
  81. />
  82. </HStack>
  83. </AlertTitle>
  84. <AlertDescription>
  85. {item.stdout ? (
  86. <>
  87. <Box>
  88. <strong>stdout:</strong>
  89. </Box>
  90. <Box>
  91. <pre>{item.stdout}</pre>
  92. </Box>
  93. </>
  94. ) : null}
  95. {item.stderr ? (
  96. <>
  97. <Box>
  98. <strong>stderr:</strong>
  99. </Box>
  100. <Box>
  101. <pre>{item.stderr}</pre>
  102. </Box>
  103. </>
  104. ) : null}
  105. </AlertDescription>
  106. </Box>
  107. </Alert>
  108. )
  109. })}
  110. <Box pt={10}>
  111. <Button onClick={clearAll} variant="link">
  112. {lang.clear_history}
  113. </Button>
  114. </Box>
  115. </VStack>
  116. )
  117. }
  118. export default CommandsHistory