history works well but only for channel related pages - need to add title update hook for other pages

This commit is contained in:
Dalton 2020-01-23 22:11:06 -06:00 committed by Sean Yesmunt
parent 85d76515f1
commit f2f19b7863
5 changed files with 40 additions and 28 deletions

View file

@ -17,56 +17,58 @@ type Props = {
},
};
// determines which slice of entries should make up the back or forward button drop-downs (isBackward vs !isBackward respectively)
const sliceEntries = (currentIndex, entries, historyLength, isBackward) => {
const l = isBackward ? 0 : currentIndex + 1;
const r = isBackward ? currentIndex : historyLength;
return entries.slice(l, r);
};
const ButtonNavigation = (props: Props) => {
const { isBackward, history } = props;
const { entries, go } = history;
const currentIndex = history.index;
const historyLength = history.length;
const [showHistory, setShowHistory] = useState(false);
const makeEntrySlice = useCallback(() => {
const left = isBackward ? 1 : history.index + 1;
const right = isBackward ? history.index : history.length;
return history.entries.slice(left, right);
}, [history, isBackward]);
// creates an <li> intended for the button's <ul>
const makeItem = useCallback(
(entry, index) => {
const goArg = isBackward ? index - history.index : history.index - index;
console.log(`index: ${index}, currentIndex: ${history.index}, goArg: ${goArg}, title: ${entry.title}`);
// difference between the current index and the index of the entry
const goArg = isBackward ? index - currentIndex : index + 1;
return (
<li
key={entry.key}
onClick={() => {
setShowHistory(!showHistory);
history.go(goArg);
onMouseDown={() => {
setShowHistory(false);
go(goArg);
}}
>
{entry.title}
</li>
);
},
[isBackward, history, showHistory]
[currentIndex, isBackward, go]
);
const slicedEntries = sliceEntries(currentIndex, entries, historyLength, isBackward);
return (
<div>
<Button
className={`header__navigation-item header__navigation-item--${isBackward ? 'back' : 'forward'}`}
description={isBackward ? __('Navigate back') : __('Navigate forward')}
onBlur={() => setShowHistory(false)}
onClick={() => (isBackward ? history.goBack() : history.goForward())}
onContextMenu={e => {
setShowHistory(!showHistory);
// the following three lines prevent the regular context menu (right click menu) from appearing
e.preventDefault();
e.stopPropagation();
return false; // returning false disables the regular context menu
return false;
}}
icon={isBackward ? ICONS.ARROW_LEFT : ICONS.ARROW_RIGHT}
iconSize={18}
/>
{showHistory && (
<ul className={'header__navigaiton-dropdown'} style={{ position: 'absolute' }}>
{makeEntrySlice().map(makeItem)}
</ul>
)}
{showHistory && <ul className={'header__navigaiton-dropdown'}>{slicedEntries.map(makeItem)}</ul>}
</div>
);
};

View file

@ -115,7 +115,7 @@ const Header = (props: Props) => {
{!authHeader && (
<div className="header__navigation-arrows">
<ButtonNavigation isBackward history={history} />
<ButtonNavigation history={history} />
<ButtonNavigation isBackward={false} history={history} />
</div>
)}
{/* @endif */}

View file

@ -25,6 +25,17 @@ type Props = {
}>,
title: string,
claimIsMine: Boolean,
history: {
entries: { title: string }[],
goBack: () => void,
goForward: () => void,
index: number,
length: number,
location: { pathname: string },
push: string => void,
state: {},
replaceState: ({}, string, string) => void,
},
};
function ShowPage(props: Props) {
@ -38,8 +49,11 @@ function ShowPage(props: Props) {
title,
claimIsMine,
isSubscribed,
history,
} = props;
const { channelName, streamName } = parseURI(uri);
const { entries } = history;
const entryIndex = history.index;
const signingChannel = claim && claim.signing_channel;
const canonicalUrl = claim && claim.canonical_url;
const claimExists = claim !== null && claim !== undefined;
@ -71,10 +85,11 @@ function ShowPage(props: Props) {
document.title = IS_WEB ? SITE_TITLE : 'LBRY';
}
entries[entryIndex].title = document.title;
return () => {
document.title = IS_WEB ? SITE_TITLE : 'LBRY';
};
}, [title, channelName, streamName]);
}, [channelName, entries, entryIndex, streamName, title]);
// Don't navigate directly to repost urls
// Always redirect to the actual content

View file

@ -149,8 +149,10 @@
}
.header__navigaiton-dropdown {
position: absolute;
list-style-type: none;
background-color: var(--color-header-background);
margin-top: 5px;
li {
margin: 0;

View file

@ -102,13 +102,6 @@ history = createMemoryHistory();
history = createBrowserHistory();
// @endif
history.listen((l, a) => {
console.log('document.title: ', document.title);
console.log('history: ', history);
document.oncontextmenu = () => false;
l.title = document.title;
});
const triggerSharedStateActions = [
ACTIONS.CHANNEL_SUBSCRIBE,
ACTIONS.CHANNEL_UNSUBSCRIBE,