@@ -13,15 +13,46 @@ export type BrowseHighlightRange = {
1313
1414export 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+
2556export 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 {
0 commit comments