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

feat: more chat fixes + message status + optimistic update for text message + refactor queries/mutations #1357

Merged
merged 6 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { IVStackProps, VStack } from "@/design-system/VStack";
import { memo } from "react";
import { useSafeAreaInsets } from "react-native-safe-area-context";

export const ConversationComposerContainer = memo(function (
props: IVStackProps
) {
const { style, ...rest } = props;

const insets = useSafeAreaInsets();

return (
<VStack
style={[
{
paddingBottom: insets.bottom,
justifyContent: "flex-end",
overflow: "hidden",
},
style,
]}
{...rest}
/>
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
useConversationMessageById,
} from "@/features/conversation/conversation-message/conversation-message.utils";
import { useCurrentAccountInboxId } from "@/hooks/use-current-account-inbox-id";
import { useCurrentConversationTopic } from "../conversation.store-context";
import { usePreferredInboxName } from "@/hooks/usePreferredInboxName";
import { HStack } from "@design-system/HStack";
import { Icon } from "@design-system/Icon/Icon";
Expand All @@ -26,7 +25,6 @@ import { DecodedMessageWithCodecsType } from "@utils/xmtpRN/client";
import {
DecodedMessage,
InboxId,
MessageId,
RemoteAttachmentCodec,
ReplyCodec,
StaticAttachmentCodec,
Expand All @@ -37,6 +35,7 @@ import {
useSharedValue,
withSpring,
} from "react-native-reanimated";
import { useCurrentConversationTopic } from "../conversation.store-context";
import {
useConversationComposerStore,
useConversationComposerStoreContext,
Expand All @@ -47,18 +46,6 @@ export const ReplyPreview = memo(function ReplyPreview() {
(state) => state.replyingToMessageId
);

if (!replyingToMessageId) {
return null;
}

return <Content replyingToMessageId={replyingToMessageId} />;
});

const Content = memo(function Content(props: {
replyingToMessageId: MessageId;
}) {
const { replyingToMessageId } = props;

const { theme } = useAppTheme();

const composerStore = useConversationComposerStore();
Expand All @@ -67,7 +54,7 @@ const Content = memo(function Content(props: {
const topic = useCurrentConversationTopic();

const { message: replyMessage } = useConversationMessageById({
messageId: replyingToMessageId,
messageId: replyingToMessageId!, // ! because we have enabled in the query
topic,
});
thierryskoda marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -115,6 +102,8 @@ const Content = memo(function Content(props: {
},
containerAS,
]}
// entering={theme.animation.reanimatedFadeInSpring}
// exiting={theme.animation.reanimatedFadeOutSpring}
thierryskoda marked this conversation as resolved.
Show resolved Hide resolved
>
{!!replyMessage && (
<AnimatedVStack
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import { sentryTrackError } from "@utils/sentry";
import React, { memo, useCallback, useEffect, useRef } from "react";
import { Platform, TextInput as RNTextInput } from "react-native";
import { useAnimatedStyle, withSpring } from "react-native-reanimated";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import { AddAttachmentButton } from "./conversation-composer-add-attachment-button";
import { ReplyPreview } from "./conversation-composer-reply-preview";
import {
useConversationComposerStore,
useConversationComposerStoreContext,
Expand Down Expand Up @@ -112,54 +110,43 @@ export const Composer = memo(function Composer(props: IComposerProps) {
// });
}, [onSend, store]);

const insets = useSafeAreaInsets();

return (
<VStack
style={{
paddingBottom: insets.bottom,
justifyContent: "flex-end",
overflow: "hidden",
// ...debugBorder("yellow"),
margin: 6, // 6 in the Figma
}}
>
<ReplyPreview />
<VStack
<HStack
style={{
// ...debugBorder("yellow"),
margin: 6, // 6 in the Figma
// ...debugBorder("red"),
alignItems: "flex-end",
}}
>
<HStack
<AddAttachmentButton />
<VStack
style={{
// ...debugBorder("red"),
alignItems: "flex-end",
flex: 1,
margin: theme.spacing.xxxs - theme.borderWidth.sm, // -theme.borderWidth.sm because of the borderWidth is count in react-native and we want exact pixels
borderWidth: theme.borderWidth.sm,
borderColor: theme.colors.border.subtle,
borderRadius: theme.borderRadius.md,
overflow: "hidden",
justifyContent: "flex-end",
}}
>
<AddAttachmentButton />
<VStack
<AttachmentsPreview />
<HStack
style={{
flex: 1,
margin: theme.spacing.xxxs - theme.borderWidth.sm, // -theme.borderWidth.sm because of the borderWidth is count in react-native and we want exact pixels
borderWidth: theme.borderWidth.sm,
borderColor: theme.colors.border.subtle,
borderRadius: theme.borderRadius.md,
overflow: "hidden",
justifyContent: "flex-end",
// ...debugBorder("blue"),
alignItems: "center",
}}
>
<AttachmentsPreview />
<HStack
style={{
// ...debugBorder("blue"),
alignItems: "center",
}}
>
<ComposerTextInput onSubmitEditing={send} />
<SendButton onPress={send} />
</HStack>
</VStack>
</HStack>
</VStack>
<ComposerTextInput onSubmitEditing={send} />
<SendButton onPress={send} />
</HStack>
</VStack>
</HStack>
</VStack>
);
});
Expand Down
87 changes: 79 additions & 8 deletions features/conversation/conversation-keyboard-filler.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,86 @@
import { memo } from "react";
import { useKeyboardIsShown } from "@/hooks/use-keyboard-is-shown";
import { AnimatedVStack } from "@design-system/VStack";
import { useAnimatedKeyboard, useAnimatedStyle } from "react-native-reanimated";
import { memo, useEffect, useRef } from "react";
import { Keyboard, TextInput } from "react-native";
import {
useAnimatedKeyboard,
useAnimatedStyle,
useSharedValue,
} from "react-native-reanimated";
import { useSafeAreaInsets } from "react-native-safe-area-context";

export const KeyboardFiller = memo(function KeyboardFiller() {
const { height: keyboardHeightAV } = useAnimatedKeyboard();
type IKeyboardFillerProps = {
messageContextMenuIsOpen: boolean;
};

export const KeyboardFiller = memo(function KeyboardFiller(
props: IKeyboardFillerProps
) {
const { messageContextMenuIsOpen } = props;
const { height: keyboardHeight } = useAnimatedKeyboard();
const insets = useSafeAreaInsets();
const lastKeyboardHeight = useSharedValue(0);
const textInputRef = useRef<TextInput>(null);
const keyboardWasOpenRef = useRef(false);
const isKeyboardShown = useKeyboardIsShown();

useEffect(() => {
console.log("messageContextMenuIsOpen:", messageContextMenuIsOpen);
console.log("keyboardWasOpenRef.current:", keyboardWasOpenRef.current);
// Context menu was hidden
if (!messageContextMenuIsOpen) {
// Reopen keyboard if it was open before context menu was shown
if (keyboardWasOpenRef.current) {
textInputRef.current?.focus();
}
}
// Context menu is shown
else {
if (isKeyboardShown) {
Keyboard.dismiss();
lastKeyboardHeight.value = keyboardHeight.value;
keyboardWasOpenRef.current = true;
} else {
keyboardWasOpenRef.current = false;
}
}
}, [
messageContextMenuIsOpen,
isKeyboardShown,
keyboardHeight,
lastKeyboardHeight,
]);

// Reset the last height when context menu is dismissed
// And make sure to wait until the keyboard is open to that the height animates back to what it was before
useEffect(() => {
if (!messageContextMenuIsOpen && isKeyboardShown) {
lastKeyboardHeight.value = 0;
}
}, [
messageContextMenuIsOpen,
isKeyboardShown,
keyboardHeight,
lastKeyboardHeight,
]);

const as = useAnimatedStyle(() => ({
height: Math.max(keyboardHeightAV.value - insets.bottom, 0),
}));
const animatedStyle = useAnimatedStyle(() => {
return {
height: Math.max(
Math.max(lastKeyboardHeight.value, keyboardHeight.value) -
insets.bottom,
0
),
};
});

return <AnimatedVStack style={as} />;
return (
<>
<AnimatedVStack style={animatedStyle} />
<TextInput
ref={textInputRef}
style={{ height: 0, width: 0, opacity: 0, position: "absolute" }}
/>
</>
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { AnimatedHStack } from "@/design-system/HStack";
import { AnimatedText } from "@/design-system/Text";
import { usePrevious } from "@/hooks/use-previous-value";
import { translate } from "@/i18n";
import { useAppTheme } from "@/theme/useAppTheme";
import { Haptics } from "@/utils/haptics";
import React, { memo, useEffect } from "react";
import { IConversationMessageStatus } from "../conversation-message/conversation-message.types";

type IConversationMessageStatusProps = {
status: IConversationMessageStatus;
};

const statusMapping: Record<IConversationMessageStatus, string> = {
// sending: translate("message_status.sending"),
sending: " ", // For now don't show anything for sending, waiting to see what UX we want
sent: translate("message_status.sent"),
error: translate("message_status.error"),
};

export const ConversationMessageStatus = memo(
function ConversationMessageStatus({
status,
}: IConversationMessageStatusProps) {
const { theme } = useAppTheme();

const previousStatus = usePrevious(status);

useEffect(() => {
if (previousStatus === "sending" && status === "sent") {
Haptics.softImpactAsync();
}
}, [status, previousStatus]);

return (
<AnimatedHStack
// {...debugBorder()}
entering={theme.animation.reanimatedFadeInSpring}
>
<AnimatedText color="secondary" size="xxs">
{statusMapping[status]}
</AnimatedText>
</AnimatedHStack>
);
}
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { DecodedMessageWithCodecsType } from "@/utils/xmtpRN/client";
import { MessageDeliveryStatus } from "@xmtp/react-native-sdk";

export function messageIsSent(message: DecodedMessageWithCodecsType) {
return message.deliveryStatus === MessageDeliveryStatus.PUBLISHED;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useSelect } from "@/data/store/storeHelpers";
import { HStack } from "@/design-system/HStack";
import { useAppTheme } from "@/theme/useAppTheme";
import { memo } from "react";
import { debugBorder } from "@/utils/debug-style";

export const MessageContentContainer = memo(
function MessageContentContainer(props: { children: React.ReactNode }) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useSelect } from "@/data/store/storeHelpers";
import { AttachmentRemoteImage } from "@/features/conversation/conversation-attachment/conversation-attachment-remote-image";
import { useMessageContextStoreContext } from "@/features/conversation/conversation-message/conversation-message.store-context";
import {
BubbleContainer,
BubbleContentContainer,
} from "@/features/conversation/conversation-message/conversation-message-bubble";
import { MessageText } from "@/features/conversation/conversation-message/conversation-message-text";
import { useMessageContextStoreContext } from "@/features/conversation/conversation-message/conversation-message.store-context";
import { useCurrentConversationTopic } from "@/features/conversation/conversation.store-context";
import { usePreferredInboxName } from "@/hooks/usePreferredInboxName";
import { getConversationMessages } from "@/queries/useConversationMessages";
Expand Down Expand Up @@ -111,8 +111,6 @@ const MessageReplyReference = memo(function MessageReplyReference(props: {

const fromMe = useMessageContextStoreContext((s) => s.fromMe);

const currentAccount = useCurrentAccount()!;

const replyMessageReference =
useConversationMessageForReplyMessage(referenceMessageId);

Expand Down
Loading
Loading