refactor: string url path (#7335)

This commit is contained in:
Vamsi Krishna
2025-07-03 18:07:02 +05:30
committed by GitHub
parent 2f22ca0aea
commit 618fcf934f
2 changed files with 38 additions and 20 deletions

View File

@@ -319,14 +319,15 @@ export const copyTextToClipboard = async (text: string): Promise<void> => {
await navigator.clipboard.writeText(text);
};
/**
* @description Joins URL path segments properly, removing duplicate slashes
* @description Joins URL path segments properly, removing duplicate slashes using URL encoding
* @param {...string} segments - URL path segments to join
* @returns {string} Properly joined URL path
* @example
* joinUrlPath("/workspace", "/projects") => "/workspace/projects"
* joinUrlPath("/workspace", "projects") => "/workspace/projects"
* joinUrlPath("workspace", "projects") => "workspace/projects"
* joinUrlPath("workspace", "projects") => "/workspace/projects"
* joinUrlPath("/workspace/", "/projects/") => "/workspace/projects/"
*/
export const joinUrlPath = (...segments: string[]): string => {
@@ -336,21 +337,38 @@ export const joinUrlPath = (...segments: string[]): string => {
const validSegments = segments.filter((segment) => segment !== "");
if (validSegments.length === 0) return "";
// Join segments and normalize slashes
const joined = validSegments
.map((segment, index) => {
// Remove leading slashes from all segments except the first
if (index > 0) {
segment = segment.replace(/^\/+/, "");
}
// Remove trailing slashes from all segments except the last
if (index < validSegments.length - 1) {
segment = segment.replace(/\/+$/, "");
}
return segment;
})
.join("/");
// Process segments to normalize slashes
const processedSegments = validSegments.map((segment, index) => {
let processed = segment;
// Clean up any duplicate slashes that might have been created
return joined.replace(/\/+/g, "/");
};
// Remove leading slashes from all segments except the first
if (index > 0) {
while (processed.startsWith("/")) {
processed = processed.substring(1);
}
}
// Remove trailing slashes from all segments except the last
if (index < validSegments.length - 1) {
while (processed.endsWith("/")) {
processed = processed.substring(0, processed.length - 1);
}
}
return processed;
});
// Join segments with single slash
const joined = processedSegments.join("/");
// Use URL constructor to normalize the path and handle double slashes
try {
// Create a dummy URL to leverage browser's URL normalization
const dummyUrl = new URL(`http://example.com/${joined}`);
return dummyUrl.pathname;
} catch {
// Fallback: manually handle double slashes by splitting and filtering
const pathParts = joined.split("/").filter((part) => part !== "");
return pathParts.length > 0 ? `/${pathParts.join("/")}` : "";
}
};

View File

@@ -73,7 +73,7 @@ const SettingsSidebarNavItem = observer((props: TSettingsSidebarNavItemProps) =>
<div className={buttonClass}>{titleElement}</div>
) : (
<Link
href={joinUrlPath("/", workspaceSlug, setting.href)}
href={joinUrlPath(workspaceSlug, setting.href)}
className={buttonClass}
onClick={() => toggleSidebar(true)}
>