|
| 1 | +import { Icon } from '@src/alchemy-components'; |
| 2 | +import { EntityAndType } from '@src/app/entity/shared/types'; |
| 3 | +import { extractTypeFromUrn } from '@src/app/entity/shared/utils'; |
| 4 | +import { SearchSelect } from '@src/app/entityV2/shared/components/styled/search/SearchSelect'; |
| 5 | +import { useHydratedEntityMap } from '@src/app/entityV2/shared/tabs/Properties/useHydratedEntityMap'; |
| 6 | +import { EntityLink } from '@src/app/homeV2/reference/sections/EntityLink'; |
| 7 | +import { EntityType } from '@src/types.generated'; |
| 8 | +import { Skeleton } from 'antd'; |
| 9 | +import React, { useEffect, useMemo, useState } from 'react'; |
| 10 | +import styled from 'styled-components'; |
| 11 | + |
| 12 | +const Container = styled.div` |
| 13 | + display: flex; |
| 14 | + width: 100%; |
| 15 | + min-height: 500px; |
| 16 | + height: 70vh; |
| 17 | + border: 1px solid #f0f0f0; |
| 18 | + border-radius: 4px; |
| 19 | +`; |
| 20 | + |
| 21 | +const SearchSection = styled.div` |
| 22 | + flex: 2; |
| 23 | + height: 100%; |
| 24 | + overflow: hidden; |
| 25 | + display: flex; |
| 26 | + flex-direction: column; |
| 27 | +`; |
| 28 | + |
| 29 | +const SubSearchSection = styled.div` |
| 30 | + flex: 1; |
| 31 | + height: 100%; |
| 32 | + overflow: hidden; |
| 33 | + display: flex; |
| 34 | + flex-direction: column; |
| 35 | +`; |
| 36 | + |
| 37 | +const CurrentSection = styled.div` |
| 38 | + flex: 1; |
| 39 | + width: 40%; |
| 40 | + border-left: 1px solid #f0f0f0; |
| 41 | + display: flex; |
| 42 | + flex-direction: column; |
| 43 | +`; |
| 44 | + |
| 45 | +const SectionHeader = styled.div` |
| 46 | + padding-left: 20px; |
| 47 | + margin-top: 10px; |
| 48 | + font-size: 16px; |
| 49 | + font-weight: 500; |
| 50 | + color: #666; |
| 51 | +`; |
| 52 | + |
| 53 | +const ScrollableContent = styled.div` |
| 54 | + flex: 1; |
| 55 | + overflow: auto; |
| 56 | + padding: 20px; |
| 57 | +`; |
| 58 | + |
| 59 | +const SelectedItem = styled.div` |
| 60 | + display: flex; |
| 61 | + padding: 8px; |
| 62 | + border-radius: 4px; |
| 63 | + margin-bottom: 8px; |
| 64 | + align-items: center; |
| 65 | + border: 1px solid #f0f0f0; |
| 66 | + justify-content: space-between; |
| 67 | +
|
| 68 | + &:hover { |
| 69 | + background-color: #fafafa; |
| 70 | + } |
| 71 | +`; |
| 72 | + |
| 73 | +const IconWrapper = styled.div` |
| 74 | + cursor: pointer; |
| 75 | +`; |
| 76 | + |
| 77 | +const INITIAL_SELECTED_ENTITIES = [] as EntityAndType[]; |
| 78 | + |
| 79 | +// New interface for the generic component |
| 80 | +interface SearchSelectUrnInputProps { |
| 81 | + allowedEntityTypes: EntityType[]; |
| 82 | + isMultiple: boolean; |
| 83 | + selectedValues: string[]; |
| 84 | + updateSelectedValues: (values: string[] | number[]) => void; |
| 85 | +} |
| 86 | + |
| 87 | +// Generic component that doesn't depend on StructuredProperty |
| 88 | +export function SearchSelectUrnInput({ |
| 89 | + allowedEntityTypes, |
| 90 | + isMultiple, |
| 91 | + selectedValues, |
| 92 | + updateSelectedValues, |
| 93 | +}: SearchSelectUrnInputProps) { |
| 94 | + const [tempSelectedEntities, setTempSelectedEntities] = useState<EntityAndType[]>(INITIAL_SELECTED_ENTITIES); |
| 95 | + |
| 96 | + // Convert the selected values (urns) to EntityAndType format for SearchSelect |
| 97 | + const selectedEntities = useMemo( |
| 98 | + () => selectedValues.map((urn) => ({ urn, type: extractTypeFromUrn(urn) })), |
| 99 | + [selectedValues], |
| 100 | + ); |
| 101 | + |
| 102 | + // only run this once |
| 103 | + useEffect(() => { |
| 104 | + if (tempSelectedEntities === INITIAL_SELECTED_ENTITIES) { |
| 105 | + setTempSelectedEntities(selectedEntities); |
| 106 | + } |
| 107 | + }, [selectedEntities, setTempSelectedEntities, tempSelectedEntities]); |
| 108 | + |
| 109 | + // call updateSelectedValues when tempSelectedEntities changes |
| 110 | + useEffect(() => { |
| 111 | + if (tempSelectedEntities !== INITIAL_SELECTED_ENTITIES) { |
| 112 | + updateSelectedValues(tempSelectedEntities.map((entity) => entity.urn)); |
| 113 | + } |
| 114 | + // this is excluded from the dependency array because updateSelectedValues is reconstructed |
| 115 | + // by the parent component on every render |
| 116 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 117 | + }, [tempSelectedEntities]); |
| 118 | + |
| 119 | + const removeEntity = (entity: EntityAndType) => { |
| 120 | + setTempSelectedEntities(tempSelectedEntities.filter((e) => e.urn !== entity.urn)); |
| 121 | + }; |
| 122 | + |
| 123 | + const hydratedEntityMap = useHydratedEntityMap(tempSelectedEntities.map((entity) => entity.urn)); |
| 124 | + |
| 125 | + return ( |
| 126 | + <Container> |
| 127 | + <SearchSection> |
| 128 | + <SectionHeader>Search Values</SectionHeader> |
| 129 | + <SubSearchSection> |
| 130 | + <SearchSelect |
| 131 | + fixedEntityTypes={allowedEntityTypes} |
| 132 | + selectedEntities={tempSelectedEntities} |
| 133 | + setSelectedEntities={setTempSelectedEntities} |
| 134 | + limit={isMultiple ? undefined : 1} |
| 135 | + /> |
| 136 | + </SubSearchSection> |
| 137 | + </SearchSection> |
| 138 | + <CurrentSection> |
| 139 | + <SectionHeader>Selected Values</SectionHeader> |
| 140 | + <ScrollableContent> |
| 141 | + {tempSelectedEntities.length === 0 ? ( |
| 142 | + <div>No values selected</div> |
| 143 | + ) : ( |
| 144 | + tempSelectedEntities.map((entity) => ( |
| 145 | + <SelectedItem key={entity.urn}> |
| 146 | + {hydratedEntityMap[entity.urn] ? ( |
| 147 | + <EntityLink entity={hydratedEntityMap[entity.urn]} /> |
| 148 | + ) : ( |
| 149 | + <Skeleton.Input active /> |
| 150 | + )} |
| 151 | + <IconWrapper> |
| 152 | + <Icon icon="X" source="phosphor" onClick={() => removeEntity(entity)} /> |
| 153 | + </IconWrapper> |
| 154 | + </SelectedItem> |
| 155 | + )) |
| 156 | + )} |
| 157 | + </ScrollableContent> |
| 158 | + </CurrentSection> |
| 159 | + </Container> |
| 160 | + ); |
| 161 | +} |
0 commit comments