import React, { useState, useEffect } from 'react'; import { useParams } from 'react-router-dom'; import { Helmet } from 'react-helmet'; import localForage from 'localforage'; import { sourceLink, infoLine, ToggleDot } from './utils.js'; const VOID_ELEMENTS = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr']; function Article({ cache }) { const { id } = useParams(); if (id in cache) console.log('cache hit'); const [story, setStory] = useState(cache[id] || false); const [error, setError] = useState(''); const [pConv, setPConv] = useState([]); const [copyButtonText, setCopyButtonText] = useState('\ue92c'); useEffect(() => { localForage.getItem(id) .then( (value) => { if (value) { setStory(value); } } ); fetch('/api/' + id) .then(res => { if (!res.ok) { throw new Error(`Server responded with ${res.status} ${res.statusText}`); } return res.json(); }) .then( (result) => { setStory(result.story); localForage.setItem(id, result.story); }, (error) => { const errorMessage = `Failed to fetch new article content (ID: ${id}). Your connection may be down or the server might be experiencing issues. ${error.toString()}.`; setError(errorMessage); } ); }, [id]); const copyLink = () => { navigator.clipboard.writeText(`${story.title}:\n${window.location.href}`).then(() => { setCopyButtonText('\uea10'); setTimeout(() => setCopyButtonText('\ue92c'), 2000); }, () => { setCopyButtonText('\uea0f'); setTimeout(() => setCopyButtonText('\ue92c'), 2000); }); }; const pConvert = (n) => { setPConv(prevPConv => [...prevPConv, n]); }; const isCodeBlock = (v) => { return v.localName === 'pre' || v.localName === 'code' || (v.firstElementChild && v.firstElementChild.localName === 'code'); }; const renderNodes = (nodes, keyPrefix = '') => { return Array.from(nodes).map((v, k) => { const key = `${keyPrefix}${k}`; if (pConv.includes(key)) { return ( {v.textContent.split('\n\n').map((x, i) =>

{x}

)}
); } if (v.nodeName === '#text') { // Only wrap top-level text nodes in

if (keyPrefix === '' && v.data.trim() !== '') { return

{v.data}

; } return v.data; } if (v.nodeType !== Node.ELEMENT_NODE) { return null; } const Tag = v.localName; if (isCodeBlock(v)) { return ( ); } const props = { key: key }; if (v.hasAttributes()) { for (const attr of v.attributes) { const name = attr.name === 'class' ? 'className' : attr.name; props[name] = attr.value; } } if (VOID_ELEMENTS.includes(Tag)) { return ; } return ( {renderNodes(v.childNodes, `${key}-`)} ); }); }; const nodes = (s) => { if (s && s.text) { let div = document.createElement('div'); div.innerHTML = s.text; return div.childNodes; } return null; }; const storyNodes = nodes(story); return (
{error &&
Connection error? Click to expand.

{error}

{story &&

Loaded article from cache.

}
} {story ?
{story.title} | QotNews

{story.title}

Source: {sourceLink(story)}
{infoLine(story)} {storyNodes ?
{renderNodes(storyNodes)}
:

Problem getting article :(

}
:

Loading...

}
); } export default Article;