-
Notifications
You must be signed in to change notification settings - Fork 0
[FEATURE] Add panel-level repeat variable support #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e710b68
fb400dc
821c684
184c638
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // Copyright The Perses Authors | ||
| // 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 { Box, SxProps, Theme } from '@mui/material'; | ||
| import { ReactNode } from 'react'; | ||
|
|
||
| export interface RepeatGridProps<T> { | ||
| rows: T[][]; | ||
| gap: number; | ||
| renderItem: (item: T, rowIndex: number, colIndex: number) => ReactNode; | ||
| containerSx?: SxProps<Theme>; | ||
| rowSx?: SxProps<Theme>; | ||
| } | ||
|
|
||
| export function RepeatGrid<T>({ rows, gap, renderItem, containerSx, rowSx }: RepeatGridProps<T>): ReactNode { | ||
| return ( | ||
| <Box | ||
| sx={[ | ||
| { display: 'flex', flexDirection: 'column', width: '100%', height: '100%', gap: `${gap}px` }, | ||
| ...(Array.isArray(containerSx) ? containerSx : [containerSx]), | ||
| ]} | ||
| > | ||
| {rows.map((rowItems, rowIndex) => ( | ||
| <Box key={rowIndex} sx={[{ display: 'flex', gap: `${gap}px` }, ...(Array.isArray(rowSx) ? rowSx : [rowSx])]}> | ||
| {rowItems.map((item, colIndex) => renderItem(item, rowIndex, colIndex))} | ||
| </Box> | ||
| ))} | ||
| </Box> | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // Copyright The Perses Authors | ||
| // 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. | ||
|
|
||
| export * from './RepeatGrid'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| // Copyright The Perses Authors | ||
| // 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 { ErrorAlert, ErrorBoundary } from '@perses-dev/components'; | ||
| import { PanelGroupId } from '@perses-dev/plugin-system'; | ||
| import { ReactElement } from 'react'; | ||
|
|
||
| import { DEFAULT_MARGIN } from '../../constants'; | ||
| import { useViewPanelGroup } from '../../context'; | ||
| import { PanelGroupItemId } from '../../model'; | ||
| import { getPerRowCount, RepeatItemMeta } from '../../utils'; | ||
| import { PanelOptions } from '../Panel/Panel'; | ||
| import { GridItemContent } from './GridItemContent'; | ||
| import { RepeatGridItemContent } from './RepeatGridItemContent'; | ||
|
|
||
| interface GridItemRendererProps { | ||
| panelGroupId: PanelGroupId; | ||
| panelGroupItemLayoutId: string; | ||
| width: number; | ||
| repeatItemMeta?: RepeatItemMeta; | ||
| groupRepeatVariable?: [string, string]; | ||
| panelOptions?: PanelOptions; | ||
| isEditMode: boolean; | ||
| } | ||
|
|
||
| export function GridItemRenderer({ | ||
| panelGroupId, | ||
| panelGroupItemLayoutId, | ||
| width, | ||
| repeatItemMeta, | ||
| groupRepeatVariable, | ||
| panelOptions, | ||
| isEditMode, | ||
| }: GridItemRendererProps): ReactElement { | ||
| const viewPanelItemId = useViewPanelGroup(); | ||
|
|
||
| const panelRepeatVariable = repeatItemMeta?.itemRepeatVariable; | ||
| const panelVariableValues = repeatItemMeta?.values; | ||
| const totalValues = repeatItemMeta?.totalValues ?? 0; | ||
| const effectiveValues = viewPanelItemId?.repeatVariable?.panel | ||
| ? [viewPanelItemId.repeatVariable.panel[1]] | ||
| : panelVariableValues; | ||
|
|
||
| const panelGroupItemId: PanelGroupItemId = { | ||
| panelGroupId, | ||
| panelGroupItemLayoutId, | ||
| repeatVariable: { group: groupRepeatVariable }, | ||
| }; | ||
|
|
||
| return ( | ||
| <ErrorBoundary FallbackComponent={ErrorAlert}> | ||
| {panelRepeatVariable && effectiveValues?.length ? ( | ||
| <RepeatGridItemContent | ||
| panelGroupId={panelGroupId} | ||
| panelGroupItemLayoutId={panelGroupItemLayoutId} | ||
| panelRepeatVariable={{ | ||
| name: panelRepeatVariable.value, | ||
| values: effectiveValues, | ||
| maxPer: getPerRowCount(panelRepeatVariable), | ||
| }} | ||
| groupRepeatVariable={groupRepeatVariable} | ||
| width={width} | ||
| itemGap={DEFAULT_MARGIN} | ||
| panelOptions={panelOptions} | ||
| isEditMode={isEditMode} | ||
| isCapped={!isEditMode && effectiveValues.length < totalValues} | ||
| /> | ||
| ) : ( | ||
| <GridItemContent panelOptions={panelOptions} panelGroupItemId={panelGroupItemId} width={width} /> | ||
| )} | ||
| </ErrorBoundary> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -141,7 +141,16 @@ export function RepeatGridLayout({ | |
| {variable.value.map((value) => ( | ||
| <VariableContext.Provider | ||
| key={`${repeatVariableName}-${value}`} | ||
| value={{ state: { ...variables, [repeatVariableName]: { value, loading: false } } }} | ||
| value={{ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. React Doctor · Every reader of this context redraws on each render because you build its Fix → Wrap the context value in |
||
| state: { | ||
| ...variables, | ||
| [repeatVariableName]: { | ||
| ...variables[repeatVariableName], | ||
| value: value, | ||
| loading: false, | ||
| }, | ||
| }, | ||
| }} | ||
| > | ||
| <Row | ||
| panelGroupId={panelGroupId} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| // Copyright The Perses Authors | ||
| // 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 { Box } from '@mui/material'; | ||
| import { RepeatGrid } from '@perses-dev/components'; | ||
| import { PanelGroupId, useVariableValues, VariableContext } from '@perses-dev/plugin-system'; | ||
| import { ReactNode, useMemo } from 'react'; | ||
|
|
||
| import { calcPerPanelWidth } from '../../utils/repeatLayoutUtils'; | ||
| import { PanelOptions } from '../Panel/Panel'; | ||
| import { GridItemContent } from './GridItemContent'; | ||
|
|
||
| interface RepeatPanelItemProps { | ||
| panelGroupId: PanelGroupId; | ||
| panelGroupItemLayoutId: string; | ||
| panelRepeatVariable: { | ||
| name: string; | ||
| values: string[]; | ||
| maxPer: number; | ||
| }; | ||
| groupRepeatVariable?: [string, string]; | ||
| width: number; | ||
| itemGap: number; | ||
| panelOptions?: PanelOptions; | ||
| isEditMode: boolean; | ||
| isCapped?: boolean; | ||
| } | ||
|
|
||
| function getRepeatPanelTooltip( | ||
| isFirst: boolean, | ||
| isEditMode: boolean, | ||
| isCapped: boolean | undefined, | ||
| repeatVariableName: string, | ||
| value: string, | ||
| ): string | undefined { | ||
| if (!isFirst && isEditMode) { | ||
| return `This panel is generated from the variable "${repeatVariableName}" with the value "${value}". To change panel definition, please edit the first panel.`; | ||
| } | ||
| if (isFirst && isCapped) { | ||
| return `Not all values are displayed. To display more, update the repeat variable limit in the server configuration.`; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Renders a grid item that repeats based on a variable. | ||
| * It calculates the number of items per row and the width of each item, | ||
| * then renders the appropriate number of GridItemContent components with the correct variable context. | ||
| */ | ||
| export function RepeatGridItemContent({ | ||
| panelGroupId, | ||
| panelGroupItemLayoutId, | ||
| panelRepeatVariable, | ||
| groupRepeatVariable, | ||
| width, | ||
| itemGap, | ||
| panelOptions, | ||
| isEditMode, | ||
| isCapped, | ||
| }: RepeatPanelItemProps): ReactNode { | ||
| const { name: repeatVariableName, values: variableValues, maxPer: perRow } = panelRepeatVariable; | ||
| const variables = useVariableValues(); | ||
|
|
||
| const rows: string[][] = useMemo(() => { | ||
| const result: string[][] = []; | ||
| for (let i = 0; i < variableValues.length; i += perRow) { | ||
| result.push(variableValues.slice(i, i + perRow)); | ||
| } | ||
| return result; | ||
| }, [variableValues, perRow]); | ||
| const perPanelWidth = useMemo(() => calcPerPanelWidth(width, itemGap, perRow), [itemGap, perRow, width]); | ||
|
|
||
| return ( | ||
| <RepeatGrid | ||
| rows={rows} | ||
| gap={itemGap} | ||
| containerSx={{ overflow: 'hidden' }} | ||
| rowSx={{ flex: 1, overflow: 'hidden' }} | ||
| renderItem={(value, rowIndex, colIndex) => { | ||
| const isFirst = colIndex + rowIndex === 0; | ||
| return ( | ||
| <VariableContext.Provider | ||
| key={`${repeatVariableName}-${value}`} | ||
| value={{ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. React Doctor · Every reader of this context redraws on each render because you build its Fix → Wrap the context value in |
||
| state: { | ||
| ...variables, | ||
| [repeatVariableName]: { ...variables[repeatVariableName], value, loading: false }, | ||
| }, | ||
| }} | ||
| > | ||
| <Box sx={{ width: perPanelWidth, overflow: 'hidden' }}> | ||
| <GridItemContent | ||
| panelOptions={panelOptions} | ||
| panelGroupItemId={{ | ||
| panelGroupId, | ||
| panelGroupItemLayoutId, | ||
| repeatVariable: { | ||
| panel: [repeatVariableName, value], | ||
| group: groupRepeatVariable, | ||
| }, | ||
| }} | ||
| width={perPanelWidth} | ||
| readonly={!isFirst} | ||
| informationTooltip={getRepeatPanelTooltip(isFirst, isEditMode, isCapped, repeatVariableName, value)} | ||
| /> | ||
| </Box> | ||
| </VariableContext.Provider> | ||
| ); | ||
| }} | ||
| /> | ||
| ); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
React Doctor ·
react-doctor/no-barrel-import(warning)This ships extra code to your users & slows page load. Import directly from "../../constants/grid-layout-config".
Fix → Import from the direct path:
import { Button } from './components/Button'instead of./componentsDocs