summaryrefslogtreecommitdiffhomepage
path: root/gui/src/renderer/components/BridgeLocations.tsx
blob: 802797eb7769ba2a99e3cf73e5e67daac8a97ce2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import * as React from 'react';
import { LiftedConstraint, RelayLocation } from '../../shared/daemon-rpc-types';
import { messages } from '../../shared/gettext';
import { IRelayLocationRedux } from '../redux/settings/reducers';
import LocationList, {
  LocationSelection,
  LocationSelectionType,
  RelayLocations,
  SpecialLocation,
  SpecialLocationIcon,
  SpecialLocations,
} from './LocationList';

export enum SpecialBridgeLocationType {
  closestToExit = 0,
}

interface IBridgeLocationsProps {
  source: IRelayLocationRedux[];
  defaultExpandedLocations?: RelayLocation[];
  selectedValue?: LiftedConstraint<RelayLocation>;
  selectedElementRef?: React.Ref<React.ReactInstance>;
  onSelect?: (value: LocationSelection<SpecialBridgeLocationType>) => void;
  onWillExpand?: (locationRect: DOMRect, expandedContentHeight: number) => void;
  onTransitionEnd?: () => void;
}

const BridgeLocations = React.forwardRef(function BridgeLocationsT(
  props: IBridgeLocationsProps,
  ref: React.Ref<LocationList<SpecialBridgeLocationType>>,
) {
  const selectedValue:
    | LocationSelection<SpecialBridgeLocationType>
    | undefined = props.selectedValue
    ? props.selectedValue === 'any'
      ? { type: LocationSelectionType.special, value: SpecialBridgeLocationType.closestToExit }
      : { type: LocationSelectionType.relay, value: props.selectedValue }
    : undefined;

  return (
    <LocationList
      ref={ref}
      defaultExpandedLocations={props.defaultExpandedLocations}
      selectedValue={selectedValue}
      selectedElementRef={props.selectedElementRef}
      onSelect={props.onSelect}>
      <SpecialLocations>
        <SpecialLocation
          icon={SpecialLocationIcon.geoLocation}
          value={SpecialBridgeLocationType.closestToExit}>
          {messages.pgettext('select-location-view', 'Closest to exit server')}
        </SpecialLocation>
      </SpecialLocations>
      <RelayLocations
        source={props.source}
        onWillExpand={props.onWillExpand}
        onTransitionEnd={props.onTransitionEnd}
      />
    </LocationList>
  );
});

export default BridgeLocations;