'use client' import React, { useState, useCallback, memo, Fragment } from 'react' import { Button, InputActions } from '@app/components/general' import { domainList as dmList } from '@app/utils' import { GrAdd, GrChapterAdd, GrClose } from 'react-icons/gr' import { AppManager } from '@app/managers' import { InputHeaders } from './forms/input-headers' import { useInputActions, useInputHeader } from './hooks' import { useWebsiteContext } from '../providers/website' import { AccessibilityStandardKeys, Standard, WCAGSelectInput, } from './select/select-input' import { FormControl } from './form-control' import { HeadlessModal } from '../modal/headless' import { TextField } from './text-field' import { useAuthContext } from '../providers/auth' import { Checkbox } from './check-box' import { RunnerSelect } from './runner-select' import { SUPER_MODE } from '@app/configs' const domainList = [...dmList, 'none'] interface FormDialogProps { buttonTitle?: string | JSX.Element okPress?: (a: any) => void buttonStyles?: string icon?: boolean | 'add' // show btn with icon iconButton?: boolean } interface InputHead { key: string value: string } const checkBoxContainerStyles = 'flex place-items-center gap-x-1 min-w-[80px] md:min-w-20 md:gap-x-2' // validate the headers inputs and send function validateHeaders(object: InputHead[]) { const headers = [] for (const p of object) { const { key, value } = p if (key && value) { headers.push(p) } } return headers } export function FormDialogWrapper({ buttonTitle = 'Subscribe', okPress, buttonStyles = '', icon, iconButton, }: FormDialogProps) { const { account } = useAuthContext() const activeSubscription = account?.activeSubscription || SUPER_MODE // custom state const [open, setOpen] = useState(false) const [websitUrl, setUrl] = useState('') const [https, setTransportType] = useState(true) const [pageInsights, setPageInsights] = useState(false) const [mobileViewport, setMobile] = useState(false) const [subdomains, setSubdomains] = useState(false) const [sitemap, setSitemap] = useState(!!activeSubscription) const [tld, setTld] = useState(false) const [ua, setUserAgent] = useState('') const [proxy, setProxy] = useState('') const [standard, setWCAGStandard] = useState( Standard[Standard.WCAG2AA] as AccessibilityStandardKeys ) const [robots, setRobots] = useState(true) const [runners, setRunners] = useState([]) const [monitoringEnabled, setMonitoring] = useState(true) const headers = useInputHeader() const actions = useInputActions() const { addWebsite } = useWebsiteContext() const handleClickOpen = useCallback(() => { setOpen(true) }, [setOpen]) const onChangeText = useCallback( (event: React.ChangeEvent) => { setUrl(event.target.value) }, [setUrl] ) const onStandardChange = useCallback( (event: React.ChangeEvent) => { setWCAGStandard(event.target.value) }, [setWCAGStandard] ) const onRunnerEvent = useCallback( (event: string[]) => { setRunners(event) }, [setRunners] ) const handleClose = useCallback(() => { setOpen(false) setUrl('') }, [setOpen, setUrl]) // fields const customActions = actions.customActions const customActionFields = actions.customFields // headers const customHeader = headers.customHeader const customFields = headers.customFields const submit = useCallback( async (event: any) => { event?.preventDefault() if (!websitUrl) { // prevent empty return AppManager.toggleSnack( true, 'Please enter a valid website url.', 'error' ) } let cleanUrl = String(websitUrl) .replace(https ? 'https' : 'http', https ? 'http' : 'https') .replace(/^(?:https?:\/\/)?/i, '') .split('/')[0] if (cleanUrl[cleanUrl.length - 1] === '/') { cleanUrl = cleanUrl.slice(0, -1) } let tpt = 'https' if (websitUrl.includes('http://') || !https) { tpt = 'http' } let urlBase = cleanUrl.includes('://') ? '' : `://` let blockExt if (cleanUrl.includes('localhost:')) { blockExt = true } // determine whether to add an extension or not const ex = blockExt || cleanUrl.includes('.') || domainList.some((element: any) => cleanUrl.includes(element)) ? '' : '.com' const websiteUrl = `${tpt}${urlBase}${cleanUrl}${ex}`.trim() const websiteCustomHeaders = customHeader ? validateHeaders(customFields) : null // make all paths start with slash const websiteActions = customActions ? customActionFields.map((items) => { const pathName = items.path && items.path[0] === '/' ? items.path : `/${items.path}` return { ...items, path: pathName, } }) : null const params = { url: websiteUrl.toLowerCase(), customHeaders: websiteCustomHeaders, pageInsights, mobile: mobileViewport, ua, standard, actions: websiteActions, robots, subdomains, tld, runners, proxy, sitemap, monitoringEnabled, } // CLOSE pre-optimistic prevent dialog unmount state error handleClose() if (okPress && typeof okPress === 'function') { try { await okPress(params) } catch (e) { console.error(e) } } else { AppManager.toggleSnack( true, 'Checking all pages for issues, please wait...', 'success', true ) await addWebsite({ variables: params, }) } }, [ handleClose, addWebsite, okPress, websitUrl, https, pageInsights, mobileViewport, standard, ua, customFields, customHeader, customActionFields, customActions, robots, subdomains, tld, runners, proxy, sitemap, monitoringEnabled, ] ) const onChangeUA = (event: React.ChangeEvent) => { setUserAgent(event.target.value) } const onChangeProxy = (event: React.ChangeEvent) => { setProxy(event.target.value) } // toggle actions form const onChangeActionsEvent = () => { if (activeSubscription) { actions.setCustomActions((v: boolean) => !v) } else { AppManager.toggleSnack( true, `Custom actions requires paid plan, upgrade your account to get access.`, 'error', false, true ) } } // toggle headers form const onChangeHeadersEvent = () => { headers.setCustomHeader((v: boolean) => !v) } const onChangeRobotsEvent = () => { setRobots((a: boolean) => !a) } const onChangeSubdomainsEvent = () => { setSubdomains((a: boolean) => !a) } const onChangeTldEvent = () => { setTld((a: boolean) => !a) } const onChangeSitemapEvent = () => { setSitemap((a: boolean) => !a) } const onChangeMonitoringEvent = () => { setMonitoring((a: boolean) => !a) } return (

Subscribe

To add a website to your watchlist, enter the url below.

Enter Website Url
{ setTransportType(!https) }} id={'https'} /> HTTPS
{ setPageInsights(!pageInsights) }} id={'lighthouse'} /> Lighthouse
{ setMobile(!mobileViewport) }} id={'mobile'} /> Mobile
Robots
Subdomains
TLDs
Sitemap
Actions
Headers
Monitor
Enter User Agent Enter Proxy
{customHeader ? : null} {customActions ? : null}
) } export const FormDialog = memo(FormDialogWrapper)