Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,76 +7,81 @@ import Image from "next/image";
import { PureCodePreviewPanel } from "./pureCodePreviewPanel";

interface CodePreviewPanelProps {
path: string;
repoName: string;
revisionName?: string;
path: string;
repoName: string;
revisionName?: string;
}

export const CodePreviewPanel = async ({ path, repoName, revisionName }: CodePreviewPanelProps) => {
const [fileSourceResponse, repoInfoResponse] = await Promise.all([
getFileSource({
fileName: path,
repository: repoName,
branch: revisionName,
}),
getRepoInfoByName(repoName),
]);
const [fileSourceResponse, repoInfoResponse] = await Promise.all([
getFileSource({
fileName: path,
repository: repoName,
branch: revisionName,
}),
getRepoInfoByName(repoName),
]);

if (isServiceError(fileSourceResponse) || isServiceError(repoInfoResponse)) {
return <div>Error loading file source</div>
}
if (isServiceError(fileSourceResponse) || isServiceError(repoInfoResponse)) {
return <div>Error loading file source</div>
}

const codeHostInfo = getCodeHostInfoForRepo({
codeHostType: repoInfoResponse.codeHostType,
name: repoInfoResponse.name,
displayName: repoInfoResponse.displayName,
webUrl: repoInfoResponse.webUrl,
});
const codeHostInfo = getCodeHostInfoForRepo({
codeHostType: repoInfoResponse.codeHostType,
name: repoInfoResponse.name,
displayName: repoInfoResponse.displayName,
webUrl: repoInfoResponse.webUrl,
});

// @todo: this is a hack to support linking to files for ADO. ADO doesn't support web urls with HEAD so we replace it with main. THis
// will break if the default branch is not main.
const fileWebUrl = repoInfoResponse.codeHostType === "azuredevops" && fileSourceResponse.webUrl ?
fileSourceResponse.webUrl.replace("version=GBHEAD", "version=GBmain") : fileSourceResponse.webUrl;
// @todo: this is a hack to support linking to files for ADO. ADO doesn't support web urls with HEAD so we replace it with main. THis
// will break if the default branch is not main.
const fileWebUrl = repoInfoResponse.codeHostType === "azuredevops" && fileSourceResponse.webUrl ?
fileSourceResponse.webUrl.replace("version=GBHEAD", "version=GBmain") : fileSourceResponse.webUrl;

return (
<>
<div className="flex flex-row py-1 px-2 items-center justify-between">
<PathHeader
path={path}
repo={{
name: repoName,
codeHostType: repoInfoResponse.codeHostType,
displayName: repoInfoResponse.displayName,
webUrl: repoInfoResponse.webUrl,
}}
branchDisplayName={revisionName}
/>
return (
<>
<div className="flex flex-row py-1 px-2 items-center justify-between ">
<div className="w-2/3">
<PathHeader
path={path}
repo={{
name: repoName,
codeHostType: repoInfoResponse.codeHostType,
displayName: repoInfoResponse.displayName,
webUrl: repoInfoResponse.webUrl,
}}
branchDisplayName={revisionName}
/>
</div>

{(fileWebUrl && codeHostInfo) && (
<div className="w-1/3 flex justify-end">
{(fileWebUrl && codeHostInfo) && (
<a
href={fileWebUrl}
target="_blank"
rel="noopener noreferrer"
className="flex flex-row items-center gap-4 px-2 py-0.5 rounded-md"
>
<Image
src={codeHostInfo.icon}
alt={codeHostInfo.codeHostName}
className={cn('w-4 h-4 flex-shrink-0', codeHostInfo.iconClassName)}
/>
<span className="text-xs font-medium">Open in {codeHostInfo.codeHostName}</span>
</a>
)}
</div>
</div>

<a
href={fileWebUrl}
target="_blank"
rel="noopener noreferrer"
className="flex flex-row items-center gap-2 px-2 py-0.5 rounded-md flex-shrink-0"
>
<Image
src={codeHostInfo.icon}
alt={codeHostInfo.codeHostName}
className={cn('w-4 h-4 flex-shrink-0', codeHostInfo.iconClassName)}
/>
<span className="text-sm font-medium">Open in {codeHostInfo.codeHostName}</span>
</a>
)}
</div>
<Separator />
<PureCodePreviewPanel
source={fileSourceResponse.source}
language={fileSourceResponse.language}
repoName={repoName}
path={path}
revisionName={revisionName ?? 'HEAD'}
/>
</>
)
}
<Separator />
<PureCodePreviewPanel
source={fileSourceResponse.source}
language={fileSourceResponse.language}
repoName={repoName}
path={path}
revisionName={revisionName ?? 'HEAD'}
/>

</>
)
}
64 changes: 32 additions & 32 deletions packages/web/src/app/[domain]/browse/[...path]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,44 +66,44 @@ export async function generateMetadata({ params: paramsPromise }: Props): Promis
}

interface BrowsePageProps {
params: Promise<{
path: string[];
}>;
params: Promise<{
path: string[];
}>;
}

export default async function BrowsePage(props: BrowsePageProps) {
const params = await props.params;
const params = await props.params;

const {
path: _rawPath,
} = params;
const {
path: _rawPath,
} = params;

const rawPath = _rawPath.join('/');
const { repoName, revisionName, path, pathType } = getBrowseParamsFromPathParam(rawPath);
const rawPath = _rawPath.join('/');
const { repoName, revisionName, path, pathType } = getBrowseParamsFromPathParam(rawPath);

return (
<div className="flex flex-col h-full">
<Suspense fallback={
<div className="flex flex-col w-full min-h-full items-center justify-center">
<Loader2 className="w-4 h-4 animate-spin" />
Loading...
</div>
}>
{pathType === 'blob' ? (
<CodePreviewPanel
path={path}
repoName={repoName}
revisionName={revisionName}
/>
) : (
<TreePreviewPanel
path={path}
repoName={repoName}
revisionName={revisionName}
/>
)}
</Suspense>
return (
<div className="flex flex-col h-full">
<Suspense fallback={
<div className="flex flex-col w-full min-h-full items-center justify-center">
<Loader2 className="w-4 h-4 animate-spin" />
Loading...
</div>
)
}>
{pathType === 'blob' ? (
<CodePreviewPanel
path={path}
repoName={repoName}
revisionName={revisionName}
/>
) : (
<TreePreviewPanel
path={path}
repoName={repoName}
revisionName={revisionName}
/>
)}
</Suspense>
</div>
)
}

114 changes: 59 additions & 55 deletions packages/web/src/app/[domain]/browse/browseStateProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,78 +4,82 @@ import { useNonEmptyQueryParam } from "@/hooks/useNonEmptyQueryParam";
import { createContext, useCallback, useEffect, useState } from "react";

export interface BrowseState {
selectedSymbolInfo?: {
symbolName: string;
repoName: string;
revisionName: string;
language: string;
}
isBottomPanelCollapsed: boolean;
isFileTreePanelCollapsed: boolean;
isFileSearchOpen: boolean;
activeExploreMenuTab: "references" | "definitions";
bottomPanelSize: number;
selectedSymbolInfo?: {
symbolName: string;
repoName: string;
revisionName: string;
language: string;
}
isBottomPanelCollapsed: boolean;
isChatPanelCollapsed: boolean;
isFileTreePanelCollapsed: boolean;
isFileSearchOpen: boolean;
activeExploreMenuTab: "references" | "definitions";
bottomPanelSize: number;
chatPanelSize: number;
}

const defaultState: BrowseState = {
selectedSymbolInfo: undefined,
isBottomPanelCollapsed: true,
isFileTreePanelCollapsed: false,
isFileSearchOpen: false,
activeExploreMenuTab: "references",
bottomPanelSize: 35,
selectedSymbolInfo: undefined,
isBottomPanelCollapsed: true,
isFileTreePanelCollapsed: false,
isFileSearchOpen: false,
activeExploreMenuTab: "references",
bottomPanelSize: 35,
isChatPanelCollapsed: true,
chatPanelSize: 20,
};

export const SET_BROWSE_STATE_QUERY_PARAM = "setBrowseState";

export const BrowseStateContext = createContext<{
state: BrowseState;
updateBrowseState: (state: Partial<BrowseState>) => void;
state: BrowseState;
updateBrowseState: (state: Partial<BrowseState>) => void;
}>({
state: defaultState,
updateBrowseState: () => {},
state: defaultState,
updateBrowseState: () => { },
});

interface BrowseStateProviderProps {
children: React.ReactNode;
children: React.ReactNode;
}

export const BrowseStateProvider = ({ children }: BrowseStateProviderProps) => {
const [state, setState] = useState<BrowseState>(defaultState);
const [state, setState] = useState<BrowseState>(defaultState);

const hydratedBrowseState = useNonEmptyQueryParam(SET_BROWSE_STATE_QUERY_PARAM);
const hydratedBrowseState = useNonEmptyQueryParam(SET_BROWSE_STATE_QUERY_PARAM);

const onUpdateState = useCallback((state: Partial<BrowseState>) => {
setState((prevState) => ({
...prevState,
...state,
}));
}, []);
const onUpdateState = useCallback((state: Partial<BrowseState>) => {
setState((prevState) => ({
...prevState,
...state,
}));
}, []);

useEffect(() => {
if (hydratedBrowseState) {
try {
const parsedState = JSON.parse(hydratedBrowseState) as Partial<BrowseState>;
onUpdateState(parsedState);
} catch (error) {
console.error("Error parsing hydratedBrowseState", error);
}
useEffect(() => {
if (hydratedBrowseState) {
try {
const parsedState = JSON.parse(hydratedBrowseState) as Partial<BrowseState>;
onUpdateState(parsedState);
} catch (error) {
console.error("Error parsing hydratedBrowseState", error);
}

// Remove the query param
const url = new URL(window.location.href);
url.searchParams.delete(SET_BROWSE_STATE_QUERY_PARAM);
window.history.replaceState({}, '', url.toString());
}
}, [hydratedBrowseState, onUpdateState]);
// Remove the query param
const url = new URL(window.location.href);
url.searchParams.delete(SET_BROWSE_STATE_QUERY_PARAM);
window.history.replaceState({}, '', url.toString());
}
}, [hydratedBrowseState, onUpdateState]);

return (
<BrowseStateContext.Provider
value={{
state,
updateBrowseState: onUpdateState,
}}
>
{children}
</BrowseStateContext.Provider>
);
};
return (
<BrowseStateContext.Provider
value={{
state,
updateBrowseState: onUpdateState,
}}
>
{children}
</BrowseStateContext.Provider>
);
};
Loading
Loading