ZmenuAppLinks.tsx 2.49 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.
 */

17
import React from 'react';
18
import { useSelector } from 'react-redux';
19 20

import * as urlFor from 'src/shared/helpers/urlHelper';
21 22
import { parseFeatureId } from 'src/content/app/browser/browserHelper';
import { buildFocusIdForUrl } from 'src/shared/state/ens-object/ensObjectHelpers';
23 24 25 26 27 28 29 30

import {
  getBrowserActiveGenomeId,
  getBrowserActiveEnsObjectId,
  isFocusObjectPositionDefault
} from '../browserSelectors';

import { ToggleButton as ToolboxToggleButton } from 'src/shared/components/toolbox';
31
import ViewInApp, { UrlObj } from 'src/shared/components/view-in-app/ViewInApp';
32 33 34 35 36 37 38 39

import styles from './Zmenu.scss';

type Props = {
  featureId: string;
};

const ZmenuAppLinks = (props: Props) => {
40 41 42 43
  const genomeId = useSelector(getBrowserActiveGenomeId);
  const activeFeatureId = useSelector(getBrowserActiveEnsObjectId);
  const isInDefaultPosition = useSelector(isFocusObjectPositionDefault);

44
  const parsedFeatureId = parseFeatureId(props.featureId);
45

46
  if (parsedFeatureId.type !== 'gene') {
47 48 49
    return null;
  }

50 51
  const featureIdForUrl = buildFocusIdForUrl(parsedFeatureId);

52
  const getBrowserLink = () =>
53
    urlFor.browser({ genomeId, focus: featureIdForUrl });
54 55 56

  const getEntityViewerLink = () =>
    urlFor.entityViewer({
57
      genomeId,
58
      entityId: featureIdForUrl
59 60 61
    });

  const shouldShowBrowserButton =
62
    props.featureId !== activeFeatureId || !isInDefaultPosition;
63

64
  const links: UrlObj = {};
65 66

  if (shouldShowBrowserButton) {
67 68 69 70
    links['genomeBrowser'] = {
      url: getBrowserLink(),
      replaceState: true
    };
71
  }
72 73

  links['entityViewer'] = { url: getEntityViewerLink() };
74

75 76
  return (
    <div className={styles.zmenuAppLinks}>
77
      <ViewInApp links={links} />
78 79 80 81 82 83 84 85
      <ToolboxToggleButton
        className={styles.zmenuToggleFooter}
        openElement={<span>Download</span>}
      />
    </div>
  );
};

86
export default ZmenuAppLinks;