feat: refactor parsePathForTitle to utilize getBrowseParamsFromPathParam for cleaner code.

This commit is contained in:
Prateek Singh 2025-10-13 22:33:01 +05:30
parent af915f7eae
commit 785a077eff

View file

@ -17,6 +17,7 @@ import { ErrorCode } from "./errorCodes";
import { NextRequest } from "next/server"; import { NextRequest } from "next/server";
import { Org } from "@sourcebot/db"; import { Org } from "@sourcebot/db";
import { OrgMetadata, orgMetadataSchema } from "@/types"; import { OrgMetadata, orgMetadataSchema } from "@/types";
import { getBrowseParamsFromPathParam } from "@/app/[domain]/browse/hooks/utils";
export function cn(...inputs: ClassValue[]) { export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs)) return twMerge(clsx(inputs))
@ -499,50 +500,31 @@ export const isHttpError = (error: unknown, status: number): boolean => {
* @returns A formatted title string. * @returns A formatted title string.
*/ */
export const parsePathForTitle = (path: string[]): string => { export const parsePathForTitle = (path: string[]): string => {
const delimiterIndex = path.indexOf('-'); const pathParam = path.join('/');
if (delimiterIndex === -1 || delimiterIndex === 0) {
return 'Browse';
}
const repoParts = path.slice(1, delimiterIndex); const { repoName, revisionName, path: filePath, pathType } = getBrowseParamsFromPathParam(pathParam);
if (repoParts.length === 0) return 'Browse';
const lastPart = decodeURIComponent(repoParts.pop()!); // Build the base repository and revision string.
const [repoNamePart, revision = ''] = lastPart.split('@'); const cleanRepoName = repoName.split('/').slice(1).join('/'); // Remove the version control system prefix
const ownerParts = repoParts; const repoAndRevision = `${cleanRepoName}${revisionName ? ` @ ${revisionName}` : ''}`;
const fullRepoName = [...ownerParts, repoNamePart].join('/');
const repoAndRevision = `${fullRepoName}${revision ? ` @ ${revision}` : ''}`;
// Check for file (`blob`) or directory (`tree`) view
const blobIndex = path.indexOf('blob');
const treeIndex = path.indexOf('tree');
// Case 1: Viewing a file
if (blobIndex !== -1 && path.length > blobIndex + 1) {
const encodedFilePath = path[blobIndex + 1];
const filePath = decodeURIComponent(encodedFilePath);
switch (pathType) {
case 'blob': {
// For blobs, get the filename from the end of the path.
const fileName = filePath.split('/').pop() || filePath; const fileName = filePath.split('/').pop() || filePath;
// Return a title like: "agents.ts - sourcebot-dev/sourcebot @ HEAD"
return `${fileName} - ${repoAndRevision}`; return `${fileName} - ${repoAndRevision}`;
} }
case 'tree': {
// Case 2: Viewing a directory // If the path is empty, it's the repo root.
if (treeIndex !== -1 && path.length > treeIndex + 1) { if (filePath === '' || filePath === '/') {
const encodedDirPath = path[treeIndex + 1];
const dirPath = decodeURIComponent(encodedDirPath);
// If we're at the root of the tree, just show the repo name
if (dirPath === '/' || dirPath === '') {
return repoAndRevision; return repoAndRevision;
} }
// Otherwise, show the directory path.
// Otherwise, show the directory path const directoryPath = filePath.endsWith('/') ? filePath : `${filePath}/`;
// Return a title like: "client/src/store/ - sourcebot-dev/sourcebot @ HEAD" return `${directoryPath} - ${repoAndRevision}`;
return `${dirPath.endsWith('/') ? dirPath : dirPath + '/'} - ${repoAndRevision}`;
} }
default:
// Case 3: Fallback to the repository root // Fallback to just the repository name.
return repoAndRevision; return repoAndRevision;
}
} }