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

View file

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

View file

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

View file

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

View file

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