|
| 1 | +import React, { |
| 2 | + createContext, |
| 3 | + useState, |
| 4 | + useMemo, |
| 5 | + useCallback, |
| 6 | + useContext } |
| 7 | +from 'react'; |
| 8 | +import type { Context } from 'react'; |
| 9 | +import { useSelector } from '../react-redux'; |
| 10 | +import TopicEditModal from '../topics/TopicEditModal'; |
| 11 | +import type { Stream, GetText } from '../types'; |
| 12 | +import { fetchSomeMessageIdForConversation } from '../message/fetchActions'; |
| 13 | +import { getAuth, getZulipFeatureLevel } from '../selectors'; |
| 14 | + |
| 15 | +type Props = $ReadOnly<{| |
| 16 | + children: Node, |
| 17 | +|}>; |
| 18 | + |
| 19 | +type TopicModalContext = $ReadOnly<{| |
| 20 | + startEditTopic: ( |
| 21 | + streamId: number, |
| 22 | + topic: string, |
| 23 | + streamsById: Map<number, Stream>, |
| 24 | + _: GetText, |
| 25 | + ) => void, |
| 26 | + closeEditTopicModal: () => void, |
| 27 | +|}>; |
| 28 | + |
| 29 | +const TopicModal: Context<TopicModalContext> = createContext(); |
| 30 | + |
| 31 | +export const useTopicModalHandler = () => useContext(TopicModal); |
| 32 | + |
| 33 | +export default function TopicModalProvider(props: Props) { |
| 34 | + const { children } = props; |
| 35 | + const auth = useSelector(getAuth); |
| 36 | + const zulipFeatureLevel = useSelector(getZulipFeatureLevel); |
| 37 | + const [topicModalState, setTopicModalState] = useState({ |
| 38 | + visible: false, |
| 39 | + topic: '', |
| 40 | + fetchArgs: { |
| 41 | + auth: null, |
| 42 | + messageId: null, |
| 43 | + zulipFeatureLevel: null |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + const startEditTopic = useCallback(async ( |
| 48 | + streamId, |
| 49 | + topic, |
| 50 | + streamsById, |
| 51 | + _, |
| 52 | + ) => { |
| 53 | + const messageId = await fetchSomeMessageIdForConversation( |
| 54 | + auth, |
| 55 | + streamId, |
| 56 | + topic, |
| 57 | + streamsById, |
| 58 | + zulipFeatureLevel, |
| 59 | + ); |
| 60 | + if (messageId == null) { |
| 61 | + throw new Error( |
| 62 | + _('No messages in topic: {streamAndTopic}', { |
| 63 | + streamAndTopic: `#${streamsById.get(streamId)?.name ?? 'unknown'} > ${topic}`, |
| 64 | + }), |
| 65 | + ); |
| 66 | + } |
| 67 | + setTopicModalState({ |
| 68 | + visible: true, |
| 69 | + topic, |
| 70 | + fetchArgs: { auth, messageId, zulipFeatureLevel } |
| 71 | + }); |
| 72 | + }, [auth, zulipFeatureLevel]); |
| 73 | + |
| 74 | + const closeEditTopicModal = useCallback(() => { |
| 75 | + setTopicModalState({ |
| 76 | + visible: false, |
| 77 | + topic: null, |
| 78 | + fetchArgs: { auth: null, messageId: null, zulipFeatureLevel: null } |
| 79 | + }); |
| 80 | + }, []); |
| 81 | + |
| 82 | + const topicModalHandler = useMemo(() => ({ |
| 83 | + startEditTopic, |
| 84 | + closeEditTopicModal, |
| 85 | + }), [startEditTopic, closeEditTopicModal]); |
| 86 | + |
| 87 | + return ( |
| 88 | + <TopicModal.Provider value={topicModalHandler}> |
| 89 | + {topicModalState.visible && ( |
| 90 | + <TopicEditModal |
| 91 | + topicModalState={topicModalState} |
| 92 | + topicModalHandler={topicModalHandler} |
| 93 | + /> |
| 94 | + )} |
| 95 | + {children} |
| 96 | + </TopicModal.Provider> |
| 97 | + ); |
| 98 | +} |
0 commit comments