mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Co-authored-by: Janne Savolainen <janne.savolainen@live.fi> Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>
59 lines
1.4 KiB
TypeScript
59 lines
1.4 KiB
TypeScript
/**
|
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
|
*/
|
|
import { pipeline } from "@ogre-tools/fp";
|
|
import { identity, map } from "lodash/fp";
|
|
import React from "react";
|
|
import { addSeparator } from "../../../common/utils/add-separator/add-separator";
|
|
|
|
interface RequiredPropertiesForItem {
|
|
id: string;
|
|
}
|
|
|
|
interface MapProps<Item extends RequiredPropertiesForItem> {
|
|
items: Item[];
|
|
children: (item: Item) => React.ReactElement;
|
|
getPlaceholder?: () => React.ReactElement;
|
|
getSeparator?: () => React.ReactElement;
|
|
}
|
|
|
|
export const Map = <Item extends RequiredPropertiesForItem>(
|
|
props: MapProps<Item>,
|
|
) => {
|
|
const { items, getPlaceholder, getSeparator, children } = props;
|
|
|
|
if (items.length === 0) {
|
|
return getPlaceholder?.() || null;
|
|
}
|
|
|
|
return (
|
|
<>
|
|
{pipeline(
|
|
items,
|
|
|
|
map((item) => ({ item, render: () => children(item) })),
|
|
|
|
getSeparator
|
|
? (items) =>
|
|
addSeparator(
|
|
(left, right) => ({
|
|
item: {
|
|
id: `separator-between-${left.item.id}-and-${right.item.id}`,
|
|
},
|
|
|
|
render: getSeparator,
|
|
}),
|
|
|
|
items,
|
|
)
|
|
: identity,
|
|
|
|
map(({ render, item }) => (
|
|
<React.Fragment key={item.id}>{render()}</React.Fragment>
|
|
)),
|
|
)}
|
|
</>
|
|
);
|
|
};
|