Skip to content

Commit 5f0bd5a

Browse files
Add useBrowsePath
1 parent f7ded19 commit 5f0bd5a

File tree

2 files changed

+75
-18
lines changed

2 files changed

+75
-18
lines changed

packages/web/src/app/[domain]/browse/hooks/useBrowseNavigation.ts

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,46 @@ export type BrowseHighlightRange = {
1313

1414
export const HIGHLIGHT_RANGE_QUERY_PARAM = 'highlightRange';
1515

16-
interface NavigateToPathOptions {
16+
export interface GetBrowsePathProps {
1717
repoName: string;
1818
revisionName?: string;
1919
path: string;
2020
pathType: 'blob' | 'tree';
2121
highlightRange?: BrowseHighlightRange;
2222
setBrowseState?: Partial<BrowseState>;
23+
domain: string;
2324
}
2425

26+
export const getBrowsePath = ({
27+
repoName,
28+
revisionName = 'HEAD',
29+
path,
30+
pathType,
31+
highlightRange,
32+
setBrowseState,
33+
domain,
34+
}: GetBrowsePathProps) => {
35+
const params = new URLSearchParams();
36+
37+
if (highlightRange) {
38+
const { start, end } = highlightRange;
39+
40+
if ('column' in start && 'column' in end) {
41+
params.set(HIGHLIGHT_RANGE_QUERY_PARAM, `${start.lineNumber}:${start.column},${end.lineNumber}:${end.column}`);
42+
} else {
43+
params.set(HIGHLIGHT_RANGE_QUERY_PARAM, `${start.lineNumber},${end.lineNumber}`);
44+
}
45+
}
46+
47+
if (setBrowseState) {
48+
params.set(SET_BROWSE_STATE_QUERY_PARAM, JSON.stringify(setBrowseState));
49+
}
50+
51+
const browsePath = `/${domain}/browse/${repoName}@${revisionName}/-/${pathType}/${path}${params.keys.length > 0 ? `?${params.toString()}` : ''}`;
52+
return browsePath;
53+
}
54+
55+
2556
export const useBrowseNavigation = () => {
2657
const router = useRouter();
2758
const domain = useDomain();
@@ -33,24 +64,18 @@ export const useBrowseNavigation = () => {
3364
pathType,
3465
highlightRange,
3566
setBrowseState,
36-
}: NavigateToPathOptions) => {
37-
const params = new URLSearchParams();
38-
39-
if (highlightRange) {
40-
const { start, end } = highlightRange;
41-
42-
if ('column' in start && 'column' in end) {
43-
params.set(HIGHLIGHT_RANGE_QUERY_PARAM, `${start.lineNumber}:${start.column},${end.lineNumber}:${end.column}`);
44-
} else {
45-
params.set(HIGHLIGHT_RANGE_QUERY_PARAM, `${start.lineNumber},${end.lineNumber}`);
46-
}
47-
}
48-
49-
if (setBrowseState) {
50-
params.set(SET_BROWSE_STATE_QUERY_PARAM, JSON.stringify(setBrowseState));
51-
}
67+
}: Omit<GetBrowsePathProps, 'domain'>) => {
68+
const browsePath = getBrowsePath({
69+
repoName,
70+
revisionName,
71+
path,
72+
pathType,
73+
highlightRange,
74+
setBrowseState,
75+
domain,
76+
});
5277

53-
router.push(`/${domain}/browse/${repoName}@${revisionName}/-/${pathType}/${path}?${params.toString()}`);
78+
router.push(browsePath);
5479
}, [domain, router]);
5580

5681
return {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use client';
2+
3+
import { useMemo } from "react";
4+
import { getBrowsePath, GetBrowsePathProps } from "./useBrowseNavigation";
5+
import { useDomain } from "@/hooks/useDomain";
6+
7+
export const useBrowsePath = ({
8+
repoName,
9+
revisionName,
10+
path,
11+
pathType,
12+
highlightRange,
13+
setBrowseState,
14+
}: Omit<GetBrowsePathProps, 'domain'>) => {
15+
const domain = useDomain();
16+
17+
const browsePath = useMemo(() => {
18+
return getBrowsePath({
19+
repoName,
20+
revisionName,
21+
path,
22+
pathType,
23+
highlightRange,
24+
setBrowseState,
25+
domain,
26+
});
27+
}, [repoName, revisionName, path, pathType, highlightRange, setBrowseState, domain]);
28+
29+
return {
30+
path: browsePath,
31+
}
32+
}

0 commit comments

Comments
 (0)