diff --git a/package.json b/package.json index eda8c4d7670..387e1c61806 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "start:prod": "NODE_ENV=production node build/server.js" }, "dependencies": { - "@ecency/ns-query": "^1.0.4", + "@ecency/ns-query": "^1.0.5", "@ecency/render-helper": "^2.2.29", "@ecency/render-helper-amp": "^1.1.0", "@emoji-mart/data": "^1.1.2", diff --git a/src/common/features/chats/components/chat-input.tsx b/src/common/features/chats/components/chat-input.tsx index cc83d1794f1..6a4e50e837b 100644 --- a/src/common/features/chats/components/chat-input.tsx +++ b/src/common/features/chats/components/chat-input.tsx @@ -22,11 +22,10 @@ import { Channel, ChatContext, DirectContact, - getUserChatPublicKey, useChannelsQuery, + useGetPublicKeysQuery, useSendMessage } from "@ecency/ns-query"; -import { useGetAccountFullQuery } from "../../../api/queries"; import Tooltip from "../../../components/tooltip"; import { ChatInputFiles } from "./chat-input-files"; import Gallery from "../../../components/gallery"; @@ -52,7 +51,9 @@ export default function ChatInput({ currentChannel, currentContact }: Props) { const [showGifPicker, setShowGifPicker] = useState(false); const [showGallery, setShowGallery] = useState(false); - const { data: contactData } = useGetAccountFullQuery(currentContact?.name); + const { data: contactKeys, isLoading: isContactKeysLoading } = useGetPublicKeysQuery( + currentContact?.name + ); const { mutateAsync: sendMessage, isLoading: isSendMessageLoading } = useSendMessage( currentChannel, currentContact, @@ -68,9 +69,10 @@ export default function ChatInput({ currentChannel, currentContact }: Props) { () => isCurrentUser && !receiverPubKey, [isCurrentUser, receiverPubKey] ); + const isJoined = useMemo(() => (contactKeys ? contactKeys.pubkey : false), [contactKeys]); const isReadOnly = useMemo( - () => (contactData ? currentContact?.pubkey !== getUserChatPublicKey(contactData) : false), - [contactData, currentContact] + () => (contactKeys && isJoined ? currentContact?.pubkey !== contactKeys.pubkey : false), + [contactKeys, currentContact, isJoined] ); const isFilesUploading = useMemo( () => (files.length > 0 ? files.length !== uploadedFileLinks.length : false), diff --git a/src/common/features/chats/components/chat-popup/chat-direct-contact-or-channel-item.tsx b/src/common/features/chats/components/chat-popup/chat-direct-contact-or-channel-item.tsx deleted file mode 100644 index e0470ee3402..00000000000 --- a/src/common/features/chats/components/chat-popup/chat-direct-contact-or-channel-item.tsx +++ /dev/null @@ -1,78 +0,0 @@ -import React, { useMemo } from "react"; -import UserAvatar from "../../../../components/user-avatar"; -import { - Channel, - DirectContact, - getUserChatPublicKey, - isCommunity, - useLastMessageQuery -} from "@ecency/ns-query"; -import { useCommunityCache } from "../../../../core"; -import { classNameObject } from "../../../../helper/class-name-object"; -import { useGetAccountFullQuery } from "../../../../api/queries"; -import { _t } from "../../../../i18n"; -import Tooltip from "../../../../components/tooltip"; -import { Button } from "@ui/button"; -import { informationOutlineSvg } from "../../../../img/svg"; - -interface Props { - username: string; - contact?: DirectContact; - channel?: Channel; - userClicked: (username: string) => void; -} - -export function ChatDirectContactOrChannelItem({ contact, channel, username, userClicked }: Props) { - const lastMessage = useLastMessageQuery(contact, channel); - const { data: community } = useCommunityCache(channel?.communityName); - - const { data: contactData } = useGetAccountFullQuery(contact?.name); - - const isJoined = useMemo( - () => (contactData ? !!getUserChatPublicKey(contactData) : true), - [contactData] - ); - const isReadOnly = useMemo( - () => (contactData && isJoined ? contact?.pubkey !== getUserChatPublicKey(contactData) : false), - [contactData, contact, isJoined] - ); - return ( -
userClicked(username)} - > -
- -
- -
-
- {isReadOnly && ( -
- {_t("chat.read-only")} - -
- )} - {!isJoined && ( -
- {_t("chat.user-not-joined")} - -
- )} -
- {isCommunity(username) && community ? community.title : username} -
-
-

{lastMessage?.content}

-
-
- ); -} diff --git a/src/common/features/chats/components/chat-popup/chat-popup-search-user.tsx b/src/common/features/chats/components/chat-popup/chat-popup-search-user.tsx index 96fc86ae163..f5d61555be8 100644 --- a/src/common/features/chats/components/chat-popup/chat-popup-search-user.tsx +++ b/src/common/features/chats/components/chat-popup/chat-popup-search-user.tsx @@ -8,6 +8,7 @@ import { Community } from "../../../../store/communities"; import { isCommunity } from "@ecency/ns-query"; import { Reputations } from "../../../../api/hive"; import { useCreateTemporaryChannel } from "../../hooks/user-create-temporary-channel"; +import useDebounce from "react-use/lib/useDebounce"; interface Props { onCommunityClicked: (communityName: string) => void; @@ -15,7 +16,8 @@ interface Props { export function ChatPopupSearchUser({ onCommunityClicked }: Props) { const [searchQuery, setSearchQuery] = useState(""); - const [selectedUser, setSelectedUser] = useState(""); + const [selectedUser, setSelectedUser] = useState(""); + const [searchInput, setSearchInput] = useState(""); const [selectedCommunity, setSelectedCommunity] = useState(""); const { data: searchUsers } = useSearchUsersQuery(searchQuery); @@ -24,16 +26,27 @@ export function ChatPopupSearchUser({ onCommunityClicked }: Props) { useCreateTemporaryContact(selectedUser); useCreateTemporaryChannel(selectedCommunity); + useDebounce( + () => { + if (searchInput) { + setSearchQuery(searchInput); + } + }, + 500, + [searchInput] + ); + return ( <>
- +
{[...searchUsers, ...searchCommunities].map((item) => ( { + setSearchInput(""); setSearchQuery(""); const community = item as Community; diff --git a/src/common/features/chats/components/chats-sidebar/chat-sidebar-direct-contact.tsx b/src/common/features/chats/components/chats-sidebar/chat-sidebar-direct-contact.tsx index 4e0f53bbab2..648c536bde6 100644 --- a/src/common/features/chats/components/chats-sidebar/chat-sidebar-direct-contact.tsx +++ b/src/common/features/chats/components/chats-sidebar/chat-sidebar-direct-contact.tsx @@ -5,11 +5,10 @@ import { ChatContext, DirectContact, getRelativeDate, - getUserChatPublicKey, + useGetPublicKeysQuery, useKeysQuery, useLastMessageQuery } from "@ecency/ns-query"; -import { useGetAccountFullQuery } from "../../../../api/queries"; import { _t } from "../../../../i18n"; import Tooltip from "../../../../components/tooltip"; import { informationOutlineSvg } from "../../../../img/svg"; @@ -27,26 +26,23 @@ export function ChatSidebarDirectContact({ contact, onClick, isLink = true }: Pr useContext(ChatContext); const { publicKey } = useKeysQuery(); - const { data: contactData, isLoading: isContactDataLoading } = useGetAccountFullQuery( + const { data: contactKeys, isLoading: isContactKeysLoading } = useGetPublicKeysQuery( contact.name ); const lastMessage = useLastMessageQuery(contact); const lastMessageDate = useMemo(() => getRelativeDate(lastMessage?.created), [lastMessage]); - const isJoined = useMemo( - () => (contactData ? !!getUserChatPublicKey(contactData) : false), - [contactData] - ); + const isJoined = useMemo(() => (contactKeys ? contactKeys.pubkey : false), [contactKeys]); const isReadOnly = useMemo( - () => (contactData && isJoined ? contact.pubkey !== getUserChatPublicKey(contactData) : false), - [contactData, contact, isJoined] + () => (contactKeys && isJoined ? contact.pubkey !== contactKeys.pubkey : false), + [contactKeys, contact, isJoined] ); const content = ( <>
@@ -54,7 +50,7 @@ export function ChatSidebarDirectContact({ contact, onClick, isLink = true }: Pr
- {isReadOnly && !isContactDataLoading && ( + {isReadOnly && !isContactKeysLoading && (
{_t("chat.read-only")} @@ -62,7 +58,7 @@ export function ChatSidebarDirectContact({ contact, onClick, isLink = true }: Pr
)} - {!isJoined && !isContactDataLoading && ( + {!isJoined && !isContactKeysLoading && (
{_t("chat.user-not-joined")} diff --git a/src/common/features/chats/components/chats-welcome.tsx b/src/common/features/chats/components/chats-welcome.tsx index 7c18c89af5e..6359659479c 100644 --- a/src/common/features/chats/components/chats-welcome.tsx +++ b/src/common/features/chats/components/chats-welcome.tsx @@ -2,20 +2,16 @@ import React, { useContext, useEffect, useMemo, useState } from "react"; import { _t } from "../../../i18n"; import { CodeInput } from "@ui/input"; import OrDivider from "../../../components/or-divider"; -import { useGetAccountFullQuery } from "../../../api/queries"; -import { useMappedStore } from "../../../store/use-mapped-store"; import { CreateAnAccount } from "./create-an-account"; import { ChatsImport } from "./chats-import"; -import { ChatContext, getUserChatPublicKey, useRestoreChatByPin } from "@ecency/ns-query"; +import { ChatContext, useKeysQuery, useRestoreChatByPin } from "@ecency/ns-query"; export function ChatsWelcome() { - const { activeUser } = useMappedStore(); - const [step, setStep] = useState(0); const [pin, setPin] = useState(""); const { hasUserJoinedChat } = useContext(ChatContext); - const { data: fullAccount } = useGetAccountFullQuery(activeUser?.username); + const { publicKey } = useKeysQuery(); const { mutateAsync: restoreByPin, @@ -23,13 +19,7 @@ export function ChatsWelcome() { isLoading: isRestoreLoading } = useRestoreChatByPin(); - const isAlreadyRegisteredInChats = useMemo(() => { - if (!fullAccount) { - return false; - } - const publicKey = getUserChatPublicKey(fullAccount); - return !!publicKey; - }, [fullAccount]); + const isAlreadyRegisteredInChats = useMemo(() => !!publicKey, [publicKey]); useEffect(() => { // Handle PIN on account restoring diff --git a/src/common/features/chats/hooks/use-create-temporary-contact.ts b/src/common/features/chats/hooks/use-create-temporary-contact.ts index 03f55f30e0e..e04ee123ef3 100644 --- a/src/common/features/chats/hooks/use-create-temporary-contact.ts +++ b/src/common/features/chats/hooks/use-create-temporary-contact.ts @@ -1,18 +1,17 @@ import { useContext, useEffect } from "react"; import { useQueryClient } from "@tanstack/react-query"; -import { ChatContext, ChatQueries, DirectContact, getUserChatPublicKey } from "@ecency/ns-query"; -import { useGetAccountFullQuery } from "../../../api/queries"; +import { ChatContext, ChatQueries, DirectContact, useGetPublicKeysQuery } from "@ecency/ns-query"; export function useCreateTemporaryContact(selectedAccount: string) { const queryClient = useQueryClient(); const { setReceiverPubKey, activeUsername } = useContext(ChatContext); - const { data: selectedAccountData } = useGetAccountFullQuery(selectedAccount); + const { data: contactKeys, isFetched, isError } = useGetPublicKeysQuery(selectedAccount); // Create temporary contact and select it when searching users // `not_joined_${selectedAccount}` – special constructor for creating a temporary contact return useEffect(() => { - if (selectedAccount && selectedAccountData) { + if (selectedAccount && (isFetched || isError)) { queryClient.setQueryData( [ChatQueries.DIRECT_CONTACTS, activeUsername], [ @@ -22,13 +21,11 @@ export function useCreateTemporaryContact(selectedAccount: string) { ]) ?? []), { name: selectedAccount, - pubkey: getUserChatPublicKey(selectedAccountData) ?? `not_joined_${selectedAccount}` + pubkey: contactKeys?.pubkey ?? `not_joined_${selectedAccount}` } ] ); - setReceiverPubKey( - getUserChatPublicKey(selectedAccountData) ?? `not_joined_${selectedAccount}` - ); + setReceiverPubKey(contactKeys?.pubkey ?? `not_joined_${selectedAccount}`); } - }, [selectedAccount, selectedAccountData]); + }, [selectedAccount, contactKeys, isFetched, isError]); } diff --git a/yarn.lock b/yarn.lock index 8a10d1f6e14..25405516020 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1374,10 +1374,10 @@ resolved "https://registry.yarnpkg.com/@discoveryjs/json-ext/-/json-ext-0.5.7.tgz#1d572bfbbe14b7704e0ba0f39b74815b84870d70" integrity sha512-dBVuXR082gk3jsFp7Rd/JI4kytwGHecnCoTtXFb7DB6CNHp4rg5k1bhg0nWdLGLnOV71lmDzGQaLMy8iPLY0pw== -"@ecency/ns-query@^1.0.4": - version "1.0.4" - resolved "https://registry.yarnpkg.com/@ecency/ns-query/-/ns-query-1.0.4.tgz#2086351741eb74bb7c7346af16275fd7edcd086a" - integrity sha512-5+S+9LoWNA7Mj/9UN8+cFVHMz/17IMAL7wFw4d2Yn7XvtD6AMU71iBXuAFvkZgjZaJXz5bnmbJrnpnPyrGUhXg== +"@ecency/ns-query@^1.0.5": + version "1.0.5" + resolved "https://registry.yarnpkg.com/@ecency/ns-query/-/ns-query-1.0.5.tgz#ed89550dc0ec1999cfd620e4d6ed0b2275423d54" + integrity sha512-1ZAKyPnD/dQV95BQeE0N5xotWLMASRMg04P2sqCQ+NBU/tChC7dby12IjFIdI8ofJurMX8rSUSFY8bZtoK9kGA== dependencies: "@tanstack/react-query" "^4.29.7" axios "^0.21.2"