From 62f06c40a4eac6d11f1a93f3b49dfe6c48ce16f8 Mon Sep 17 00:00:00 2001
From: Armand Philippot <git@armandphilippot.com>
Date: Mon, 11 Apr 2022 19:24:04 +0200
Subject: chore: add a Meta component

---
 src/components/molecules/layout/meta.tsx | 70 ++++++++++++++++++++++++++++++++
 1 file changed, 70 insertions(+)
 create mode 100644 src/components/molecules/layout/meta.tsx

(limited to 'src/components/molecules/layout/meta.tsx')

diff --git a/src/components/molecules/layout/meta.tsx b/src/components/molecules/layout/meta.tsx
new file mode 100644
index 0000000..ace6f89
--- /dev/null
+++ b/src/components/molecules/layout/meta.tsx
@@ -0,0 +1,70 @@
+import DescriptionList, {
+  type DescriptionListProps,
+  type DescriptionListItem,
+} from '@components/atoms/lists/description-list';
+import { ReactNode, VFC } from 'react';
+
+export type MetaItem = {
+  /**
+   * The meta name.
+   */
+  name: string;
+  /**
+   * The meta value.
+   */
+  value: ReactNode | ReactNode[];
+};
+
+export type MetaMap = {
+  [key: string]: MetaItem | undefined;
+};
+
+export type MetaProps = {
+  /**
+   * Set additional classnames to the meta wrapper.
+   */
+  className?: string;
+  /**
+   * The meta data.
+   */
+  data: MetaMap;
+  /**
+   * The meta layout.
+   */
+  layout?: DescriptionListProps['layout'];
+};
+
+/**
+ * Meta component
+ *
+ * Renders the page metadata.
+ */
+const Meta: VFC<MetaProps> = ({ data, ...props }) => {
+  /**
+   * Transform the metadata to description list item format.
+   *
+   * @param {MetaMap} items - The meta.
+   * @returns {DescriptionListItem[]} The formatted description list items.
+   */
+  const getItems = (items: MetaMap): DescriptionListItem[] => {
+    const listItems: DescriptionListItem[] = Object.entries(items)
+      .map(([key, item]) => {
+        if (!item) return;
+
+        const { name, value } = item;
+
+        return {
+          id: key,
+          term: name,
+          value: Array.isArray(value) ? value : [value],
+        } as DescriptionListItem;
+      })
+      .filter((item): item is DescriptionListItem => !!item);
+
+    return listItems;
+  };
+
+  return <DescriptionList items={getItems(data)} {...props} />;
+};
+
+export default Meta;
-- 
cgit v1.2.3

