-
Notifications
You must be signed in to change notification settings - Fork 1.5k
chore: migrate MessagesView to Hooks #5922
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
Open
OtavioStasiak
wants to merge
14
commits into
develop
Choose a base branch
from
chore-migrate-to-hooks-messages-view
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ed67925
chore: started migration to hooks
OtavioStasiak b91564e
chore: created useMessagesActions hook
OtavioStasiak 11f7388
minor changes
OtavioStasiak 3fe5a31
minor changes
OtavioStasiak 28249f1
chore: updated hooks and organized methods
OtavioStasiak 1ca2bee
minor changes
OtavioStasiak e17431a
chore: renamed functions and remove useless types
OtavioStasiak 0ec9a58
minor changes
OtavioStasiak 5afc864
Merge branch 'develop' into chore-migrate-to-hooks-messages-view
OtavioStasiak 4f1b6e8
chore: improve code organization
OtavioStasiak e98d4d1
fix: updated fetchMessages to use getMessages 7.0 params
OtavioStasiak f91774a
Merge remote-tracking branch 'origin/develop' into chore-migrate-to-h…
diegolmello ef2c056
chore: collapse MessagesView per-screen helpers into one config lookup
diegolmello 77522ff
fix: harden MessagesView message loading and screen typing
diegolmello File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { type CompositeNavigationProp, type RouteProp } from '@react-navigation/core'; | ||
| import { type NativeStackNavigationProp } from '@react-navigation/native-stack'; | ||
|
|
||
| import { type ChatsStackParamList } from '../../stacks/types'; | ||
| import { type TNavigation } from '../../stacks/stackType'; | ||
| import { type MasterDetailInsideStackParamList } from '../../stacks/MasterDetailStack/types'; | ||
| import { type TMessageModel, type ISubscription, type SubscriptionType } from '../../definitions'; | ||
|
|
||
| export interface IMessagesViewProps { | ||
| navigation: CompositeNavigationProp< | ||
| NativeStackNavigationProp<ChatsStackParamList, 'MessagesView'>, | ||
| NativeStackNavigationProp<MasterDetailInsideStackParamList & TNavigation> | ||
| >; | ||
| route: RouteProp<ChatsStackParamList, 'MessagesView'>; | ||
| } | ||
|
|
||
| export interface IParams { | ||
| rid: string; | ||
| t: SubscriptionType; | ||
| tmid?: string; | ||
| message?: TMessageModel; | ||
| name?: string; | ||
| fname?: string; | ||
| prid?: string; | ||
| room?: ISubscription; | ||
| jumpToMessageId?: string; | ||
| jumpToThreadId?: string; | ||
| roomUserId?: string; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { useState, useEffect, useRef } from 'react'; | ||
|
|
||
| import { type IMessage } from '../../../definitions'; | ||
|
|
||
| interface IUseMessages { | ||
| fetchMessages: (offset: number) => Promise<any>; | ||
| } | ||
|
|
||
| export const useMessages = ({ fetchMessages }: IUseMessages) => { | ||
| const loadingRef = useRef(false); | ||
| const [loading, setLoading] = useState(true); | ||
| const [messages, setMessages] = useState<IMessage[]>([]); | ||
| const [total, setTotal] = useState(-1); | ||
|
|
||
| const load = async () => { | ||
| if (loadingRef.current || messages.length === total) return; | ||
|
|
||
| loadingRef.current = true; | ||
| setLoading(true); | ||
|
|
||
| try { | ||
| const result = await fetchMessages(messages.length); | ||
| if (result?.success) { | ||
| const urlRenderMessages = result?.messages?.map((message: IMessage) => ({ | ||
| ...message, | ||
| urls: message.urls?.map((url, index) => ({ | ||
| _id: index, | ||
| title: url.meta?.pageTitle, | ||
| description: url.meta?.ogDescription, | ||
| image: url.meta?.ogImage, | ||
| url: url.url | ||
| })) | ||
| })); | ||
| setMessages(prevMessages => [...prevMessages, ...urlRenderMessages]); | ||
| setTotal(result.total); | ||
| } | ||
| } catch (error) { | ||
| console.error(error); | ||
| } finally { | ||
| loadingRef.current = false; | ||
| setLoading(false); | ||
| } | ||
| }; | ||
|
|
||
| const updateMessageOnActionPress = (message_id: string) => { | ||
| setMessages(prevState => prevState.filter((item: IMessage) => item._id !== message_id)); | ||
| setTotal(prevState => prevState - 1); | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| load(); | ||
| }, []); | ||
|
|
||
| return { messages, loading, updateMessageOnActionPress, loadMore: load }; | ||
| }; | ||
|
|
||
| export default useMessages; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.