diff --git a/components/react-query-wrapper/ReactQueryWrapper.tsx b/components/react-query-wrapper/ReactQueryWrapper.tsx index 9028dde3e5..8b24921283 100644 --- a/components/react-query-wrapper/ReactQueryWrapper.tsx +++ b/components/react-query-wrapper/ReactQueryWrapper.tsx @@ -8,7 +8,6 @@ import { } from "@/entities/IProfile"; import { RateMatter } from "@/enums"; import { ApiDrop } from "@/generated/models/ApiDrop"; -import { ApiFeedItemType } from "@/generated/models/ApiFeedItemType"; import { ApiProfileProxy } from "@/generated/models/ApiProfileProxy"; import { ApiWave } from "@/generated/models/ApiWave"; import { ApiWaveDropsFeed } from "@/generated/models/ApiWaveDropsFeed"; @@ -18,8 +17,7 @@ import { convertActivityLogParams } from "@/helpers/profile-logs.helpers"; import { Time } from "@/helpers/time"; import { CountlessPage, Page } from "@/helpers/Types"; import { useQueryKeyListener } from "@/hooks/useQueryKeyListener"; -import { TypedFeedItem } from "@/types/feed.types"; -import { InfiniteData, useQueryClient } from "@tanstack/react-query"; +import { InfiniteData, type QueryClient, useQueryClient } from "@tanstack/react-query"; import Cookies from "js-cookie"; import { createContext, useMemo } from "react"; import { ActivityLogParams } from "../profile-activity/ProfileActivityLogs"; @@ -135,12 +133,8 @@ type ReactQueryWrapperContextType = { readonly setProfileProxy: (profileProxy: ApiProfileProxy) => void; readonly onProfileProxyModify: ({ profileProxyId, - createdByHandle, - grantedToHandle, }: { readonly profileProxyId: string; - readonly createdByHandle: string; - readonly grantedToHandle: string; }) => void; onProfileCICModify: (params: { readonly targetProfile: ApiIdentity; @@ -174,8 +168,8 @@ type ReactQueryWrapperContextType = { }: { activityLogs: InitProfileActivityLogsParams; }) => void; - waitAndInvalidateDrops: () => void; - addOptimisticDrop: (params: { readonly drop: ApiDrop }) => void; + waitAndInvalidateDrops: () => Promise; + addOptimisticDrop: (params: { readonly drop: ApiDrop }) => Promise; readonly invalidateDrops: () => void; onGroupRemoved: ({ groupId }: { readonly groupId: string }) => void; onGroupChanged: ({ groupId }: { readonly groupId: string }) => void; @@ -208,8 +202,8 @@ export const ReactQueryWrapperContext = initProfileRepPage: () => { }, initProfileIdentityPage: () => { }, initCommunityActivityPage: () => { }, - waitAndInvalidateDrops: () => { }, - addOptimisticDrop: () => { }, + waitAndInvalidateDrops: async () => {}, + addOptimisticDrop: async () => {}, invalidateDrops: () => { }, onGroupRemoved: () => { }, onGroupChanged: () => { }, @@ -222,29 +216,25 @@ export const ReactQueryWrapperContext = invalidateIdentityTdhStats: () => { }, }); -export default function ReactQueryWrapper({ - children, -}: { - readonly children: React.ReactNode; -}) { - const queryClient = useQueryClient(); +const getHandlesFromProfile = (profile: ApiIdentity): string[] => { + const handles: string[] = []; + if (profile.handle) { + handles.push(profile.handle.toLowerCase()); + } - const getHandlesFromProfile = (profile: ApiIdentity): string[] => { - const handles: string[] = []; - if (profile.handle) { - handles.push(profile.handle.toLowerCase()); + profile.wallets?.forEach((wallet) => { + if (wallet.display) { + handles.push(wallet.display.toLowerCase()); } + handles.push(wallet.wallet.toLowerCase()); + }); - profile.wallets?.forEach((wallet) => { - if (wallet.display) { - handles.push(wallet.display.toLowerCase()); - } - handles.push(wallet.wallet.toLowerCase()); - }); - - return handles; - }; + return handles; +}; +const createReactQueryContextValue = ( + queryClient: QueryClient +): ReactQueryWrapperContextType => { const invalidateQueries = ({ key, values, @@ -349,12 +339,8 @@ export default function ReactQueryWrapper({ const onProfileProxyModify = ({ profileProxyId, - createdByHandle, - grantedToHandle, }: { readonly profileProxyId: string; - readonly createdByHandle: string; - readonly grantedToHandle: string; }): void => { queryClient.invalidateQueries({ queryKey: [QueryKey.PROFILE_PROXY, { id: profileProxyId }], @@ -362,9 +348,6 @@ export default function ReactQueryWrapper({ queryClient.invalidateQueries({ queryKey: [QueryKey.PROFILE_PROFILE_PROXIES], }); - queryClient.invalidateQueries({ - queryKey: [QueryKey.PROFILE_PROFILE_PROXIES], - }); }; const invalidateProfileRaterCICState = ({ @@ -804,152 +787,6 @@ export default function ReactQueryWrapper({ }); }; - const increaseFeedItemsDropRedropCount = ({ - drop, - }: { - readonly drop: ApiDrop; - }): void => { - queryClient.setQueryData( - [QueryKey.FEED_ITEMS], - ( - oldData: - | { - pages: TypedFeedItem[][]; - } - | undefined - ) => { - if (!oldData?.pages.length) { - return oldData; - } - const pages: TypedFeedItem[][] = JSON.parse( - JSON.stringify(oldData.pages) - ); - const quotedDrops = drop.parts - .map((part) => part.quoted_drop) - .filter((quotedDrop) => !!quotedDrop); - if (quotedDrops.length) { - const modifiedPages = pages.map((items) => { - const modifiedItems = items.map((item) => { - if (item.type === ApiFeedItemType.DropCreated) { - const modifiedParts = item.item.parts.map((part) => { - const isQuoted = quotedDrops.find( - (qd) => - qd && - item.item.id === qd.drop_id && - part.part_id === qd.drop_part_id - ); - - return part; - }); - return { - ...item, - item: { - ...item.item, - parts: modifiedParts, - }, - }; - } - if (item.type === ApiFeedItemType.DropReplied) { - const modifiedParts = item.item.reply.parts.map((part) => { - const isQuoted = quotedDrops.find( - (qd) => - qd && - item.item.reply.id === qd.drop_id && - part.part_id === qd.drop_part_id - ); - - return part; - }); - return { - ...item, - item: { - ...item.item, - reply: { - ...item.item.reply, - parts: modifiedParts, - }, - }, - }; - } - return item; - }); - return modifiedItems; - }); - return { - ...oldData, - pages: modifiedPages, - }; - } - return { - ...oldData, - pages, - }; - } - ); - }; - - const increaseDropsDropRedropCount = ({ - drop, - }: { - readonly drop: ApiDrop; - }): void => { - queryClient.setQueryData( - [ - QueryKey.DROPS, - { - limit: `10`, - context_profile: drop.author.handle, - wave_id: drop.wave.id, - include_replies: "true", - }, - ], - ( - oldData: - | { - pages: ApiDrop[][]; - } - | undefined - ) => { - if (!oldData?.pages.length) { - return oldData; - } - const pages: ApiDrop[][] = JSON.parse(JSON.stringify(oldData.pages)); - const quotedDrops = drop.parts - .map((part) => part.quoted_drop) - .filter((quotedDrop) => !!quotedDrop); - if (quotedDrops.length) { - const modifiedPages = pages.map((items) => { - const modifiedItems = items.map((item) => { - const modifiedParts = item.parts.map((part) => { - const isQuoted = quotedDrops.find( - (qd) => - qd && - item.id === qd.drop_id && - part.part_id === qd.drop_part_id - ); - return part; - }); - - return { - ...item, - parts: modifiedParts, - }; - }); - return modifiedItems; - }); - return { - ...oldData, - pages: modifiedPages, - }; - } - return { - ...oldData, - pages, - }; - } - ); - }; - const addReplyToDropDiscussion = ({ drop, }: { @@ -994,82 +831,6 @@ export default function ReactQueryWrapper({ ); }; - const increaseFeedItemsDropDiscussionCount = ({ - drop, - }: { - readonly drop: ApiDrop; - }): void => { - queryClient.setQueryData( - [QueryKey.FEED_ITEMS], - ( - oldData: - | { - pages: TypedFeedItem[][]; - } - | undefined - ) => { - if (!oldData?.pages.length) { - return oldData; - } - const pages: TypedFeedItem[][] = JSON.parse( - JSON.stringify(oldData.pages) - ); - const repliedDrop = drop.reply_to; - if (repliedDrop) { - const modifiedPages = pages.map((items) => { - const modifiedItems = items.map((item) => { - if (item.type === ApiFeedItemType.DropCreated) { - const modifiedParts = item.item.parts.map((part) => { - const isReplied = - item.item.id === repliedDrop.drop_id && - part.part_id === repliedDrop.drop_part_id; - - return part; - }); - return { - ...item, - item: { - ...item.item, - parts: modifiedParts, - }, - }; - } - if (item.type === ApiFeedItemType.DropReplied) { - const modifiedParts = item.item.reply.parts.map((part) => { - const isReplied = - item.item.reply.id === repliedDrop.drop_id && - part.part_id === repliedDrop.drop_part_id; - - return part; - }); - return { - ...item, - item: { - ...item.item, - reply: { - ...item.item.reply, - parts: modifiedParts, - }, - }, - }; - } - return item; - }); - return modifiedItems; - }); - return { - ...oldData, - pages: modifiedPages, - }; - } - return { - ...oldData, - pages, - }; - } - ); - }; - const addOptimisticDrop = async ({ drop, }: { @@ -1077,11 +838,8 @@ export default function ReactQueryWrapper({ }): Promise => { addDropToDrops(queryClient, { drop }); increaseWavesOverviewDropsCount(queryClient, drop.wave.id); - increaseFeedItemsDropRedropCount({ drop }); - increaseDropsDropRedropCount({ drop }); if (drop.reply_to) { addReplyToDropDiscussion({ drop }); - increaseFeedItemsDropDiscussionCount({ drop }); } }; @@ -1213,6 +971,44 @@ export default function ReactQueryWrapper({ }); }; + return { + setProfile, + setWave, + setWavesOverviewPage, + setWaveDrops, + setProfileProxy, + onProfileProxyModify, + onProfileCICModify, + onProfileRepModify, + onProfileEdit, + onProfileStatementAdd, + onProfileStatementRemove, + initProfileRepPage, + initProfileIdentityPage, + initCommunityActivityPage, + onGroupRemoved, + onGroupChanged, + waitAndInvalidateDrops, + addOptimisticDrop, + onIdentityBulkRate, + onGroupCreate, + onWaveCreated, + onWaveFollowChange, + invalidateAll, + onIdentityFollowChange, + invalidateDrops, + invalidateNotifications, + invalidateIdentityTdhStats, + }; +}; + +export default function ReactQueryWrapper({ + children, +}: { + readonly children: React.ReactNode; +}) { + const queryClient = useQueryClient(); + useQueryKeyListener([QueryKey.FEED_ITEMS], () => { Cookies.set([QueryKey.FEED_ITEMS].toString(), `${Time.now().toMillis()}`); }); @@ -1225,64 +1021,8 @@ export default function ReactQueryWrapper({ }); const value = useMemo( - () => ({ - setProfile, - setWave, - setWavesOverviewPage, - setWaveDrops, - setProfileProxy, - onProfileProxyModify, - onProfileCICModify, - onProfileRepModify, - onProfileEdit, - onProfileStatementAdd, - onProfileStatementRemove, - initProfileRepPage, - initProfileIdentityPage, - initCommunityActivityPage, - onGroupRemoved, - onGroupChanged, - waitAndInvalidateDrops, - addOptimisticDrop, - onIdentityBulkRate, - onGroupCreate, - onWaveCreated, - onWaveFollowChange, - invalidateAll, - onIdentityFollowChange, - invalidateDrops, - invalidateNotifications, - invalidateIdentityTdhStats, - }), - [ - setProfile, - setWave, - setWavesOverviewPage, - setWaveDrops, - setProfileProxy, - onProfileProxyModify, - onProfileCICModify, - onProfileRepModify, - onProfileEdit, - onProfileStatementAdd, - onProfileStatementRemove, - initProfileRepPage, - initProfileIdentityPage, - initCommunityActivityPage, - onGroupRemoved, - onGroupChanged, - waitAndInvalidateDrops, - addOptimisticDrop, - onIdentityBulkRate, - onGroupCreate, - onWaveCreated, - onWaveFollowChange, - invalidateAll, - onIdentityFollowChange, - invalidateDrops, - invalidateNotifications, - invalidateIdentityTdhStats, - ] + () => createReactQueryContextValue(queryClient), + [queryClient] ); return ( diff --git a/components/user/proxy/create/ProxyCreate.tsx b/components/user/proxy/create/ProxyCreate.tsx index f977cfd673..511bac77f5 100644 --- a/components/user/proxy/create/ProxyCreate.tsx +++ b/components/user/proxy/create/ProxyCreate.tsx @@ -84,9 +84,7 @@ export default function ProxyCreate({ } setProfileProxy(newProfileProxy); onProfileProxyModify({ - profileProxyId: newProfileProxy.id, - grantedToHandle: newProfileProxy.granted_to.handle, - createdByHandle: newProfileProxy.created_by.handle, + profileProxyId: newProfileProxy.id }); onModeChange(ProxyMode.LIST); }; diff --git a/components/user/proxy/proxy/action/ProxyActionAcceptanceButton.tsx b/components/user/proxy/proxy/action/ProxyActionAcceptanceButton.tsx index 4c3af3cf53..bc34b37df0 100644 --- a/components/user/proxy/proxy/action/ProxyActionAcceptanceButton.tsx +++ b/components/user/proxy/proxy/action/ProxyActionAcceptanceButton.tsx @@ -21,20 +21,6 @@ import HeaderProxyNewModal from "@/components/header/proxy/HeaderProxyNewModal"; import CommonAnimationWrapper from "@/components/utils/animation/CommonAnimationWrapper"; import CommonAnimationOpacity from "@/components/utils/animation/CommonAnimationOpacity"; -const ACTION_LABEL: Record = { - [AcceptActionRequestActionEnum.Accept]: "Accept", - [AcceptActionRequestActionEnum.Reject]: "Reject", - [AcceptActionRequestActionEnum.Revoke]: "Revoke", - [AcceptActionRequestActionEnum.Restore]: "Restore", -}; - -const ACTION_CLASSES: Record = { - [AcceptActionRequestActionEnum.Accept]: "tw-text-green", - [AcceptActionRequestActionEnum.Reject]: "tw-text-red", - [AcceptActionRequestActionEnum.Revoke]: "tw-text-red", - [AcceptActionRequestActionEnum.Restore]: "tw-text-green", -}; - export default function ProxyActionAcceptanceButton({ action, profile, @@ -112,16 +98,8 @@ export default function ProxyActionAcceptanceButton({ }); }, onSuccess: (_, variables) => { - if ( - !profileProxy.granted_to?.handle || - !profileProxy.created_by?.handle - ) { - return; - } onProfileProxyModify({ profileProxyId: profileProxy.id, - grantedToHandle: profileProxy.granted_to.handle, - createdByHandle: profileProxy.created_by.handle, }); setToast({ message: "Action status changed", @@ -190,14 +168,14 @@ export default function ProxyActionAcceptanceButton({ {possibleActions.includes( AcceptActionRequestActionEnum.Restore ) && ( - - )} + + )} diff --git a/components/user/proxy/proxy/action/utils/credit/ProfileProxyCreditEdit.tsx b/components/user/proxy/proxy/action/utils/credit/ProfileProxyCreditEdit.tsx index 212472cdc6..3828133dc8 100644 --- a/components/user/proxy/proxy/action/utils/credit/ProfileProxyCreditEdit.tsx +++ b/components/user/proxy/proxy/action/utils/credit/ProfileProxyCreditEdit.tsx @@ -1,6 +1,6 @@ "use client"; -import { useContext, useEffect, useState } from "react"; +import { useContext, useState } from "react"; import CommonInput from "@/components/utils/input/CommonInput"; import { ApiProfileProxy } from "@/generated/models/ApiProfileProxy"; import { ApiProfileProxyAction } from "@/generated/models/ApiProfileProxyAction"; @@ -29,21 +29,8 @@ export default function ProfileProxyCreditEdit({ profileProxyAction.credit_amount ?? 0 ); - const getIsChangedAndValid = () => { - if (profileProxyAction.credit_amount === creditAmount) { - return false; - } - if (creditAmount <= 0) { - return false; - } - return true; - }; - - const [isChangedAndValid, setIsChangedAndValid] = useState( - getIsChangedAndValid() - ); - - useEffect(() => setIsChangedAndValid(getIsChangedAndValid()), [creditAmount]); + const isChangedAndValid = + profileProxyAction.credit_amount !== creditAmount && creditAmount > 0; const [submitting, setSubmitting] = useState(false); const profileProxyActionCreditMutation = useMutation({ @@ -57,16 +44,8 @@ export default function ProfileProxyCreditEdit({ }); }, onSuccess: () => { - if ( - !profileProxy.granted_to?.handle || - !profileProxy.created_by?.handle - ) { - return; - } onProfileProxyModify({ profileProxyId: profileProxy.id, - grantedToHandle: profileProxy.granted_to.handle, - createdByHandle: profileProxy.created_by.handle, }); setToast({ message: "Action credit updated successfully!", @@ -121,13 +100,12 @@ export default function ProfileProxyCreditEdit({