main
ts 26 lines 925 Bytes
Raw
1 import { apiCall } from '@hfs/shared/api'
2 import { createElement as h } from 'react'
3 import { toast } from './dialog'
4 import { IconBtn, IconBtnProps } from './mui'
5 import { Block } from '@mui/icons-material'
6 import { useBatch } from '@hfs/shared'
7
8 export function BlockIpBtn({ ip, comment, ...rest }: { ip: string, comment: string } & Partial<IconBtnProps>) {
9 const { data, refresh } = useBatch(isIpBlocked, ip, { delay: 100, expireAfter: 5_000 })
10 return h(IconBtn, {
11 icon: Block,
12 title: "Block IP",
13 confirm: "Block address " + ip,
14 ...data && { disabled: true, title: "Blocked" },
15 ...rest,
16 async onClick() {
17 await apiCall('add_block', { ip, merge: { comment } })
18 refresh()
19 toast("Blocked", 'success')
20 }
21 })
22 }
23
24 async function isIpBlocked(ips: string[]) {
25 return apiCall('is_ip_blocked', { ips }).then(x => x.blocked)
26 }