legacy UI defaults to showing all users (#1430)

This is basically a backport of #1371 to the legacy UI.

Test plan: Manual inspection verifies it's doing the right thing. `yarn
test` passes.

Part of https://discourse.sourcecred.io/t/fixup-legacy-explorer/316
This commit is contained in:
Dandelion Mané 2019-10-28 23:55:53 -06:00 committed by GitHub
parent dfc7ee8524
commit d47e6e28c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 37 deletions

View File

@ -20,7 +20,6 @@ import {
type StateTransitionMachineInterface, type StateTransitionMachineInterface,
initialState, initialState,
} from "./state"; } from "./state";
import {userNodeType} from "../../plugins/github/declaration";
const credOverviewUrl = const credOverviewUrl =
"https://discourse.sourcecred.io/t/a-gentle-introduction-to-cred/20"; "https://discourse.sourcecred.io/t/a-gentle-introduction-to-cred/20";
@ -116,7 +115,6 @@ export function createApp(
const pnd = appState.pagerankNodeDecomposition; const pnd = appState.pagerankNodeDecomposition;
pagerankTable = ( pagerankTable = (
<PagerankTable <PagerankTable
defaultNodeType={userNodeType}
weightConfig={weightConfig} weightConfig={weightConfig}
weightFileManager={weightFileManager} weightFileManager={weightFileManager}
manualWeights={this.state.weights.nodeManualWeights} manualWeights={this.state.weights.nodeManualWeights}

View File

@ -8,7 +8,6 @@ import {WeightsFileManager} from "../../weights/WeightsFileManager";
import {Graph, NodeAddress, type NodeAddressT} from "../../../core/graph"; import {Graph, NodeAddress, type NodeAddressT} from "../../../core/graph";
import type {PagerankNodeDecomposition} from "../../../analysis/pagerankNodeDecomposition"; import type {PagerankNodeDecomposition} from "../../../analysis/pagerankNodeDecomposition";
import {NodeRowList} from "./Node"; import {NodeRowList} from "./Node";
import {type NodeType} from "../../../analysis/types";
import {type PluginDeclaration} from "../../../analysis/pluginDeclaration"; import {type PluginDeclaration} from "../../../analysis/pluginDeclaration";
type PagerankTableProps = {| type PagerankTableProps = {|
@ -16,27 +15,23 @@ type PagerankTableProps = {|
+declarations: $ReadOnlyArray<PluginDeclaration>, +declarations: $ReadOnlyArray<PluginDeclaration>,
+graph: Graph, +graph: Graph,
+maxEntriesPerList: number, +maxEntriesPerList: number,
+defaultNodeType: ?NodeType,
+manualWeights: Map<NodeAddressT, number>, +manualWeights: Map<NodeAddressT, number>,
+onManualWeightsChange: (NodeAddressT, number) => void, +onManualWeightsChange: (NodeAddressT, number) => void,
+weightConfig: React$Element<typeof WeightConfig>, +weightConfig: React$Element<typeof WeightConfig>,
+weightFileManager: React$Element<typeof WeightsFileManager>, +weightFileManager: React$Element<typeof WeightsFileManager>,
|}; |};
type PagerankTableState = {| type PagerankTableState = {|
selectedNodeTypePrefix: NodeAddressT, selectedNodeTypePrefix: NodeAddressT | null,
showWeightConfig: boolean, showWeightConfig: boolean,
|}; |};
export class PagerankTable extends React.PureComponent< export class PagerankTable extends React.PureComponent<
PagerankTableProps, PagerankTableProps,
PagerankTableState PagerankTableState
> { > {
constructor(props: PagerankTableProps): void { constructor(): void {
super(); super();
const {defaultNodeType} = props;
const selectedNodeTypePrefix =
defaultNodeType != null ? defaultNodeType.prefix : NodeAddress.empty;
this.state = { this.state = {
selectedNodeTypePrefix, selectedNodeTypePrefix: null,
showWeightConfig: false, showWeightConfig: false,
}; };
} }
@ -135,6 +130,15 @@ export class PagerankTable extends React.PureComponent<
onManualWeightsChange, onManualWeightsChange,
graph, graph,
}; };
const userTypes = [].concat(...declarations.map((p) => p.userTypes));
const userPrefixes = userTypes.map((x) => x.prefix);
const filterAllUsers = (n) =>
userPrefixes.some((p) => NodeAddress.hasPrefix(n, p));
const {selectedNodeTypePrefix} = this.state;
const nodeFilter =
selectedNodeTypePrefix == null
? filterAllUsers
: (n) => NodeAddress.hasPrefix(n, selectedNodeTypePrefix);
return ( return (
<table <table
style={{ style={{
@ -155,9 +159,7 @@ export class PagerankTable extends React.PureComponent<
<tbody> <tbody>
<NodeRowList <NodeRowList
sharedProps={sharedProps} sharedProps={sharedProps}
nodes={Array.from(pnd.keys()).filter((node) => nodes={Array.from(pnd.keys()).filter(nodeFilter)}
NodeAddress.hasPrefix(node, this.state.selectedNodeTypePrefix)
)}
/> />
</tbody> </tbody>
</table> </table>

View File

@ -9,12 +9,11 @@ import {PagerankTable} from "./Table";
import {example, COLUMNS} from "./sharedTestUtils"; import {example, COLUMNS} from "./sharedTestUtils";
import {NodeRowList} from "./Node"; import {NodeRowList} from "./Node";
import {WeightConfig} from "../../weights/WeightConfig"; import {WeightConfig} from "../../weights/WeightConfig";
import {type NodeType} from "../../../analysis/types";
require("../../../webutil/testUtil").configureEnzyme(); require("../../../webutil/testUtil").configureEnzyme();
describe("explorer/legacy/pagerankTable/Table", () => { describe("explorer/legacy/pagerankTable/Table", () => {
describe("PagerankTable", () => { describe("PagerankTable", () => {
async function setup(defaultNodeType?: NodeType) { async function setup() {
const { const {
pnd, pnd,
sharedProps, sharedProps,
@ -26,7 +25,6 @@ describe("explorer/legacy/pagerankTable/Table", () => {
} = await example(); } = await example();
const element = shallow( const element = shallow(
<PagerankTable <PagerankTable
defaultNodeType={defaultNodeType}
weightConfig={weightConfig} weightConfig={weightConfig}
weightFileManager={weightFileManager} weightFileManager={weightFileManager}
pnd={pnd} pnd={pnd}
@ -122,9 +120,8 @@ describe("explorer/legacy/pagerankTable/Table", () => {
const value = option.prop("value"); const value = option.prop("value");
expect(value).not.toEqual(NodeAddress.empty); expect(value).not.toEqual(NodeAddress.empty);
const previousNodes = element.find("NodeRowList").prop("nodes"); const previousNodes = element.find("NodeRowList").prop("nodes");
expect( // No user nodes, so no nodes shown
previousNodes.every((n) => NodeAddress.hasPrefix(n, value)) expect(previousNodes).toHaveLength(0);
).toBe(false);
element.find("select").simulate("change", {target: {value}}); element.find("select").simulate("change", {target: {value}});
const actualNodes = element.find("NodeRowList").prop("nodes"); const actualNodes = element.find("NodeRowList").prop("nodes");
expect(actualNodes.every((n) => NodeAddress.hasPrefix(n, value))).toBe( expect(actualNodes.every((n) => NodeAddress.hasPrefix(n, value))).toBe(
@ -134,20 +131,7 @@ describe("explorer/legacy/pagerankTable/Table", () => {
}); });
it("filter defaults to show all if defaultNodeType not passed", async () => { it("filter defaults to show all if defaultNodeType not passed", async () => {
const {element} = await setup(); const {element} = await setup();
expect(element.state().selectedNodeTypePrefix).toEqual( expect(element.state().selectedNodeTypePrefix).toEqual(null);
NodeAddress.empty
);
});
it("selectedNodeTypePrefix defaults to provided NodeType, if available", async () => {
const nodeType: NodeType = {
name: "testNodeType",
pluralName: "testNodeTypes",
prefix: NodeAddress.fromParts(["foo"]),
defaultWeight: 1,
description: "test type",
};
const {element} = await setup(nodeType);
expect(element.state().selectedNodeTypePrefix).toEqual(nodeType.prefix);
}); });
}); });
@ -157,11 +141,10 @@ describe("explorer/legacy/pagerankTable/Table", () => {
const nrl = element.find(NodeRowList); const nrl = element.find(NodeRowList);
expect(nrl.props().sharedProps).toEqual(sharedProps); expect(nrl.props().sharedProps).toEqual(sharedProps);
}); });
it("including all nodes by default", async () => { it("including all user nodes by default", async () => {
const {element, pnd} = await setup(); const {element} = await setup();
const nrl = element.find(NodeRowList); const nrl = element.find(NodeRowList);
const expectedNodes = Array.from(pnd.keys()); expect(nrl.props().nodes).toEqual([]);
expect(nrl.props().nodes).toEqual(expectedNodes);
}); });
}); });
}); });