Skip to content

Commit

Permalink
optimisic profile update
Browse files Browse the repository at this point in the history
  • Loading branch information
mozzius committed Sep 20, 2023
1 parent 2496e9f commit 5b82dd1
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions apps/expo/src/components/screens/profile/profile-info.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ import { useColorScheme } from "nativewind";

import { blockAccount, muteAccount } from "~/lib/account-actions";
import { useAgent } from "~/lib/agent";
import { useHaptics } from "~/lib/hooks/preferences";
import { cx } from "~/lib/utils/cx";
import { produce } from "~/lib/utils/produce";
import { useLists } from "../../lists/context";
import { RichTextWithoutFacets } from "../../rich-text";
import { Text } from "../../text";
Expand Down Expand Up @@ -66,6 +68,7 @@ export const ProfileInfo = ({ profile, backButton }: Props) => {
const { colorScheme } = useColorScheme();
const queryClient = useQueryClient();
const theme = useTheme();
const haptics = useHaptics();

const toggleFollow = useMutation({
mutationKey: ["follow", profile.did],
Expand All @@ -76,10 +79,28 @@ export const ProfileInfo = ({ profile, backButton }: Props) => {
await agent.follow(profile.did);
}
},
onMutate: () => {
haptics.impact();

// Optimistically update the profile
const updated = produce(profile, (draft) => {
if (draft.viewer) {
if (draft.viewer.following) {
delete draft.viewer.following;
} else {
draft.viewer.following = "pending";
}
}
});

queryClient.setQueryData(["profile", profile.handle], updated);
queryClient.setQueryData(["profile", profile.did], updated);
},
onSettled: () => {
void queryClient.invalidateQueries(["profile"]);
void queryClient.invalidateQueries(["network"]);
},
onError: (err: Error) => Alert.alert("Error", err.message),
});

useEffect(() => {
Expand All @@ -91,6 +112,7 @@ export const ProfileInfo = ({ profile, backButton }: Props) => {
alt: profile.displayName ?? `@${profile.handle}`,
fullsize: profile.avatar,
thumb: profile.avatar,
aspectRatio: { width: 1, height: 1 },
} satisfies AppBskyEmbedImages.ViewImage,
],
);
Expand Down

1 comment on commit 5b82dd1

@vercel
Copy link

@vercel vercel bot commented on 5b82dd1 Sep 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.