Files
Oleg Isonen 9f497a3513 feat: Redirects improvements (#5619)
Show a warning banner when a redirect path matches the current page
path, indicating the redirect will override access to the page.

Changes:
- Add PanelBanner component with warning variant to page settings
- Implement findMatchingRedirect utility to detect redirect conflicts
- Show contextual warning with link to Redirects settings section
- Add comprehensive tests for redirect conflict detection

Code quality:
- Replace 'return undefined' with 'return' per style guide
- Use 'return null' for React components returning nothing
2026-02-19 11:26:07 +01:00

43 lines
1.3 KiB
TypeScript

import { rawTheme, theme, type CSS } from "@webstudio-is/design-system";
import { getPagePath, type Pages, type PageRedirect } from "@webstudio-is/sdk";
import { matchPathnamePattern } from "~/builder/shared/url-pattern";
export const leftPanelWidth = rawTheme.spacing[26];
export const rightPanelWidth = rawTheme.spacing[35];
export const sectionSpacing: CSS = {
paddingInline: theme.panel.paddingInline,
};
export const getExistingRoutePaths = (pages?: Pages): Set<string> => {
const paths: Set<string> = new Set();
if (pages === undefined) {
return paths;
}
for (const page of pages.pages) {
const pagePath = getPagePath(page.id, pages);
if (pagePath === undefined) {
continue;
}
paths.add(pagePath);
}
return paths;
};
/**
* Find a redirect that would match the given page path.
* Uses URLPattern for proper pattern matching (wildcards, dynamic segments).
*/
export const findMatchingRedirect = (
pagePath: string,
redirects: Array<PageRedirect>
): PageRedirect | undefined => {
for (const redirect of redirects) {
// matchPathnamePattern returns matched groups if pattern matches, undefined otherwise
const match = matchPathnamePattern(redirect.old, pagePath);
if (match !== undefined) {
return redirect;
}
}
};