diff --git a/package.json b/package.json index e924939..78493ad 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/routes.d.ts b/routes.d.ts new file mode 100644 index 0000000..8112cbe --- /dev/null +++ b/routes.d.ts @@ -0,0 +1 @@ +declare module "routes"; diff --git a/src/index.tsx b/src/index.tsx index a51fb4c..0a81ecd 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -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(); diff --git a/src/lib/router.ts b/src/lib/router.ts new file mode 100644 index 0000000..490eed6 --- /dev/null +++ b/src/lib/router.ts @@ -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)); +} diff --git a/tslint.json b/tslint.json index 09b2fba..11a7997 100644 --- a/tslint.json +++ b/tslint.json @@ -2,5 +2,8 @@ "extends": [ "tslint:latest", "tslint-config-prettier" - ] + ], + "rules": { + "no-submodule-imports": false + } } diff --git a/yarn.lock b/yarn.lock index 8f0ab5b..76f7158 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"