go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/ui/src/common/components/params_pager/params_pager_utils.ts (about)

     1  // Copyright 2024 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  const DEFAULT_PAGE_SIZE = 50;
    16  
    17  export function getPageSize(params: URLSearchParams) {
    18    return Number(params.get('limit')) || DEFAULT_PAGE_SIZE;
    19  }
    20  
    21  export function pageSizeUpdater(newPageSize: number) {
    22    return (params: URLSearchParams) => {
    23      const searchParams = new URLSearchParams(params);
    24      if (newPageSize === DEFAULT_PAGE_SIZE) {
    25        searchParams.delete('limit');
    26      } else {
    27        searchParams.set('limit', String(newPageSize));
    28      }
    29      return searchParams;
    30    };
    31  }
    32  
    33  export function getPageToken(params: URLSearchParams) {
    34    return params.get('cursor') || '';
    35  }
    36  
    37  export function pageTokenUpdater(newPageToken: string) {
    38    return (params: URLSearchParams) => {
    39      const searchParams = new URLSearchParams(params);
    40      if (!newPageToken) {
    41        searchParams.delete('cursor');
    42      } else {
    43        searchParams.set('cursor', newPageToken);
    44      }
    45      return searchParams;
    46    };
    47  }