diff --git a/lib/shared/community-utils.js b/lib/shared/community-utils.js index f6f4f79ca..b7ba5c4a1 100644 --- a/lib/shared/community-utils.js +++ b/lib/shared/community-utils.js @@ -1,142 +1,148 @@ // @flow import * as React from 'react'; import { createOrUpdateFarcasterChannelTagActionTypes, useCreateOrUpdateFarcasterChannelTag, deleteFarcasterChannelTagActionTypes, useDeleteFarcasterChannelTag, } from '../actions/community-actions.js'; import { createLoadingStatusSelector } from '../selectors/loading-selectors.js'; import type { SetState } from '../types/hook-types.js'; import { useDispatchActionPromise } from '../utils/redux-promise-utils.js'; import { useSelector } from '../utils/redux-utils.js'; const tagFarcasterChannelCopy = { DESCRIPTION: 'Tag a Farcaster channel so followers can find your Comm community!', CHANNEL_NAME_HEADER: 'Selected channel:', NO_CHANNEL_TAGGED: 'No Farcaster channel tagged', }; +const tagFarcasterChannelErrorMessages: { +[string]: string } = { + already_in_use: 'This Farcaster channel is already tagged to a community.', + channel_not_found: 'Could not find a channel with the provided name.', +}; + function farcasterChannelTagBlobHash(farcasterChannelID: string): string { return `farcaster_channel_tag_${farcasterChannelID}`; } const createOrUpdateFarcasterChannelTagStatusSelector = createLoadingStatusSelector(createOrUpdateFarcasterChannelTagActionTypes); function useCreateFarcasterChannelTag( commCommunityID: string, setError: SetState, onSuccessCallback?: () => mixed, ): { +createTag: (farcasterChannelID: string) => mixed, +isLoading: boolean, } { const dispatchActionPromise = useDispatchActionPromise(); const createOrUpdateFarcasterChannelTag = useCreateOrUpdateFarcasterChannelTag(); const createCreateOrUpdateActionPromise = React.useCallback( async (farcasterChannelID: string) => { try { const res = await createOrUpdateFarcasterChannelTag({ commCommunityID, farcasterChannelID, }); onSuccessCallback?.(); return res; } catch (e) { setError(e.message); throw e; } }, [ commCommunityID, createOrUpdateFarcasterChannelTag, onSuccessCallback, setError, ], ); const createTag = React.useCallback( (farcasterChannelID: string) => { void dispatchActionPromise( createOrUpdateFarcasterChannelTagActionTypes, createCreateOrUpdateActionPromise(farcasterChannelID), ); }, [createCreateOrUpdateActionPromise, dispatchActionPromise], ); const createOrUpdateFarcasterChannelTagStatus = useSelector( createOrUpdateFarcasterChannelTagStatusSelector, ); const isLoading = createOrUpdateFarcasterChannelTagStatus === 'loading'; return { createTag, isLoading, }; } const deleteFarcasterChannelTagStatusSelector = createLoadingStatusSelector( deleteFarcasterChannelTagActionTypes, ); function useRemoveFarcasterChannelTag( commCommunityID: string, farcasterChannelID: string, setError: SetState, ): { +removeTag: () => mixed, +isLoading: boolean, } { const dispatchActionPromise = useDispatchActionPromise(); const deleteFarcasterChannelTag = useDeleteFarcasterChannelTag(); const createDeleteActionPromise = React.useCallback(async () => { try { return await deleteFarcasterChannelTag({ commCommunityID, farcasterChannelID, }); } catch (e) { setError(e.message); throw e; } }, [ commCommunityID, deleteFarcasterChannelTag, farcasterChannelID, setError, ]); const removeTag = React.useCallback(() => { void dispatchActionPromise( deleteFarcasterChannelTagActionTypes, createDeleteActionPromise(), ); }, [createDeleteActionPromise, dispatchActionPromise]); const deleteFarcasterChannelTagStatus = useSelector( deleteFarcasterChannelTagStatusSelector, ); const isLoading = deleteFarcasterChannelTagStatus === 'loading'; return { removeTag, isLoading, }; } export { tagFarcasterChannelCopy, + tagFarcasterChannelErrorMessages, farcasterChannelTagBlobHash, useCreateFarcasterChannelTag, useRemoveFarcasterChannelTag, }; diff --git a/native/community-settings/tag-farcaster-channel/tag-farcaster-channel-by-name.react.js b/native/community-settings/tag-farcaster-channel/tag-farcaster-channel-by-name.react.js index c0be6b80a..ac0f3c615 100644 --- a/native/community-settings/tag-farcaster-channel/tag-farcaster-channel-by-name.react.js +++ b/native/community-settings/tag-farcaster-channel/tag-farcaster-channel-by-name.react.js @@ -1,152 +1,154 @@ // @flow import invariant from 'invariant'; import * as React from 'react'; import { View, Text } from 'react-native'; import { NeynarClientContext } from 'lib/components/neynar-client-provider.react.js'; -import { useCreateFarcasterChannelTag } from 'lib/shared/community-utils.js'; +import { + tagFarcasterChannelErrorMessages, + useCreateFarcasterChannelTag, +} from 'lib/shared/community-utils.js'; import type { TagFarcasterChannelNavigationProp } from './tag-farcaster-channel-navigator.react.js'; -import { tagFarcasterChannelErrorMessages } from './tag-farcaster-channel-utils.js'; import RegistrationButton from '../../account/registration/registration-button.react.js'; import TextInput from '../../components/text-input.react.js'; import type { NavigationRoute } from '../../navigation/route-names.js'; import { useStyles, useColors } from '../../themes/colors.js'; export type TagFarcasterChannelByNameParams = { +communityID: string, }; type Props = { +navigation: TagFarcasterChannelNavigationProp<'TagFarcasterChannelByName'>, +route: NavigationRoute<'TagFarcasterChannelByName'>, }; function TagFarcasterChannelByName(prop: Props): React.Node { const { navigation, route } = prop; const { goBack } = navigation; const { communityID } = route.params; const styles = useStyles(unboundStyles); const colors = useColors(); const [channelSelectionText, setChannelSelectionText] = React.useState(''); const [error, setError] = React.useState(null); const neynarClientContext = React.useContext(NeynarClientContext); invariant(neynarClientContext, 'NeynarClientContext is missing'); const { createTag, isLoading } = useCreateFarcasterChannelTag( communityID, setError, goBack, ); const onPressTagChannel = React.useCallback(async () => { const channelInfo = await neynarClientContext.client.fetchFarcasterChannelByName( channelSelectionText, ); if (!channelInfo) { setError('channel_not_found'); return; } createTag(channelInfo.id); }, [channelSelectionText, createTag, neynarClientContext.client]); const errorMessage = React.useMemo(() => { if (!error) { return ; } return ( {tagFarcasterChannelErrorMessages[error] ?? 'Unknown error.'} ); }, [error, styles.error, styles.errorPlaceholder]); let submitButtonVariant = 'disabled'; if (isLoading) { submitButtonVariant = 'loading'; } else if (channelSelectionText.length > 0) { submitButtonVariant = 'enabled'; } return ( CHANNEL NAME {errorMessage} ); } const unboundStyles = { container: { paddingTop: 24, }, header: { color: 'panelBackgroundLabel', fontSize: 12, fontWeight: '400', paddingBottom: 4, paddingHorizontal: 16, }, panelSectionContainer: { backgroundColor: 'panelForeground', padding: 16, borderBottomColor: 'panelSeparator', borderBottomWidth: 1, borderTopColor: 'panelSeparator', borderTopWidth: 1, }, inputContainer: { flexDirection: 'row', justifyContent: 'space-between', paddingHorizontal: 16, paddingVertical: 12, borderWidth: 1, borderColor: 'panelSecondaryForegroundBorder', borderRadius: 8, marginBottom: 8, }, input: { color: 'panelForegroundLabel', fontSize: 16, }, error: { fontSize: 12, fontWeight: '400', lineHeight: 18, textAlign: 'center', color: 'redText', }, errorPlaceholder: { height: 18, }, }; export default TagFarcasterChannelByName; diff --git a/native/community-settings/tag-farcaster-channel/tag-farcaster-channel-utils.js b/native/community-settings/tag-farcaster-channel/tag-farcaster-channel-utils.js deleted file mode 100644 index 36384cbc2..000000000 --- a/native/community-settings/tag-farcaster-channel/tag-farcaster-channel-utils.js +++ /dev/null @@ -1,8 +0,0 @@ -// @flow - -const tagFarcasterChannelErrorMessages: { +[string]: string } = { - already_in_use: 'This Farcaster channel is already tagged to a community.', - channel_not_found: 'Could not find a channel with the provided name.', -}; - -export { tagFarcasterChannelErrorMessages }; diff --git a/native/community-settings/tag-farcaster-channel/tag-farcaster-channel.react.js b/native/community-settings/tag-farcaster-channel/tag-farcaster-channel.react.js index 68257277e..3604fb01c 100644 --- a/native/community-settings/tag-farcaster-channel/tag-farcaster-channel.react.js +++ b/native/community-settings/tag-farcaster-channel/tag-farcaster-channel.react.js @@ -1,169 +1,171 @@ // @flow import * as React from 'react'; import { View, Text } from 'react-native'; -import { tagFarcasterChannelCopy } from 'lib/shared/community-utils.js'; +import { + tagFarcasterChannelCopy, + tagFarcasterChannelErrorMessages, +} from 'lib/shared/community-utils.js'; import type { CommunityInfo } from 'lib/types/community-types.js'; import RemoveTagButton from './remove-tag-button.react.js'; import TagChannelButton from './tag-channel-button.react.js'; import type { TagFarcasterChannelNavigationProp } from './tag-farcaster-channel-navigator.react.js'; -import { tagFarcasterChannelErrorMessages } from './tag-farcaster-channel-utils.js'; import { type NavigationRoute } from '../../navigation/route-names.js'; import { useSelector } from '../../redux/redux-utils.js'; import { useStyles } from '../../themes/colors.js'; export type TagFarcasterChannelParams = { +communityID: string, }; type Props = { +navigation: TagFarcasterChannelNavigationProp<'TagFarcasterChannel'>, +route: NavigationRoute<'TagFarcasterChannel'>, }; function TagFarcasterChannel(props: Props): React.Node { const { route } = props; const { communityID } = route.params; const communityInfo: ?CommunityInfo = useSelector( state => state.communityStore.communityInfos[communityID], ); const styles = useStyles(unboundStyles); const [error, setError] = React.useState(null); const errorMessage = React.useMemo(() => { if (!error) { return null; } return ( {tagFarcasterChannelErrorMessages[error] ?? 'Unknown error.'} ); }, [error, styles.error]); const channelNameTextContent = React.useMemo(() => { if (!communityInfo?.farcasterChannelID) { return ( {tagFarcasterChannelCopy.NO_CHANNEL_TAGGED} ); } return ( {`/${communityInfo.farcasterChannelID}`} ); }, [ communityInfo?.farcasterChannelID, styles.channelNameText, styles.noChannelText, ]); const sectionButton = React.useMemo( () => communityInfo?.farcasterChannelID ? ( ) : ( ), [communityID, communityInfo?.farcasterChannelID], ); const tagFarcasterChannel = React.useMemo( () => ( {tagFarcasterChannelCopy.DESCRIPTION} FARCASTER CHANNEL {tagFarcasterChannelCopy.CHANNEL_NAME_HEADER} {channelNameTextContent} {sectionButton} {errorMessage} ), [ styles.panelSectionContainer, styles.sectionText, styles.sectionHeaderText, styles.channelNameContainer, styles.errorContainer, channelNameTextContent, sectionButton, errorMessage, ], ); return tagFarcasterChannel; } const unboundStyles = { panelSectionContainer: { backgroundColor: 'panelForeground', padding: 16, borderBottomColor: 'panelSeparator', borderBottomWidth: 1, borderTopColor: 'panelSeparator', borderTopWidth: 1, }, sectionText: { color: 'panelForegroundLabel', fontSize: 14, }, sectionHeaderText: { fontSize: 14, fontWeight: '400', lineHeight: 20, color: 'panelForegroundLabel', paddingHorizontal: 16, paddingBottom: 4, marginTop: 24, }, channelNameContainer: { marginTop: 8, marginBottom: 24, height: 20, }, channelNameText: { fontSize: 16, fontWeight: '600', color: 'panelForegroundLabel', }, noChannelText: { fontSize: 16, color: 'panelForegroundLabel', }, errorContainer: { height: 18, }, error: { fontSize: 12, fontWeight: '400', lineHeight: 18, textAlign: 'center', color: 'redText', }, }; export default TagFarcasterChannel;