Skip to content

Commit

Permalink
initial router setup
Browse files Browse the repository at this point in the history
  • Loading branch information
rikukissa committed Dec 31, 2017
1 parent 55d79cb commit 28c090b
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 42 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
"emotion": "^8.0.12",
"jsx-dom": "^5.2.0",
"react-scripts-ts": "2.8.0",
"routes": "^2.1.0",
"select-dom": "^4.1.1"
},
"scripts": {
"start": "HTTPS=true react-scripts-ts start",
"start": "BROWSER=none HTTPS=true react-scripts-ts start",
"build": "react-scripts-ts build && ./post-build",
"test": "react-scripts-ts test --env=jsdom",
"eject": "react-scripts-ts eject",
Expand Down
1 change: 1 addition & 0 deletions routes.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "routes";
87 changes: 47 additions & 40 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,51 @@
import { UnauthorizedError } from "./api";
import diffSelect from "./features/diff-select";
import fadeOutUnrelatedCommits from "./features/fade-out-unrelated-commits";
import mergeWarning from "./features/merge-warning";
import parentPRSelect from "./features/parent-pr-select";
import showStackingInList from "./features/show-stacking-in-list";
import { setConfig } from "./lib/config";
import { createContext } from "./lib/context";
import { isPRView } from "./lib/location";
// import { UnauthorizedError } from "./api";
// import diffSelect from "./features/diff-select";
// import fadeOutUnrelatedCommits from "./features/fade-out-unrelated-commits";
// import mergeWarning from "./features/merge-warning";
// import parentPRSelect from "./features/parent-pr-select";
// import showStackingInList from "./features/show-stacking-in-list";
// import { setConfig } from "./lib/config";
// import { createContext } from "./lib/context";

async function run() {
if (!isPRView(document.location)) {
return;
}
const context = await createContext(document.location);
try {
await diffSelect(context);
await showStackingInList(context);
await parentPRSelect(context);
await fadeOutUnrelatedCommits(context);
await mergeWarning(context);
} catch (err) {
if (err instanceof UnauthorizedError) {
const token = window.prompt("Please enter an access token");
if (token) {
setConfig({ token });
window.location.reload();
}
}
// tslint:disable-next-line no-console
console.log(err);
}
}
// import { isPRView } from "./lib/location";
import createRouter from "./lib/router";

const observer = new MutationObserver(run);
const el = document.querySelector("#js-repo-pjax-container");
createRouter({
"/:owner/:repo/pulls": ({ repo, owner }: { repo: string; owner: string }) =>
console.log(repo, owner)
});

if (el) {
observer.observe(el, {
childList: true
});
}
// async function run() {
// if (!isPRView(document.location)) {
// return;
// }
// const context = await createContext(document.location);
// try {
// await diffSelect(context);
// await showStackingInList(context);
// await parentPRSelect(context);
// await fadeOutUnrelatedCommits(context);
// await mergeWarning(context);
// } catch (err) {
// if (err instanceof UnauthorizedError) {
// const token = window.prompt("Please enter an access token");
// if (token) {
// setConfig({ token });
// window.location.reload();
// }
// }
// // tslint:disable-next-line no-console
// console.log(err);
// }
// }

run();
// const observer = new MutationObserver(run);
// const el = document.querySelector("#js-repo-pjax-container");

// if (el) {
// observer.observe(el, {
// childList: true
// });
// }

// run();
24 changes: 24 additions & 0 deletions src/lib/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Router from "routes";
import { parse } from "url";

export default function createRouter(routes: {
[path: string]: (...args: any[]) => void;
}) {
const router = Router();

Object.keys(routes).forEach(path => {
router.addRoute(path, routes[path]);
});

const pushState = window.history.pushState;

window.history.pushState = (...args: any[]) => {
const [, , url] = args;
console.log(parse(url).pathname);

console.log(router.match(parse(url).pathname));

return pushState.apply(history, args);
};
console.log(router.match(location.pathname));
}
5 changes: 4 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"extends": [
"tslint:latest",
"tslint-config-prettier"
]
],
"rules": {
"no-submodule-imports": false
}
}
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4778,6 +4778,10 @@ ripemd160@^2.0.0, ripemd160@^2.0.1:
hash-base "^2.0.0"
inherits "^2.0.1"

routes@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/routes/-/routes-2.1.0.tgz#475571192a48f99b6c065dd926bb75e8ae83e8a2"

run-async@^2.2.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/run-async/-/run-async-2.3.0.tgz#0371ab4ae0bdd720d4166d7dfda64ff7a445a6c0"
Expand Down

0 comments on commit 28c090b

Please sign in to comment.