📦 EqualifyEverything / equalify

📄 Audits.tsx · 193 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193import { useQuery } from "@tanstack/react-query";
import { formatId, useGlobalStore } from "../utils";
import * as API from "aws-amplify/api";
import { Link, useNavigate } from "react-router-dom";
import { StyledButton } from "#src/components/StyledButton.tsx";
import { LuClipboard, LuClipboardPlus } from "react-icons/lu";
import { Card } from "#src/components/Card.tsx";
const apiClient = API.generateClient();
import styles from "./Audits.module.scss";
import { DataRow } from "#src/components/DataRow.tsx";
import { SkeletonAuditGrid } from "#src/components/Skeleton.tsx";
import { AuditsTable } from "#src/components/AuditsTable.tsx";
import * as Tabs from "@radix-ui/react-tabs";
import { FaTable, FaTableList } from "react-icons/fa6";

export interface Scan {
  blockers_aggregate: {
    aggregate: {
      count: number;
    };
  };
  percentage: number;
  updated_at: string;
}
/* 
export interface Audit {
  created_at: string;
  id: string;
  interval: string;
  name: string;
  scans: Scan[];
  urls_aggregate: {
    aggregate: {
      count: number;
    };
  };
} */

export const Audits = () => {
  const navigate = useNavigate();
  const {auditsTableView, setAuditsTableView} = useGlobalStore();
  const { data: audits, isLoading } = useQuery({
    queryKey: ["audits"],
    queryFn: async () =>
      (
        await apiClient.graphql({
          //query: `{audits(order_by: {created_at: desc}) {id created_at name}}`,
          query: `
            {
          audits(order_by: {created_at: desc}) {
            id
            created_at
            name
            interval
            scans(order_by: {created_at: desc}, limit: 1) {
            blockers_aggregate(order_by: {created_at: desc}, where: {}) {
                aggregate {
                count
                }
            }
            percentage
            updated_at
            }
            urls_aggregate {
            aggregate {
                count
            }
            }
        }
        }
            `,
        })
      )?.data?.audits,
  });
  //console.log(audits);
  return (
    <div className={styles.Audits}>
      <div>
        <div className={styles["audits-header"]}>
          <h1 className="initial-focus-element">Audits</h1>

          <StyledButton
            onClick={() => navigate("/audits/build")}
            label="Add Audit"
            variant="dark"
            icon={<LuClipboardPlus className="icon-small" />}
          />
        </div>
      </div>
      <>
        <Tabs.Root
          orientation="horizontal"
          className="audit-tabs"
          value={auditsTableView}
          onValueChange={(value)=>setAuditsTableView(value)}
          activationMode="manual"
        >
          <Tabs.List aria-label="Select a View" className={styles["audits-view-selector"]}>
            <Tabs.Trigger value="cards" className={styles["audits-view-trigger"]} asChild>
              <StyledButton
                icon={<FaTable />}
                onClick={() => {}}
                label={"Cards View"}
                showLabel={false}
              />
            </Tabs.Trigger>
            <Tabs.Trigger value="table" className={styles["audits-view-trigger"]} asChild>
              <StyledButton
                icon={<FaTableList />}
                onClick={() => {}}
                label={"Cards View"}
                showLabel={false}
              />
            </Tabs.Trigger>
          </Tabs.List>
          <Tabs.Content value="cards">
            <div className="cards-33">
              {isLoading ? (
                <SkeletonAuditGrid count={6} />
              ) : (
                audits?.map((row: any, index: number) => (
                  <Card variant="light" key={index}>
                    <Link
                      to={`/audits/${formatId(row.id)}`}
                    >
                      <h2>
                        <LuClipboard className="icon-small" />
                        {row.name}
                      </h2>
                    </Link>
                    <div className={styles["dataRow-list"]}>
                      <DataRow
                        variant="highlight"
                        the_key="Blockers"
                        the_value={
                          row.scans[0]?.blockers_aggregate?.aggregate?.count ??
                          "—"
                        }
                      />
                      <DataRow
                        the_key="URLs"
                        the_value={row.urls_aggregate.aggregate.count}
                      />
                      <DataRow the_key="Runs" the_value={row.interval} />
                      <DataRow
                        the_key="Last Scan"
                        the_value={
                          row.scans[0]
                            ? prettyDate(row.scans[0].updated_at) +
                              " at " +
                              prettyTime(row.scans[0].updated_at)
                            : "Not scanned yet"
                        }
                      />
                      <DataRow
                        variant="no-border"
                        the_key="Created"
                        the_value={prettyDate(row.created_at)}
                      />

                      {/* {row.scans[0].percentage}% */}
                    </div>
                  </Card>
                ))
              )}
            </div>
          </Tabs.Content>
          <Tabs.Content value="table">
            <AuditsTable audits={audits} isLoading={isLoading} />
          </Tabs.Content>
        </Tabs.Root>
      </>
    </div>
  );
};

function prettyDate(dateTime: string) {
  return new Date(dateTime).toLocaleDateString("en-US", {
    weekday: "short",
    year: "numeric",
    month: "short",
    day: "numeric",
  });
}

function prettyTime(dateTime: string) {
  const time = new Date(dateTime);
  return time.toLocaleTimeString(navigator.language, {
    hour: "2-digit",
    minute: "2-digit",
  });
}