📦 EqualifyEverything / equalify

📄 AuditCrawlInput.tsx · 142 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142import { useState } from "react";
import { useGlobalStore } from "../utils";
import style from "./AuditCrawlInput.module.scss";
import { Card } from "./Card";
import { StyledLabeledInput } from "./StyledLabeledInput";
import { StyledButton } from "./StyledButton";
import { AuditPagesInputTable } from "./AuditPagesInputTable";

import * as API from "aws-amplify/api";
import { MdCheckCircle, MdError } from "react-icons/md";
import { TbAlertTriangle } from "react-icons/tb";
import { LuSearch } from "react-icons/lu";

const URL_SOFT_LIMIT = 10_000;

interface Page {
  url: string;
  type: "html" | "pdf";
  id?: string;
}
interface ChildProps {
  pages: Page[];
  setParentPages: (newValue: Page[]) => void;
}

export const AuditCrawlInput: React.FC<ChildProps> = ({
  pages,
  setParentPages,
}) => {
  const { setAnnounceMessage } = useGlobalStore();
  const [crawlUrl, setCrawlUrl] = useState("");
  const [isCrawling, setIsCrawling] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const [crawlMethod, setCrawlMethod] = useState<string | null>(null);

  const handleCrawl = async (e: React.MouseEvent<HTMLButtonElement>) => {
    e.preventDefault();
    if (!crawlUrl.trim()) return;

    setIsCrawling(true);
    setError(null);
    setCrawlMethod(null);
    setParentPages([]);

    try {
      const response = await API.post({
        apiName: "auth",
        path: "/crawlUrl",
        options: { body: { url: crawlUrl.trim() } },
      }).response;

      const result = (await response.body.json()) as any;

      if (result.error) {
        setError(result.error);
        return;
      }

      setCrawlMethod(result.method);
      const discoveredPages: Page[] = (result.urls || []).map((url: string) => ({
        url,
        type: url.toLowerCase().endsWith(".pdf") ? "pdf" as const : "html" as const,
      }));

      setParentPages(discoveredPages);
      setAnnounceMessage(
        `Found ${discoveredPages.length} URL(s) via ${result.method}!`,
        "success"
      );
    } catch (err) {
      setError("Failed to crawl site. Please check the URL and try again.");
    } finally {
      setIsCrawling(false);
    }
  };

  return (
    <div className={style.AuditCrawlInput}>
      <Card variant="inset-light">
        <h3>Crawl a Website</h3>
        <p className="font-small">
          Enter a website URL to automatically discover pages via its sitemap.
        </p>
        <StyledLabeledInput>
          <label htmlFor="crawl-url">Website URL</label>
          <input
            id="crawl-url"
            type="url"
            placeholder="example.com"
            value={crawlUrl}
            onChange={(e) => setCrawlUrl(e.target.value)}
          />
        </StyledLabeledInput>
        <StyledButton
          icon={<LuSearch />}
          label={isCrawling ? "Crawling..." : "Crawl Site"}
          onClick={handleCrawl}
          disabled={!crawlUrl.trim() || isCrawling}
          loading={isCrawling}
        />
        {error && (
          <Card variant="short-error">
            <MdError className="icon-small" />
            <div className="font-small">
              <b>Crawl failed.</b> {error}
            </div>
          </Card>
        )}
        {crawlMethod && pages.length > 0 && (
          <Card variant="short-success">
            <MdCheckCircle className="icon-small" />
            <div className="font-small">
              <b>
                Found {pages.length} URL(s)
              </b>{" "}
              via {crawlMethod}.
            </div>
          </Card>
        )}
        {pages.length >= URL_SOFT_LIMIT && (
          <Card variant="short-error">
            <TbAlertTriangle className="icon-small" />
            <div className="font-small">
              <b>Large audit:</b> This site has{" "}
              {pages.length.toLocaleString()} URLs. Large audits take
              significantly longer to scan.
            </div>
          </Card>
        )}
        {pages.length > 0 && (
          <AuditPagesInputTable
            pages={pages}
            removePages={() => {}}
            isShared={true}
            updatePageType={() => {}}
          />
        )}
      </Card>
    </div>
  );
};