jan/web/hooks/useDeleteBot.ts
2023-10-26 09:54:43 +07:00

26 lines
825 B
TypeScript

import { useSetAtom } from 'jotai'
import { activeBotAtom } from '@helpers/atoms/Bot.atom'
import { rightSideBarExpandStateAtom } from '@helpers/atoms/SideBarExpand.atom'
import { executeSerial } from '@services/pluginService'
import { DataService } from '@janhq/core'
export default function useDeleteBot() {
const setActiveBot = useSetAtom(activeBotAtom)
const setRightPanelVisibility = useSetAtom(rightSideBarExpandStateAtom)
const deleteBot = async (botId: string): Promise<'success' | 'failed'> => {
try {
await executeSerial(DataService.DeleteBot, botId)
setRightPanelVisibility(false)
setActiveBot(undefined)
return 'success'
} catch (err) {
alert(`Failed to delete bot ${botId}: ${err}`)
console.error(err)
return 'failed'
}
}
return { deleteBot }
}