icon.tsx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright 2021-2024 Avaiga Private Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  5. * the License. You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  10. * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  11. * specific language governing permissions and limitations under the License.
  12. */
  13. import React, { useEffect, useMemo, useRef } from "react";
  14. import axios from "axios";
  15. import Avatar from "@mui/material/Avatar";
  16. import { SxProps, useTheme, Theme } from "@mui/material/styles";
  17. /**
  18. * An Icon representation.
  19. */
  20. export interface Icon {
  21. /** The URL to the image. */
  22. path: string;
  23. /** The text. */
  24. text: string;
  25. /** light theme path */
  26. lightPath?: string;
  27. /** dark theme path */
  28. darkPath?: string;
  29. }
  30. /**
  31. * A string or an icon.
  32. */
  33. export type stringIcon = string | Icon;
  34. interface IconProps {
  35. id?: string;
  36. img: Icon;
  37. className?: string;
  38. sx?: SxProps<Theme>;
  39. }
  40. export const avatarSx = { bgcolor: (theme: Theme) => theme.palette.text.primary };
  41. export const IconAvatar = ({ id, img, className, sx }: IconProps) => {
  42. const avtRef = useRef<HTMLDivElement>(null);
  43. const theme = useTheme();
  44. const path = useMemo(
  45. () => (theme.palette.mode === "dark" ? img.darkPath : img.lightPath) || img.path || "",
  46. [img.path, img.lightPath, img.darkPath, theme.palette.mode]
  47. );
  48. const [svg, svgContent, inlineSvg] = useMemo(() => {
  49. const p = path.trim();
  50. if (p.length > 3) {
  51. const svgFile = p.substring(p.length - 4).toLowerCase() === ".svg";
  52. const svgXml = p.substring(0, 4).toLowerCase() === "<svg";
  53. return [
  54. svgFile && path,
  55. svgXml && path,
  56. svgFile || svgXml
  57. ];
  58. }
  59. return [undefined, undefined, false];
  60. }, [path]);
  61. const avtSx = useMemo(() => (sx ? { ...avatarSx, ...sx } : avatarSx), [sx]);
  62. useEffect(() => {
  63. if (svg) {
  64. axios.get<string>(svg).then((response) => avtRef.current && (avtRef.current.innerHTML = response.data));
  65. } else if (svgContent && avtRef.current) {
  66. avtRef.current.innerHTML = svgContent;
  67. }
  68. }, [svg, svgContent]);
  69. return inlineSvg ? (
  70. <Avatar alt={img.text || id} className={className} ref={avtRef} sx={avtSx} />
  71. ) : (
  72. <Avatar alt={img.text || id} src={path} className={className} sx={avtSx} />
  73. );
  74. };