From 443115ac0f745aaca2d19e3af034a2f9ca1a3743 Mon Sep 17 00:00:00 2001 From: "Tanner Collin (aider)" Date: Mon, 7 Jul 2025 18:01:06 +0000 Subject: [PATCH] refactor: Refactor Feed component to functional with hooks --- webclient/src/Feed.js | 87 +++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 49 deletions(-) diff --git a/webclient/src/Feed.js b/webclient/src/Feed.js index a498a80..1b3e737 100644 --- a/webclient/src/Feed.js +++ b/webclient/src/Feed.js @@ -1,28 +1,22 @@ -import React from 'react'; +import React, { useState, useEffect } from 'react'; import { Link } from 'react-router-dom'; import { Helmet } from 'react-helmet'; import localForage from 'localforage'; import { sourceLink, infoLine, logos } from './utils.js'; -class Feed extends React.Component { - constructor(props) { - super(props); +function Feed({ updateCache }) { + const [stories, setStories] = useState(() => JSON.parse(localStorage.getItem('stories')) || false); + const [error, setError] = useState(false); - this.state = { - stories: JSON.parse(localStorage.getItem('stories')) || false, - error: false, - }; - } - - componentDidMount() { + useEffect(() => { fetch('/api') .then(res => res.json()) .then( (result) => { - const updated = !this.state.stories || this.state.stories[0].id !== result.stories[0].id; + const updated = !stories || stories[0].id !== result.stories[0].id; console.log('updated:', updated); - this.setState({ stories: result.stories }); + setStories(result.stories); localStorage.setItem('stories', JSON.stringify(result.stories)); if (updated) { @@ -33,53 +27,48 @@ class Feed extends React.Component { .then(result => { localForage.setItem(x.id, result.story) .then(console.log('preloaded', x.id, x.title)); - this.props.updateCache(x.id, result.story); + updateCache(x.id, result.story); }, error => {} ); }); } }, (error) => { - this.setState({ error: true }); + setError(true); } ); - } + }, [updateCache]); - render() { - const stories = this.state.stories; - const error = this.state.error; + return ( +
+ + QotNews + + + {error &&

Connection error?

} + {stories ? +
+ {stories.map(x => +
+
+ + source logo {x.title} + - return ( -
- - QotNews - - - {error &&

Connection error?

} - {stories ? -
- {stories.map(x => -
-
- - source logo {x.title} - - - - ({sourceLink(x)}) - -
- - {infoLine(x)} + + ({sourceLink(x)}) +
- )} -
- : -

loading...

- } -
- ); - } + + {infoLine(x)} +
+ )} +
+ : +

loading...

+ } +
+ ); } export default Feed;