|
| 1 | +import * as React from "react"; |
| 2 | +import { useMemo } from "react"; |
| 3 | +import styled from "styled-components"; |
| 4 | +import { ExternalApiUsage } from "../../data-extensions-editor/external-api-usage"; |
| 5 | +import { ModeledMethod } from "../../data-extensions-editor/modeled-method"; |
| 6 | +import { ModeledMethodDataGrid } from "./ModeledMethodDataGrid"; |
| 7 | + |
| 8 | +const LibraryContainer = styled.div` |
| 9 | + margin-bottom: 1rem; |
| 10 | +`; |
| 11 | + |
| 12 | +type Props = { |
| 13 | + externalApiUsages: ExternalApiUsage[]; |
| 14 | + modeledMethods: Record<string, ModeledMethod>; |
| 15 | + onChange: ( |
| 16 | + externalApiUsage: ExternalApiUsage, |
| 17 | + modeledMethod: ModeledMethod, |
| 18 | + ) => void; |
| 19 | +}; |
| 20 | + |
| 21 | +export const ModeledMethodsList = ({ |
| 22 | + externalApiUsages, |
| 23 | + modeledMethods, |
| 24 | + onChange, |
| 25 | +}: Props) => { |
| 26 | + const groupedByLibrary = useMemo(() => { |
| 27 | + const groupedByLibrary: Record<string, ExternalApiUsage[]> = {}; |
| 28 | + |
| 29 | + for (const externalApiUsage of externalApiUsages) { |
| 30 | + groupedByLibrary[externalApiUsage.library] ??= []; |
| 31 | + groupedByLibrary[externalApiUsage.library].push(externalApiUsage); |
| 32 | + } |
| 33 | + |
| 34 | + return groupedByLibrary; |
| 35 | + }, [externalApiUsages]); |
| 36 | + |
| 37 | + const sortedLibraryNames = useMemo(() => { |
| 38 | + return Object.keys(groupedByLibrary).sort(); |
| 39 | + }, [groupedByLibrary]); |
| 40 | + |
| 41 | + return ( |
| 42 | + <> |
| 43 | + {sortedLibraryNames.map((libraryName) => ( |
| 44 | + <LibraryContainer key={libraryName}> |
| 45 | + <h3>{libraryName}</h3> |
| 46 | + <ModeledMethodDataGrid |
| 47 | + externalApiUsages={groupedByLibrary[libraryName]} |
| 48 | + modeledMethods={modeledMethods} |
| 49 | + onChange={onChange} |
| 50 | + /> |
| 51 | + </LibraryContainer> |
| 52 | + ))} |
| 53 | + </> |
| 54 | + ); |
| 55 | +}; |
0 commit comments