@@ -4,7 +4,43 @@ import { CodePreviewPanel } from "./components/codePreviewPanel";
44import { Loader2 } from "lucide-react" ;
55import { TreePreviewPanel } from "./components/treePreviewPanel" ;
66import { Metadata } from "next" ;
7- import { parsePathForTitle } from "@/lib/utils" ;
7+
8+ /**
9+ * Parses the URL path to generate a descriptive title.
10+ * It handles three cases:
11+ * 1. File view (`blob`): "filename.ts - owner/repo"
12+ * 2. Directory view (`tree`): "directory/ - owner/repo"
13+ * 3. Repository root: "owner/repo"
14+ *
15+ * @param path The array of path segments from Next.js params.
16+ * @returns A formatted title string.
17+ */
18+ export const parsePathForTitle = ( path : string [ ] ) : string => {
19+ const pathParam = path . join ( '/' ) ;
20+
21+ const { repoName, revisionName, path : filePath , pathType } = getBrowseParamsFromPathParam ( pathParam ) ;
22+
23+ // Build the base repository and revision string.
24+ const cleanRepoName = repoName . split ( '/' ) . slice ( 1 ) . join ( '/' ) ; // Remove the version control system prefix
25+ const repoAndRevision = `${ cleanRepoName } ${ revisionName ? ` @ ${ revisionName } ` : '' } ` ;
26+
27+ switch ( pathType ) {
28+ case 'blob' : {
29+ // For blobs, get the filename from the end of the path.
30+ const fileName = filePath . split ( '/' ) . pop ( ) || filePath ;
31+ return `${ fileName } - ${ repoAndRevision } ` ;
32+ }
33+ case 'tree' : {
34+ // If the path is empty, it's the repo root.
35+ if ( filePath === '' || filePath === '/' ) {
36+ return repoAndRevision ;
37+ }
38+ // Otherwise, show the directory path.
39+ const directoryPath = filePath . endsWith ( '/' ) ? filePath : `${ filePath } /` ;
40+ return `${ directoryPath } - ${ repoAndRevision } ` ;
41+ }
42+ }
43+ }
844
945type Props = {
1046 params : Promise < {
@@ -21,8 +57,6 @@ export async function generateMetadata({ params: paramsPromise }: Props): Promis
2157 title = parsePathForTitle ( params . path ) ;
2258
2359 } catch ( error ) {
24- // TODO: Maybe I need to look into a better way of handling this error.
25- // for now, it is just a log, fallback tab title and prevents the app from crashing.
2660 console . error ( "Failed to generate metadata title from path:" , error ) ;
2761 }
2862
0 commit comments