PagePathLabel.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import type { FC, ReactNode } from 'react';
  2. import { DevidedPagePath } from '@growi/core/dist/models';
  3. type TextElemProps = {
  4. children?: ReactNode;
  5. isHTML?: boolean;
  6. };
  7. const TextElement: FC<TextElemProps> = (props: TextElemProps) => (
  8. <>
  9. {props.isHTML ? (
  10. <span
  11. // biome-ignore lint/security/noDangerouslySetInnerHtml: ignore
  12. dangerouslySetInnerHTML={{ __html: props.children?.toString() || '' }}
  13. />
  14. ) : (
  15. <>{props.children}</>
  16. )}
  17. </>
  18. );
  19. type Props = {
  20. path: string;
  21. isLatterOnly?: boolean;
  22. isFormerOnly?: boolean;
  23. isPathIncludedHtml?: boolean;
  24. additionalClassNames?: string[];
  25. };
  26. export const PagePathLabel: FC<Props> = (props: Props) => {
  27. const {
  28. isLatterOnly,
  29. isFormerOnly,
  30. isPathIncludedHtml,
  31. additionalClassNames,
  32. path,
  33. } = props;
  34. const dPagePath = new DevidedPagePath(path, false, true);
  35. const classNames = additionalClassNames || [];
  36. let textElem: JSX.Element | undefined;
  37. if (isLatterOnly) {
  38. textElem = (
  39. <TextElement isHTML={isPathIncludedHtml}>{dPagePath.latter}</TextElement>
  40. );
  41. } else if (isFormerOnly) {
  42. textElem = dPagePath.isFormerRoot ? (
  43. <>/</>
  44. ) : (
  45. <TextElement isHTML={isPathIncludedHtml}>{dPagePath.former}</TextElement>
  46. );
  47. } else {
  48. textElem = dPagePath.isRoot ? (
  49. <strong>/</strong>
  50. ) : (
  51. <TextElement isHTML={isPathIncludedHtml}>
  52. {dPagePath.former}/<strong>{dPagePath.latter}</strong>
  53. </TextElement>
  54. );
  55. }
  56. return <span className={classNames.join(' ')}>{textElem}</span>;
  57. };