Skip to content

Commit ea9443f

Browse files
authored
Merge pull request #180 from ssoda01/feature/add-index-download-btn
2 parents 98984f2 + ecba20b commit ea9443f

File tree

4 files changed

+75
-36
lines changed

4 files changed

+75
-36
lines changed

src/components/OperationCard.tsx

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
Button,
23
Card,
34
Drawer,
45
DrawerSize,
@@ -11,6 +12,7 @@ import {
1112
import { Tooltip2 } from '@blueprintjs/popover2'
1213

1314
import { useState } from 'react'
15+
import { handleCopyShortCode, handleDownloadJSON } from 'services/operation'
1416

1517
import { RelativeTime } from 'components/RelativeTime'
1618
import { OperationRating } from 'components/viewer/OperationRating'
@@ -32,6 +34,7 @@ export const OperationCard = ({
3234
}) => {
3335
const levels = useLevels()?.data?.data || []
3436
const [drawerOpen, setDrawerOpen] = useState(false)
37+
3538
return (
3639
<>
3740
<Drawer
@@ -52,9 +55,37 @@ export const OperationCard = ({
5255
onClick={() => setDrawerOpen(true)}
5356
>
5457
<div className="flex items-start">
55-
<H4 className="inline-block pb-1 border-b-2 border-zinc-200 border-solid mb-2">
58+
<H4 className="inline-block pb-1 border-b-2 border-zinc-200 border-solid mb-2 mr-2">
5659
{operationDoc.doc.title}
5760
</H4>
61+
<Tooltip2
62+
placement="bottom"
63+
content={<div className="max-w-sm">下载原 JSON</div>}
64+
>
65+
<Button
66+
small
67+
className="mr-2 mb-2"
68+
icon="download"
69+
onClick={(e) => {
70+
e.stopPropagation()
71+
handleDownloadJSON(operationDoc)
72+
}}
73+
/>
74+
</Tooltip2>
75+
<Tooltip2
76+
placement="bottom"
77+
content={<div className="max-w-sm">复制神秘代码</div>}
78+
>
79+
<Button
80+
small
81+
className="mb-2"
82+
icon="clipboard"
83+
onClick={(e) => {
84+
e.stopPropagation()
85+
handleCopyShortCode(operation)
86+
}}
87+
/>
88+
</Tooltip2>
5889
<div className="flex-1" />
5990
<div className="flex flex-col items-end">
6091
<div className="w-full flex justify-end text-zinc-500">

src/components/Toaster.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ import { Position, Toaster } from '@blueprintjs/core'
22

33
export const AppToaster = Toaster.create({
44
position: Position.BOTTOM_LEFT,
5+
className: '!fixed',
56
})

src/components/viewer/OperationViewer.tsx

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { useAtom } from 'jotai'
2222
import { noop } from 'lodash-es'
2323
import { ComponentType, FC, useEffect, useMemo, useState } from 'react'
2424
import { Link } from 'react-router-dom'
25+
import { handleCopyShortCode, handleDownloadJSON } from 'services/operation'
2526

2627
import { FactItem } from 'components/FactItem'
2728
import { Paragraphs } from 'components/Paragraphs'
@@ -141,39 +142,6 @@ export const OperationViewer: ComponentType<{
141142
}
142143
}, [error])
143144

144-
const handleCopyShortCode = () => {
145-
const shortCode = toShortCode(operation.id)
146-
navigator.clipboard.writeText(shortCode)
147-
148-
AppToaster.show({
149-
message: '已复制神秘代码,前往 MAA 粘贴即可使用~',
150-
intent: 'success',
151-
})
152-
}
153-
154-
const handleDownloadJSON = () => {
155-
// pretty print the JSON
156-
const json = JSON.stringify(
157-
snakeCaseKeysUnicode(operationDoc, { deep: true }),
158-
null,
159-
2,
160-
)
161-
const blob = new Blob([json], {
162-
type: 'application/json',
163-
})
164-
const url = URL.createObjectURL(blob)
165-
const link = document.createElement('a')
166-
link.href = url
167-
link.download = `MAACopilot_${operationDoc.doc.title}.json`
168-
link.click()
169-
URL.revokeObjectURL(url)
170-
171-
AppToaster.show({
172-
message: '已下载作业 JSON 文件,前往 MAA 选择即可使用~',
173-
intent: 'success',
174-
})
175-
}
176-
177145
const handleRating = async (decision: OpRatingType) => {
178146
// cancel rating if already rated by the same type
179147
if (decision === operation.ratingType) {
@@ -220,15 +188,15 @@ export const OperationViewer: ComponentType<{
220188
className="ml-4"
221189
icon="download"
222190
text="下载原 JSON"
223-
onClick={handleDownloadJSON}
191+
onClick={() => handleDownloadJSON(operationDoc)}
224192
/>
225193

226194
<Button
227195
className="ml-4"
228196
icon="clipboard"
229197
text="复制神秘代码"
230198
intent="primary"
231-
onClick={handleCopyShortCode}
199+
onClick={() => handleCopyShortCode(operation)}
232200
/>
233201
</>
234202
}

src/services/operation.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { AppToaster } from 'components/Toaster'
2+
3+
import { CopilotDocV1 } from '../models/copilot.schema'
4+
import { OperationListItem } from '../models/operation'
5+
import { toShortCode } from '../models/shortCode'
6+
import { snakeCaseKeysUnicode } from '../utils/object'
7+
8+
export const handleDownloadJSON = (operationDoc: CopilotDocV1.Operation) => {
9+
// pretty print the JSON
10+
const json = JSON.stringify(
11+
snakeCaseKeysUnicode(operationDoc, { deep: true }),
12+
null,
13+
2,
14+
)
15+
const blob = new Blob([json], {
16+
type: 'application/json',
17+
})
18+
const url = URL.createObjectURL(blob)
19+
const link = document.createElement('a')
20+
link.href = url
21+
link.download = `MAACopilot_${operationDoc.doc.title}.json`
22+
link.click()
23+
URL.revokeObjectURL(url)
24+
25+
AppToaster.show({
26+
message: '已下载作业 JSON 文件,前往 MAA 选择即可使用~',
27+
intent: 'success',
28+
})
29+
}
30+
31+
export const handleCopyShortCode = (operation: OperationListItem) => {
32+
const shortCode = toShortCode(operation.id)
33+
navigator.clipboard.writeText(shortCode)
34+
35+
AppToaster.show({
36+
message: '已复制神秘代码,前往 MAA 粘贴即可使用~',
37+
intent: 'success',
38+
})
39+
}

0 commit comments

Comments
 (0)