Skip to content
This repository was archived by the owner on Aug 16, 2024. It is now read-only.

Commit 2b287d3

Browse files
committed
feat: retry button
1 parent c871d58 commit 2b287d3

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

src/components/svg/Refresh.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react'
2+
3+
export default function Refresh(): React.ReactElement {
4+
return (
5+
<svg
6+
xmlns="http://www.w3.org/2000/svg"
7+
fill="none"
8+
width="16"
9+
height="16"
10+
viewBox="0 0 24 24"
11+
stroke="currentColor">
12+
<path
13+
strokeLinecap="round"
14+
strokeLinejoin="round"
15+
strokeWidth={2}
16+
d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"
17+
/>
18+
</svg>
19+
)
20+
}

src/pages/Content/components/TranslationItem.tsx renamed to src/pages/Content/components/TranslationItem/index.tsx

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@ import { Collapse } from 'react-collapse'
55
import scrollParent from 'scrollparent'
66
import tw, { css } from 'twin.macro'
77

8-
import logger from '../../../common/logger'
9-
import { TranslateResult } from '../../../common/types'
10-
import IconButton from '../../../components/IconButton'
11-
import ArrowRight from '../../../components/svg/ArrowRight'
12-
import ClipboardCopy from '../../../components/svg/ClipboardCopy'
13-
import client from '../common/client'
14-
import { TranslateJob } from '../common/types'
15-
import { cleanText } from '../common/utils'
16-
import { useConfig } from '../providers/config'
8+
import logger from '../../../../common/logger'
9+
import { TranslateResult } from '../../../../common/types'
10+
import IconButton from '../../../../components/IconButton'
11+
import ArrowRight from '../../../../components/svg/ArrowRight'
12+
import ClipboardCopy from '../../../../components/svg/ClipboardCopy'
13+
import Refresh from '../../../../components/svg/Refresh'
14+
import client from '../../common/client'
15+
import { TranslateJob } from '../../common/types'
16+
import { cleanText } from '../../common/utils'
17+
import { useConfig } from '../../providers/config'
1718

1819
const TranslationItem: React.FC<{
1920
job: TranslateJob
2021
}> = ({ job }) => {
2122
const config = useConfig()
2223
const [loading, setLoading] = useState(true)
24+
const [error, setError] = useState<string>()
2325
const [result, setResult] = useState<string[]>()
26+
const [dirty, setDirty] = useState(0)
2427
const [collapse, setCollapse] = useState(true)
2528
const { enqueueSnackbar } = useSnackbar()
2629

@@ -56,6 +59,10 @@ const TranslationItem: React.FC<{
5659
}
5760
}
5861

62+
const refreshResult = () => {
63+
setDirty((val) => val + 1)
64+
}
65+
5966
useEffect(() => {
6067
if (!config) return
6168

@@ -85,16 +92,17 @@ const TranslationItem: React.FC<{
8592
setResult(result)
8693
setLoading(false)
8794
})
88-
.catch((err) => {
95+
.catch((err: Error) => {
8996
logger.error({
9097
msg: 'translate failed',
9198
data: err,
9299
})
93100

101+
setError(err.message)
94102
setLoading(false)
95103
enqueueSnackbar(`翻译失败:${err.message}`, { variant: 'error' })
96104
})
97-
}, [job, config])
105+
}, [job, config, enqueueSnackbar, dirty])
98106

99107
return (
100108
<div tw="p-3 text-gray-800 space-y-3 select-text">
@@ -128,6 +136,7 @@ const TranslationItem: React.FC<{
128136
翻译中…
129137
</div>
130138
) : undefined}
139+
131140
{result ? (
132141
<div tw="rounded bg-yellow-50 p-3 space-y-2 leading-normal">
133142
{result.map((item, index) => (
@@ -163,6 +172,16 @@ const TranslationItem: React.FC<{
163172
<ClipboardCopy />
164173
</Clipboard>
165174
) : undefined}
175+
176+
{!loading && error ? (
177+
<IconButton
178+
tw="p-1 border-red-500 bg-red-100 text-red-500 hover:bg-red-200"
179+
onClick={() => refreshResult()}
180+
title="重试">
181+
<Refresh />
182+
</IconButton>
183+
) : undefined}
184+
166185
<IconButton
167186
tw="p-1"
168187
onClick={() => findOriginal()}

0 commit comments

Comments
 (0)