|
| 1 | +// Copyright The Perses Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +import { ReactNode, useMemo } from 'react'; |
| 15 | +import { useVariableValues, VariableContext } from '@perses-dev/plugin-system'; |
| 16 | +import { GridItemContent, PanelOptions } from '@perses-dev/dashboards'; |
| 17 | +import { PanelGroupId } from '@perses-dev/spec'; |
| 18 | +import { Box } from '@mui/material'; |
| 19 | +import { RepeatGrid } from '@perses-dev/components'; |
| 20 | + |
| 21 | +interface RepeatPanelItemProps { |
| 22 | + panelGroupId: PanelGroupId; |
| 23 | + panelGroupItemLayoutId: string; |
| 24 | + panelRepeatVariable: { |
| 25 | + name: string; |
| 26 | + values: string[]; |
| 27 | + maxPer?: number; |
| 28 | + }; |
| 29 | + groupRepeatVariable?: [string, string]; |
| 30 | + width: number; |
| 31 | + itemGap: number; |
| 32 | + panelOptions?: PanelOptions; |
| 33 | + isEditMode: boolean; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Renders a grid item that repeats based on a variable. |
| 38 | + * It calculates the number of items per row and the width of each item, |
| 39 | + * then renders the appropriate number of GridItemContent components with the correct variable context. |
| 40 | + */ |
| 41 | +export function RepeatGridItemContent({ |
| 42 | + panelGroupId, |
| 43 | + panelGroupItemLayoutId, |
| 44 | + panelRepeatVariable, |
| 45 | + groupRepeatVariable, |
| 46 | + width, |
| 47 | + itemGap, |
| 48 | + panelOptions, |
| 49 | + isEditMode, |
| 50 | +}: RepeatPanelItemProps): ReactNode { |
| 51 | + const { name: repeatVariableName, values: variableValues, maxPer } = panelRepeatVariable; |
| 52 | + const variables = useVariableValues(); |
| 53 | + const perRow = useMemo(() => { |
| 54 | + const maxPerRow = maxPer ?? variableValues.length; |
| 55 | + if (variableValues.length < maxPerRow) { |
| 56 | + return variableValues.length; |
| 57 | + } |
| 58 | + return maxPerRow; |
| 59 | + }, [maxPer, variableValues.length]); |
| 60 | + const rows: string[][] = useMemo(() => { |
| 61 | + const result: string[][] = []; |
| 62 | + for (let i = 0; i < variableValues.length; i += perRow) { |
| 63 | + result.push(variableValues.slice(i, i + perRow)); |
| 64 | + } |
| 65 | + return result; |
| 66 | + }, [variableValues, perRow]); |
| 67 | + const perPanelWidth = Math.floor((width - itemGap * (perRow - 1)) / perRow); |
| 68 | + |
| 69 | + return ( |
| 70 | + <RepeatGrid |
| 71 | + rows={rows} |
| 72 | + gap={itemGap} |
| 73 | + containerSx={{ overflow: 'hidden' }} |
| 74 | + rowSx={{ flex: 1, overflow: 'hidden' }} |
| 75 | + renderItem={(value, rowIndex, colIndex) => { |
| 76 | + const isNotFirst = colIndex + rowIndex !== 0; |
| 77 | + return ( |
| 78 | + <VariableContext.Provider |
| 79 | + key={`${repeatVariableName}-${value}`} |
| 80 | + value={{ |
| 81 | + state: { |
| 82 | + ...variables, |
| 83 | + [repeatVariableName]: { ...variables[repeatVariableName], value, loading: false }, |
| 84 | + }, |
| 85 | + }} |
| 86 | + > |
| 87 | + <Box sx={{ width: perPanelWidth, overflow: 'hidden' }}> |
| 88 | + <GridItemContent |
| 89 | + panelOptions={panelOptions} |
| 90 | + panelGroupItemId={{ |
| 91 | + panelGroupId, |
| 92 | + panelGroupItemLayoutId, |
| 93 | + repeatVariable: { |
| 94 | + panel: [repeatVariableName, value], |
| 95 | + group: groupRepeatVariable, |
| 96 | + }, |
| 97 | + }} |
| 98 | + width={perPanelWidth} |
| 99 | + readonly={isNotFirst} |
| 100 | + informationTooltip={ |
| 101 | + isNotFirst && isEditMode |
| 102 | + ? `This panel is generated from the variable "${repeatVariableName}" with the value "${value}". To change panel definition, please edit the first panel.` |
| 103 | + : undefined |
| 104 | + } |
| 105 | + /> |
| 106 | + </Box> |
| 107 | + </VariableContext.Provider> |
| 108 | + ); |
| 109 | + }} |
| 110 | + /> |
| 111 | + ); |
| 112 | +} |
0 commit comments