domain-watchdog/assets/pages/search/DomainSearchPage.tsx

102 lines
3.8 KiB
TypeScript
Raw Permalink Normal View History

import React, {useContext, useEffect, useState} from 'react'
import type {FormProps} from 'antd'
2025-12-10 11:03:24 +01:00
import { Empty, Flex, FloatButton, message, Skeleton} from 'antd'
2025-11-02 02:48:43 +01:00
import type {Domain, Watchlist} from '../../utils/api'
import {addDomainToWatchlist, getDomain} from '../../utils/api'
2024-12-31 13:55:42 +01:00
import type {AxiosError} from 'axios'
2024-07-28 15:36:22 +02:00
import {t} from 'ttag'
import type {FieldType} from '../../components/search/DomainSearchBar'
2024-12-31 13:55:42 +01:00
import {DomainSearchBar} from '../../components/search/DomainSearchBar'
2024-12-30 23:50:15 +01:00
import {DomainResult} from '../../components/search/DomainResult'
import {showErrorAPI} from '../../utils/functions/showErrorAPI'
import {useNavigate, useParams} from 'react-router-dom'
2025-11-01 23:21:54 +01:00
import {PlusOutlined} from '@ant-design/icons'
import WatchlistSelectionModal from '../../components/tracking/watchlist/WatchlistSelectionModal'
import {AuthenticatedContext} from "../../contexts"
2024-07-27 14:40:08 +02:00
export default function DomainSearchPage() {
const {query} = useParams()
const [domain, setDomain] = useState<Domain | null>()
const domainLdhName = domain?.ldhName
2025-11-01 22:59:49 +01:00
const [loading, setLoading] = useState(false)
const [addToWatchlistModal, setAddToWatchlistModal] = useState(false)
const {isAuthenticated} = useContext(AuthenticatedContext)
2024-07-27 01:35:00 +02:00
const [messageApi, contextHolder] = message.useMessage()
const navigate = useNavigate()
2024-07-27 01:35:00 +02:00
const onFinish: FormProps<FieldType>['onFinish'] = (values) => {
navigate('/search/domain/' + values.ldhName)
if (loading) return
setLoading(true)
setDomain(null)
2025-01-01 14:10:23 +01:00
getDomain(values.ldhName, values.isRefreshForced).then(d => {
2024-07-27 16:45:20 +02:00
setDomain(d)
2024-07-28 15:36:22 +02:00
messageApi.success(t`Found !`)
2024-07-27 16:45:20 +02:00
}).catch((e: AxiosError) => {
setDomain(undefined)
2024-08-07 15:57:16 +02:00
showErrorAPI(e, messageApi)
}).finally(() => setLoading(false))
}
useEffect(() => {
if (query === undefined) return
2025-01-01 14:10:23 +01:00
onFinish({ldhName: query, isRefreshForced: false})
}, [])
2024-12-27 21:37:19 +01:00
2025-11-01 22:59:49 +01:00
const addToWatchlist = async (watchlist: Watchlist) => {
await addDomainToWatchlist(watchlist, domain!.ldhName).then(() => {
setAddToWatchlistModal(false)
const ldhName = domain?.ldhName
messageApi.success(t`${ldhName} added to ${watchlist.name}`)
2025-11-01 22:59:49 +01:00
}).catch((e: AxiosError) => {
showErrorAPI(e, messageApi)
})
}
return <>
2024-12-30 23:50:15 +01:00
<Flex gap='middle' align='center' justify='center' vertical>
{contextHolder}
<DomainSearchBar initialValue={query} onFinish={onFinish}/>
2024-07-27 14:40:08 +02:00
2024-12-30 23:50:15 +01:00
<Skeleton loading={domain === null} active>
{
(domain != null) &&
(!domain.deleted
? <DomainResult domain={domain}/>
: <Empty
description={t`Although the domain exists in my database, it has been deleted from the WHOIS by its registrar.`}
/>)
}
</Skeleton>
</Flex>
{domain && isAuthenticated
2025-11-01 23:16:49 +01:00
&& <FloatButton
2025-11-01 22:59:49 +01:00
style={{
position: 'fixed',
insetInlineEnd: (100 - 40) / 2,
bottom: 100 - 40 / 2
}}
2025-11-02 13:44:28 +01:00
tooltip={t`Add to Watchlist`}
2025-11-01 23:16:49 +01:00
type="primary"
icon={<PlusOutlined/>}
onClick={() => setAddToWatchlistModal(true)}
/>
2025-11-01 22:59:49 +01:00
}
<WatchlistSelectionModal
open={addToWatchlistModal}
onFinish={addToWatchlist}
modalProps={{
2025-11-02 13:44:28 +01:00
title: t`Add ${domainLdhName} to a Watchlist`,
2025-11-01 22:59:49 +01:00
onCancel: () => setAddToWatchlistModal(false),
onClose: () => setAddToWatchlistModal(false),
2025-11-02 13:44:28 +01:00
cancelText: t`Cancel`,
okText: t`Add`
}}
2025-11-01 22:59:49 +01:00
/>
</>
2024-12-30 23:50:15 +01:00
}