Skip to content

Commit 0326d2e

Browse files
committed
Add refresh item in the context menu. #611
1 parent 94090b3 commit 0326d2e

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

src/main/actions/checkUpdate.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import compareVersions from 'compare-versions'
1414
import { v4 as uuid4 } from 'uuid'
1515

1616
const getUniqueId = async (): Promise<string> => {
17-
let uid: string = await localdb.dict.local.get('uid', '')
17+
let uid: string | undefined = await localdb.dict.local.get('uid')
1818
if (!uid) {
1919
uid = uuid4()
2020
await localdb.dict.local.set('uid', uid)

src/renderer/components/EditHostsInfo.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* EditHosts
2+
* EditHostsInfo
33
* @author: oldj
44
* @homepage: https://oldj.net
55
*/

src/renderer/components/List/ListItem.tsx

+56-4
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@
77
import { useModel } from '@@/plugin-model/useModel'
88
import ItemIcon from '@renderer/components/ItemIcon'
99
import SwitchButton from '@renderer/components/SwitchButton'
10-
import { agent } from '@renderer/core/agent'
10+
import { actions, agent } from '@renderer/core/agent'
1111
import { PopupMenu } from '@renderer/core/PopupMenu'
1212
import { IHostsListObject } from '@root/common/data'
1313
import { updateOneItem } from '@root/common/hostsFn'
1414
import clsx from 'clsx'
1515
import React, { useEffect, useRef, useState } from 'react'
1616
import { BiEdit } from 'react-icons/bi'
17-
import { Center } from '@chakra-ui/react'
17+
import { Center, ToastId, useToast } from '@chakra-ui/react'
1818
import scrollIntoView from 'smooth-scroll-into-view-if-needed'
1919
import styles from './ListItem.less'
2020
import events from '@root/common/events'
21+
import { IMenuItemOption } from '@root/common/types'
2122

2223
interface Props {
2324
data: IHostsListObject
@@ -34,6 +35,9 @@ const ListItem = (props: Props) => {
3435
const [is_on, setIsOn] = useState(data.on)
3536
const el = useRef<HTMLDivElement>(null)
3637
// const [item_height, setItemHeight] = useState(0)
38+
const ref_toast_refresh = useRef<ToastId | null>(null)
39+
40+
const toast = useToast()
3741

3842
useEffect(() => {
3943
setIsOn(data.on)
@@ -95,13 +99,55 @@ const ListItem = (props: Props) => {
9599
deal_count = selected_ids.length
96100
}
97101

98-
const menu = new PopupMenu([
102+
let menu_items: IMenuItemOption[] = [
99103
{
100104
label: lang.edit,
101105
click() {
102106
agent.broadcast(events.edit_hosts_info, data)
103107
},
104108
},
109+
{
110+
label: lang.refresh,
111+
async click() {
112+
ref_toast_refresh.current = toast({
113+
status: 'loading',
114+
description: lang.loading,
115+
})
116+
117+
actions
118+
.refreshHosts(data.id)
119+
.then((r) => {
120+
console.log(r)
121+
if (!r.success) {
122+
toast({
123+
status: 'error',
124+
description: r.message || r.code || 'Error!',
125+
isClosable: true,
126+
})
127+
return
128+
}
129+
130+
toast({
131+
status: 'success',
132+
description: 'OK!',
133+
isClosable: true,
134+
})
135+
})
136+
.catch((e) => {
137+
console.log(e)
138+
toast({
139+
status: 'error',
140+
description: e.message,
141+
isClosable: true,
142+
})
143+
})
144+
.finally(() => {
145+
if (ref_toast_refresh.current) {
146+
toast.close(ref_toast_refresh.current)
147+
}
148+
})
149+
},
150+
},
105151
{
106152
type: 'separator',
107153
},
@@ -117,7 +163,13 @@ const ListItem = (props: Props) => {
117163
agent.broadcast(events.move_to_trashcan, ids)
118164
},
119165
},
120-
])
166+
]
167+
168+
if (data.type !== 'remote') {
169+
menu_items = menu_items.filter((i) => i.label !== lang.refresh)
170+
}
171+
172+
const menu = new PopupMenu(menu_items)
121173

122174
!data.is_sys && !is_tray && menu.show()
123175
e.preventDefault()

0 commit comments

Comments
 (0)