github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/webapp/javascript/pages/formatTitle.ts (about)

     1  import { Query } from '@webapp/models/query';
     2  
     3  /**
     4   * takes a page name and 2 optional queries
     5   * handling it appropriately when they are not preset
     6   * and returns only the page title if no query is set
     7   */
     8  export function formatTitle(
     9    pageName: string,
    10    leftQuery?: Query,
    11    rightQuery?: Query
    12  ) {
    13    const separator = ' | ';
    14  
    15    if (leftQuery && rightQuery) {
    16      return [pageName, formatTwoQueries(leftQuery, rightQuery)]
    17        .filter(Boolean)
    18        .join(separator);
    19    }
    20  
    21    if (leftQuery) {
    22      return [pageName, leftQuery].filter(Boolean).join(separator);
    23    }
    24  
    25    if (rightQuery) {
    26      return [pageName, rightQuery].filter(Boolean).join(separator);
    27    }
    28  
    29    // None of them is defined, this may happen when there's no query in the URL
    30    return pageName;
    31  }
    32  
    33  /** formatTwoQueries assumes they both are defined and non empty */
    34  function formatTwoQueries(leftQuery: Query, rightQuery: Query) {
    35    const separator = ' and ';
    36    if (leftQuery === rightQuery) {
    37      return leftQuery;
    38    }
    39  
    40    return [leftQuery, rightQuery].join(separator);
    41  }