[BUGFIX] Expand "$var" inside URL-encoded Explore panel links - #285
Conversation
344cb25 to
09173ac
Compare
There was a problem hiding this comment.
🟡 Changes recommended
URL reconstruction can change link targets and discard repeated query parameters.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Fixes variable expansion inside URL-encoded Explore panel links.
Changes:
- Adds URL-aware variable replacement with unit tests.
- Integrates replacement into rendered dashboard and panel links.
- Exports the new utility.
File summaries
| File | Description |
|---|---|
dashboards/src/utils/replaceVariablesInUrl.ts |
Adds URL query-variable expansion. |
dashboards/src/utils/replaceVariablesInUrl.test.ts |
Tests URL substitution behavior. |
dashboards/src/utils/index.ts |
Exports the utility. |
dashboards/src/components/LinksDisplay/LinksDisplay.tsx |
Applies the utility to rendered links. |
Review details
- Files reviewed: 4/4 changed files
- Comments generated: 3
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
7d44efd to
6bba7f5
Compare
| const isAbsolute = /^https?:\/\//i.test(result); | ||
| const isProtocolRelative = result.startsWith('//'); | ||
| // Skip non-http schemes (mailto:, etc.) — only expand via step 1. | ||
| if (!isAbsolute && !isProtocolRelative && /^[a-z][a-z0-9+.-]*:/i.test(result)) { |
There was a problem hiding this comment.
Improvement
For consistency and readability, could you please keep the result following statement in a descriptively named variable?
/^[a-z][a-z0-9+.-]*:/i.test(result)| const parseInput = isProtocolRelative ? `http:${result}` : result; | ||
| const u = new URL(parseInput, isAbsolute || isProtocolRelative ? undefined : 'http://perses.local'); |
There was a problem hiding this comment.
Improvement
parsedUrl variable name would be better.
| u.searchParams.delete(key); | ||
| for (const v of replacedValues) { | ||
| u.searchParams.append(key, v); | ||
| } | ||
| } |
There was a problem hiding this comment.
Suggestion
I think there might be a chance that the order of searchParams change. Ideally this should not happen.
Let's add a unit test as well.
| const out = replaceVariablesInUrl('?data=%24var', vars); | ||
| expect(out.startsWith('?')).toBe(true); | ||
| expect(out).toContain('my-value'); | ||
| expect(out).not.toContain('%24var'); |
There was a problem hiding this comment.
Improvement
Instead of 3 different expect statements, would it possible to simply compare it with the final string?
| it('leaves absolute URLs absolute', () => { | ||
| const url = 'https://example.com/explore?q=$var'; | ||
| const out = replaceVariablesInUrl(url, vars); | ||
| expect(out).toMatch(/^https:\/\/example\.com\/explore\?q=my-value/); | ||
| }); |
There was a problem hiding this comment.
d8b5d07 to
554b57f
Compare
replaceVariables only matched unencoded $name. Explore deep-links put PromQL in data= where $var is percent-encoded. - replaceVariablesInUrl next to replaceVariables - useReplaceVariablesInUrl hook (mirror useReplaceVariablesInString) - LinksDisplay uses the hook for all dashboard/panel link hrefs - rebuild query pairs in original order; preserve relative path prefix Signed-off-by: colivi <charles.olivi@gmail.com>
554b57f to
713feff
Compare
[BUGFIX] Expand "$var" inside URL-encoded Explore panel links
Description
Panel links that deep-link into Explore embed PromQL inside the "data=" query parameter. Dashboard template variables such as "$var" are percent-encoded there as "%24var".
"LinksDisplay" already called "useReplaceVariablesInString(link.url)", but only on the raw URL string. "VARIABLE_REGEX" looks for "$(\w+)", so it never matches "%24var". The Explore view then runs PromQL with literal "$var" and returns no series.
Fix: add "replaceVariablesInUrl":
Files
Screenshots
N/A (behavior of existing link menu; no visual layout change).
Before: Explore query contained literal "stack=~"$var"" (empty result).
After: same link opens Explore with the current dashboard variable value substituted inside "data=".
Checklist
following "catalog_entry" values: "FEATURE", "ENHANCEMENT", "BUGFIX", "BREAKINGCHANGE", "DOC","IGNORE".
UI Changes
(No layout change; unit tests cover the URL substitution. Happy to add a short before/after Explore screencast if preferred.)
(Covered by unit tests on "replaceVariablesInUrl"; no new E2E — pure string/URL logic, no time or network.)