2024-07-27 18:53:07 +02:00
|
|
|
import React, {useEffect, useState} from "react";
|
2024-07-29 18:54:28 +02:00
|
|
|
import {Card, Flex, Form, message, Skeleton} from "antd";
|
|
|
|
|
import {EventAction, getWatchlists, postWatchlist} from "../../utils/api";
|
2024-07-27 18:53:07 +02:00
|
|
|
import {AxiosError} from "axios";
|
2024-07-28 15:36:22 +02:00
|
|
|
import {t} from 'ttag'
|
2024-07-29 18:54:28 +02:00
|
|
|
import {WatchlistForm} from "../../components/tracking/WatchlistForm";
|
|
|
|
|
import {WatchlistsList} from "../../components/tracking/WatchlistsList";
|
2024-07-30 00:55:36 +02:00
|
|
|
import {Connector, getConnectors} from "../../utils/api/connectors";
|
2024-07-27 18:53:07 +02:00
|
|
|
|
|
|
|
|
|
2024-07-27 20:44:10 +02:00
|
|
|
type Watchlist = { token: string, domains: { ldhName: string }[], triggers?: { event: EventAction, action: string }[] }
|
|
|
|
|
|
2024-07-27 17:19:25 +02:00
|
|
|
export default function WatchlistPage() {
|
2024-07-29 00:04:45 +02:00
|
|
|
|
2024-07-27 18:53:07 +02:00
|
|
|
const [form] = Form.useForm()
|
|
|
|
|
const [messageApi, contextHolder] = message.useMessage()
|
2024-07-27 20:44:10 +02:00
|
|
|
const [watchlists, setWatchlists] = useState<Watchlist[] | null>()
|
2024-07-30 00:55:36 +02:00
|
|
|
const [connectors, setConnectors] = useState<(Connector & { id: string })[] | null>()
|
2024-07-27 18:53:07 +02:00
|
|
|
|
2024-07-30 00:55:36 +02:00
|
|
|
const onCreateWatchlist = (values: {
|
|
|
|
|
domains: string[],
|
|
|
|
|
triggers: { event: string, action: string, connector?: string }[]
|
2024-07-30 22:03:04 +02:00
|
|
|
connector?: string
|
2024-07-30 00:55:36 +02:00
|
|
|
}) => {
|
2024-07-27 18:53:07 +02:00
|
|
|
const domainsURI = values.domains.map(d => '/api/domains/' + d)
|
2024-07-30 22:03:04 +02:00
|
|
|
postWatchlist({
|
|
|
|
|
domains: domainsURI,
|
|
|
|
|
triggers: values.triggers,
|
|
|
|
|
connector: values.connector !== undefined ? '/api/connectors/' + values.connector : undefined
|
|
|
|
|
}).then((w) => {
|
2024-07-27 18:53:07 +02:00
|
|
|
form.resetFields()
|
|
|
|
|
refreshWatchlists()
|
2024-07-28 15:36:22 +02:00
|
|
|
messageApi.success(t`Watchlist created !`)
|
2024-07-27 18:53:07 +02:00
|
|
|
}).catch((e: AxiosError) => {
|
|
|
|
|
const data = e?.response?.data as { detail: string }
|
2024-07-28 15:36:22 +02:00
|
|
|
messageApi.error(data.detail ?? t`An error occurred`)
|
2024-07-27 18:53:07 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const refreshWatchlists = () => getWatchlists().then(w => {
|
|
|
|
|
setWatchlists(w['hydra:member'])
|
2024-07-29 19:37:17 +02:00
|
|
|
}).catch((e: AxiosError) => {
|
|
|
|
|
const data = e?.response?.data as { detail: string }
|
|
|
|
|
messageApi.error(data.detail ?? t`An error occurred`)
|
|
|
|
|
setWatchlists(undefined)
|
|
|
|
|
})
|
2024-07-27 18:53:07 +02:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
refreshWatchlists()
|
2024-07-30 00:55:36 +02:00
|
|
|
getConnectors().then(c => setConnectors(c['hydra:member']))
|
2024-07-27 18:53:07 +02:00
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
return <Flex gap="middle" align="center" justify="center" vertical>
|
2024-07-29 19:37:17 +02:00
|
|
|
<Card title={t`Create a Watchlist`} style={{width: '100%'}}>
|
2024-07-27 18:53:07 +02:00
|
|
|
{contextHolder}
|
2024-07-30 00:55:36 +02:00
|
|
|
{
|
|
|
|
|
connectors &&
|
|
|
|
|
<WatchlistForm form={form} onCreateWatchlist={onCreateWatchlist} connectors={connectors}/>
|
|
|
|
|
}
|
2024-07-27 18:53:07 +02:00
|
|
|
</Card>
|
|
|
|
|
|
2024-07-27 20:44:10 +02:00
|
|
|
|
|
|
|
|
<Skeleton loading={watchlists === undefined} active>
|
2024-07-29 00:10:06 +02:00
|
|
|
{watchlists && watchlists.length > 0 && <Card title={t`My Watchlists`} style={{width: '100%'}}>
|
2024-07-29 18:54:28 +02:00
|
|
|
<WatchlistsList watchlists={watchlists} onDelete={refreshWatchlists}/>
|
2024-07-27 20:44:10 +02:00
|
|
|
</Card>
|
|
|
|
|
}
|
|
|
|
|
</Skeleton>
|
2024-07-27 18:53:07 +02:00
|
|
|
</Flex>
|
2024-07-27 17:19:25 +02:00
|
|
|
}
|