Skip to content

Commit

Permalink
ui fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sakul-budhathoki committed Sep 26, 2023
1 parent a4f75ff commit 796fd6d
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 250 deletions.
1 change: 1 addition & 0 deletions packages/screens/Message/MessageScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export const MessageScreen: ScreenFC<"Message"> = ({ route }) => {
<View
style={{
flexDirection: "row",
flex: 1,
}}
>
<SideBarChats />
Expand Down
76 changes: 12 additions & 64 deletions packages/screens/Message/components/ChatSection.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import moment from "moment";
import React, {
RefObject,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from "react";
import React, { RefObject, useMemo, useRef, useState } from "react";
import {
View,
TouchableOpacity,
Expand Down Expand Up @@ -57,14 +50,9 @@ import {
MessageFileData,
ReplyTo,
} from "../../../utils/types/message";
import { GroupInfo_Reply } from "../../../weshnet";
import { weshClient } from "../../../weshnet/client";
import { getNewConversationText } from "../../../weshnet/client/messageHelpers";
import { sendMessage } from "../../../weshnet/client/services";
import {
bytesFromString,
stringFromBytes,
} from "../../../weshnet/client/utils";
import { bytesFromString } from "../../../weshnet/client/utils";

interface ChatSectionProps {
conversation: IConversation;
Expand All @@ -81,17 +69,12 @@ export const ChatSection = ({ conversation }: ChatSectionProps) => {
const [inputRef, setInputRef] = useState<RefObject<any> | null>(null);
const [file, setFile] = useState<MessageFileData>();

const { width: windowWidth } = useWindowDimensions();

const [searchInput, setSearchInput] = useState("");

const flatListRef = useRef<FlatList>(null);

const { setToastError } = useFeedbacks();
const [groupInfo, setGroupInfo] = useState<GroupInfo_Reply>();
const messages = useSelector(
selectMessageListByGroupPk(stringFromBytes(groupInfo?.group?.publicKey))
);
const messages = useSelector(selectMessageListByGroupPk(conversation.id));

const searchResults = useMemo(() => {
if (!searchInput) {
Expand All @@ -102,46 +85,13 @@ export const ChatSection = ({ conversation }: ChatSectionProps) => {
);
}, [messages, searchInput]);

const getGroupInfo = useCallback(async () => {
let _group: GroupInfo_Reply;

try {
if (conversation.type === "group") {
_group = await weshClient.client.GroupInfo({
groupPk: bytesFromString(conversation.id),
});
await weshClient.client.ActivateGroup({
groupPk: _group.group?.publicKey,
});
} else {
_group = await weshClient.client.GroupInfo({
contactPk: bytesFromString(conversation.members[0].id),
});
await weshClient.client.ActivateGroup({
groupPk: _group.group?.publicKey,
});
}

setGroupInfo(_group);
} catch (err: any) {
setToastError({
title: "Failed to get group info",
message: err?.message,
});
}
}, [conversation, setToastError]);

useEffect(() => {
getGroupInfo();
}, [getGroupInfo]);

const handleSend = async (data?: any) => {
if (!message && !data?.message) {
return;
}
try {
await sendMessage({
groupPk: groupInfo?.group?.publicKey,
groupPk: bytesFromString(conversation.id),
message: {
type: "message",
parentId: replyTo?.id || "",
Expand Down Expand Up @@ -240,7 +190,7 @@ export const ChatSection = ({ conversation }: ChatSectionProps) => {
}}
conversation={conversation}
message={item}
groupPk={groupInfo?.group?.publicKey}
groupPk={bytesFromString(conversation.id)}
/>
</>
);
Expand Down Expand Up @@ -298,7 +248,7 @@ export const ChatSection = ({ conversation }: ChatSectionProps) => {
conversation={conversation}
onReply={setReplyTo}
message={item}
groupPk={groupInfo?.group?.publicKey}
groupPk={bytesFromString(conversation.id)}
isMessageChain={
previousMessage?.senderId === item.senderId
}
Expand Down Expand Up @@ -423,24 +373,22 @@ export const ChatSection = ({ conversation }: ChatSectionProps) => {
<TextInputCustom
fullWidth
setRef={setInputRef}
containerStyle={{}}
autoFocus={Platform.OS === "web"}
height={Math.max(50, inputHeight)}
height={Math.max(50, Math.min(inputHeight - 50, 120))}
name="message"
placeHolder={
replyTo?.message ? "Add reply message" : "Add a Message"
}
multiline={Platform.OS !== "web"}
multiline
numberOfLines={6}
value={message}
onChangeText={setMessage}
label=""
hideLabel
blurOnSubmit={false}
textInputStyle={
{
// height: Math.max(30, inputHeight - 30),
}
}
textInputStyle={{
height: Math.max(30, Math.min(inputHeight - 30, 100)),
}}
returnKeyType="send"
onSubmitEditing={() => {
if (message.length) {
Expand Down
2 changes: 1 addition & 1 deletion packages/screens/Message/components/Conversation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const Conversation = ({
const [isForwarding, setIsForwarding] = useState(false);

const isSender =
message.senderId === stringFromBytes(weshConfig.config.accountPk);
message.senderId === stringFromBytes(weshConfig?.config?.accountPk);

const reactions = useMemo(() => {
if (!message?.reactions?.length) {
Expand Down
39 changes: 21 additions & 18 deletions packages/screens/Message/components/SideBarChats.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useEffect } from "react";
import {
Platform,
ScrollView,
TouchableOpacity,
View,
useWindowDimensions,
Expand Down Expand Up @@ -90,24 +91,26 @@ export const SideBarChats = () => {
<Separator horizontal={false} color={neutral22} />
<SpacerColumn size={1.5} />
</>
<View style={{ zIndex: 1 }}>
{conversationList.map((item, index) => (
<ChatItem
data={item}
key={index}
isActive={item.id === activeConversation?.id}
onPress={() => {
if (Platform.OS === "web") {
setActiveConversation?.(item);
navigate("Message");
} else {
navigate("ChatSection", item);
}
}}
isLastItem={index === conversationList.length - 1}
/>
))}
</View>
<ScrollView style={{}}>
<View style={{ zIndex: 1 }}>
{conversationList.map((item, index) => (
<ChatItem
data={item}
key={index}
isActive={item.id === activeConversation?.id}
onPress={() => {
if (Platform.OS === "web") {
setActiveConversation?.(item);
navigate("Message");
} else {
navigate("ChatSection", item);
}
}}
isLastItem={index === conversationList.length - 1}
/>
))}
</View>
</ScrollView>
</View>
);
};
16 changes: 11 additions & 5 deletions packages/store/store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { combineReducers, configureStore } from "@reduxjs/toolkit";
import { useDispatch } from "react-redux";
import { persistStore, persistReducer } from "redux-persist";
import { persistStore, persistReducer, REHYDRATE } from "redux-persist";

import { persistConfig } from "./config";
import { dAppsReducer, dAppsReducerPersisted } from "./slices/dapps-store";
Expand Down Expand Up @@ -37,15 +37,21 @@ const rootReducer = combineReducers({

const persistedReducer = persistReducer(persistConfig, rootReducer);

const createRehydrateRootReducer = (reducer) => (state, action) => {
if (action.type === REHYDRATE) {
console.log("rehydrated");
bootWeshModule();
}
return reducer(state, action);
};

export const store = configureStore({
reducer: persistedReducer,
reducer: createRehydrateRootReducer(persistedReducer),
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({ serializableCheck: false }),
});

export const persistor = persistStore(store, null, () => {
bootWeshModule();
});
export const persistor = persistStore(store, null);

export type RootState = ReturnType<typeof store.getState>;

Expand Down
2 changes: 1 addition & 1 deletion packages/weshnet/client/processData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export const handleMetadata = async (data: GroupMetadataEvent) => {

store.dispatch(
setContactRequestList({
id: parsedData.eventContext.id,
id: stringFromBytes(parsedData.payload.contactPk),
contactId: stringFromBytes(parsedData.payload.contactPk),
rdvSeed: stringFromBytes(parsedData.payload.contactRendezvousSeed),
name: parsedData.payload.contactMetadata.name,
Expand Down
18 changes: 15 additions & 3 deletions packages/weshnet/client/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ import { Platform } from "react-native";

import { weshClient } from "./client";
import { weshConfig } from "./config";
import { subscribeMetadata } from "./subscribers";
import { subscribeMessages, subscribeMetadata } from "./subscribers";
import { bytesFromString, encodeJSON, stringFromBytes } from "./utils";
import {
MessageState,
selectConversationList,
setContactInfo,
setPeerList,
} from "../../store/slices/message";
import { store } from "../../store/store";
import { Message } from "../../utils/types/message";
import { CONVERSATION_TYPES, Message } from "../../utils/types/message";
import { GroupInfo_Request } from "../protocoltypes";

let getPeerListIntervalId;
Expand Down Expand Up @@ -69,10 +70,21 @@ export const bootWeshnet = async () => {
}
getPeerListIntervalId = setInterval(() => {
getAndUpdatePeerList();
}, 60 * 1000);
}, 30 * 1000);
} catch (err) {
console.log("create config err", err);
}
bootSubscribeMessages();
};

const bootSubscribeMessages = () => {
const conversations = selectConversationList(CONVERSATION_TYPES.ACTIVE)(
store.getState()
);

conversations.forEach((item) => {
subscribeMessages(item.id);
});
};

export const createSharableLink = (
Expand Down
Loading

0 comments on commit 796fd6d

Please sign in to comment.