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

Commit ea7a335

Browse files
committed
feat: collapse the original
1 parent 3cd3ad3 commit ea7a335

File tree

8 files changed

+71
-12
lines changed

8 files changed

+71
-12
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"@types/pino": "^6.3.4",
3636
"@types/rangy": "^0.0.33",
3737
"@types/react": "^17.0.0",
38+
"@types/react-collapse": "^5.0.0",
3839
"@types/react-dom": "^17.0.0",
3940
"@types/uuid": "^8.3.0",
4041
"@typescript-eslint/eslint-plugin": "^4.5.0",
@@ -74,6 +75,7 @@
7475
"rangy": "^1.3.0",
7576
"react": "^17.0.1",
7677
"react-clipboard.js": "^2.0.16",
78+
"react-collapse": "^5.1.0",
7779
"react-dom": "^17.0.1",
7880
"react-draggable": "^4.4.3",
7981
"react-hot-loader": "^4.13.0",

src/common/api.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import axios from 'axios'
22

3-
import { TranslateResult } from './types'
3+
import { APIRegions, TranslateResult } from './types'
44

55
class Client {
66
axios = axios.create({
7-
baseURL: 'https://a-translator-api.nerdynerd.org',
7+
baseURL: this.getAPI(),
88
})
99

10-
constructor(private apiToken: string) {
10+
constructor(private apiToken: string, private region: APIRegions) {
1111
this.axios.interceptors.response.use(
1212
function (response) {
1313
return response
@@ -34,6 +34,17 @@ class Client {
3434

3535
return this.axios.post('/v2/translate', form).then((res) => res.data)
3636
}
37+
38+
private getAPI(): string {
39+
switch (this.region) {
40+
case 'global':
41+
return 'https://a-translator-api.nerdynerd.org'
42+
case 'dev':
43+
return 'http://localhost:1337'
44+
default:
45+
return 'https://a-translator-api.nerdynerd.org'
46+
}
47+
}
3748
}
3849

3950
export default Client

src/common/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export interface Config {
22
token: string
33
targetLang: SupportLanguages
4+
region: APIRegions
45
}
56

67
export type SupportLanguages =
@@ -16,6 +17,8 @@ export type SupportLanguages =
1617
| 'PL'
1718
| 'RU'
1819

20+
export type APIRegions = 'default' | 'global' | 'dev'
21+
1922
export type TranslateResult = {
2023
translations: Array<{
2124
detected_source_language: SupportLanguages

src/pages/Background/common/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const onTranslate: Handler<{
1919
})
2020

2121
const config: Config = await cc(chrome.storage.sync, 'get')
22-
const client = new Client(config.token)
22+
const client = new Client(config.token, config.region)
2323

2424
if (process.env.USE_MOCK_TRANSLATE === 'true') {
2525
resolve({

src/pages/Content/components/TranslationItem.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { useEffect, useState } from 'react'
22
import Clipboard from 'react-clipboard.js'
33
import { useSnackbar } from 'notistack'
4+
import { Collapse } from 'react-collapse'
45

56
import logger from '../../../common/logger'
67
import { TranslateResult } from '../../../common/types'
@@ -17,6 +18,7 @@ const TranslationItem: React.FC<{
1718
const config = useConfig()
1819
const [loading, setLoading] = useState(true)
1920
const [result, setResult] = useState<string>()
21+
const [collapse, setCollapse] = useState(true)
2022
const { enqueueSnackbar } = useSnackbar()
2123

2224
const findOriginal = () => {
@@ -67,13 +69,14 @@ const TranslationItem: React.FC<{
6769
return (
6870
<div className="ate_TranslationItem">
6971
<div className="ate_TranslationItem__upper">
70-
<div>
71-
{job.text.length > 100 ? (
72-
<div>{job.text.substring(0, 100)}...</div>
73-
) : (
72+
<div
73+
className="ate_TranslationItem__original"
74+
onClick={() => setCollapse((prev) => !prev)}>
75+
<Collapse isOpened={!collapse}>
7476
<div>{job.text}</div>
75-
)}
77+
</Collapse>
7678
</div>
79+
7780
{loading ? (
7881
<div className="ate_TranslationItem__result">翻译中…</div>
7982
) : undefined}

src/pages/Content/styles/components/TranslationItem.scss

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@
1111
@apply space-x-2
1212
}
1313
}
14+
&__original {
15+
@apply bg-gray-50 hover:bg-gray-100 p-3 rounded cursor-pointer;
16+
17+
.ReactCollapse--collapse {
18+
min-height: 36px;
19+
will-change: height;
20+
transition-property: height;
21+
22+
@apply ease-in-out duration-300;
23+
}
24+
}
1425
&__result {
1526
@apply rounded bg-yellow-50 p-3;
1627
}

src/pages/Options/Options.tsx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,22 @@ import { Global } from '@emotion/react'
99
import cc from 'chrome-call'
1010

1111
import Client from '../../common/api'
12-
import { Config } from '../../common/types'
12+
import { APIRegions, Config } from '../../common/types'
1313

1414
import OptionSection from './components/OptionSection'
1515

1616
const Options: React.FC = () => {
1717
const [targetLang, setTargetLang] = useState('ZH')
1818
const [token, setToken] = useState('')
19+
const [region, setRegion] = useState<APIRegions>('default')
1920

2021
const onSubmit: FormEventHandler = (e) => {
2122
e.preventDefault()
2223
;(async () => {
2324
await cc(chrome.storage.sync, 'set', {
2425
targetLang,
2526
token,
27+
region,
2628
})
2729

2830
window.alert('保存成功')
@@ -37,7 +39,7 @@ const Options: React.FC = () => {
3739
return
3840
}
3941

40-
const client = new Client(token)
42+
const client = new Client(token, region)
4143

4244
client
4345
.translate('This is a test message.', 'ZH')
@@ -53,6 +55,7 @@ const Options: React.FC = () => {
5355
cc(chrome.storage.sync, 'get').then((config: Config) => {
5456
setTargetLang(config.targetLang)
5557
setToken(config.token)
58+
setRegion(config.region)
5659
})
5760
}, [])
5861

@@ -90,7 +93,7 @@ const Options: React.FC = () => {
9093
<OptionSection title={'目标语言'}>
9194
<select
9295
tw="px-4 py-3 rounded-md"
93-
name="target-language"
96+
name="target-lang"
9497
value={targetLang}
9598
onChange={(e) => setTargetLang(e.target.value)}>
9699
<option value="ZH">中文</option>
@@ -118,6 +121,20 @@ const Options: React.FC = () => {
118121
onChange={(e) => setToken(e.target.value)}
119122
/>
120123
</OptionSection>
124+
125+
<OptionSection title={'API Region'}>
126+
<select
127+
tw="px-4 py-3 rounded-md"
128+
name="region"
129+
value={region}
130+
onChange={(e) => setRegion(e.target.value as APIRegions)}>
131+
<option value="default">默认</option>
132+
<option value="global">全球(非亚洲地区)</option>
133+
{process.env.NODE_ENV !== 'production' ? (
134+
<option value="dev">DEV</option>
135+
) : undefined}
136+
</select>
137+
</OptionSection>
121138
</div>
122139

123140
<div tw="p-5 space-x-4 justify-self-end">

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,6 +1531,13 @@
15311531
resolved "https://registry.yarnpkg.com/@types/rangy/-/rangy-0.0.33.tgz#9f1afff01b4ba840360713a2c2ef3a36e7d49576"
15321532
integrity sha512-j2lhwVgbxZORhiB7yP+cly6+ow75MYmyHIkkSBwDl7XzrO5TMUECKkXqCf0pZVDjW5TgulIvhKg7SyEWSSgzmA==
15331533

1534+
"@types/react-collapse@^5.0.0":
1535+
version "5.0.0"
1536+
resolved "https://registry.yarnpkg.com/@types/react-collapse/-/react-collapse-5.0.0.tgz#ffe6173db457b4b147319e1bd522b5b9d3d68fac"
1537+
integrity sha512-iM5QPtS0JVbL5TWm8vilaG4iYiQIFSSqXDHcRUA50qBLSgC0GURk7VZYmgo6u4YF0LnpvKBQYRhPv1ut9DukIg==
1538+
dependencies:
1539+
"@types/react" "*"
1540+
15341541
"@types/react-dom@^17.0.0":
15351542
version "17.0.0"
15361543
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-17.0.0.tgz#b3b691eb956c4b3401777ee67b900cb28415d95a"
@@ -6314,6 +6321,11 @@ react-clipboard.js@^2.0.16:
63146321
clipboard "^2.0.0"
63156322
prop-types "^15.5.0"
63166323

6324+
react-collapse@^5.1.0:
6325+
version "5.1.0"
6326+
resolved "https://registry.yarnpkg.com/react-collapse/-/react-collapse-5.1.0.tgz#36f69ecb0fe797f976aaf5e4f2b2c248d2760140"
6327+
integrity sha512-5v0ywsn9HjiR/odNzbRDs0RZfrnbdSippJebWOBCFFDA12Vx8DddrbI4qWVf1P2wTiVagrpcSy07AU0b6+gM9Q==
6328+
63176329
react-dom@^17.0.1:
63186330
version "17.0.1"
63196331
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-17.0.1.tgz#1de2560474ec9f0e334285662ede52dbc5426fc6"

0 commit comments

Comments
 (0)