urlHelper.ts 2.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

Andrey Azov's avatar
Andrey Azov committed
17 18
import queryString from 'query-string';

19
export const home = () => '/';
Andrey Azov's avatar
Andrey Azov committed
20 21
export const speciesSelector = () => '/species-selector';
export const customDownload = () => '/custom-download';
22 23

type BrowserUrlParams = {
Andrey Azov's avatar
Andrey Azov committed
24 25 26
  genomeId?: string | null;
  focus?: string | null;
  location?: string | null;
27 28
};

Andrey Azov's avatar
Andrey Azov committed
29 30
type EntityViewerUrlParams = {
  genomeId?: string | null;
31
  entityId?: string | null;
32
  view?: string | null;
33
  proteinId?: string | null;
Andrey Azov's avatar
Andrey Azov committed
34 35
};

36 37 38 39 40 41 42 43 44 45
type SpeciesPageUrlParams = {
  genomeId: string;
};

export const speciesPage = (params: SpeciesPageUrlParams) => {
  const speciesPageRootPath = '/species';

  return `${speciesPageRootPath}/${params.genomeId}`;
};

46
export const browser = (params?: BrowserUrlParams) => {
Andrey Azov's avatar
Andrey Azov committed
47
  const browserRootPath = '/genome-browser';
Andrey Azov's avatar
Andrey Azov committed
48
  if (params) {
Andrey Azov's avatar
Andrey Azov committed
49
    const path = `${browserRootPath}/${params.genomeId}`;
50 51
    // NOTE: if a parameter passed to queryString is null, it will still get into query;
    // so assign it to undefined in order to omit it from the query
Andrey Azov's avatar
Andrey Azov committed
52 53
    const query = queryString.stringify(
      {
54 55
        focus: params.focus || undefined,
        location: params.location || undefined
Andrey Azov's avatar
Andrey Azov committed
56 57 58 59 60 61
      },
      {
        encode: false
      }
    );
    return query ? `${path}?${query}` : path;
62
  } else {
Andrey Azov's avatar
Andrey Azov committed
63
    return browserRootPath;
64 65
  }
};
Andrey Azov's avatar
Andrey Azov committed
66 67 68 69 70 71

export const entityViewer = (params?: EntityViewerUrlParams) => {
  if (!params?.genomeId && params?.entityId) {
    // this should never happen; this combination doesn't make sense
    throw 'Malformed Entity Viewer url';
  }
72

Andrey Azov's avatar
Andrey Azov committed
73 74
  const genomeId = params?.genomeId || '';
  const entityId = params?.entityId || '';
Andrey Azov's avatar
Andrey Azov committed
75 76 77 78 79 80 81
  let path = '/entity-viewer';
  if (genomeId) {
    path += `/${genomeId}`;
  }
  if (entityId) {
    path += `/${entityId}`;
  }
82 83
  const query = queryString.stringify(
    {
84 85
      view: params?.view || undefined,
      protein_id: params?.proteinId || undefined
86 87 88 89 90
    },
    {
      encode: false
    }
  );
Andrey Azov's avatar
Andrey Azov committed
91

92
  return query ? `${path}?${query}` : path;
Andrey Azov's avatar
Andrey Azov committed
93
};