2024-07-27 18:53:07 +02:00
|
|
|
import React, {useEffect, useState} from "react";
|
2024-07-31 12:37:43 +02:00
|
|
|
import {Card, Divider, Flex, Form, message} from "antd";
|
2024-07-29 18:54:28 +02:00
|
|
|
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-08-02 17:23:27 +02:00
|
|
|
export type Watchlist = {
|
2024-08-03 19:16:58 +02:00
|
|
|
name?: string
|
2024-08-02 17:23:27 +02:00
|
|
|
token: string,
|
|
|
|
|
domains: { ldhName: string }[],
|
|
|
|
|
triggers?: { event: EventAction, action: string }[],
|
2024-08-04 17:16:00 +02:00
|
|
|
connector?: {
|
|
|
|
|
id: string
|
|
|
|
|
provider: string
|
|
|
|
|
createdAt: string
|
|
|
|
|
}
|
|
|
|
|
createdAt: string
|
2024-08-02 17:23:27 +02:00
|
|
|
}
|
2024-07-27 20:44:10 +02:00
|
|
|
|
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: {
|
2024-08-03 19:16:58 +02:00
|
|
|
name?: string
|
2024-07-30 00:55:36 +02:00
|
|
|
domains: string[],
|
2024-08-03 18:56:37 +02:00
|
|
|
emailTriggers: 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({
|
2024-08-03 19:16:58 +02:00
|
|
|
name: values.name,
|
2024-07-30 22:03:04 +02:00
|
|
|
domains: domainsURI,
|
2024-08-03 18:56:37 +02:00
|
|
|
triggers: values.emailTriggers.map(t => ({event: t, action: 'email'})),
|
2024-07-30 22:03:04 +02:00
|
|
|
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-31 02:55:20 +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 }
|
2024-07-31 02:55:20 +02:00
|
|
|
messageApi.error(data?.detail ?? t`An error occurred`)
|
2024-07-29 19:37:17 +02:00
|
|
|
setWatchlists(undefined)
|
|
|
|
|
})
|
2024-07-27 18:53:07 +02:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
refreshWatchlists()
|
2024-07-31 02:55:20 +02:00
|
|
|
getConnectors()
|
|
|
|
|
.then(c => setConnectors(c['hydra:member']))
|
|
|
|
|
.catch((e: AxiosError) => {
|
|
|
|
|
const data = e?.response?.data as { detail: string }
|
|
|
|
|
messageApi.error(data?.detail ?? t`An error occurred`)
|
|
|
|
|
})
|
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-31 12:37:43 +02:00
|
|
|
<Divider/>
|
2024-07-27 20:44:10 +02:00
|
|
|
|
2024-08-02 17:23:27 +02:00
|
|
|
{watchlists && watchlists.length > 0 &&
|
|
|
|
|
<WatchlistsList watchlists={watchlists} onDelete={refreshWatchlists}/>}
|
2024-07-27 18:53:07 +02:00
|
|
|
</Flex>
|
2024-07-27 17:19:25 +02:00
|
|
|
}
|