Skip to content

Commit

Permalink
Improve flexibility of url parsing (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry authored and mvines committed Jun 12, 2020
1 parent 82fa2ca commit 25eabc9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
3 changes: 2 additions & 1 deletion explorer/src/providers/network.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ function networkReducer(state: State, action: Action): State {
}

function initState(): State {
const networkUrlParam = findGetParameter("networkUrl");
const networkUrlParam =
findGetParameter("clusterUrl") || findGetParameter("networkUrl");

let network;
let customUrl = DEFAULT_CUSTOM_URL;
Expand Down
17 changes: 15 additions & 2 deletions explorer/src/providers/transactions.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import { TransactionSignature, Connection } from "@solana/web3.js";
import { findGetParameter } from "../utils";
import { findGetParameter, findPathSegment } from "../utils";
import { useNetwork } from "../providers/network";

export enum Status {
Expand Down Expand Up @@ -85,9 +85,22 @@ function reducer(state: State, action: Action): State {
return state;
}

function urlSignatures(): Array<string> {
const signatures: Array<string> = [];
return signatures
.concat(findGetParameter("tx")?.split(",") || [])
.concat(findGetParameter("txn")?.split(",") || [])
.concat(findGetParameter("txs")?.split(",") || [])
.concat(findGetParameter("txns")?.split(",") || [])
.concat(findGetParameter("transaction")?.split(",") || [])
.concat(findGetParameter("transactions")?.split(",") || [])
.concat(findPathSegment("transaction")?.split(",") || [])
.concat(findPathSegment("transactions")?.split(",") || []);
}

function initState(): State {
let idCounter = 0;
const signatures = findGetParameter("txs")?.split(",") || [];
const signatures = urlSignatures();
const transactions = signatures.reduce(
(transactions: Transactions, signature) => {
const id = ++idCounter;
Expand Down
14 changes: 14 additions & 0 deletions explorer/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ export function findGetParameter(parameterName: string): string | null {
return result;
}

export function findPathSegment(pathName: string): string | null {
const segments = window.location.pathname.substr(1).split("/");
if (segments.length < 2) return null;

// remove all but last two segments
segments.splice(0, segments.length - 2);

if (segments[0] === pathName) {
return segments[1];
}

return null;
}

export function assertUnreachable(x: never): never {
throw new Error("Unreachable!");
}

0 comments on commit 25eabc9

Please sign in to comment.