Skip to content

Commit

Permalink
[wip] try to fix state sync
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdover committed Feb 5, 2020
1 parent f5251b5 commit 0798810
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
13 changes: 5 additions & 8 deletions examples/state_containers_examples/public/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { AppMountParameters } from 'kibana/public';
import ReactDOM from 'react-dom';
import React from 'react';
import { createHashHistory, createBrowserHistory } from 'history';
import { createHashHistory } from 'history';
import { TodoAppPage } from './todo';

export interface AppOptions {
Expand All @@ -35,13 +35,10 @@ export enum History {
}

export const renderApp = (
{ appBasePath, element }: AppMountParameters,
{ appBasePath, element, history: platformHistory }: AppMountParameters,
{ appInstanceId, appTitle, historyType }: AppOptions
) => {
const history =
historyType === History.Browser
? createBrowserHistory({ basename: appBasePath })
: createHashHistory();
const history = historyType === History.Browser ? platformHistory : createHashHistory();
ReactDOM.render(
<TodoAppPage
history={history}
Expand All @@ -54,8 +51,8 @@ export const renderApp = (
const currentAppUrl = stripTrailingSlash(history.createHref(history.location));
if (historyType === History.Browser) {
// browser history
const basePath = stripTrailingSlash(appBasePath);
return currentAppUrl === basePath && !history.location.search && !history.location.hash;
// const basePath = stripTrailingSlash(appBasePath);
return currentAppUrl === '' && !history.location.search && !history.location.hash;
} else {
// hashed history
return currentAppUrl === '#' && !history.location.search;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export const createKbnUrlControls = (
let shouldReplace = true;

function updateUrl(newUrl: string, replace = false): string | undefined {
const currentUrl = getCurrentUrl();
const currentUrl = getCurrentUrl(history);
if (newUrl === currentUrl) return undefined; // skip update

const historyPath = getRelativeToHistoryPath(newUrl, history);
Expand All @@ -164,7 +164,7 @@ export const createKbnUrlControls = (
history.push(historyPath);
}

return getCurrentUrl();
return getCurrentUrl(history);
}

// queue clean up
Expand All @@ -186,7 +186,10 @@ export const createKbnUrlControls = (

function getPendingUrl() {
if (updateQueue.length === 0) return undefined;
const resultUrl = updateQueue.reduce((url, nextUpdate) => nextUpdate(url), getCurrentUrl());
const resultUrl = updateQueue.reduce(
(url, nextUpdate) => nextUpdate(url),
getCurrentUrl(history)
);

return resultUrl;
}
Expand Down Expand Up @@ -234,8 +237,9 @@ export function getRelativeToHistoryPath(absoluteUrl: string, history: History):
const stripLeadingHash = (_: string) => (_.charAt(0) === '#' ? _.substr(1) : _);
const stripTrailingSlash = (_: string) =>
_.charAt(_.length - 1) === '/' ? _.substr(0, _.length - 1) : _;
const baseName = stripLeadingHash(stripTrailingSlash(history.createHref({})));
return path.startsWith(baseName) ? path.substr(baseName.length) : path;
// const baseName = stripLeadingHash(stripTrailingSlash(history.createHref({})));
// return path.startsWith(baseName) ? path.substr(baseName.length) : path;
return stripLeadingHash(stripTrailingSlash(path));
}
const isHashHistory = history.createHref({}).includes('#');
const parsedUrl = isHashHistory ? parseUrlHash(absoluteUrl)! : parseUrl(absoluteUrl);
Expand Down
7 changes: 4 additions & 3 deletions src/plugins/kibana_utils/public/state_management/url/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
*/

import { parse as _parseUrl } from 'url';
import { History } from 'history';

export const parseUrl = (url: string) => _parseUrl(url, true);
export const parseUrlHash = (url: string) => {
const hash = parseUrl(url).hash;
return hash ? parseUrl(hash.slice(1)) : null;
};
export const getCurrentUrl = () => window.location.href;
export const parseCurrentUrl = () => parseUrl(getCurrentUrl());
export const parseCurrentUrlHash = () => parseUrlHash(getCurrentUrl());
export const getCurrentUrl = (history: History) => history.createHref(history.location);
export const parseCurrentUrl = (history: History) => parseUrl(getCurrentUrl(history));
export const parseCurrentUrlHash = (history: History) => parseUrlHash(getCurrentUrl(history));

0 comments on commit 0798810

Please sign in to comment.