@@ -19,45 +19,59 @@ function getQueryParam(qs: string, paramName: string): string {
1919 return params . get ( paramName ) || '' ;
2020}
2121
22- export function getSearchQuery ( location : { search : string } ) : string {
22+ export function getSearchQuery ( location ?: { search : string } ) : string {
23+ if ( ! location ) return '' ;
2324 return getQueryParam ( location . search , 'q' ) ;
2425}
2526
26- export function getColumnsSpec ( location : { search : string } ) : string {
27+ export function getColumnsSpec ( location ?: { search : string } ) : string {
28+ if ( ! location ) return '' ;
2729 return getQueryParam ( location . search , 'columns' ) ;
2830}
2931
30- export function getColumnOptions ( location : { search : string } ) : string {
32+ export function getColumnOptions ( location ?: { search : string } ) : string {
33+ if ( ! location ) return '' ;
3134 return getQueryParam ( location . search , 'column_options' ) ;
3235}
3336
34- export function getSortSpec ( location : { search : string } ) : string {
37+ export function getSortSpec ( location ?: { search : string } ) : string {
38+ if ( ! location ) return '' ;
3539 return getQueryParam ( location . search , 'sort' ) ;
3640}
3741
38- export function getPaginationStart ( location : { search : string } ) : number {
42+ export function getPaginationStart ( location ?: { search : string } ) : number {
43+ if ( ! location ) return 0 ;
3944 return Number ( getQueryParam ( location . search , 'start' ) ) ;
4045}
4146
42- export function getWPTMetricView ( location : { search : string } ) : string {
47+ export function getWPTMetricView ( location ?: { search : string } ) : string {
48+ if ( ! location ) return '' ;
4349 return getQueryParam ( location . search , 'wpt_metric_view' ) ;
4450}
4551
46- export function getLegacySearchID ( location : { search : string } ) : string {
52+ export function getLegacySearchID ( location ?: { search : string } ) : string {
53+ if ( ! location ) return '' ;
4754 return getQueryParam ( location . search , 'search_id' ) ;
4855}
4956
50- export function getEditSavedSearch ( location : { search : string } ) : boolean {
57+ export function getEditSavedSearch ( location ?: { search : string } ) : boolean {
58+ if ( ! location ) return false ;
5159 return Boolean ( getQueryParam ( location . search , 'edit_saved_search' ) ) ;
5260}
5361
62+ export function getSubscribeToSavedSearch ( location ?: { search : string } ) : boolean {
63+ if ( ! location ) return false ;
64+ return Boolean ( getQueryParam ( location . search , 'subscribe' ) ) ;
65+ }
66+
5467export interface DateRange {
5568 start ?: Date ;
5669 end ?: Date ;
5770}
5871
5972// getDate is used to get the date range specified in the URL.
60- export function getDateRange ( location : { search : string } ) : DateRange {
73+ export function getDateRange ( location ?: { search : string } ) : DateRange {
74+ if ( ! location ) return { } ;
6175 const start = getQueryParam ( location . search , 'startDate' ) ;
6276 const end = getQueryParam ( location . search , 'endDate' ) ;
6377
@@ -68,7 +82,8 @@ export function getDateRange(location: {search: string}): DateRange {
6882}
6983
7084export const DEFAULT_ITEMS_PER_PAGE = 25 ;
71- export function getPageSize ( location : { search : string } ) : number {
85+ export function getPageSize ( location ?: { search : string } ) : number {
86+ if ( ! location ) return DEFAULT_ITEMS_PER_PAGE ;
7287 const num = Number (
7388 getQueryParam ( location . search , 'num' ) || DEFAULT_ITEMS_PER_PAGE ,
7489 ) ;
@@ -85,6 +100,7 @@ export type QueryStringOverrides = {
85100 dateRange ?: DateRange ;
86101 column_options ?: string [ ] ;
87102 edit_saved_search ?: boolean ;
103+ subscribe ?: boolean ;
88104} ;
89105
90106/* Given the router location object, return a query string with
@@ -161,6 +177,12 @@ function getContextualQueryStringParams(
161177 searchParams . set ( 'edit_saved_search' , '' + editBookmark ) ;
162178 }
163179
180+ const subscribe =
181+ 'subscribe' in overrides ? overrides . subscribe : undefined ;
182+ if ( subscribe ) {
183+ searchParams . set ( 'subscribe' , '' + subscribe ) ;
184+ }
185+
164186 return searchParams . toString ( ) ? '?' + searchParams . toString ( ) : '' ;
165187}
166188
0 commit comments