Skip to content

[FEATURE] Hide panel groups according to layout visibility - #371

Merged
shahrokni merged 20 commits into
perses:mainfrom
colivi:feat/row-visibility
Dec 3, 2026
Merged

shahrokni merged 20 commits into
perses:mainfrom
colivi:feat/row-visibility

Conversation

@colivi

@colivi colivi commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Description

Renders a panel group (Grid layout) only when its display.visibility rule is satisfied by the
current variable selection. A hidden group is not rendered, so its panels do not run their queries.

Depends on perses/spec#116 (adds display.visibility to the Grid layout). Opened as a draft until
that PR is merged and released; the @perses-dev/spec bump will be added to this PR then.

Use case: a dashboard shared by several environments where one row only makes sense for some of
them (e.g. a row backed by a service that only exists in non-production phases). Today the row is
displayed empty for the other environments.

layouts:
  - kind: Grid
    spec:
      display:
        title: Background workers
        visibility:
          variable: phase
          operator: equals        # equals (default) | notEquals | matches | notMatches
          value: [DEV, TST]

Behavior

  • A group without visibility is always displayed.
  • equals / notEquals: at least one selected value is (not) listed in value.
  • matches / notMatches: patterns are anchored (full match), like Prometheus matchers;
    an invalid pattern never matches.
  • Multi-value variables: the rule is evaluated against all selected values.
  • An unknown variable, or one still loading with no value yet, hides the group. A refresh that already has a value leaves the group mounted.
  • Edit mode: hidden groups stay displayed so they can still be edited.
  • visibility is kept in the panel group model and written back on save (no data loss on round trip).
  • Applies before repeatVariable expansion: a hidden group is not repeated either.
  • There is no editor for visibility yet, set the field in the dashboard JSON or YAML.
  • Out of scope: Tabs layouts.

Implementation

  • isPanelGroupVisible() in dashboards/src/utils/visibilityUtils.ts.
  • GridLayout returns null when the rule is not satisfied (outside edit mode).
  • PanelGroupDefinition.visibility, loaded in panel-group-slice.ts and saved in useDashboard.tsx.

Screenshots

Local stack: Perses server built with the spec change + this branch via SHARED_DEV, a mock
Prometheus, and a dashboard with a phase variable and four rows (no rule / equals [DEV, TST] /
equals [PRD] / notEquals [PRD]).

Phase Rows displayed Queries received by the mock Prometheus
PRD Application overview, Production only 2
DEV Application overview, Background workers, Everything but production 2
UAT Application overview, Everything but production 1

The query of a hidden row is never sent (e.g. the Background workers query is never requested
for PRD or UAT).

phase-DEV phase-PRD phase-UAT

Tests

  • Unit tests of every operator (single/multi value, empty, null, unknown, loading, invalid pattern).
  • GridLayout rendering: visible, hidden, edit mode, no rule.
  • Panel group slice conversion and useDashboard round trip keep visibility.
  • type-check, test and lint pass for @perses-dev/dashboards (no new Oxlint warning).

Checklist

  • Pull request has a descriptive title and context useful to a reviewer.
  • Pull request title follows the [<catalog_entry>] naming convention.
  • All commits have DCO signoffs.

UI Changes

  • Screenshots of the relevant changes are included.
  • Code follows the UI guidelines.

Signed-off-by: colivi <charles.olivi@gmail.com>
@Gladorme

Gladorme commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

I think if we go for this feature, we may want it by panel too.

For operators, I prefer we use: "=", "!=", "=~" and "!~". Because for now we never call them "equal", "matches", ... But I may be wrong 🙈

When editing a dashboard, panel group or dashboard with visibility option added should have a indicator (icon or overlay) on the panel group or panel to show it clearly.

@colivi

colivi commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

Ok Guillaume, panel visibility stays out of this PR.
The same rule (=, !=, =~, !~) will apply to a panel display, with the same edit-mode indicator, once group visibility is accepted.

Edit mode keeps an eye on a conditional group. The struck eye means the
current selection would hide it. Operators follow =, !=, =~ and !~.

Signed-off-by: colivi <charles.olivi@gmail.com>
@AntoineThebaud

Copy link
Copy Markdown
Contributor

Ok Guillaume, panel visibility stays out of this PR. The same rule (=, !=, =~, !~) will apply to a panel display, with the same edit-mode indicator, once group visibility is accepted.

Please check https://github.com/perses/spec/pull/116/changes#r4387420359

equals, notEquals, matches and notMatches. An omitted operator still means equals.

Signed-off-by: colivi <charles.olivi@gmail.com>
Visibility operators now come from the released spec. URL editors narrow the Link union, and the panel editor schema is asserted back to PanelEditorValues because Zod 4 types the spec schemas with an unknown input.

Signed-off-by: colivi <charles.olivi@gmail.com>
Signed-off-by: colivi <charles.olivi@gmail.com>
oxfmt wraps two long lines and sorts the eye icons. React 19 calls a function component with an undefined ref, so the grid test only checks the visibility notice.

Signed-off-by: colivi <charles.olivi@gmail.com>
Keep @perses-dev/spec 0.4.0-beta.0. The workspace packages follow main at 0.56.0-beta.1.

Signed-off-by: colivi <charles.olivi@gmail.com>

@Gladorme Gladorme left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mainly review with AI.

The issue with conditional groups disappear on every refresh is pretty big 😅

Comment thread client/package.json
"sideEffects": false,
"dependencies": {
"@perses-dev/spec": "0.3.0",
"@perses-dev/spec": "0.4.0-beta.0",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer wait breaking changes are fixed before (in PR: #375), before reviewing this one. It's like reviewing multiple time the same PR :/

Comment thread dashboards/src/utils/visibilityUtils.ts Outdated
return true;
}
const state = variableValues[visibility.variable];
if (!state || state.loading) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loading comes from variablesOptionsQuery.isFetching (Variable.tsx), so it is also true during background refetches, not only on first load. Every refresh / auto-refresh tick / time range change runs invalidateQueries(['variable']) (TimeRangeProvider.tsx), so a conditional group unmounts and remounts each time: panels lose their state, query again, and the layout jumps.

Could we only hide while there is no value yet? e.g.

if (!state || (state.loading && state.value == null)) {
  return false;
}

A test for "loading with a value already selected" would be nice too.

Comment thread dashboards/src/utils/visibilityUtils.ts Outdated
// Patterns are anchored like Prometheus label matchers; an invalid pattern never matches.
function matchesPattern(pattern: string, value: string): boolean {
try {
return new RegExp(`^(?:${pattern})$`).test(value);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On perses/spec#116 matches/notMatches were described as anchored Go RE2 patterns, but JS RegExp is a different flavor. For example (?i)dev, common in Prometheus, throws here, so it's treated as "never matches" and the group is silently hidden. JS also accepts lookaheads that Go rejects.

Could we either handle the inline (?i) flag or document the differences? The regex behavior documentation mentioned on the spec PR isn't in this PR yet.

Comment thread dashboards/src/utils/visibilityUtils.ts Outdated
const operator = visibility.operator ?? 'equals';
return {
hidden: !isPanelGroupVisible(visibility, variableValues),
description: `Shown when ${visibility.variable} ${operator} ${visibility.value.join(', ')}`,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the tooltip exposes spec keywords (Shown when phase notEquals PRD). Something more readable like is, is not, matches, does not match would be nicer for users.

}

/**
* Renders the title for a Grid section, optionally also supporting expanding

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this doc comment now sits above VisibilityMark instead of GridTitle; it should move down to GridTitle.

/**
* The group is only rendered when the selected values of the variable satisfy the operator against the values.
*/
export type PanelGroupVisibility = Visibility;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this alias adds public API without adding anything over Visibility from @perses-dev/spec; could we use Visibility directly?

title?: string;
repeatedOriginId?: PanelGroupId; // ID of the original panel group from which this repeated group is derived
repeatVariable?: string; // Optional, used for repeated panel groups
visibility?: PanelGroupVisibility;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: there is no UI to set visibility, so it can only be added through JSON/YAML. Fine for a first step, but worth stating in the PR description (or a follow-up issue).

* Layout component that arranges children in a Grid based on the definition.
*/
export function GridLayout(props: GridLayoutProps): ReactElement {
export function GridLayout(props: GridLayoutProps): ReactElement | null {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GridLayout return type is now ReactElement | null

Signed-off-by: colivi <charles.olivi@gmail.com>
A refresh keeps the group mounted when the variable already has a value. A leading (?i) is case-insensitive, the edit-mode tooltip uses plain words, and a rule without a title no longer renders an empty heading. The group field uses Visibility from the spec.

Signed-off-by: colivi <charles.olivi@gmail.com>
Signed-off-by: colivi <charles.olivi@gmail.com>
Signed-off-by: colivi <charles.olivi@gmail.com>
Keep panel-group visibility and take main's tag-link editors.

Signed-off-by: colivi <charles.olivi@gmail.com>
@shahrokni
shahrokni self-requested a review December 3, 2026 09:55
[hasViewPanel, isEditMode, updatePanelGroupLayouts],
);

// Edit mode keeps hidden groups reachable.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the condition is self-explanatory. A comment is not needed here.

Comment thread dashboards/src/utils/visibilityUtils.ts Outdated
switch (visibility.operator) {
case 'notEquals':
case 'notMatches':
return !selected.some(matchesAny);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a bug here.
The catch block returns false in case an invalid regex. Here the negation makes it true which makes it visible!

return !selected.some(matchesAny);
  try {
    return new RegExp(`^(?:${source})$`, caseInsensitive ? 'i' : '').test(value);
  } catch {
    return false;
  }

Comment thread dashboards/src/utils/visibilityUtils.ts Outdated
Comment on lines +42 to +46
const selected: string[] = [state.value ?? []].flat();
const matchesAny = (value: string): boolean =>
visibility.operator === 'matches' || visibility.operator === 'notMatches'
? visibility.value.some((pattern) => matchesPattern(pattern, value))
: visibility.value.includes(value);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When no variable value is selected, selected.some(matchesAny) returns false, causing both notEquals and notMatches to return true

Comment thread dashboards/src/utils/visibilityUtils.ts Outdated
Comment on lines +62 to +67
const operatorText: Record<NonNullable<Visibility['operator']>, string> = {
equals: 'is',
notEquals: 'is not',
matches: 'matches',
notMatches: 'does not match',
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Better to be ReadOnly

const operator = visibility.operator ?? 'equals';
return {
hidden: !isPanelGroupVisible(visibility, variableValues),
description: `Shown when ${visibility.variable} ${operatorText[operator]} ${visibility.value.join(', ')}`,

@shahrokni shahrokni Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to make the description clear for a better UX experience.
We could build the string using the Operator from the following map.

  • equals => is any of
  • notEquals => is none of
  • matches => matches any of
  • notMatches => matches none of

…ttern

An invalid pattern no longer makes notMatches show the group. An empty
selection hides the group for every operator. The edit-mode tooltip says
is any of, is none of, matches any of, and matches none of.

Signed-off-by: colivi <charles.olivi@gmail.com>
@shahrokni
shahrokni merged commit 56352fd into perses:main Dec 3, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants