github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ui/src/redux/locations.ts (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  import d3 from "d3";
    12  import { createSelector } from "reselect";
    13  
    14  import * as protos from "src/js/protos";
    15  import { AdminUIState } from "src/redux/state";
    16  import { Pick } from "src/util/pick";
    17  
    18  export type ILocation = protos.cockroach.server.serverpb.LocationsResponse.ILocation;
    19  
    20  type LocationState = Pick<AdminUIState, "cachedData", "locations">;
    21  
    22  export function selectLocationsRequestStatus(state: LocationState) {
    23    return state.cachedData.locations;
    24  }
    25  
    26  export function selectLocations(state: LocationState) {
    27    if (!state.cachedData.locations.data) {
    28      return [];
    29    }
    30  
    31    return state.cachedData.locations.data.locations;
    32  }
    33  
    34  const nestLocations = d3.nest()
    35    .key((loc: ILocation) => loc.locality_key)
    36    .key((loc: ILocation) => loc.locality_value)
    37    .rollup((locations) => locations[0]) // cannot collide since ^^ is primary key
    38    .map;
    39  
    40  export interface LocationTree {
    41    [key: string]: {
    42      [value: string]: ILocation,
    43    };
    44  }
    45  
    46  export const selectLocationTree = createSelector(
    47    selectLocations,
    48    (ls: ILocation[]) => nestLocations(ls), // TSLint won't let this be point-free
    49  );