Skip to content

Commit 198b31d

Browse files
committed
fix: use state store
1 parent 5f15088 commit 198b31d

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

package/src/components/Poll/CreatePollContent.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { StyleSheet, Switch, Text, View } from 'react-native';
44
import { ScrollView } from 'react-native-gesture-handler';
55
import Animated, { LinearTransition, useSharedValue } from 'react-native-reanimated';
66

7-
import { PollComposerState, VotingVisibility } from 'stream-chat';
7+
import { PollComposerState, StateStore, VotingVisibility } from 'stream-chat';
88

99
import { CreatePollOptions, CurrentOptionPositionsCache } from './components';
1010

@@ -13,6 +13,7 @@ import { MultipleAnswersField } from './components/MultipleAnswersField';
1313
import { NameField } from './components/NameField';
1414

1515
import {
16+
CreatePollModalState,
1617
CreatePollContentContextValue,
1718
CreatePollContentProvider,
1819
InputMessageInputContextValue,
@@ -219,21 +220,23 @@ export const CreatePoll = ({
219220
> &
220221
Pick<InputMessageInputContextValue, 'CreatePollContent'>) => {
221222
const messageComposer = useMessageComposer();
222-
const [isClosing, setIsClosing] = useState(false);
223+
const [modalStateStore] = useState(
224+
() => new StateStore<CreatePollModalState>({ isClosing: false }),
225+
);
223226
const closeFrameRef = useRef<number | null>(null);
224227

225228
const closeCreatePollDialog = useCallback(() => {
226-
if (closeFrameRef.current !== null) {
229+
if (closeFrameRef.current !== null || modalStateStore.getLatestValue().isClosing) {
227230
return;
228231
}
229232

230-
setIsClosing(true);
231233
// Let the modal render once with exit animations disabled before we dismiss it.
234+
modalStateStore.partialNext({ isClosing: true });
232235
closeFrameRef.current = requestAnimationFrame(() => {
233236
closeFrameRef.current = null;
234237
closePollCreationDialog?.();
235238
});
236-
}, [closePollCreationDialog]);
239+
}, [closePollCreationDialog, modalStateStore]);
237240

238241
useEffect(() => {
239242
return () => {
@@ -261,7 +264,7 @@ export const CreatePoll = ({
261264
closePollCreationDialog: closeCreatePollDialog,
262265
createAndSendPoll,
263266
createPollOptionGap,
264-
isClosing,
267+
modalStateStore,
265268
sendMessage,
266269
}}
267270
>

package/src/components/Poll/components/MultipleVotesSettings.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ const pollComposerStateSelector = (state: PollComposerState) => ({
1717
max_votes_allowed: state.data.max_votes_allowed,
1818
});
1919

20+
const modalStateSelector = (state: { isClosing: boolean }) => ({
21+
isClosing: state.isClosing,
22+
});
23+
2024
const MaxVotesTextInput = () => {
2125
const messageComposer = useMessageComposer();
2226
const { pollComposer } = messageComposer;
@@ -91,7 +95,8 @@ const MaxVotesTextInput = () => {
9195
export const MultipleVotesSettings = () => {
9296
const [allowMaxVotesPerPerson, setAllowMaxVotesPerPerson] = useState<boolean>(false);
9397
const { t } = useTranslationContext();
94-
const { isClosing } = useCreatePollContentContext();
98+
const { modalStateStore } = useCreatePollContentContext();
99+
const { isClosing = false } = useStateStore(modalStateStore, modalStateSelector) ?? {};
95100
const messageComposer = useMessageComposer();
96101
const { pollComposer } = messageComposer;
97102
const { updateFields } = pollComposer;

package/src/contexts/pollContext/createPollContentContext.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
import React, { PropsWithChildren, useContext } from 'react';
22

3+
import { StateStore } from 'stream-chat';
4+
35
import { MessageInputContextValue } from '../messageInputContext/MessageInputContext';
46
import { DEFAULT_BASE_CONTEXT_VALUE } from '../utils/defaultBaseContextValue';
57

68
import { isTestEnvironment } from '../utils/isTestEnvironment';
79

10+
export type CreatePollModalState = {
11+
isClosing: boolean;
12+
};
13+
814
export type CreatePollContentContextValue = {
915
createAndSendPoll: () => Promise<void>;
1016
sendMessage: MessageInputContextValue['sendMessage'];
1117
closePollCreationDialog?: () => void;
12-
isClosing?: boolean;
18+
modalStateStore?: StateStore<CreatePollModalState>;
1319
/**
1420
* Vertical gap between poll options in the poll creation screen.
1521
*

0 commit comments

Comments
 (0)