Skip to content

Commit 5e9f2ed

Browse files
pshrmnCompuIves
authored andcommitted
Keep history in one place (codesandbox#2051)
* Keep history in one place * drop type
1 parent db3fd1b commit 5e9f2ed

File tree

2 files changed

+43
-65
lines changed

2 files changed

+43
-65
lines changed

packages/common/src/components/Preview/index.tsx

Lines changed: 37 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ export type Props = {
5252

5353
type State = {
5454
frameInitialized: boolean;
55-
history: string[];
56-
historyPosition: number;
55+
url: string;
5756
urlInAddressBar: string;
58-
url: string | undefined;
57+
back: boolean;
58+
forward: boolean;
5959
showScreenshot: boolean;
6060
useFallbackDomain: boolean;
6161
};
@@ -81,12 +81,14 @@ class BasePreview extends React.Component<Props, State> {
8181
// templates that are executed in a docker container.
8282
this.serverPreview = getTemplate(props.sandbox.template).isServer;
8383

84+
const initialUrl = this.currentUrl();
85+
8486
this.state = {
8587
frameInitialized: false,
86-
history: [],
87-
historyPosition: 0,
88-
urlInAddressBar: this.currentUrl(),
89-
url: null,
88+
urlInAddressBar: initialUrl,
89+
url: initialUrl,
90+
forward: false,
91+
back: false,
9092
showScreenshot: true,
9193
useFallbackDomain: false,
9294
};
@@ -241,12 +243,13 @@ class BasePreview extends React.Component<Props, State> {
241243
}, 800);
242244
}
243245

244-
this.setState({
245-
history: [url],
246-
historyPosition: 0,
247-
urlInAddressBar: url,
248-
showScreenshot: true,
249-
});
246+
this.setState(
247+
{
248+
urlInAddressBar: url,
249+
showScreenshot: true,
250+
},
251+
() => this.handleRefresh()
252+
);
250253
};
251254

252255
handleDependenciesChange = () => {
@@ -281,7 +284,7 @@ class BasePreview extends React.Component<Props, State> {
281284
break;
282285
}
283286
case 'urlchange': {
284-
this.commitUrl(data.url, data.action, data.diff);
287+
this.commitUrl(data.url, data.back, data.forward);
285288
break;
286289
}
287290
case 'resize': {
@@ -418,25 +421,25 @@ class BasePreview extends React.Component<Props, State> {
418421
this.element.src = urlInAddressBar;
419422

420423
this.setState({
421-
history: [urlInAddressBar],
422-
historyPosition: 0,
423-
urlInAddressBar,
424+
url: urlInAddressBar,
425+
back: false,
426+
forward: false,
424427
});
425428
}
426429
};
427430

428431
handleRefresh = () => {
429-
const { history, historyPosition, urlInAddressBar } = this.state;
430-
const url = history[historyPosition] || urlInAddressBar;
432+
const { urlInAddressBar, url } = this.state;
433+
const urlToSet = url || urlInAddressBar;
431434

432435
if (this.element) {
433-
this.element.src = url || this.currentUrl();
436+
this.element.src = urlToSet || this.currentUrl();
434437
}
435438

436439
this.setState({
437-
history: [url],
438-
historyPosition: 0,
439-
urlInAddressBar: url,
440+
urlInAddressBar: urlToSet,
441+
back: false,
442+
forward: false,
440443
});
441444
};
442445

@@ -452,36 +455,13 @@ class BasePreview extends React.Component<Props, State> {
452455
});
453456
};
454457

455-
commitUrl = (url: string, action: string, diff: number) => {
456-
const { history, historyPosition } = this.state;
457-
458-
switch (action) {
459-
case 'POP':
460-
this.setState(prevState => {
461-
const newPosition = prevState.historyPosition + diff;
462-
return {
463-
historyPosition: newPosition,
464-
urlInAddressBar: url,
465-
};
466-
});
467-
break;
468-
case 'REPLACE':
469-
this.setState(prevState => ({
470-
history: [
471-
...prevState.history.slice(0, historyPosition),
472-
url,
473-
...prevState.history.slice(historyPosition + 1),
474-
],
475-
urlInAddressBar: url,
476-
}));
477-
break;
478-
default:
479-
this.setState({
480-
history: [...history.slice(0, historyPosition + 1), url],
481-
historyPosition: historyPosition + 1,
482-
urlInAddressBar: url,
483-
});
484-
}
458+
commitUrl = (url: string, back: boolean, forward: boolean) => {
459+
this.setState({
460+
urlInAddressBar: url,
461+
url,
462+
back,
463+
forward,
464+
});
485465
};
486466

487467
toggleProjectView = () => {
@@ -504,7 +484,7 @@ class BasePreview extends React.Component<Props, State> {
504484
overlayMessage,
505485
} = this.props;
506486

507-
const { historyPosition, history, urlInAddressBar } = this.state;
487+
const { urlInAddressBar, back, forward } = this.state;
508488

509489
const url = urlInAddressBar || this.currentUrl();
510490

@@ -527,13 +507,11 @@ class BasePreview extends React.Component<Props, State> {
527507
>
528508
{showNavigation && (
529509
<Navigator
530-
url={decodeURIComponent(url)}
510+
url={url}
531511
onChange={this.updateUrl}
532512
onConfirm={this.sendUrl}
533-
onBack={historyPosition > 0 ? this.handleBack : null}
534-
onForward={
535-
historyPosition < history.length - 1 ? this.handleForward : null
536-
}
513+
onBack={back ? this.handleBack : null}
514+
onForward={forward ? this.handleForward : null}
537515
onRefresh={this.handleRefresh}
538516
isProjectView={isInProjectView}
539517
toggleProjectView={

packages/sandbox-hooks/url-listeners.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { dispatch, isStandalone, listen } from 'codesandbox-api';
22

3-
function sendUrlChange(url, action, diff) {
3+
function sendUrlChange(url) {
44
dispatch({
55
type: 'urlchange',
66
url,
7-
diff,
8-
action,
7+
back: historyPosition > 0,
8+
forward: historyPosition < historyList.length - 1,
99
});
1010
}
1111

@@ -49,7 +49,7 @@ export default function setupHistoryListeners() {
4949
const oldURL = document.location.href;
5050
origHistoryProto.replaceState.call(window.history, state, '', url);
5151
const newURL = document.location.href;
52-
sendUrlChange(newURL, 'POP', delta);
52+
sendUrlChange(newURL);
5353
if (newURL.indexOf('#') === -1) {
5454
window.dispatchEvent(new PopStateEvent('popstate', { state }));
5555
} else {
@@ -78,7 +78,7 @@ export default function setupHistoryListeners() {
7878
replaceState(state, title, url) {
7979
origHistoryProto.replaceState.call(window.history, state, title, url);
8080
historyList[historyPosition] = { state, url };
81-
sendUrlChange(document.location.href, 'REPLACE');
81+
sendUrlChange(document.location.href);
8282
},
8383
});
8484

@@ -138,7 +138,7 @@ export default function setupHistoryListeners() {
138138
pushHistory(pathWithHash(document.location), null);
139139

140140
setTimeout(() => {
141-
sendUrlChange(document.location.href, 'REPLACE');
141+
sendUrlChange(document.location.href);
142142
});
143143
}
144144
return listen(handleMessage);

0 commit comments

Comments
 (0)