Skip to content

Commit

Permalink
Merge pull request #91 from KelvinTegelaar/dev
Browse files Browse the repository at this point in the history
[pull] dev from KelvinTegelaar:dev
  • Loading branch information
pull[bot] authored Feb 11, 2025
2 parents 8dbef6f + 384d892 commit 0cc99c2
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 23 deletions.
87 changes: 75 additions & 12 deletions src/components/CippComponents/CippApiResults.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import { Close } from "@mui/icons-material";
import { Alert, CircularProgress, Collapse, IconButton, Typography } from "@mui/material";
import { Close, Download, RouterOutlined } from "@mui/icons-material";
import {
Alert,
CircularProgress,
Collapse,
IconButton,
Stack,
Typography,
Box,
SvgIcon,
Tooltip,
} from "@mui/material";
import { useEffect, useState, useMemo, useCallback } from "react";
import { getCippError } from "../../utils/get-cipp-error";
import { CippCopyToClipBoard } from "./CippCopyToClipboard";
import { Grid } from "@mui/system";
import React from "react";
import { CippTableDialog } from "./CippTableDialog";
import { EyeIcon } from "@heroicons/react/24/outline";
import { useDialog } from "../../hooks/use-dialog";
import { useRouter } from "next/router";

const extractAllResults = (data) => {
const results = [];
Expand Down Expand Up @@ -106,6 +120,9 @@ export const CippApiResults = (props) => {
const [errorVisible, setErrorVisible] = useState(false);
const [fetchingVisible, setFetchingVisible] = useState(false);
const [finalResults, setFinalResults] = useState([]);
const tableDialog = useDialog();
const router = useRouter();
const pageTitle = `${document.title} - Results`;
const correctResultObj = useMemo(() => {
if (!apiObject.isSuccess) return;

Expand Down Expand Up @@ -174,12 +191,33 @@ export const CippApiResults = (props) => {
setFinalResults((prev) => prev.map((r) => (r.id === id ? { ...r, visible: false } : r)));
}, []);

const handleDownloadCsv = useCallback(() => {
if (!finalResults?.length) return;

const baseName = document.title.toLowerCase().replace(/[^a-z0-9]/g, "-");
const fileName = `${baseName}-results.csv`;

const headers = Object.keys(finalResults[0]);
const rows = finalResults.map((item) =>
headers.map((header) => `"${item[header] || ""}"`).join(",")
);
const csvContent = [headers.join(","), ...rows].join("\n");
const blob = new Blob([csvContent], { type: "text/csv;charset=utf-8;" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.setAttribute("href", url);
link.setAttribute("download", fileName);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}, [finalResults, apiObject]);

const hasVisibleResults = finalResults.some((r) => r.visible);
return (
<>
<Stack spacing={2}>
{/* Loading alert */}
{!errorsOnly && (
<Collapse in={fetchingVisible}>
<Collapse in={fetchingVisible} unmountOnExit>
<Alert
sx={alertSx}
action={
Expand All @@ -203,7 +241,7 @@ export const CippApiResults = (props) => {
)}

{/* Error alert */}
<Collapse in={errorVisible}>
<Collapse in={errorVisible} unmountOnExit>
{apiObject.isError && (
<Alert
sx={alertSx}
Expand All @@ -227,10 +265,10 @@ export const CippApiResults = (props) => {

{/* Individual result alerts */}
{apiObject.isSuccess && !errorsOnly && hasVisibleResults && (
<Grid container spacing={2}>
<>
{finalResults.map((resultObj) => (
<Grid item size={12} key={resultObj.id}>
<Collapse in={resultObj.visible}>
<React.Fragment key={resultObj.id}>
<Collapse in={resultObj.visible} unmountOnExit>
<Alert
sx={alertSx}
variant="filled"
Expand All @@ -252,10 +290,35 @@ export const CippApiResults = (props) => {
{resultObj.text}
</Alert>
</Collapse>
</Grid>
</React.Fragment>
))}
</Grid>
</>
)}
{apiObject.isSuccess || apiObject.isError ? (
<Box display="flex" flexDirection="row">
<Tooltip title="View Results">
<IconButton onClick={() => tableDialog.handleOpen()}>
<SvgIcon>
<EyeIcon />
</SvgIcon>
</IconButton>
</Tooltip>
<Tooltip title="Download Results">
<IconButton aria-label="download-csv" onClick={handleDownloadCsv}>
<Download />
</IconButton>
</Tooltip>
</Box>
) : null}
{tableDialog.open && (
<CippTableDialog
createDialog={tableDialog}
title={pageTitle}
data={finalResults}
noCard={true}
simpleColumns={["severity", "text", "copyField"]}
/>
)}
</>
</Stack>
);
};
3 changes: 3 additions & 0 deletions src/components/CippFormPages/CippJSONView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ function CippJsonView({
};

useEffect(() => {
if (!type && (object?.omaSettings || object?.settings || object?.added)) {
type = "intune";
}
const blacklist = [
"selectedOption",
"GUID",
Expand Down
2 changes: 1 addition & 1 deletion src/pages/endpoint/MEM/list-compliance-policies/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const Page = () => {
url: "/api/AddIntuneTemplate",
data: {
ID: "id",
URLName: "URLName",
ODataType: "@odata.type",
},
confirmText: "Are you sure you want to create a template based on this policy?",
icon: <Book />,
Expand Down
2 changes: 1 addition & 1 deletion src/pages/endpoint/MEM/list-policies/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const Page = () => {
url: "/api/AddIntuneTemplate",
data: {
ID: "id",
URLName: "URLName",
ODataType: "@odata.type",
},
confirmText: "Are you sure you want to create a template based on this policy?",
icon: <Book />,
Expand Down
23 changes: 14 additions & 9 deletions src/pages/tools/community-repos/repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,20 @@ const Page = () => {
const [selectedBranch, setSelectedBranch] = useState(branch);
const [selectedRepo, setSelectedRepo] = useState(name);

const searchMutation = ApiPostCall({
onResult: (resp) => {
if (resp?.Results === null || resp?.Results?.[0] === null) return;
setFileResults(resp?.Results || []);
},
});

const fileQuery = ApiPostCall({
onResult: (resp) => {
setJsonContent(JSON.parse(resp?.Results?.content || "{}"));
let content = resp?.Results?.content?.trim() || "{}";
// remove non-printable characters from the beginning and end
content = content.replace(
/^[\u0000-\u001F\u007F-\u009F]+|[\u0000-\u001F\u007F-\u009F]+$/g,
""
);
try {
setJsonContent(JSON.parse(content));
} catch (e) {
console.error("Invalid JSON content:", e);
setJsonContent({});
}
},
});

Expand Down Expand Up @@ -171,7 +175,8 @@ const Page = () => {
? { label: selectedBranch, value: selectedBranch }
: { label: "Loading branches", value: "" }
}
onChange={(event, newValue) => {
onChange={(newValue) => {
if (newValue.value === selectedBranch) return;
setSelectedBranch(newValue.value);
updateQueryParams("branch", newValue.value);
}}
Expand Down

0 comments on commit 0cc99c2

Please sign in to comment.