nicolium: remove setQueryData/getQueryData explict typing
Signed-off-by: nicole mikołajczyk <git@mkljczk.pl>
This commit is contained in:
@@ -119,12 +119,12 @@ const importEntities =
|
||||
}
|
||||
if (!isEmpty(polls)) {
|
||||
for (const poll of Object.values(polls)) {
|
||||
queryClient.setQueryData<BasePoll>(queryKeys.statuses.polls.show(poll.id), poll);
|
||||
queryClient.setQueryData(queryKeys.statuses.polls.show(poll.id), poll);
|
||||
}
|
||||
}
|
||||
if (!isEmpty(relationships)) {
|
||||
for (const relationship of Object.values(relationships)) {
|
||||
queryClient.setQueryData<BaseRelationship>(
|
||||
queryClient.setQueryData(
|
||||
queryKeys.accountRelationships.show(relationship.id),
|
||||
relationship,
|
||||
);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { InfiniteData, useQueryClient } from '@tanstack/react-query';
|
||||
import { useQueryClient } from '@tanstack/react-query';
|
||||
import clsx from 'clsx';
|
||||
import debounce from 'lodash/debounce';
|
||||
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||
@@ -277,20 +277,17 @@ const NotificationsColumn: React.FC<INotificationsColumn> = ({ multiColumn }) =>
|
||||
}, [notifications, markNotificationsRead]);
|
||||
|
||||
const handleRefresh = useCallback(() => {
|
||||
queryClient.setQueryData<InfiniteData<any>>(
|
||||
queryKeys.notifications.list(activeFilter),
|
||||
(data) => {
|
||||
if (!data) return data;
|
||||
queryClient.setQueryData(queryKeys.notifications.list(activeFilter), (data) => {
|
||||
if (!data) return data;
|
||||
|
||||
// from https://github.com/TanStack/query/discussions/875#discussioncomment-754458
|
||||
// TODO: maybe needed in more places so maybe make a helper for this
|
||||
return {
|
||||
...data,
|
||||
pages: data.pages.slice(0, 1),
|
||||
pageParams: data.pageParams.slice(0, 1),
|
||||
};
|
||||
},
|
||||
);
|
||||
// from https://github.com/TanStack/query/discussions/875#discussioncomment-754458
|
||||
// TODO: maybe needed in more places so maybe make a helper for this
|
||||
return {
|
||||
...data,
|
||||
pages: data.pages.slice(0, 1),
|
||||
pageParams: data.pageParams.slice(0, 1),
|
||||
};
|
||||
});
|
||||
refetch().catch(console.error);
|
||||
}, [refetch]);
|
||||
|
||||
|
||||
@@ -35,10 +35,7 @@ const useAccount = (accountId?: string, withRelationship = false) => {
|
||||
queryKey: queryKeys.accounts.show(accountId!),
|
||||
queryFn: async () => {
|
||||
const account = await client.accounts.getAccount(accountId!);
|
||||
queryClient.setQueryData<string>(
|
||||
queryKeys.accounts.lookup(account.acct.toLowerCase()),
|
||||
account.id,
|
||||
);
|
||||
queryClient.setQueryData(queryKeys.accounts.lookup(account.acct.toLowerCase()), account.id);
|
||||
return account;
|
||||
},
|
||||
enabled: !!accountId,
|
||||
|
||||
@@ -15,7 +15,7 @@ const useAccounts = (accountIds: Array<string>) => {
|
||||
queryKey: queryKeys.accounts.show(accountId),
|
||||
queryFn: async () => {
|
||||
const response = await client.accounts.getAccount(accountId);
|
||||
queryClient.setQueryData<string>(
|
||||
queryClient.setQueryData(
|
||||
queryKeys.accounts.lookup(response.acct.toLowerCase()),
|
||||
response.id,
|
||||
);
|
||||
|
||||
@@ -45,7 +45,7 @@ const useDeleteAntenna = () => {
|
||||
mutationKey: ['antennas', 'delete'],
|
||||
mutationFn: (antennaId: string) => client.antennas.deleteAntenna(antennaId),
|
||||
onSuccess: (_, deletedAntennaId) => {
|
||||
queryClient.setQueryData<Array<Antenna>>(queryKeys.antennas.all, (prevData) =>
|
||||
queryClient.setQueryData(queryKeys.antennas.all, (prevData) =>
|
||||
prevData?.filter(({ id }) => id !== deletedAntennaId),
|
||||
);
|
||||
},
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
import {
|
||||
type InfiniteData,
|
||||
type UseQueryResult,
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from '@tanstack/react-query';
|
||||
import { type UseQueryResult, useMutation, useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { useClient } from '@/hooks/use-client';
|
||||
import { useFeatures } from '@/hooks/use-features';
|
||||
@@ -14,7 +9,7 @@ import { filterById } from '../utils/filter-id';
|
||||
import { makePaginatedResponseQuery } from '../utils/make-paginated-response-query';
|
||||
import { minifyAccountList } from '../utils/minify-list';
|
||||
|
||||
import type { Circle, PaginatedResponse } from 'pl-api';
|
||||
import type { Circle } from 'pl-api';
|
||||
|
||||
function useCircles<T>(select: (data: Array<Circle>) => T): UseQueryResult<T, Error>;
|
||||
function useCircles(): UseQueryResult<Array<Circle>, Error>;
|
||||
@@ -50,7 +45,7 @@ const useDeleteCircle = () => {
|
||||
mutationKey: ['circles', 'delete'],
|
||||
mutationFn: (circleId: string) => client.circles.deleteCircle(circleId),
|
||||
onSuccess: (_, deletedCircleId) => {
|
||||
queryClient.setQueryData<Array<Circle>>(queryKeys.circles.all, (prevData) =>
|
||||
queryClient.setQueryData(queryKeys.circles.all, (prevData) =>
|
||||
prevData?.filter(({ id }) => id !== deletedCircleId),
|
||||
);
|
||||
},
|
||||
@@ -93,7 +88,7 @@ const useRemoveAccountsFromCircle = (circleId: string) => {
|
||||
mutationFn: (accountIds: Array<string>) =>
|
||||
client.circles.deleteCircleAccounts(circleId, accountIds),
|
||||
onSettled: (_, __, accountIds) => {
|
||||
queryClient.setQueryData<InfiniteData<PaginatedResponse<string>>>(
|
||||
queryClient.setQueryData(
|
||||
queryKeys.accountsLists.circleMembers(circleId),
|
||||
filterById(accountIds),
|
||||
);
|
||||
|
||||
@@ -11,19 +11,16 @@ import { filterById } from '../utils/filter-id';
|
||||
import type { PaginatedResponse, PlApiClient } from 'pl-api';
|
||||
|
||||
const appendFollowRequest = (accountId: string) =>
|
||||
queryClient.setQueryData<InfiniteData<PaginatedResponse<string>>>(
|
||||
queryKeys.accountsLists.followRequests,
|
||||
(data) => {
|
||||
if (!data || data.pages.some((page) => page.items.includes(accountId))) return data;
|
||||
queryClient.setQueryData(queryKeys.accountsLists.followRequests, (data) => {
|
||||
if (!data || data.pages.some((page) => page.items.includes(accountId))) return data;
|
||||
|
||||
return {
|
||||
...data,
|
||||
pages: data.pages.map((page, index) =>
|
||||
index === 0 ? { ...page, items: [accountId, ...page.items] } : page,
|
||||
),
|
||||
};
|
||||
},
|
||||
);
|
||||
return {
|
||||
...data,
|
||||
pages: data.pages.map((page, index) =>
|
||||
index === 0 ? { ...page, items: [accountId, ...page.items] } : page,
|
||||
),
|
||||
};
|
||||
});
|
||||
|
||||
const removeFollowRequest = (accountId: string) =>
|
||||
queryClient.setQueryData(queryKeys.accountsLists.followRequests, filterById(accountId));
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { type InfiniteData, useMutation, useQuery } from '@tanstack/react-query';
|
||||
import { useMutation, useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { useClient } from '@/hooks/use-client';
|
||||
import { useFeatures } from '@/hooks/use-features';
|
||||
@@ -9,7 +9,7 @@ import { filterById } from '../utils/filter-id';
|
||||
import { makePaginatedResponseQuery } from '../utils/make-paginated-response-query';
|
||||
import { minifyAccountList } from '../utils/minify-list';
|
||||
|
||||
import type { CreateListParams, List, PaginatedResponse, UpdateListParams } from 'pl-api';
|
||||
import type { CreateListParams, List, UpdateListParams } from 'pl-api';
|
||||
|
||||
const useLists = <T>(select?: (data: Array<List>) => T) => {
|
||||
const client = useClient();
|
||||
@@ -43,7 +43,7 @@ const useDeleteList = () => {
|
||||
mutationKey: ['lists', 'delete'],
|
||||
mutationFn: (listId: string) => client.lists.deleteList(listId),
|
||||
onSuccess: (_, deletedListId) => {
|
||||
queryClient.setQueryData<Array<List>>(queryKeys.lists.all, (prevData) =>
|
||||
queryClient.setQueryData(queryKeys.lists.all, (prevData) =>
|
||||
prevData?.filter(({ id }) => id !== deletedListId),
|
||||
);
|
||||
},
|
||||
@@ -74,7 +74,7 @@ const useAddAccountsToList = (listId: string) => {
|
||||
onSettled: (_, __, accountIds) => {
|
||||
queryClient.invalidateQueries({ queryKey: queryKeys.accountsLists.listMembers(listId) });
|
||||
accountIds.forEach((accountId) =>
|
||||
queryClient.setQueryData<Array<string>>(queryKeys.lists.forAccount(accountId), (listIds) =>
|
||||
queryClient.setQueryData(queryKeys.lists.forAccount(accountId), (listIds) =>
|
||||
listIds ? [...listIds, listId] : undefined,
|
||||
),
|
||||
);
|
||||
@@ -89,12 +89,9 @@ const useRemoveAccountsFromList = (listId: string) => {
|
||||
mutationKey: ['accountsLists', 'lists', listId, 'remove'],
|
||||
mutationFn: (accountIds: Array<string>) => client.lists.deleteListAccounts(listId, accountIds),
|
||||
onSettled: (_, __, accountIds) => {
|
||||
queryClient.setQueryData<InfiniteData<PaginatedResponse<string>>>(
|
||||
queryKeys.accountsLists.listMembers(listId),
|
||||
filterById(accountIds),
|
||||
);
|
||||
queryClient.setQueryData(queryKeys.accountsLists.listMembers(listId), filterById(accountIds));
|
||||
accountIds.forEach((accountId) =>
|
||||
queryClient.setQueryData<Array<string>>(queryKeys.lists.forAccount(accountId), (listIds) =>
|
||||
queryClient.setQueryData(queryKeys.lists.forAccount(accountId), (listIds) =>
|
||||
listIds?.filter((id) => id !== listId),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -14,7 +14,6 @@ import { useOwnAccount } from '@/hooks/use-own-account';
|
||||
import { queryKeys } from '@/queries/keys';
|
||||
import { useContextsActions } from '@/stores/contexts';
|
||||
|
||||
import type { MinifiedSuggestion } from '../trends/use-suggested-accounts';
|
||||
import type {
|
||||
BlockAccountParams,
|
||||
FollowAccountParams,
|
||||
@@ -174,12 +173,10 @@ const useBlockAccountMutation = (accountId: string) => {
|
||||
onSuccess: (data) => {
|
||||
queryClient.setQueryData(queryKeys.accountRelationships.show(accountId), data);
|
||||
|
||||
queryClient.setQueryData<Array<MinifiedSuggestion>>(
|
||||
queryKeys.suggestions.all,
|
||||
(suggestions) =>
|
||||
suggestions
|
||||
? suggestions.filter((suggestion) => suggestion.account_id !== accountId)
|
||||
: undefined,
|
||||
queryClient.setQueryData(queryKeys.suggestions.all, (suggestions) =>
|
||||
suggestions
|
||||
? suggestions.filter((suggestion) => suggestion.account_id !== accountId)
|
||||
: undefined,
|
||||
);
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
@@ -247,12 +244,10 @@ const useMuteAccountMutation = (accountId: string) => {
|
||||
onSuccess: (data) => {
|
||||
queryClient.setQueryData(queryKeys.accountRelationships.show(accountId), data);
|
||||
|
||||
queryClient.setQueryData<Array<MinifiedSuggestion>>(
|
||||
queryKeys.suggestions.all,
|
||||
(suggestions) =>
|
||||
suggestions
|
||||
? suggestions.filter((suggestion) => suggestion.account_id !== accountId)
|
||||
: undefined,
|
||||
queryClient.setQueryData(queryKeys.suggestions.all, (suggestions) =>
|
||||
suggestions
|
||||
? suggestions.filter((suggestion) => suggestion.account_id !== accountId)
|
||||
: undefined,
|
||||
);
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import { type InfiniteData, useMutation } from '@tanstack/react-query';
|
||||
import { useMutation } from '@tanstack/react-query';
|
||||
import { create } from 'mutative';
|
||||
import {
|
||||
adminAnnouncementSchema,
|
||||
PaginatedResponse,
|
||||
type AdminAnnouncement,
|
||||
type AdminCreateAnnouncementParams,
|
||||
type AdminUpdateAnnouncementParams,
|
||||
} from 'pl-api';
|
||||
@@ -49,15 +47,13 @@ const useUpdateAnnouncementMutation = () => {
|
||||
client.admin.announcements.updateAnnouncement(id, params),
|
||||
retry: false,
|
||||
onSuccess: (data) => {
|
||||
queryClient.setQueryData<InfiniteData<PaginatedResponse<AdminAnnouncement>>>(
|
||||
queryKeys.admin.announcements,
|
||||
(prevData) =>
|
||||
create(prevData, (draft) => {
|
||||
draft?.pages.forEach(({ items }) => {
|
||||
const index = items.findIndex(({ id }) => id === data.id);
|
||||
if (index !== -1) items[index] = v.parse(adminAnnouncementSchema, data);
|
||||
});
|
||||
}),
|
||||
queryClient.setQueryData(queryKeys.admin.announcements, (prevData) =>
|
||||
create(prevData, (draft) => {
|
||||
draft?.pages.forEach(({ items }) => {
|
||||
const index = items.findIndex(({ id }) => id === data.id);
|
||||
if (index !== -1) items[index] = v.parse(adminAnnouncementSchema, data);
|
||||
});
|
||||
}),
|
||||
);
|
||||
queryClient.invalidateQueries({ queryKey: queryKeys.announcements.root });
|
||||
},
|
||||
@@ -71,14 +67,12 @@ const useDeleteAnnouncementMutation = () => {
|
||||
mutationFn: (id: string) => client.admin.announcements.deleteAnnouncement(id),
|
||||
retry: false,
|
||||
onSuccess: (_, deletedAnnouncementId) => {
|
||||
queryClient.setQueryData<InfiniteData<PaginatedResponse<AdminAnnouncement>>>(
|
||||
queryKeys.admin.announcements,
|
||||
(prevData) =>
|
||||
create(prevData, (draft) => {
|
||||
draft?.pages.forEach(
|
||||
(page) => (page.items = page.items.filter(({ id }) => id !== deletedAnnouncementId)),
|
||||
);
|
||||
}),
|
||||
queryClient.setQueryData(queryKeys.admin.announcements, (prevData) =>
|
||||
create(prevData, (draft) => {
|
||||
draft?.pages.forEach(
|
||||
(page) => (page.items = page.items.filter(({ id }) => id !== deletedAnnouncementId)),
|
||||
);
|
||||
}),
|
||||
);
|
||||
queryClient.invalidateQueries({ queryKey: queryKeys.announcements.root });
|
||||
},
|
||||
|
||||
@@ -4,8 +4,6 @@ import { useClient } from '@/hooks/use-client';
|
||||
|
||||
import { queryKeys } from '../keys';
|
||||
|
||||
import type { Account } from 'pl-api';
|
||||
|
||||
const useAdminSuggestAccountMutation = (accountId: string) => {
|
||||
const client = useClient();
|
||||
const queryClient = useQueryClient();
|
||||
@@ -14,12 +12,12 @@ const useAdminSuggestAccountMutation = (accountId: string) => {
|
||||
mutationKey: ['admin', 'accounts', accountId, 'suggest'],
|
||||
mutationFn: () => client.admin.accounts.suggestUser(accountId),
|
||||
onMutate: () => {
|
||||
queryClient.setQueryData<Account>(queryKeys.accounts.show(accountId), (account) =>
|
||||
queryClient.setQueryData(queryKeys.accounts.show(accountId), (account) =>
|
||||
account ? { ...account, is_suggested: true } : undefined,
|
||||
);
|
||||
},
|
||||
onError: () => {
|
||||
queryClient.setQueryData<Account>(queryKeys.accounts.show(accountId), (account) =>
|
||||
queryClient.setQueryData(queryKeys.accounts.show(accountId), (account) =>
|
||||
account ? { ...account, is_suggested: false } : undefined,
|
||||
);
|
||||
},
|
||||
@@ -34,12 +32,12 @@ const useAdminUnsuggestAccountMutation = (accountId: string) => {
|
||||
mutationKey: ['admin', 'accounts', accountId, 'unsuggest'],
|
||||
mutationFn: () => client.admin.accounts.unsuggestUser(accountId),
|
||||
onMutate: () => {
|
||||
queryClient.setQueryData<Account>(queryKeys.accounts.show(accountId), (account) =>
|
||||
queryClient.setQueryData(queryKeys.accounts.show(accountId), (account) =>
|
||||
account ? { ...account, is_suggested: false } : undefined,
|
||||
);
|
||||
},
|
||||
onError: () => {
|
||||
queryClient.setQueryData<Account>(queryKeys.accounts.show(accountId), (account) =>
|
||||
queryClient.setQueryData(queryKeys.accounts.show(accountId), (account) =>
|
||||
account ? { ...account, is_suggested: true } : undefined,
|
||||
);
|
||||
},
|
||||
|
||||
@@ -36,12 +36,12 @@ const useAdminVerifyAccountMutation = (accountId: string) => {
|
||||
mutationKey: ['admin', 'accounts', accountId, 'verify'],
|
||||
mutationFn: () => client.admin.accounts.tagUser(accountId, ['verified']),
|
||||
onMutate: () => {
|
||||
queryClient.setQueryData<Account>(queryKeys.accounts.show(accountId), (account) =>
|
||||
queryClient.setQueryData(queryKeys.accounts.show(accountId), (account) =>
|
||||
setVerified(account, true),
|
||||
);
|
||||
},
|
||||
onError: () => {
|
||||
queryClient.setQueryData<Account>(queryKeys.accounts.show(accountId), (account) =>
|
||||
queryClient.setQueryData(queryKeys.accounts.show(accountId), (account) =>
|
||||
setVerified(account, false),
|
||||
);
|
||||
},
|
||||
@@ -56,12 +56,12 @@ const useAdminUnverifyAccountMutation = (accountId: string) => {
|
||||
mutationKey: ['admin', 'accounts', accountId, 'unverify'],
|
||||
mutationFn: () => client.admin.accounts.untagUser(accountId, ['verified']),
|
||||
onMutate: () => {
|
||||
queryClient.setQueryData<Account>(queryKeys.accounts.show(accountId), (account) =>
|
||||
queryClient.setQueryData(queryKeys.accounts.show(accountId), (account) =>
|
||||
setVerified(account, false),
|
||||
);
|
||||
},
|
||||
onError: () => {
|
||||
queryClient.setQueryData<Account>(queryKeys.accounts.show(accountId), (account) =>
|
||||
queryClient.setQueryData(queryKeys.accounts.show(accountId), (account) =>
|
||||
setVerified(account, true),
|
||||
);
|
||||
},
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
import {
|
||||
type InfiniteData,
|
||||
useInfiniteQuery,
|
||||
useMutation,
|
||||
useQueryClient,
|
||||
} from '@tanstack/react-query';
|
||||
import { useInfiniteQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { create } from 'mutative';
|
||||
import { useMemo } from 'react';
|
||||
|
||||
@@ -45,31 +40,28 @@ const importConversationEntities = (conversations: Conversation[]) => {
|
||||
const updateConversations = (conversation: Conversation) => {
|
||||
importConversationEntities([conversation]);
|
||||
|
||||
queryClient.setQueryData<InfiniteData<PaginatedResponse<MinifiedConversation>>>(
|
||||
['conversations'],
|
||||
(data) => {
|
||||
if (!data || !data.pages.length) return data;
|
||||
queryClient.setQueryData(queryKeys.conversations.all, (data) => {
|
||||
if (!data || !data.pages.length) return data;
|
||||
|
||||
return create(data, (draft) => {
|
||||
const updatedConversation = minifyConversation(conversation);
|
||||
return create(data, (draft) => {
|
||||
const updatedConversation = minifyConversation(conversation);
|
||||
|
||||
let found = false;
|
||||
let found = false;
|
||||
|
||||
for (const page of draft.pages) {
|
||||
const index = page.items.findIndex((item) => item.id === updatedConversation.id);
|
||||
if (index !== -1) {
|
||||
page.items[index] = updatedConversation;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
for (const page of draft.pages) {
|
||||
const index = page.items.findIndex((item) => item.id === updatedConversation.id);
|
||||
if (index !== -1) {
|
||||
page.items[index] = updatedConversation;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
draft.pages[0].items.unshift(updatedConversation);
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
if (!found) {
|
||||
draft.pages[0].items.unshift(updatedConversation);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const useConversations = () => {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useMutation, type InfiniteData } from '@tanstack/react-query';
|
||||
import { useMutation } from '@tanstack/react-query';
|
||||
|
||||
import { useClient } from '@/hooks/use-client';
|
||||
import { queryClient } from '@/queries/client';
|
||||
@@ -11,25 +11,19 @@ import { filterById } from '../utils/filter-id';
|
||||
import { removeGroupMember } from './use-group-members';
|
||||
|
||||
const appendGroupBlock = (groupId: string, accountId: string) =>
|
||||
queryClient.setQueryData<InfiniteData<ReturnType<typeof minifyAccountList>>>(
|
||||
queryKeys.accountsLists.groupBlocks(groupId),
|
||||
(data) => {
|
||||
if (!data || data.pages.some((page) => page.items.includes(accountId))) return data;
|
||||
queryClient.setQueryData(queryKeys.accountsLists.groupBlocks(groupId), (data) => {
|
||||
if (!data || data.pages.some((page) => page.items.includes(accountId))) return data;
|
||||
|
||||
return {
|
||||
...data,
|
||||
pages: data.pages.map((page, index) =>
|
||||
index === 0 ? { ...page, items: [accountId, ...page.items] } : page,
|
||||
),
|
||||
};
|
||||
},
|
||||
);
|
||||
return {
|
||||
...data,
|
||||
pages: data.pages.map((page, index) =>
|
||||
index === 0 ? { ...page, items: [accountId, ...page.items] } : page,
|
||||
),
|
||||
};
|
||||
});
|
||||
|
||||
const removeGroupBlock = (groupId: string, accountId: string) =>
|
||||
queryClient.setQueryData<InfiniteData<ReturnType<typeof minifyAccountList>>>(
|
||||
queryKeys.accountsLists.groupBlocks(groupId),
|
||||
filterById(accountId),
|
||||
);
|
||||
queryClient.setQueryData(queryKeys.accountsLists.groupBlocks(groupId), filterById(accountId));
|
||||
|
||||
const useGroupBlocks = makePaginatedResponseQuery(
|
||||
(groupId: string) => queryKeys.accountsLists.groupBlocks(groupId),
|
||||
|
||||
@@ -33,30 +33,24 @@ const useJoinGroupMutation = (id: string) => {
|
||||
onMutate: () => {
|
||||
let previousRelationship: GroupRelationship | undefined = undefined;
|
||||
|
||||
queryClient.setQueryData<GroupRelationship>(
|
||||
queryKeys.groupRelationships.show(id),
|
||||
(relationship) => {
|
||||
previousRelationship = relationship;
|
||||
if (!relationship) return undefined;
|
||||
return {
|
||||
...relationship,
|
||||
requested: true,
|
||||
};
|
||||
},
|
||||
);
|
||||
queryClient.setQueryData(queryKeys.groupRelationships.show(id), (relationship) => {
|
||||
previousRelationship = relationship;
|
||||
if (!relationship) return undefined;
|
||||
return {
|
||||
...relationship,
|
||||
requested: true,
|
||||
};
|
||||
});
|
||||
|
||||
return previousRelationship;
|
||||
},
|
||||
onError: (_, __, previousRelationship) => {
|
||||
if (previousRelationship) {
|
||||
queryClient.setQueryData<GroupRelationship>(
|
||||
['groupRelationships', id],
|
||||
previousRelationship,
|
||||
);
|
||||
queryClient.setQueryData(queryKeys.groupRelationships.show(id), previousRelationship);
|
||||
}
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
queryClient.setQueryData<GroupRelationship>(queryKeys.groupRelationships.show(id), data);
|
||||
queryClient.setQueryData(queryKeys.groupRelationships.show(id), data);
|
||||
},
|
||||
});
|
||||
};
|
||||
@@ -70,32 +64,26 @@ const useLeaveGroupMutation = (id: string) => {
|
||||
onMutate: () => {
|
||||
let previousRelationship: GroupRelationship | undefined = undefined;
|
||||
|
||||
queryClient.setQueryData<GroupRelationship>(
|
||||
queryKeys.groupRelationships.show(id),
|
||||
(relationship) => {
|
||||
previousRelationship = relationship;
|
||||
if (!relationship) return undefined;
|
||||
return {
|
||||
...relationship,
|
||||
requested: false,
|
||||
member: false,
|
||||
role: undefined,
|
||||
};
|
||||
},
|
||||
);
|
||||
queryClient.setQueryData(queryKeys.groupRelationships.show(id), (relationship) => {
|
||||
previousRelationship = relationship;
|
||||
if (!relationship) return undefined;
|
||||
return {
|
||||
...relationship,
|
||||
requested: false,
|
||||
member: false,
|
||||
role: undefined,
|
||||
};
|
||||
});
|
||||
|
||||
return previousRelationship;
|
||||
},
|
||||
onError: (_, __, previousRelationship) => {
|
||||
if (previousRelationship) {
|
||||
queryClient.setQueryData<GroupRelationship>(
|
||||
['groupRelationships', id],
|
||||
previousRelationship,
|
||||
);
|
||||
queryClient.setQueryData(queryKeys.groupRelationships.show(id), previousRelationship);
|
||||
}
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
queryClient.setQueryData<GroupRelationship>(queryKeys.groupRelationships.show(id), data);
|
||||
queryClient.setQueryData(queryKeys.groupRelationships.show(id), data);
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { MinifiedInteractionRequest } from './statuses/use-interaction-requests';
|
||||
|
||||
import type { MinifiedScrobble } from './accounts/account-scrobble';
|
||||
import type { FilterType } from './notifications/use-notifications';
|
||||
import type { DraftStatus } from './statuses/use-draft-statuses';
|
||||
import type { MinifiedInteractionRequest } from './statuses/use-interaction-requests';
|
||||
import type { MinifiedStatusEdit } from './statuses/use-status-history';
|
||||
import type { MinifiedEmojiReaction } from './statuses/use-status-interactions';
|
||||
import type { TimelineEntry } from './timelines/use-home-timeline';
|
||||
import type { MinifiedSuggestion } from './trends/use-suggested-accounts';
|
||||
import type {
|
||||
MinifiedAdminAccount,
|
||||
@@ -356,7 +356,7 @@ const notifications = {
|
||||
root: ['notifications'] as const,
|
||||
list: (activeFilter?: FilterType) => {
|
||||
const key = ['notifications', activeFilter] as const;
|
||||
return key as TaggedKey<typeof key, Array<NotificationGroup>>;
|
||||
return key as TaggedKey<typeof key, InfiniteData<PaginatedResponse<NotificationGroup>>>;
|
||||
},
|
||||
};
|
||||
|
||||
@@ -407,7 +407,7 @@ const suggestions = {
|
||||
|
||||
const timelines = {
|
||||
root: ['timelines'] as const,
|
||||
home: ['timelines', 'home'] as const,
|
||||
home: ['timelines', 'home'] as TaggedKey<['timelines', 'home'], Array<TimelineEntry>>,
|
||||
};
|
||||
|
||||
const timelineIds = {
|
||||
@@ -518,7 +518,7 @@ const lists = {
|
||||
all: ['lists'] as TaggedKey<['lists'], Array<List>>,
|
||||
forAccount: (accountId: string) => {
|
||||
const key = ['lists', 'forAccount', accountId] as const;
|
||||
return key as TaggedKey<typeof key, Array<List>>;
|
||||
return key as TaggedKey<typeof key, Array<string>>;
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
import {
|
||||
type InfiniteData,
|
||||
useInfiniteQuery,
|
||||
useMutation,
|
||||
useQueryClient,
|
||||
} from '@tanstack/react-query';
|
||||
import { useInfiniteQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import 'intl-pluralrules';
|
||||
import { useCallback, useEffect } from 'react';
|
||||
import { useIntl } from 'react-intl';
|
||||
@@ -33,12 +28,7 @@ import { queryKeys } from '../keys';
|
||||
import { useNotificationsMarker } from '../markers/use-markers';
|
||||
import { minifyGroupedNotifications } from '../utils/minify-list';
|
||||
|
||||
import type {
|
||||
GetGroupedNotificationsParams,
|
||||
Notification,
|
||||
NotificationGroup,
|
||||
PaginatedResponse,
|
||||
} from 'pl-api';
|
||||
import type { GetGroupedNotificationsParams, Notification, NotificationGroup } from 'pl-api';
|
||||
|
||||
const FILTER_TYPES = {
|
||||
all: undefined,
|
||||
@@ -305,25 +295,22 @@ const comparator = (
|
||||
};
|
||||
|
||||
const prependNotification = (notification: NotificationGroup, filter: FilterType) => {
|
||||
queryClient.setQueryData<InfiniteData<PaginatedResponse<NotificationGroup>>>(
|
||||
queryKeys.notifications.list(filter),
|
||||
(data) => {
|
||||
if (!data || !data.pages.length) return data;
|
||||
queryClient.setQueryData(queryKeys.notifications.list(filter), (data) => {
|
||||
if (!data || !data.pages.length) return data;
|
||||
|
||||
const [firstPage, ...restPages] = data.pages;
|
||||
const [firstPage, ...restPages] = data.pages;
|
||||
|
||||
return {
|
||||
...data,
|
||||
pages: [
|
||||
{
|
||||
...firstPage,
|
||||
items: [notification, ...firstPage.items].toSorted(comparator).filter(filterUnique),
|
||||
},
|
||||
...restPages,
|
||||
],
|
||||
};
|
||||
},
|
||||
);
|
||||
return {
|
||||
...data,
|
||||
pages: [
|
||||
{
|
||||
...firstPage,
|
||||
items: [notification, ...firstPage.items].toSorted(comparator).filter(filterUnique),
|
||||
},
|
||||
...restPages,
|
||||
],
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
export {
|
||||
|
||||
@@ -5,7 +5,6 @@ import { queryKeys } from '../keys';
|
||||
import { makePaginatedResponseQueryOptions } from '../utils/make-paginated-response-query-options';
|
||||
import { mutationOptions } from '../utils/mutation-options';
|
||||
|
||||
import type { MinifiedSuggestion } from '../trends/use-suggested-accounts';
|
||||
import type { RootState, Store } from '@/store';
|
||||
import type { Account } from 'pl-api';
|
||||
|
||||
@@ -26,7 +25,7 @@ const blockDomainMutationOptions = mutationOptions({
|
||||
const accounts = selectAccountsByDomain(store.getState(), domain);
|
||||
if (!accounts) return;
|
||||
|
||||
queryClient.setQueryData<Array<MinifiedSuggestion>>(queryKeys.suggestions.all, (suggestions) =>
|
||||
queryClient.setQueryData(queryKeys.suggestions.all, (suggestions) =>
|
||||
suggestions
|
||||
? suggestions.filter((suggestion) => !accounts.includes(suggestion.account_id))
|
||||
: undefined,
|
||||
|
||||
@@ -7,7 +7,7 @@ import { useFeatures } from '@/hooks/use-features';
|
||||
|
||||
import { queryKeys } from '../keys';
|
||||
|
||||
import type { CreateFilterParams, Filter, UpdateFilterParams } from 'pl-api';
|
||||
import type { CreateFilterParams, UpdateFilterParams } from 'pl-api';
|
||||
|
||||
const useFilters = () => {
|
||||
const client = useClient();
|
||||
@@ -56,7 +56,7 @@ const useCreateFilter = () => {
|
||||
mutationFn: (data: CreateFilterParams) => client.filtering.createFilter(data),
|
||||
onSettled: (data) => {
|
||||
queryClient.invalidateQueries({ queryKey: queryKeys.filters.all });
|
||||
if (data) queryClient.setQueryData<Filter>(queryKeys.filters.show(data.id), data);
|
||||
if (data) queryClient.setQueryData(queryKeys.filters.show(data.id), data);
|
||||
},
|
||||
});
|
||||
};
|
||||
@@ -70,7 +70,7 @@ const useUpdateFilter = (filterId: string) => {
|
||||
mutationFn: (data: UpdateFilterParams) => client.filtering.updateFilter(filterId, data),
|
||||
onSettled: (data) => {
|
||||
queryClient.invalidateQueries({ queryKey: queryKeys.filters.all });
|
||||
if (data) queryClient.setQueryData<Filter>(queryKeys.filters.show(filterId), data);
|
||||
if (data) queryClient.setQueryData(queryKeys.filters.show(filterId), data);
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
@@ -23,7 +23,7 @@ const usePollVoteMutation = (pollId: string) => {
|
||||
mutationKey: ['statuses', 'polls', pollId, 'vote'],
|
||||
mutationFn: (choices: number[]) => client.polls.vote(pollId, choices),
|
||||
onSuccess: (poll) => {
|
||||
queryClient.setQueryData<Poll>(queryKeys.statuses.polls.show(pollId), poll);
|
||||
queryClient.setQueryData(queryKeys.statuses.polls.show(pollId), poll);
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
@@ -198,7 +198,7 @@ const useBookmarkStatus = (statusId: string) => {
|
||||
queryClient.invalidateQueries({ queryKey: queryKeys.accountsLists.statusReblogs(statusId) });
|
||||
|
||||
if (previousFolder) {
|
||||
queryClient.setQueryData<InfiniteData<PaginatedResponse<string>>>(
|
||||
queryClient.setQueryData(
|
||||
queryKeys.statusLists.bookmarks(previousFolder),
|
||||
filterById(statusId),
|
||||
);
|
||||
|
||||
@@ -145,7 +145,7 @@ const useHomeTimeline = () => {
|
||||
|
||||
const timelinePage = processPage(response);
|
||||
|
||||
queryClient.setQueryData<Array<TimelineEntry>>(queryKeys.timelines.home, (oldData) => {
|
||||
queryClient.setQueryData(queryKeys.timelines.home, (oldData) => {
|
||||
if (!oldData) return timelinePage;
|
||||
|
||||
const index = oldData.indexOf(entry);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { type InfiniteData, type QueryKey } from '@tanstack/react-query';
|
||||
import { PaginatedResponse } from 'pl-api';
|
||||
|
||||
import { queryClient } from '@/queries/client';
|
||||
|
||||
import type { InfiniteData, QueryKey } from '@tanstack/react-query';
|
||||
import type { PaginatedResponse } from 'pl-api';
|
||||
|
||||
const updatePaginatedResponse = <T>(
|
||||
queryKey: QueryKey,
|
||||
updater: (items: PaginatedResponse<T>['items']) => PaginatedResponse<T>['items'],
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { type PlApiClient, type ShoutMessage as BaseShoutMessage, Account } from 'pl-api';
|
||||
import { useEffect } from 'react';
|
||||
import { create } from 'zustand';
|
||||
import { mutative } from 'zustand-mutative';
|
||||
@@ -9,6 +8,8 @@ import { useLoggedIn } from '@/hooks/use-logged-in';
|
||||
import { queryClient } from '@/queries/client';
|
||||
import { queryKeys } from '@/queries/keys';
|
||||
|
||||
import type { PlApiClient, ShoutMessage as BaseShoutMessage } from 'pl-api';
|
||||
|
||||
const minifyMessage = ({ author, ...message }: BaseShoutMessage) => ({
|
||||
author_id: author.id,
|
||||
...message,
|
||||
@@ -37,7 +38,7 @@ const useShoutboxStore = create<State>()(
|
||||
setMessages: (messages) => {
|
||||
set((state: State) => {
|
||||
for (const { author } of messages.toReversed()) {
|
||||
queryClient.setQueryData<Account>(
|
||||
queryClient.setQueryData(
|
||||
queryKeys.accounts.show(author.id),
|
||||
(account) => account || author,
|
||||
);
|
||||
@@ -48,10 +49,7 @@ const useShoutboxStore = create<State>()(
|
||||
},
|
||||
pushMessage: (message) => {
|
||||
set((state: State) => {
|
||||
queryClient.setQueryData<Account>(
|
||||
queryKeys.accounts.show(message.author.id),
|
||||
message.author,
|
||||
);
|
||||
queryClient.setQueryData(queryKeys.accounts.show(message.author.id), message.author);
|
||||
state.messages.push(minifyMessage(message));
|
||||
});
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user