Skip to content

Commit 785a077

Browse files
committed
feat: refactor parsePathForTitle to utilize getBrowseParamsFromPathParam for cleaner code.
1 parent af915f7 commit 785a077

File tree

1 file changed

+23
-41
lines changed

1 file changed

+23
-41
lines changed

packages/web/src/lib/utils.ts

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { ErrorCode } from "./errorCodes";
1717
import { NextRequest } from "next/server";
1818
import { Org } from "@sourcebot/db";
1919
import { OrgMetadata, orgMetadataSchema } from "@/types";
20+
import { getBrowseParamsFromPathParam } from "@/app/[domain]/browse/hooks/utils";
2021

2122
export function cn(...inputs: ClassValue[]) {
2223
return twMerge(clsx(inputs))
@@ -499,50 +500,31 @@ export const isHttpError = (error: unknown, status: number): boolean => {
499500
* @returns A formatted title string.
500501
*/
501502
export const parsePathForTitle = (path: string[]): string => {
502-
const delimiterIndex = path.indexOf('-');
503-
if (delimiterIndex === -1 || delimiterIndex === 0) {
504-
return 'Browse';
505-
}
506-
507-
const repoParts = path.slice(1, delimiterIndex);
508-
if (repoParts.length === 0) return 'Browse';
509-
510-
const lastPart = decodeURIComponent(repoParts.pop()!);
511-
const [repoNamePart, revision = ''] = lastPart.split('@');
512-
const ownerParts = repoParts;
513-
const fullRepoName = [...ownerParts, repoNamePart].join('/');
514-
const repoAndRevision = `${fullRepoName}${revision ? ` @ ${revision}` : ''}`;
503+
const pathParam = path.join('/');
515504

516-
// Check for file (`blob`) or directory (`tree`) view
517-
const blobIndex = path.indexOf('blob');
518-
const treeIndex = path.indexOf('tree');
505+
const { repoName, revisionName, path: filePath, pathType } = getBrowseParamsFromPathParam(pathParam);
519506

520-
// Case 1: Viewing a file
521-
if (blobIndex !== -1 && path.length > blobIndex + 1) {
522-
const encodedFilePath = path[blobIndex + 1];
523-
const filePath = decodeURIComponent(encodedFilePath);
524-
525-
const fileName = filePath.split('/').pop() || filePath;
526-
527-
// Return a title like: "agents.ts - sourcebot-dev/sourcebot @ HEAD"
528-
return `${fileName} - ${repoAndRevision}`;
529-
}
507+
// Build the base repository and revision string.
508+
const cleanRepoName = repoName.split('/').slice(1).join('/'); // Remove the version control system prefix
509+
const repoAndRevision = `${cleanRepoName}${revisionName ? ` @ ${revisionName}` : ''}`;
530510

531-
// Case 2: Viewing a directory
532-
if (treeIndex !== -1 && path.length > treeIndex + 1) {
533-
const encodedDirPath = path[treeIndex + 1];
534-
const dirPath = decodeURIComponent(encodedDirPath);
535-
536-
// If we're at the root of the tree, just show the repo name
537-
if (dirPath === '/' || dirPath === '') {
538-
return repoAndRevision;
511+
switch (pathType) {
512+
case 'blob': {
513+
// For blobs, get the filename from the end of the path.
514+
const fileName = filePath.split('/').pop() || filePath;
515+
return `${fileName} - ${repoAndRevision}`;
539516
}
540-
541-
// Otherwise, show the directory path
542-
// Return a title like: "client/src/store/ - sourcebot-dev/sourcebot @ HEAD"
543-
return `${dirPath.endsWith('/') ? dirPath : dirPath + '/'} - ${repoAndRevision}`;
517+
case 'tree': {
518+
// If the path is empty, it's the repo root.
519+
if (filePath === '' || filePath === '/') {
520+
return repoAndRevision;
521+
}
522+
// Otherwise, show the directory path.
523+
const directoryPath = filePath.endsWith('/') ? filePath : `${filePath}/`;
524+
return `${directoryPath} - ${repoAndRevision}`;
525+
}
526+
default:
527+
// Fallback to just the repository name.
528+
return repoAndRevision;
544529
}
545-
546-
// Case 3: Fallback to the repository root
547-
return repoAndRevision;
548530
}

0 commit comments

Comments
 (0)