Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: observer in MessageSeen.jsx #676

Merged
merged 5 commits into from
Aug 20, 2024
Merged

refactor: observer in MessageSeen.jsx #676

merged 5 commits into from
Aug 20, 2024

Conversation

damirgros
Copy link
Contributor

@damirgros damirgros commented Aug 20, 2024

Fixes Issue

My PR closes #669

πŸ‘¨β€πŸ’» Changes proposed(What did you do ?)

Refactored observer logic in the "MessageSeen.jsx" by creating useObserver hook in the new file "useObserver.js" in the folder "hooks".

Transfered observer logic from "MessageSeen.jsx" to "useObserver.js" and also memoized it so it doesn't get re-created every time component re-renders.

Removed unused imports from "MessageSeen.jsx" and initiated observer variable with useObserver hook.

βœ”οΈ Check List (Check all the applicable boxes)

  • My code follows the code style of this project.
  • This PR does not contain plagiarized content.
  • The title and description of the PR is clear and explains the approach.

Note to reviewers

πŸ“· Screenshots

Copy link

vercel bot commented Aug 20, 2024

@damirgros is attempting to deploy a commit to the dunsin's projects Team on Vercel.

A member of the Team first needs to authorize it.

@Dun-sin
Copy link
Owner

Dun-sin commented Aug 20, 2024

@damirgros great work just one thing, can you replace the code with a text description of what you did this is because we can see the code in the code tab already, but we won't see a text description.

And a good convention is to delete a fork to follow a linear history on GitHub, this is so your code doesn't combine the last code which was already merged. Or you could just create a new branch from the main and then pull it so it's linear (for this one, no need to delete the PR just a correction)

Copy link

vercel bot commented Aug 20, 2024

The latest updates on your projects. Learn more about Vercel for Git β†—οΈŽ

Name Status Preview Comments Updated (UTC)
whisper-b2p2 βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback Aug 20, 2024 11:24am

@Dun-sin
Copy link
Owner

Dun-sin commented Aug 20, 2024

in production, this doesn't work:
brave_XFr2dt6FKQ

@damirgros
Copy link
Contributor Author

Intersection Observer API doesn't have connect method.

Can you try in production only changing useEffect in MessageSeen.jsx to look like this (I added the check for if observer and sortedMessages exist, removed observer.connect and added observer dependancy):

useEffect(() => {
		// Only proceed if the observer and sortedMessages are available
		if (!observer || !sortedMessages.length) return;

		sortedMessages.forEach((message) => {
			if (message.isRead) return;

			const messageElement = document.getElementById(`message-${message.id}`);
			if (messageElement && isTabVisible) {
				observer.observe(messageElement); 
			}
		});

		return () => {
			if (observer) observer.disconnect();
		};
	}, [sortedMessages, isTabVisible, observer]);

@Dun-sin
Copy link
Owner

Dun-sin commented Aug 20, 2024

Intersection Observer API doesn't have connect method.

Can you try in production only changing useEffect in MessageSeen.jsx to look like this (I added the check for if observer and sortedMessages exist, removed observer.connect and added observer dependancy):

useEffect(() => {
		// Only proceed if the observer and sortedMessages are available
		if (!observer || !sortedMessages.length) return;

		sortedMessages.forEach((message) => {
			if (message.isRead) return;

			const messageElement = document.getElementById(`message-${message.id}`);
			if (messageElement && isTabVisible) {
				observer.observe(messageElement); 
			}
		});

		return () => {
			if (observer) observer.disconnect();
		};
	}, [sortedMessages, isTabVisible, observer]);

Push it to GitHub and I'll set it up here

@Dun-sin Dun-sin changed the title refactor: observer in MessageSeen.jsx (#669) refactor: observer in MessageSeen.jsx Aug 20, 2024
@damirgros
Copy link
Contributor Author

It is pushed.

@Dun-sin
Copy link
Owner

Dun-sin commented Aug 20, 2024

@damirgros great work just one thing, can you replace the code with a text description of what you did this is because we can see the code in the code tab already, but we won't see a text description.

And a good convention is to delete a fork to follow a linear history on GitHub, this is so your code doesn't combine the last code which was already merged. Or you could just create a new branch from the main and then pull it so it's linear (for this one, no need to delete the PR just a correction)

@damirgros Alright it works in prod, also address this please the first one

Copy link
Owner

@Dun-sin Dun-sin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great, thanks for it.

@damirgros
Copy link
Contributor Author

@damirgros great work just one thing, can you replace the code with a text description of what you did this is because we can see the code in the code tab already, but we won't see a text description.
And a good convention is to delete a fork to follow a linear history on GitHub, this is so your code doesn't combine the last code which was already merged. Or you could just create a new branch from the main and then pull it so it's linear (for this one, no need to delete the PR just a correction)

@damirgros Alright it works in prod, also address this please the first one

Added text description in the PR

@Dun-sin Dun-sin merged commit 49f6d5e into Dun-sin:main Aug 20, 2024
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[OTHER] move the observer into a hook from the messageseen component
2 participants