Skip to content

Commit

Permalink
more code
Browse files Browse the repository at this point in the history
  • Loading branch information
AaruBama committed Jan 22, 2025
1 parent 17c5ff3 commit ac00972
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/components/HashtagTool/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/components/Posts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
59 changes: 40 additions & 19 deletions src/context/HashtagContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand All @@ -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 (
Expand Down

0 comments on commit ac00972

Please sign in to comment.