Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
112 changes: 112 additions & 0 deletions app/src/components/modal/BottomSheet.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { useEffect, useRef, useState } from 'react';
import {
Animated,
Dimensions,
Modal,
PanResponder,
Pressable,
StyleSheet,
View,
} from 'react-native';

import { bottomSheetStyle } from '@/styles';

type BottomSheetProps = {
visible: boolean;
onClose: () => void;
children: React.ReactNode;
snapHeight?: number;
testID: string;
};

const SCREEN_HEIGHT = Dimensions.get('window').height;
const DEFAULT_SNAP_HEIGHT = 0.9;
const CLOSE_THRESHOLD = 120;
const DRAG_ACTIVATION_THRESHOLD = 5;

const BottomSheet = ({
visible,
onClose,
children,
snapHeight = DEFAULT_SNAP_HEIGHT,
}: BottomSheetProps) => {
const [internalVisible, setInternalVisible] = useState(false);
const translateY = useRef(new Animated.Value(SCREEN_HEIGHT)).current;
const SHEET_MAX_HEIGHT = SCREEN_HEIGHT * snapHeight;

const sheetMaxHeightRef = useRef(SHEET_MAX_HEIGHT);

useEffect(() => {
sheetMaxHeightRef.current = SCREEN_HEIGHT * snapHeight;
}, [snapHeight]);

const panResponder = useRef(
PanResponder.create({
onStartShouldSetPanResponder: () => true,
onMoveShouldSetPanResponder: (_, gestureState) =>
Math.abs(gestureState.dy) > DRAG_ACTIVATION_THRESHOLD,
onPanResponderMove: (_, gestureState) => {
const sheetMaxHeight = sheetMaxHeightRef.current;
const newY = SCREEN_HEIGHT - sheetMaxHeight + gestureState.dy;
if (newY >= SCREEN_HEIGHT - sheetMaxHeight) {
translateY.setValue(newY);
}
},
onPanResponderRelease: (_, gestureState) => {
if (gestureState.dy > CLOSE_THRESHOLD) {
onClose();
} else {
Animated.spring(translateY, {
toValue: SCREEN_HEIGHT - sheetMaxHeightRef.current,
useNativeDriver: true,
stiffness: 100,
damping: 20,
}).start();
}
},
}),
).current;

useEffect(() => {
if (visible) {
setInternalVisible(true);
Animated.spring(translateY, {
toValue: SCREEN_HEIGHT - sheetMaxHeightRef.current,
useNativeDriver: true,
stiffness: 250,
damping: 28,
mass: 1,
}).start();
} else {
Animated.timing(translateY, {
toValue: SCREEN_HEIGHT,
duration: 220,
useNativeDriver: true,
}).start(() => setInternalVisible(false));
}
}, [visible, translateY]);

if (!internalVisible) {
return null;
}

return (
<Modal transparent animationType="none" visible={internalVisible}>
<Pressable style={styles.backdrop} onPress={onClose} />
<Animated.View
style={[styles.sheet, { height: SHEET_MAX_HEIGHT, transform: [{ translateY }] }]}
>
<View style={styles.grabber} {...panResponder.panHandlers} />
{children}
</Animated.View>
</Modal>
);
};

const styles = StyleSheet.create({
backdrop: bottomSheetStyle.backdrop,
sheet: bottomSheetStyle.sheet,
grabber: bottomSheetStyle.grabber,
});

export default BottomSheet;
13 changes: 11 additions & 2 deletions app/src/components/navigation/CreateChatButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@ import { TouchableOpacity, StyleSheet } from 'react-native';

import OutlinedIcon from '@/components/common/OutlinedIcon';
import { navigationStyle } from '@/styles';
import { TestIDs } from '@/utils/testID';

const CreateChatButton = () => {
type CreateChatButtonProps = {
onPress?: () => void;
};

const CreateChatButton = ({ onPress }: CreateChatButtonProps) => {
return (
<TouchableOpacity style={styles.topBarIconWrapper} onPress={() => {}}>
<TouchableOpacity
style={styles.topBarIconWrapper}
onPress={onPress}
testID={TestIDs.CHAT_CREATE_BUTTON}
>
<OutlinedIcon
name="edit-square"
color={navigationStyle.header.rightIconColorSecondary}
Expand Down
13 changes: 9 additions & 4 deletions app/src/i18n/en/chat.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
{
"chatsScreen": {
"emptyStateTitle": "No chats",
"emptyStateDescription": "No chats found."
},
"chatsScreen": {
"emptyStateTitle": "No chats",
"emptyStateDescription": "No chats found."
},
"messagesScreen": {
"emptyStateTitle": "No messages",
"emptyStateDescription": "No messages in this conversation yet."
},
"chat": {
"messageInputPlaceholder": "Type a message"
},
"createChatWizard": {
"title": "Create chat",
"next": "Next",
"cancel": "Cancel"
}
}
13 changes: 12 additions & 1 deletion app/src/screens/chat/ChatScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { useNavigation, useFocusEffect } from '@react-navigation/native';
import type { StackNavigationProp } from '@react-navigation/stack';
import { useQueryClient } from '@tanstack/react-query';
import { useEffect, useCallback } from 'react';
import { useEffect, useCallback, useLayoutEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';

import StatusMessage from '@/components/common/EmptyStateHelper';
import ListViewChat from '@/components/list/ListViewChat';
import CreateChatButton from '@/components/navigation/CreateChatButton';
import { useAccount } from '@/context/AccountContext';
import { useChats, ChatsProvider } from '@/context/ChatsContext';
import { useSignalR } from '@/context/SignalRContext';
import CreateChatModal from '@/screens/chat/CreateChatModal';
import type { RootStackParamList } from '@/types/navigation';
import type { EntitySubscription } from '@/types/signalr';
import { TestIDs } from '@/utils/testID';
Expand All @@ -30,6 +32,8 @@ const ChatScreenContent = () => {
fetchChats,
} = useChats();

const [isCreateChatVisible, setCreateChatVisible] = useState(false);

const { userData } = useAccount();
const userId = userData?.id;
const currentAccountId = userData?.currentAccount?.id;
Expand All @@ -40,6 +44,12 @@ const ChatScreenContent = () => {
const navigation = useNavigation<StackNavigationProp<RootStackParamList>>();
const { subscribe, addMessageListener, isConnected } = useSignalR();

useLayoutEffect(() => {
navigation.setOptions({
headerRight: () => <CreateChatButton onPress={() => setCreateChatVisible(true)} />,
});
}, [navigation, setCreateChatVisible]);

useEffect(() => {
void subscribe(CHAT_SUBSCRIPTIONS);
}, [subscribe, isConnected]);
Expand Down Expand Up @@ -91,6 +101,7 @@ const ChatScreenContent = () => {
});
}}
/>
<CreateChatModal visible={isCreateChatVisible} onClose={() => setCreateChatVisible(false)} />
</StatusMessage>
);
};
Expand Down
132 changes: 132 additions & 0 deletions app/src/screens/chat/CreateChatModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { useNavigation } from '@react-navigation/native';
import type { StackNavigationProp } from '@react-navigation/stack';
import { useState, useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import {
View,
Text,
TextInput,
ScrollView,
StyleSheet,
KeyboardAvoidingView,
TouchableOpacity,
Platform,
Button,
} from 'react-native';

import BottomSheet from '@/components/modal/BottomSheet';
import { createChatWizardStyle, inputStyle } from '@/styles';
import type { RootStackParamList } from '@/types/navigation';
import { TestIDs } from '@/utils/testID';

type Props = {
visible: boolean;
onClose: () => void;
};

const CreateChatModal = ({ visible, onClose }: Props) => {
const [step, setStep] = useState(0);
const [chatType, setChatType] = useState<'group' | 'direct' | null>(null);
const [chatName, setChatName] = useState('');

const { t } = useTranslation();

const navigation = useNavigation<StackNavigationProp<RootStackParamList>>();

const handleClose = useCallback(() => {
setStep(0);
setChatType(null);
setChatName('');
onClose();
}, [onClose]);

const handleNext = () => {
if (step === 0) {
if (!chatType) return;
setStep(1);
} else if (step === 1) {
if (!chatName.trim()) return;
setStep(2);
} else if (step === 2) {
navigation.navigate('chatConversation', { id: 'CHT-123-4565' });
handleClose();
}
};

return (
<BottomSheet visible={visible} onClose={handleClose} testID={TestIDs.CHAT_CREATE_MODAL}>
<View style={styles.headerRow}>
<View style={styles.headerSide}>
<TouchableOpacity onPress={handleClose}>
<Text style={styles.headerTextCancel}>{t('createChatWizard.cancel')}</Text>
</TouchableOpacity>
</View>
<View style={styles.headerCenter}>
<Text style={styles.headerTitle}>{t('createChatWizard.title')}</Text>
</View>
<View style={styles.headerSide}>
{step > 0 && (
<TouchableOpacity onPress={handleNext}>
<Text style={styles.headerTextNext}>{t('createChatWizard.next')}</Text>
</TouchableOpacity>
)}
</View>
</View>
<KeyboardAvoidingView
behavior="height"
keyboardVerticalOffset={Platform.OS === 'ios' ? 200 : 0}
>
{step === 0 && (
<View>
<Button
title="Group Chat"
onPress={() => {
setChatType('group');
setStep(1);
}}
/>
<Button
title="Direct Chat: John Doe"
onPress={() => {
handleClose();
navigation.navigate('chatConversation', { id: 'CHT-234-2345' });
}}
/>
</View>
)}

{step === 1 && (
<View>
<TextInput
placeholder="Enter chat name"
value={chatName}
onChangeText={setChatName}
style={styles.input}
autoFocus
/>
</View>
)}

{step === 2 && (
<View>
<ScrollView>
<Text>User list placeholder</Text>
</ScrollView>
</View>
)}
</KeyboardAvoidingView>
</BottomSheet>
);
};

const styles = StyleSheet.create({
headerRow: createChatWizardStyle.headerRow,
headerSide: createChatWizardStyle.headerSide,
headerCenter: createChatWizardStyle.headerCenter,
headerTitle: createChatWizardStyle.headerTitle,
headerTextCancel: createChatWizardStyle.headerTextCancel,
headerTextNext: createChatWizardStyle.headerTextNext,
input: inputStyle.container,
});

export default CreateChatModal;
1 change: 1 addition & 0 deletions app/src/screens/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export { default as EnrollmentDetailsScreen } from './enrollments/EnrollmentDeta
export { default as CertificateDetailsScreen } from './certificates/CertificateDetailsScreen';
export { default as ChatScreen } from './chat/ChatScreen';
export { default as ChatConversationScreen } from './chat/ChatConversationScreen';
export { default as CreateChatModal } from './chat/CreateChatModal';

export { WelcomeScreen } from './auth';
export { LoadingScreen } from './loading';
30 changes: 30 additions & 0 deletions app/src/styles/components/bottomSheet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { BorderRadius, Color, Spacing } from '../tokens';

export const bottomSheetStyle = {
backdrop: {
position: 'absolute',
left: Spacing.spacing0,
right: Spacing.spacing0,
top: Spacing.spacing0,
bottom: Spacing.spacing0,
backgroundColor: Color.fills.overlay,
},
sheet: {
position: 'absolute',
bottom: Spacing.spacing0,
width: '100%',
backgroundColor: Color.brand.white,
borderTopLeftRadius: BorderRadius.md,
borderTopRightRadius: BorderRadius.md,
paddingHorizontal: Spacing.spacing2,
paddingBottom: Spacing.spacing2,
},
grabber: {
width: 36,
height: 5,
borderRadius: BorderRadius.xxs,
backgroundColor: Color.gray.gray3,
alignSelf: 'center',
marginTop: Spacing.spacingSmall6,
},
} as const;
Loading
Loading