/**
* Copyright (c) 2026-present, Goldman Sachs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { observer } from 'mobx-react-lite';
import { useCallback, useEffect, useRef, useState } from 'react';
import { IconButton } from '@mui/material';
import {
ArrowRightCircleOutlineIcon,
EmptyStarIcon,
ExternalLinkIcon,
MinusIcon,
PlusIcon,
RocketTakeoffOutlineIcon,
StarIcon,
clsx,
useResizeDetector,
} from '@finos/legend-art';
import type {
LegendMarketplaceDataAPIsStore,
LegendServiceCardState,
} from '../../stores/dataAPIs/LegendMarketplaceDataAPIsStore.js';
import type { LegendServiceTreeNode } from '../../stores/dataAPIs/LegendServiceTree.js';
const ROW_HEIGHT_PX = 34;
const INDENT_PX = 20;
const OVERSCAN_ROWS = 10;
const FALLBACK_VIEWPORT_PX = 600;
const LegendServiceTreeRow = observer(
(props: {
node: LegendServiceTreeNode;
top: number;
store: LegendMarketplaceDataAPIsStore;
onServiceClick: (serviceCardState: LegendServiceCardState) => void;
onOpenGroup: (groupPath: string) => void;
onLaunchService: ((service: LegendServiceCardState) => void) | undefined;
}) => {
const { node, top, store, onServiceClick, onOpenGroup, onLaunchService } =
props;
const isGroup = node.isGroup;
const isExpanded = isGroup && store.isTreeGroupExpanded(node.path);
const renderFavoriteButton = (
service: LegendServiceCardState,
idx: number,
): React.ReactNode => {
const pattern = service.service.pattern;
const isFavorite = store.isFavorite(pattern);
return (
store.toggleFavorite(pattern)}
title={isFavorite ? 'Remove from favorites' : 'Add to favorites'}
aria-label={`${
isFavorite ? 'Remove from favorites' : 'Add to favorites'
}: ${service.title}`}
aria-pressed={isFavorite}
size="small"
>
{isFavorite ? : }
);
};
const renderServiceActions = (
service: LegendServiceCardState,
idx: number,
): React.ReactNode => (
{service.title !== node.label && (
{service.title}
)}
onServiceClick(service)}
title="View service details"
aria-label={`View details: ${service.title}`}
size="small"
>
{onLaunchService && (
onLaunchService(service)}
title="Launch service"
aria-label={`Launch service: ${service.title}`}
size="small"
>
)}
);
return (
{isGroup ? (
{node.serviceCount}
) : (
{node.path}
{node.services.map(renderFavoriteButton)}
)}
{isGroup && (
onOpenGroup(node.path)}
title="Show only this group"
aria-label={`Go to ${node.label}`}
size="small"
>
)}
{node.services.map(renderServiceActions)}
);
},
);
/**
* Only the rows inside the scroll viewport (plus a small overscan) are
* mounted, so the cost of any click does not depend on how many services
* are expanded.
*/
export const LegendServiceTreeView = observer(
(props: {
store: LegendMarketplaceDataAPIsStore;
onServiceClick: (serviceCardState: LegendServiceCardState) => void;
onOpenGroup: (groupPath: string) => void;
onLaunchService?: ((service: LegendServiceCardState) => void) | undefined;
}) => {
const { store, onServiceClick, onOpenGroup, onLaunchService } = props;
const rows = store.visibleTreeRows;
const frameRef = useRef(0);
const { ref: scrollRef, height } = useResizeDetector();
const [scrollTop, setScrollTop] = useState(0);
const viewportHeight =
height !== undefined && height > 0 ? height : FALLBACK_VIEWPORT_PX;
useEffect(() => () => cancelAnimationFrame(frameRef.current), []);
const onScroll = useCallback(() => {
cancelAnimationFrame(frameRef.current);
frameRef.current = requestAnimationFrame(() =>
setScrollTop(scrollRef.current?.scrollTop ?? 0),
);
}, [scrollRef]);
const maxScrollTop = Math.max(
0,
rows.length * ROW_HEIGHT_PX - viewportHeight,
);
const effectiveScrollTop = Math.min(scrollTop, maxScrollTop);
const first = Math.max(
0,
Math.floor(effectiveScrollTop / ROW_HEIGHT_PX) - OVERSCAN_ROWS,
);
const last = Math.min(
rows.length,
Math.ceil((effectiveScrollTop + viewportHeight) / ROW_HEIGHT_PX) +
OVERSCAN_ROWS,
);
return (
{rows.slice(first, last).map((node, idx) => (
))}
);
},
);