-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathAPIMenu.tsx
70 lines (61 loc) · 2.09 KB
/
APIMenu.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import type { Endpoint } from '../../types'
import { LOCAL_STORAGE_KEY } from './APIExplorer'
import { useHistory } from '../../lib/hooks/use-history'
import { formatJSON } from '@/lib/utils'
import {
DropdownMenuGroup,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
} from '../ui/dropdown-menu'
import TrashIcon from '@heroicons/react/24/outline/TrashIcon'
import { ArrowDownOnSquareIcon } from '@heroicons/react/24/outline'
import ResourceDropdownMenu from '../shared/ResourceDropdownMenu'
interface Props {
selected: Endpoint
onAfterClear: () => void
}
const APIMenu: React.FC<Props> = ({ selected, onAfterClear }) => {
const { deleteHistory } = useHistory('apis')
const clearHistory = async () => {
const prefix = `${LOCAL_STORAGE_KEY}-${selected.api}-`
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i)
if (key?.startsWith(prefix)) {
localStorage.removeItem(key)
}
}
localStorage.removeItem(`${LOCAL_STORAGE_KEY}-requests`)
await deleteHistory()
onAfterClear()
}
const downloadSpec = () => {
const json = formatJSON(selected.doc)
const blob = new Blob([json], { type: 'application/json' })
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = `${selected.api}-spec.json`
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
URL.revokeObjectURL(url)
}
return (
<ResourceDropdownMenu>
<DropdownMenuLabel className="text-foreground">API Menu</DropdownMenuLabel>
<DropdownMenuSeparator />
<DropdownMenuGroup>
<DropdownMenuItem onClick={downloadSpec} className="text-foreground">
<ArrowDownOnSquareIcon className="mr-2 h-4 w-4" />
<span>Export Spec</span>
</DropdownMenuItem>
<DropdownMenuItem onClick={clearHistory} className="text-foreground">
<TrashIcon className="mr-2 h-4 w-4" />
<span>Clear History</span>
</DropdownMenuItem>
</DropdownMenuGroup>
</ResourceDropdownMenu>
)
}
export default APIMenu