Skip to content

Commit

Permalink
fix icon + add utilitary for MUI icons
Browse files Browse the repository at this point in the history
  • Loading branch information
Florent Mariotti committed Jan 10, 2025
1 parent 3831733 commit d98c9c5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
5 changes: 4 additions & 1 deletion frontend/src/components/board-card/useResourceTypeDisplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import {
import TableChartIcon from "@mui/icons-material/TableChart";

import { RESOURCE_TYPE } from "~/core/enums/resource-type.enum";
import { getMuiIconPath } from "~/hooks/useMUIIconPath";

export const useResourceTypeDisplay = (resourceType: string) => {
const tableChartPath = getMuiIconPath(TableChartIcon);

return useMemo(() => {
let icon: string = mdiFileMultiple;
let type: string = "Fichier";
Expand Down Expand Up @@ -44,7 +47,7 @@ export const useResourceTypeDisplay = (resourceType: string) => {
break;
}
case RESOURCE_TYPE.BOARD: {
icon = mdiFileMultiple; //TODO : change to MUI TableIcon
icon = tableChartPath;
type = "Tableau";
break;
}
Expand Down
37 changes: 37 additions & 0 deletions frontend/src/hooks/useMUIIconPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { createElement } from "react";

import { SvgIconTypeMap } from "@mui/material";
import { OverridableComponent } from "@mui/material/OverridableComponent";
import { renderToString } from "react-dom/server";

type MuiIconComponent = OverridableComponent<SvgIconTypeMap<{}, "svg">> & {
muiName: string;
};

export const getMuiIconPath = (IconComponent: MuiIconComponent): string => {
try {
// Render the icon component to string
const svgString = renderToString(createElement(IconComponent, {}));

// Create a temporary DOM element to parse the SVG
const div = document.createElement("div");
div.innerHTML = svgString;

// Get the path element
const pathElement = div.querySelector("path");
if (!pathElement) {
throw new Error("No path element found in SVG");
}

// Get the d attribute which contains the path data
const pathData = pathElement.getAttribute("d");
if (!pathData) {
throw new Error("No path data found");
}

return pathData;
} catch (error) {
console.error("Error extracting path from MUI icon:", error);
return "";
}
};

0 comments on commit d98c9c5

Please sign in to comment.