App directory refactoring
This commit is contained in:
parent
a53a26f4c0
commit
b312a8c862
153 changed files with 1532 additions and 1447 deletions
126
apps/leafcutter/app/_components/QueryText.tsx
Normal file
126
apps/leafcutter/app/_components/QueryText.tsx
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
"use client";
|
||||
|
||||
import { FC, useState, useEffect } from "react";
|
||||
import { Box, Grid } from "@mui/material";
|
||||
import { useTranslate } from "react-polyglot";
|
||||
import taxonomy from "app/_config/taxonomy.json";
|
||||
import { colors } from "styles/theme";
|
||||
import { useAppContext } from "./AppProvider";
|
||||
|
||||
export const QueryText: FC = () => {
|
||||
const t = useTranslate();
|
||||
const {
|
||||
typography: { h6 },
|
||||
query: q,
|
||||
} = useAppContext();
|
||||
|
||||
const displayNames: any = {
|
||||
incidentType: t("incidentType"),
|
||||
startDate: t("startDate"),
|
||||
endDate: t("endDate"),
|
||||
relativeDate: t("relativeDate"),
|
||||
targetedGroup: t("targetedGroup"),
|
||||
platform: t("platform"),
|
||||
device: t("device"),
|
||||
service: t("service"),
|
||||
maker: t("maker"),
|
||||
country: t("country"),
|
||||
subregion: t("subregion"),
|
||||
continent: t("continent"),
|
||||
};
|
||||
|
||||
const createClause = (query: any, key: string) => {
|
||||
const { values, queryType } = query[key];
|
||||
const color =
|
||||
queryType === "include"
|
||||
? colors.leafcutterElectricBlue
|
||||
: colors.warningPink;
|
||||
|
||||
if (values.length > 0) {
|
||||
return `where <span style="color: ${color};"><strong>${
|
||||
displayNames[key]
|
||||
}</strong> ${
|
||||
queryType === "include" ? ` ${t("is")} ` : ` ${t("isNot")} `
|
||||
} ${values
|
||||
.map(
|
||||
// @ts-expect-error
|
||||
(value: string) => `<em>${taxonomy[key]?.[value]?.display ?? ""}</em>`
|
||||
)
|
||||
.join(` ${t("or")} `)}</span>`;
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
const createDateClause = (query: any, key: string) => {
|
||||
const { values } = query[key];
|
||||
const color = colors.leafcutterElectricBlue;
|
||||
if (values.length > 0) {
|
||||
const range = key === "startDate" ? t("onOrAfter") : t("onOrBefore");
|
||||
return `${t("where")} <span style="color: ${color};"><strong>${
|
||||
displayNames[key]
|
||||
}</strong> is ${range} <em>${values[0]?.toLocaleDateString()}</em></span>`;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
const createRelativeDateClause = (query: any, key: string) => {
|
||||
const { values } = query[key];
|
||||
const color = colors.leafcutterElectricBlue;
|
||||
|
||||
if (query[key].values.length > 0) {
|
||||
const range = t("onOrAfter");
|
||||
return `${t("where")} <span style="color: ${color};"><strong>${
|
||||
displayNames[key]
|
||||
}</strong> is ${range} <em>${values[0]} days ago</em></span>`;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
const [queryText, setQueryText] = useState(t("findAllIncidents"));
|
||||
useEffect(() => {
|
||||
const generateQueryText = (query: any) => {
|
||||
const incidentClause = createClause(query, "incidentType");
|
||||
const startDateClause = createDateClause(query, "startDate");
|
||||
const endDateClause = createDateClause(query, "endDate");
|
||||
const relativeDateClause = createRelativeDateClause(
|
||||
query,
|
||||
"relativeDate"
|
||||
);
|
||||
const targetedGroupClause = createClause(query, "targetedGroup");
|
||||
const platformClause = createClause(query, "platform");
|
||||
const deviceClause = createClause(query, "device");
|
||||
const serviceClause = createClause(query, "service");
|
||||
const makerClause = createClause(query, "maker");
|
||||
const countryClause = createClause(query, "country");
|
||||
const subregionClause = createClause(query, "subregion");
|
||||
const continentClause = createClause(query, "continent");
|
||||
const joinedClauses = [
|
||||
incidentClause,
|
||||
startDateClause,
|
||||
endDateClause,
|
||||
relativeDateClause,
|
||||
targetedGroupClause,
|
||||
platformClause,
|
||||
deviceClause,
|
||||
serviceClause,
|
||||
makerClause,
|
||||
countryClause,
|
||||
subregionClause,
|
||||
continentClause,
|
||||
]
|
||||
.filter((clause) => clause !== null)
|
||||
.join(" and ");
|
||||
|
||||
return `${t("findAllIncidents")} ${joinedClauses}`;
|
||||
};
|
||||
const text = generateQueryText(q);
|
||||
setQueryText(text);
|
||||
}, [q]);
|
||||
|
||||
return (
|
||||
<Grid container direction="column">
|
||||
<Grid item>
|
||||
<Box sx={h6} dangerouslySetInnerHTML={{ __html: queryText }} />
|
||||
</Grid>
|
||||
</Grid>
|
||||
);
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue