mirror of
https://github.com/sourcebot-dev/sourcebot.git
synced 2025-12-12 20:35:24 +00:00
Compare commits
3 commits
a00851dcc5
...
ad732e4d34
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ad732e4d34 | ||
|
|
785a077eff | ||
|
|
af915f7eae |
2 changed files with 28 additions and 46 deletions
|
|
@ -7,22 +7,23 @@ import { Metadata } from "next";
|
||||||
import { parsePathForTitle} from "@/lib/utils";
|
import { parsePathForTitle} from "@/lib/utils";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
params: {
|
params: Promise<{
|
||||||
domain: string;
|
domain: string;
|
||||||
path: string[];
|
path: string[];
|
||||||
};
|
}>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function generateMetadata({ params }: Props): Promise<Metadata> {
|
export async function generateMetadata({ params: paramsPromise }: Props): Promise<Metadata> {
|
||||||
let title = 'Browse'; // Default Fallback
|
let title = 'Browse'; // Default Fallback
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const params = await paramsPromise;
|
||||||
title = parsePathForTitle(params.path);
|
title = parsePathForTitle(params.path);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// TODO: Maybe I need to look into a better way of handling this error.
|
// TODO: Maybe I need to look into a better way of handling this error.
|
||||||
// for now, it is just a log, fallback tab title and prevents the app from crashing.
|
// for now, it is just a log, fallback tab title and prevents the app from crashing.
|
||||||
console.error("Failed to generate metadata title from path:", params.path, error);
|
console.error("Failed to generate metadata title from path:", error);
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
@ -44,7 +45,6 @@ export default async function BrowsePage(props: BrowsePageProps) {
|
||||||
} = params;
|
} = params;
|
||||||
|
|
||||||
const rawPath = _rawPath.join('/');
|
const rawPath = _rawPath.join('/');
|
||||||
console.log("rawPath:", rawPath);
|
|
||||||
const { repoName, revisionName, path, pathType } = getBrowseParamsFromPathParam(rawPath);
|
const { repoName, revisionName, path, pathType } = getBrowseParamsFromPathParam(rawPath);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue