add internal link handling

This commit is contained in:
dank074 2021-11-19 00:44:07 -06:00
parent 97b8454c06
commit c5d6da6e97

View File

@ -1,13 +1,15 @@
import { FC, useCallback, useState } from 'react'; import { MouseEventType } from '@nitrots/nitro-renderer';
import { AddEventLinkTracker, GetConfiguration } from '../../api'; import { FC, useCallback, useEffect, useRef, useState } from 'react';
import { UseMountEffect } from '../../hooks'; import { AddEventLinkTracker, CreateLinkEvent, GetConfiguration, RemoveLinkEventTracker } from '../../api';
import { NitroCardContentView, NitroCardHeaderView, NitroCardView } from '../../layout'; import { NitroCardContentView, NitroCardHeaderView, NitroCardView } from '../../layout';
const regex = /\n\r|\n|\r/mg; const newLineRegex = /\n\r|\n|\r/mg;
const internalLinkPrefix = 'event:';
export const NitropediaView: FC<{}> = props => export const NitropediaView: FC<{}> = props =>
{ {
const [ content, setContent ] = useState<string>(null); const [ content, setContent ] = useState<string>(null);
const [ header, setHeader ] = useState<string>(''); const [ header, setHeader ] = useState<string>('');
const elementRef = useRef<HTMLDivElement>(null);
const openPage = useCallback((link: string) => const openPage = useCallback((link: string) =>
{ {
@ -15,7 +17,7 @@ export const NitropediaView: FC<{}> = props =>
.then(response => response.text()) .then(response => response.text())
.then(data => .then(data =>
{ {
const splitData = data.split(regex); const splitData = data.split(newLineRegex);
setHeader(splitData.shift()); setHeader(splitData.shift());
setContent(splitData.join('')); setContent(splitData.join(''));
}) })
@ -32,10 +34,52 @@ export const NitropediaView: FC<{}> = props =>
openPage(GetConfiguration<string>('habbopages.url') + value.join('/')); openPage(GetConfiguration<string>('habbopages.url') + value.join('/'));
}, [openPage]); }, [openPage]);
UseMountEffect(() => const onClick = useCallback((event: MouseEvent) =>
{ {
AddEventLinkTracker({ linkReceived: onLinkReceived, eventUrlPrefix: 'habbopages/' }); if(event.target instanceof HTMLAnchorElement)
}); {
const link = event.target.href;
console.log(link);
if(link.startsWith(internalLinkPrefix))
{
const internalLink = link.substring(internalLinkPrefix.length);
CreateLinkEvent(internalLink);
}
else
{
window.open(link);
}
}
},[]);
useEffect(() =>
{
const linkTracker = { linkReceived: onLinkReceived, eventUrlPrefix: 'habbopages/' };
AddEventLinkTracker(linkTracker);
return () =>
{
RemoveLinkEventTracker(linkTracker);
}
}, [onLinkReceived]);
useEffect(() =>
{
const element = elementRef.current;
if(element)
{
element.addEventListener(MouseEventType.MOUSE_CLICK, onClick);
}
return () =>
{
if(element) element.removeEventListener(MouseEventType.MOUSE_CLICK, onClick);
}
}, [onClick, content]);
if(!content) return null; if(!content) return null;
@ -43,7 +87,7 @@ export const NitropediaView: FC<{}> = props =>
<NitroCardView className="nitropedia"> <NitroCardView className="nitropedia">
<NitroCardHeaderView headerText={header} onCloseClick={() => setContent(null)}/> <NitroCardHeaderView headerText={header} onCloseClick={() => setContent(null)}/>
<NitroCardContentView> <NitroCardContentView>
{content && <div className="w-100 h-100 text-black" dangerouslySetInnerHTML={{ __html: content }} />} <div ref={elementRef} className="w-100 h-100 text-black" dangerouslySetInnerHTML={{ __html: content }} />
</NitroCardContentView> </NitroCardContentView>
</NitroCardView> </NitroCardView>
); );