/* eslint-disable @typescript-eslint/indent */ import React, { useState, useEffect, useCallback } from 'react'; import { useAtomValue, useSetAtom } from 'jotai'; import { FormattedMessage, useIntl } from 'react-intl'; import styled from 'styled-components/macro'; import useGetStyleKey from 'hooks/useGetStyleKey'; import updateStyle from 'common/utils/update-style'; import { Select, SelectTrigger, SelectValue, SelectIcon, SelectContent, SelectViewport, SelectItem, SelectItemText, SelectItemIndicator, } from 'common/select'; import InputNumber from 'common/input-number'; // import Gradiant from 'common/gradiant'; import ColorPicker from 'common/color-picker'; import { Column, Selector, Label, PairsWrap, StyledRow, Description, Star, } from 'common/styles'; import { splitArray } from 'common/utils'; import { ReactComponent as Arrow } from '../../assets/icons/arrow-down.svg'; import { ReactComponent as Check } from '../../assets/icons/tick.svg'; import { ReactComponent as Plus } from '../../assets/icons/plus.svg'; import { ReactComponent as Delete } from '../../assets/icons/delete.svg'; import { ReactComponent as Step } from '../../assets/icons/step.svg'; import { ReactComponent as Match } from '../../assets/icons/match.svg'; import { layerState, mapState, selectedLayerIDState, styleObjState, } from 'atoms/map'; import { columnsState, distinctState } from 'atoms/general'; import type { DataDrivenPropertyValueSpecification } from 'maplibre-gl'; interface IProps { type: | 'size' | 'color' | 'stroke' | 'stroke-color' | 'stroke-size' | 'stroke-opacity' | 'weight' | 'intensity'; method: 'match' | 'step'; selectedCol: string; } const Conditional = ({ type, method, selectedCol }: IProps) => { const intl = useIntl(); const map = useAtomValue(mapState); const openLayerID = useAtomValue(selectedLayerIDState); const setStyleObj = useSetAtom(styleObjState); const columns = useAtomValue(columnsState); const distinctFunc = useAtomValue(distinctState); const layer = useAtomValue(layerState); const { styleKey, property } = useGetStyleKey(type); const [conditionType, setCondition] = useState>(method); const [pairs, setPairs] = useState<(number | string)[][]>([]); // Pairs of zoom/value or zoom/color const [colName, setColName] = useState(selectedCol); const [distinctValues, setDistincts] = useState([]); useEffect(() => { // @ts-ignore line // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access const expression = layer?.[styleKey]?.[property]; const minzoom = layer?.minzoom ?? 1; const maxzoom = layer?.maxzoom ?? 20; if ( (expression as string[])?.[0] === 'match' || (expression as string[])?.[0] === 'step' ) { let arr = splitArray((expression as string[])?.slice(2), 2); if ((expression as string[])?.[0] === 'step') { const popped = arr.pop() ?? []; const reversed = arr.map((a) => a.reverse()); arr = [...reversed, popped]; } setPairs(arr); } else { const temp = [ [ minzoom, expression ?? (['color', 'stroke-color'].includes(type) ? '#C11010' : 1), ], [maxzoom, ['color', 'stroke-color'].includes(type) ? '#2585f3' : 1], [['color', 'stroke-color'].includes(type) ? '#000000' : 1], ]; setPairs(temp); } }, [layer, property, styleKey]); const styleValue = useCallback( (value: (number | string)[][]) => { let arr = value; if (conditionType === 'step') { const popped = arr.pop() ?? []; const reversed = arr.map((a) => a.reverse()); arr = [...reversed, popped]; } return [conditionType, ['get', colName], ...arr.flat()]; }, [colName, conditionType] ); const applyStyles = useCallback( (value: DataDrivenPropertyValueSpecification) => { if (openLayerID && map && property && styleKey && value) { updateStyle(openLayerID, map, styleKey, property, value, setStyleObj); } }, [openLayerID, map, styleKey, property] ); useEffect(() => { if (colName) void distinctFunc?.(colName).then((res: string[]) => { setDistincts(res); const arg = styleValue( pairs ) as DataDrivenPropertyValueSpecification; applyStyles(arg); }); }, [colName]); useEffect(() => { const arg = styleValue( pairs ) as DataDrivenPropertyValueSpecification; applyStyles(arg); }, [conditionType]); return ( */} {columns?.map((column) => ( {column} ))} { setCondition('step'); }} /> { setCondition('match'); }} /> {/* {['color', 'stroke-color'].includes(type) ? ( ) : ( )} */} * : { const temp = [...pairs]; temp.splice(pairs?.length - 1, 0, [ 'asas', ['color', 'stroke-color'].includes(type) ? '#FFB800' : 1, ]); const arg = styleValue( temp ) as DataDrivenPropertyValueSpecification; applyStyles(arg); }} /> {pairs?.map((pair, index) => ( {['color', 'stroke-color'].includes(type) ? ( // { const temp = [...pairs]; temp[index] = !pair?.[1] ? [color.toUpperCase()] : [temp[index][0], color.toUpperCase()]; const arg = styleValue( temp ) as DataDrivenPropertyValueSpecification; applyStyles(arg); }} /> ) : ( { const temp = [...pairs]; temp[index] = !pair?.[1] ? [value] : [temp[index][0], value]; const arg = styleValue( temp ) as DataDrivenPropertyValueSpecification; applyStyles(arg); }} /> )}{' '} {/* value or color */}: {!pair?.[1] ? (
) : ( )}{' '} {/* zoom */}
{pair?.[1] ? ( { if (pairs.length < 3) return; const temp = pairs?.filter((c, index2) => index !== index2); const arg = styleValue( temp ) as DataDrivenPropertyValueSpecification; applyStyles(arg); }} /> ) : null}
))}
); }; export default Conditional; const IconWrapper = styled.div` cursor: pointer; display: flex; `;