7
7
import { useModel } from '@@/plugin-model/useModel'
8
8
import ItemIcon from '@renderer/components/ItemIcon'
9
9
import SwitchButton from '@renderer/components/SwitchButton'
10
- import { agent } from '@renderer/core/agent'
10
+ import { actions , agent } from '@renderer/core/agent'
11
11
import { PopupMenu } from '@renderer/core/PopupMenu'
12
12
import { IHostsListObject } from '@root/common/data'
13
13
import { updateOneItem } from '@root/common/hostsFn'
14
14
import clsx from 'clsx'
15
15
import React , { useEffect , useRef , useState } from 'react'
16
16
import { BiEdit } from 'react-icons/bi'
17
- import { Center } from '@chakra-ui/react'
17
+ import { Center , ToastId , useToast } from '@chakra-ui/react'
18
18
import scrollIntoView from 'smooth-scroll-into-view-if-needed'
19
19
import styles from './ListItem.less'
20
20
import events from '@root/common/events'
21
+ import { IMenuItemOption } from '@root/common/types'
21
22
22
23
interface Props {
23
24
data : IHostsListObject
@@ -34,6 +35,9 @@ const ListItem = (props: Props) => {
34
35
const [ is_on , setIsOn ] = useState ( data . on )
35
36
const el = useRef < HTMLDivElement > ( null )
36
37
// const [item_height, setItemHeight] = useState(0)
38
+ const ref_toast_refresh = useRef < ToastId | null > ( null )
39
+
40
+ const toast = useToast ( )
37
41
38
42
useEffect ( ( ) => {
39
43
setIsOn ( data . on )
@@ -95,13 +99,55 @@ const ListItem = (props: Props) => {
95
99
deal_count = selected_ids . length
96
100
}
97
101
98
- const menu = new PopupMenu ( [
102
+ let menu_items : IMenuItemOption [ ] = [
99
103
{
100
104
label : lang . edit ,
101
105
click ( ) {
102
106
agent . broadcast ( events . edit_hosts_info , data )
103
107
} ,
104
108
} ,
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
+ } ,
105
151
{
106
152
type : 'separator' ,
107
153
} ,
@@ -117,7 +163,13 @@ const ListItem = (props: Props) => {
117
163
agent . broadcast ( events . move_to_trashcan , ids )
118
164
} ,
119
165
} ,
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 )
121
173
122
174
! data . is_sys && ! is_tray && menu . show ( )
123
175
e . preventDefault ( )
0 commit comments