Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: invalidate content preference from tag and source #4087

Merged
merged 2 commits into from
Jan 21, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 73 additions & 1 deletion packages/shared/src/hooks/useTagAndSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import useMutateFilters from './useMutateFilters';
import type { Source } from '../graphql/sources';
import AlertContext from '../contexts/AlertContext';
import type { BooleanPromise } from '../components/filters/common';
import { generateQueryKey } from '../lib/query';
import { generateQueryKey, RequestKey } from '../lib/query';
import useDebounceFn from './useDebounceFn';
import { SharedFeedPage } from '../components/utilities';
import type { Origin } from '../lib/log';
import { LogEvent } from '../lib/log';
import type { AuthTriggersType } from '../lib/auth';
import { ContentPreferenceType } from '../graphql/contentPreference';

export interface TagActionArguments {
tags: Array<string>;
Expand Down Expand Up @@ -82,6 +83,29 @@ export default function useTagAndSource({
});
}, 100);

const invalidateContentPreferences = useCallback(
({
entity,
subQuery,
}: {
entity: ContentPreferenceType;
subQuery: RequestKey;
}) => {
queryClient.invalidateQueries({
queryKey: generateQueryKey(
RequestKey.ContentPreference,
user,
subQuery,
{
feedId: feedId || user?.id,
entity,
},
),
});
},
[queryClient, user, feedId],
);

const onFollowTags = useCallback(
async ({ tags, category, requireLogin }: TagActionArguments) => {
if (shouldShowLogin(requireLogin)) {
Expand All @@ -101,6 +125,11 @@ export default function useTagAndSource({

invalidateQueries();

invalidateContentPreferences({
entity: ContentPreferenceType.Keyword,
subQuery: RequestKey.UserFollowing,
});
Comment on lines +128 to +131
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Create new function without debounce since invalidation happens for different queries then the ones shown.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Debounce also does not support function having args


return { successful: true };
},
[
Expand All @@ -115,6 +144,7 @@ export default function useTagAndSource({
alerts?.filter,
invalidateQueries,
shouldUpdateAlerts,
invalidateContentPreferences,
],
);

Expand All @@ -134,6 +164,11 @@ export default function useTagAndSource({

invalidateQueries();

invalidateContentPreferences({
entity: ContentPreferenceType.Keyword,
subQuery: RequestKey.UserFollowing,
});

return { successful: true };
},
[
Expand All @@ -144,6 +179,7 @@ export default function useTagAndSource({
showLogin,
unfollowTags,
invalidateQueries,
invalidateContentPreferences,
],
);

Expand All @@ -164,6 +200,11 @@ export default function useTagAndSource({

invalidateQueries();

invalidateContentPreferences({
entity: ContentPreferenceType.Keyword,
subQuery: RequestKey.UserBlocked,
});

return { successful: true };
},
[
Expand All @@ -175,6 +216,7 @@ export default function useTagAndSource({
blockTag,
postId,
invalidateQueries,
invalidateContentPreferences,
],
);

Expand All @@ -194,6 +236,11 @@ export default function useTagAndSource({

invalidateQueries();

invalidateContentPreferences({
entity: ContentPreferenceType.Keyword,
subQuery: RequestKey.UserBlocked,
});

return { successful: true };
},
[
Expand All @@ -205,6 +252,7 @@ export default function useTagAndSource({
unblockTag,
postId,
invalidateQueries,
invalidateContentPreferences,
],
);

Expand All @@ -225,6 +273,11 @@ export default function useTagAndSource({

invalidateQueries();

invalidateContentPreferences({
entity: ContentPreferenceType.Source,
subQuery: RequestKey.UserBlocked,
});

return { successful: true };
},
[
Expand All @@ -236,6 +289,7 @@ export default function useTagAndSource({
unblockSource,
postId,
invalidateQueries,
invalidateContentPreferences,
],
);

Expand All @@ -256,6 +310,11 @@ export default function useTagAndSource({

invalidateQueries();

invalidateContentPreferences({
entity: ContentPreferenceType.Source,
subQuery: RequestKey.UserBlocked,
});

return { successful: true };
},
[
Expand All @@ -267,6 +326,7 @@ export default function useTagAndSource({
blockSource,
postId,
invalidateQueries,
invalidateContentPreferences,
],
);

Expand All @@ -288,6 +348,11 @@ export default function useTagAndSource({

invalidateQueries();

invalidateContentPreferences({
entity: ContentPreferenceType.Source,
subQuery: RequestKey.UserFollowing,
});

return { successful: true };
},
[
Expand All @@ -299,6 +364,7 @@ export default function useTagAndSource({
followSource,
postId,
invalidateQueries,
invalidateContentPreferences,
],
);

Expand All @@ -320,6 +386,11 @@ export default function useTagAndSource({

invalidateQueries();

invalidateContentPreferences({
entity: ContentPreferenceType.Source,
subQuery: RequestKey.UserFollowing,
});

return { successful: true };
},
[
Expand All @@ -331,6 +402,7 @@ export default function useTagAndSource({
unfollowSource,
invalidateQueries,
showLogin,
invalidateContentPreferences,
],
);

Expand Down
Loading