From c5d6da6e97212536c87bee480281133600e976c6 Mon Sep 17 00:00:00 2001 From: dank074 Date: Fri, 19 Nov 2021 00:44:07 -0600 Subject: [PATCH] add internal link handling --- src/views/nitropedia/NitropediaView.tsx | 62 +++++++++++++++++++++---- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/src/views/nitropedia/NitropediaView.tsx b/src/views/nitropedia/NitropediaView.tsx index 15f6e6fb..823fe466 100644 --- a/src/views/nitropedia/NitropediaView.tsx +++ b/src/views/nitropedia/NitropediaView.tsx @@ -1,13 +1,15 @@ -import { FC, useCallback, useState } from 'react'; -import { AddEventLinkTracker, GetConfiguration } from '../../api'; -import { UseMountEffect } from '../../hooks'; +import { MouseEventType } from '@nitrots/nitro-renderer'; +import { FC, useCallback, useEffect, useRef, useState } from 'react'; +import { AddEventLinkTracker, CreateLinkEvent, GetConfiguration, RemoveLinkEventTracker } from '../../api'; 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 => { const [ content, setContent ] = useState(null); const [ header, setHeader ] = useState(''); + const elementRef = useRef(null); const openPage = useCallback((link: string) => { @@ -15,7 +17,7 @@ export const NitropediaView: FC<{}> = props => .then(response => response.text()) .then(data => { - const splitData = data.split(regex); + const splitData = data.split(newLineRegex); setHeader(splitData.shift()); setContent(splitData.join('')); }) @@ -32,10 +34,52 @@ export const NitropediaView: FC<{}> = props => openPage(GetConfiguration('habbopages.url') + value.join('/')); }, [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; @@ -43,7 +87,7 @@ export const NitropediaView: FC<{}> = props => setContent(null)}/> - {content &&
} +
);