Browse Source

Displays a toast alert when a custom command fails to execute. #586

oldj 3 years ago
parent
commit
5e76a26897

+ 1 - 0
src/common/events.ts

@@ -8,6 +8,7 @@ export default {
   add_new: 'add_new',
   browser_link: 'browser_link',
   close_find: 'close_find',
+  cmd_run_result: 'cmd_run_result',
   config_updated: 'config_updated',
   edit_hosts_info: 'edit_hosts_info',
   editor_content_change: 'editor:content_change',

+ 3 - 0
src/main/actions/cmd/tryToRun.ts

@@ -7,6 +7,8 @@
 import { cfgdb } from '@main/data'
 import { ICommandRunResult } from '@root/common/data'
 import { exec } from 'child_process'
+import { broadcast } from '@main/core/agent'
+import events from '@root/common/events'
 
 const run = (cmd: string): Promise<ICommandRunResult> =>
   new Promise((resolve) => {
@@ -34,6 +36,7 @@ export default async () => {
   let result = await run(cmd)
   console.log(result)
   await cfgdb.collection.cmd_history.insert(result)
+  broadcast(events.cmd_run_result, result)
 
   // auto delete old records
   const max_records = 200

+ 13 - 0
src/renderer/components/MainPanel/index.tsx

@@ -11,12 +11,14 @@ import useOnBroadcast from '@renderer/core/useOnBroadcast'
 import events from '@root/common/events'
 import React, { useEffect, useState } from 'react'
 import styles from './index.less'
+import { useToast } from '@chakra-ui/react'
 
 interface Props {}
 
 const MainPanel = (props: Props) => {
   const { current_hosts } = useModel('useHostsData')
   const [system_hosts_content, setSystemHostsContent] = useState('')
+  const toast = useToast()
 
   useEffect(() => {
     if (!current_hosts) {
@@ -34,6 +36,17 @@ const MainPanel = (props: Props) => {
     [current_hosts],
   )
 
+  useOnBroadcast(events.cmd_run_result, (result) => {
+    // console.log(result)
+    if (!result.success) {
+      toast({
+        status: 'error',
+        description: result.stderr || 'cmd run error',
+        isClosable: true,
+      })
+    }
+  })
+
   return (
     <div className={styles.root}>
       <HostsEditor