Skip to content

Commit

Permalink
fix: mirror cache (#4139)
Browse files Browse the repository at this point in the history
  • Loading branch information
bigint authored Dec 7, 2023
1 parent 8ed6045 commit 8720706
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
8 changes: 8 additions & 0 deletions apps/web/src/components/Publication/Actions/Share/Mirror.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ const Mirror: FC<MirrorProps> = ({ isLoading, publication, setIsLoading }) => {
checkDispatcherPermissions(currentProfile);

const updateCache = () => {
cache.modify({
fields: {
operations: (existingValue) => {
return { ...existingValue, hasMirrored: true };
}
},
id: cache.identify(targetPublication)
});
cache.modify({
fields: { mirrors: () => shares + 1 },
id: cache.identify(targetPublication.stats)
Expand Down
23 changes: 18 additions & 5 deletions apps/web/src/components/Publication/Actions/Share/UndoMirror.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { ArrowsRightLeftIcon } from '@heroicons/react/24/outline';
import { Errors } from '@hey/data/errors';
import { PUBLICATION } from '@hey/data/tracking';
import { useHidePublicationMutation } from '@hey/lens';
import { useApolloClient } from '@hey/lens/apollo';
import { isMirrorPublication } from '@hey/lib/publicationHelpers';
import cn from '@hey/ui/cn';
import errorToast from '@lib/errorToast';
import { Leafwatch } from '@lib/leafwatch';
Expand All @@ -24,6 +26,21 @@ const UndoMirror: FC<MirrorProps> = ({
setIsLoading
}) => {
const currentProfile = useProfileStore((state) => state.currentProfile);
const targetPublication = isMirrorPublication(publication)
? publication?.mirrorOn
: publication;

const { cache } = useApolloClient();

const updateCache = () => {
cache.modify({
fields: { mirrors: () => targetPublication.stats.mirrors - 1 },
id: cache.identify(targetPublication.stats)
});
cache.evict({
id: `${publication?.__typename}:${publication?.id}`
});
};

const onError = (error?: any) => {
setIsLoading(false);
Expand All @@ -35,11 +52,7 @@ const UndoMirror: FC<MirrorProps> = ({
Leafwatch.track(PUBLICATION.UNDO_MIRROR);
toast.success('Undone mirror successfully');
},
update: (cache) => {
cache.evict({
id: `${publication?.__typename}:${publication?.id}`
});
}
update: updateCache
});

const undoMirror = async () => {
Expand Down

1 comment on commit 8720706

@vercel
Copy link

@vercel vercel bot commented on 8720706 Dec 7, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

web – ./

web-heyxyz.vercel.app
heyxyz.vercel.app
web-git-main-heyxyz.vercel.app
hey.xyz

Please sign in to comment.