diff --git a/src/components/HashtagTool/index.js b/src/components/HashtagTool/index.js index 679c9f7..00cf021 100644 --- a/src/components/HashtagTool/index.js +++ b/src/components/HashtagTool/index.js @@ -49,7 +49,7 @@ export function useResetScrollOnFilterChange(filterTags) { export function HashtagTool() { const { notes, LoadMoreMedia, isLoading, filterTags } = useHashTagContext(); - console.log('notes are ', notes); + console.log('notes are ', notes, ' with filter tags ', filterTags); const [newPostModal, setNewPostModal] = useState(false); const [loadingMorePosts, setLoadingMorePosts] = useState(false); const [showMemeEditor, setShowMemeEditor] = useState(false); diff --git a/src/components/Posts/index.js b/src/components/Posts/index.js index 931b9b9..5b295e2 100644 --- a/src/components/Posts/index.js +++ b/src/components/Posts/index.js @@ -271,7 +271,6 @@ export function getLocalLikeCountForPost(postId) { function Posts(props) { const mediaLinks = extractLinksFromText(props.note.content); - console.log("note's profile received: ", props.note.profile); const [votesCount, setVotesCount] = useState(0); const commentCount = useState( sessionStorage.getItem('cc_' + props.note.id), diff --git a/src/context/HashtagContext.js b/src/context/HashtagContext.js index 18421e5..e52df91 100644 --- a/src/context/HashtagContext.js +++ b/src/context/HashtagContext.js @@ -12,20 +12,20 @@ export const HashTagToolProvider = ({ const [lastCreatedAt, setLastCreatedAt] = useState(null); const [isLoading, setIsLoading] = useState(false); const [scrollPosition, setScrollPosition] = useState(0); - + const memoizedFilterTags = React.useMemo(() => filterTags, [filterTags]); useEffect(() => { const loadNotes = async () => { setNotes([]); setIsLoading(true); console.log('load notes'); - if (notesCache[filterTags]) { + if (notesCache[memoizedFilterTags]) { // Use cached notes - setNotes(notesCache[filterTags]); + setNotes(notesCache[memoizedFilterTags]); setIsLoading(false); return; } - console.log('load notes with filter tags', filterTags); - const filters = { limit: 3, '#t': filterTags }; + console.log('load notes with filter tags', memoizedFilterTags); + const filters = { limit: 12, '#t': memoizedFilterTags }; // const notes = await fetchNotes(filters); const notes = await fetchNotesWithProfiles(filters); console.log('notes with profiles are ', notes); @@ -40,7 +40,7 @@ export const HashTagToolProvider = ({ filteredNotes.forEach(note => { note.voteCount = votes[note.id] || 0; }); - notesCache[filterTags] = filteredNotes; + notesCache[memoizedFilterTags] = filteredNotes; setNotes(filteredNotes); setLastCreatedAt( filteredNotes[filteredNotes.length - 1]?.created_at || null, @@ -49,44 +49,65 @@ export const HashTagToolProvider = ({ }; loadNotes(); - }, [filterTags]); + }, [memoizedFilterTags]); const LoadMoreMedia = async () => { + console.log('Fetching more notes'); if (!lastCreatedAt) { return; } const filters = { - limit: 25, + limit: 5, '#t': filterTags, - until: lastCreatedAt - 5 * 60, + until: lastCreatedAt - 5 * 60, // Fetch notes before this time }; + const notes = await fetchNotesWithProfiles(filters); + // Filter for valid media content const filteredNotes = notes.filter(note => /(https?:\/\/[^\s]+(\.jpg|\.mp4|\.gif))/gi.test(note.content), ); + // Fetch vote counts const postIds = filteredNotes.map(note => note.id); const votes = await getVotes(postIds); filteredNotes.forEach(note => { note.voteCount = votes[note.id] || 0; }); - if (notesCache[filterTags]) { - console.log('adding more notes to notesCache.'); + + // Remove duplicates + const filteredNotesWithoutDuplicates = filteredNotes.filter( + note => + !notesCache[filterTags]?.some( + cachedNote => cachedNote.id === note.id, + ), + ); + + if (filteredNotesWithoutDuplicates.length > 0) { + console.log('Adding unique notes to notesCache.'); + + // Append only unique notes to the cache notesCache[filterTags] = [ - ...notesCache[filterTags], - ...filteredNotes, + ...(notesCache[filterTags] || []), + ...filteredNotesWithoutDuplicates, ]; + + // Update state with the cached notes + setNotes([...notesCache[filterTags]]); + + // Update lastCreatedAt for pagination + const lastNote = + filteredNotesWithoutDuplicates[ + filteredNotesWithoutDuplicates.length - 1 + ]; + console.log(lastNote.created_at); + setLastCreatedAt(lastNote?.created_at || null); } else { - notesCache[filterTags] = filteredNotes; + console.log('No new unique notes found.'); } - setNotes(notesCache[filterTags]); - // setNotes((prevNotes) => [...prevNotes, ...filteredNotes]); - setLastCreatedAt( - filteredNotes[filteredNotes.length - 1]?.created_at || null, - ); }; return (