mirror of
https://github.com/status-im/sourcecred.git
synced 2025-01-14 14:46:30 +00:00
Update page title on route change
Test Plan: Navigate to `/`, then click the “Explorer” link, and note that the page title has updated. wchargin-branch: update-page-title
This commit is contained in:
parent
256582d8b3
commit
08e8494170
@ -3,10 +3,20 @@
|
||||
import React from "react";
|
||||
import {Router, browserHistory} from "react-router";
|
||||
|
||||
import {createRoutes} from "./routes";
|
||||
import {createRoutes, resolveTitleFromPath} from "./routes";
|
||||
|
||||
export default class App extends React.Component<{}> {
|
||||
render() {
|
||||
return <Router history={browserHistory} routes={createRoutes()} />;
|
||||
return (
|
||||
<Router
|
||||
history={browserHistory}
|
||||
routes={createRoutes()}
|
||||
onUpdate={function() {
|
||||
const router = this;
|
||||
const path: string = router.state.location.pathname;
|
||||
document.title = resolveTitleFromPath(path);
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -40,3 +40,19 @@ export function createRoutes() {
|
||||
</Route>
|
||||
);
|
||||
}
|
||||
|
||||
function resolveRouteFromPath(path: string): ?RouteDatum {
|
||||
const matches = (candidateRoute) => {
|
||||
const candidatePath = candidateRoute.path;
|
||||
const start = path.substring(0, candidatePath.length);
|
||||
const end = path.substring(candidatePath.length);
|
||||
return start === candidatePath && (end.length === 0 || end === "/");
|
||||
};
|
||||
return routeData.filter(matches)[0] || null;
|
||||
}
|
||||
|
||||
export function resolveTitleFromPath(path: string): string {
|
||||
const route = resolveRouteFromPath(path);
|
||||
const fallback = "SourceCred";
|
||||
return route ? route.title : fallback;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user