@@ -1,22 +0,0 @@
|
||||
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { importEntities } from 'pl-fe/actions/importer';
|
||||
import { useAppDispatch } from 'pl-fe/hooks/use-app-dispatch';
|
||||
import { useClient } from 'pl-fe/hooks/use-client';
|
||||
|
||||
const useBirthdayReminders = (month: number, day: number) => {
|
||||
const client = useClient();
|
||||
const dispatch = useAppDispatch();
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['accountsLists', 'birthdayReminders', month, day],
|
||||
queryFn: () => client.accounts.getBirthdays(day, month).then((accounts) => {
|
||||
dispatch(importEntities({ accounts }));
|
||||
|
||||
return accounts.map(({ id }) => id);
|
||||
}),
|
||||
});
|
||||
};
|
||||
|
||||
export { useBirthdayReminders };
|
||||
@@ -1,27 +0,0 @@
|
||||
import { useInfiniteQuery } from '@tanstack/react-query';
|
||||
|
||||
import { importEntities } from 'pl-fe/actions/importer';
|
||||
import { useAppDispatch } from 'pl-fe/hooks/use-app-dispatch';
|
||||
import { useClient } from 'pl-fe/hooks/use-client';
|
||||
|
||||
const useDirectory = (order: 'active' | 'new', local: boolean = false) => {
|
||||
const client = useClient();
|
||||
const dispatch = useAppDispatch();
|
||||
|
||||
return useInfiniteQuery({
|
||||
queryKey: ['accountsLists', 'directory', order, local],
|
||||
queryFn: ({ pageParam: offset }) => client.instance.profileDirectory({
|
||||
order,
|
||||
local,
|
||||
offset,
|
||||
}).then((accounts) => {
|
||||
dispatch(importEntities({ accounts }));
|
||||
return accounts.map(({ id }) => id);
|
||||
}),
|
||||
initialPageParam: 0,
|
||||
getNextPageParam: (_, allPages) => allPages.at(-1)?.length === 0 ? undefined : allPages.flat().length,
|
||||
select: (data) => data?.pages.flat(),
|
||||
});
|
||||
};
|
||||
|
||||
export { useDirectory };
|
||||
@@ -1,20 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { importEntities } from 'pl-fe/actions/importer';
|
||||
import { useAppDispatch } from 'pl-fe/hooks/use-app-dispatch';
|
||||
import { useClient } from 'pl-fe/hooks/use-client';
|
||||
|
||||
const useEndorsedAccounts = (accountId: string) => {
|
||||
const client = useClient();
|
||||
const dispatch = useAppDispatch();
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['accountsLists', 'endorsedAccounts', accountId],
|
||||
queryFn: () => client.accounts.getAccountEndorsements(accountId).then((accounts) => {
|
||||
dispatch(importEntities({ accounts }));
|
||||
return accounts.map(({ id }) => id);
|
||||
}),
|
||||
});
|
||||
};
|
||||
|
||||
export { useEndorsedAccounts };
|
||||
@@ -1,56 +0,0 @@
|
||||
|
||||
import { type InfiniteData, useMutation } from '@tanstack/react-query';
|
||||
|
||||
import { importEntities } from 'pl-fe/actions/importer';
|
||||
import { makePaginatedResponseQuery } from 'pl-fe/api/utils/make-paginated-response-query';
|
||||
import { minifyList } from 'pl-fe/api/utils/minify-list';
|
||||
import { useClient } from 'pl-fe/hooks/use-client';
|
||||
import { queryClient } from 'pl-fe/queries/client';
|
||||
import { store } from 'pl-fe/store';
|
||||
|
||||
import type { PlApiClient } from 'pl-api';
|
||||
|
||||
const minifyRequestList = (response: Awaited<ReturnType<(InstanceType<typeof PlApiClient>)['events']['getEventParticipationRequests']>>) =>
|
||||
minifyList(
|
||||
response,
|
||||
({ account, participation_message }) => ({ account_id: account.id, participation_message }),
|
||||
(requests) => store.dispatch(importEntities({ accounts: requests.map(request => request.account) }) as any),
|
||||
);
|
||||
|
||||
type MinifiedRequestList = ReturnType<typeof minifyRequestList>
|
||||
|
||||
const removeRequest = (statusId: string, accountId: string) =>
|
||||
queryClient.setQueryData<InfiniteData<MinifiedRequestList>>(['accountsLists', 'eventParticipationRequests', statusId], (data) => data ? {
|
||||
...data,
|
||||
pages: data.pages.map(({ items, ...page }) => ({ ...page, items: items.filter(({ account_id }) => account_id !== accountId) })),
|
||||
} : undefined);
|
||||
|
||||
const useEventParticipationRequests = makePaginatedResponseQuery(
|
||||
(statusId: string) => ['accountsLists', 'eventParticipationRequests', statusId],
|
||||
(client, params) => client.events.getEventParticipationRequests(...params).then(minifyRequestList),
|
||||
);
|
||||
|
||||
const useAcceptEventParticipationRequestMutation = (statusId: string, accountId: string) => {
|
||||
const client = useClient();
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ['accountsLists', 'eventParticipationRequests', statusId, accountId],
|
||||
mutationFn: () => client.events.acceptEventParticipationRequest(statusId, accountId),
|
||||
onSettled: () => removeRequest(statusId, accountId),
|
||||
});
|
||||
};
|
||||
|
||||
const useRejectEventParticipationRequestMutation = (statusId: string, accountId: string) => {
|
||||
const client = useClient();
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ['accountsLists', 'eventParticipationRequests', statusId, accountId],
|
||||
mutationFn: () => client.events.rejectEventParticipationRequest(statusId, accountId),
|
||||
onSettled: () => removeRequest(statusId, accountId),
|
||||
});
|
||||
};
|
||||
export {
|
||||
useEventParticipationRequests,
|
||||
useAcceptEventParticipationRequestMutation,
|
||||
useRejectEventParticipationRequestMutation,
|
||||
};
|
||||
@@ -1,9 +0,0 @@
|
||||
import { makePaginatedResponseQuery } from 'pl-fe/api/utils/make-paginated-response-query';
|
||||
import { minifyAccountList } from 'pl-fe/api/utils/minify-list';
|
||||
|
||||
const useEventParticipations = makePaginatedResponseQuery(
|
||||
(statusId: string) => ['accountsLists', 'eventParticipations', statusId],
|
||||
(client, params) => client.events.getEventParticipations(...params).then(minifyAccountList),
|
||||
);
|
||||
|
||||
export { useEventParticipations };
|
||||
@@ -1,28 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { importEntities } from 'pl-fe/actions/importer';
|
||||
import { useAppDispatch } from 'pl-fe/hooks/use-app-dispatch';
|
||||
import { useClient } from 'pl-fe/hooks/use-client';
|
||||
import { useFeatures } from 'pl-fe/hooks/use-features';
|
||||
import { useLoggedIn } from 'pl-fe/hooks/use-logged-in';
|
||||
|
||||
const useFamiliarFollowers = (accountId: string) => {
|
||||
const client = useClient();
|
||||
const dispatch = useAppDispatch();
|
||||
const features = useFeatures();
|
||||
const { isLoggedIn } = useLoggedIn();
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['accountsLists', 'endorsedAccounts', accountId],
|
||||
queryFn: () => client.accounts.getFamiliarFollowers([accountId]).then((response) => {
|
||||
const result = response.find(({ id }) => id === accountId);
|
||||
if (!result) return [];
|
||||
|
||||
dispatch(importEntities({ accounts: result.accounts }));
|
||||
return result.accounts.map(({ id }) => id);
|
||||
}),
|
||||
enabled: isLoggedIn && features.familiarFollowers,
|
||||
});
|
||||
};
|
||||
|
||||
export { useFamiliarFollowers };
|
||||
@@ -1,69 +0,0 @@
|
||||
import { useMutation, type InfiniteData } from '@tanstack/react-query';
|
||||
|
||||
import { makePaginatedResponseQuery } from 'pl-fe/api/utils/make-paginated-response-query';
|
||||
import { minifyAccountList } from 'pl-fe/api/utils/minify-list';
|
||||
import { useClient } from 'pl-fe/hooks/use-client';
|
||||
import { queryClient } from 'pl-fe/queries/client';
|
||||
|
||||
import type { PaginatedResponse, PlApiClient } from 'pl-api';
|
||||
|
||||
const appendFollowRequest = (accountId: string) =>
|
||||
queryClient.setQueryData<InfiniteData<ReturnType<typeof minifyAccountList>>>(['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),
|
||||
};
|
||||
});
|
||||
|
||||
const removeFollowRequest = (accountId: string) =>
|
||||
queryClient.setQueryData<InfiniteData<ReturnType<typeof minifyAccountList>>>(['accountsLists', 'followRequests'], (data) => data ? {
|
||||
...data,
|
||||
pages: data.pages.map(({ items, ...page }) => ({ ...page, items: items.filter((id) => id !== accountId) })),
|
||||
} : undefined);
|
||||
|
||||
const makeUseFollowRequests = <T>(select: ((data: InfiniteData<PaginatedResponse<string>>) => T)) => makePaginatedResponseQuery(
|
||||
() => ['accountsLists', 'followRequests'],
|
||||
(client) => client.myAccount.getFollowRequests().then(minifyAccountList),
|
||||
select,
|
||||
);
|
||||
|
||||
const useFollowRequests = makeUseFollowRequests((data) => data.pages.map(page => page.items).flat());
|
||||
|
||||
const useFollowRequestsCount = makeUseFollowRequests((data) => data.pages.map(page => page.items).flat().length);
|
||||
|
||||
const useAcceptFollowRequestMutation = (accountId: string) => {
|
||||
const client = useClient();
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ['accountsLists', 'followRequests', accountId],
|
||||
mutationFn: () => client.myAccount.acceptFollowRequest(accountId),
|
||||
onSettled: () => removeFollowRequest(accountId),
|
||||
});
|
||||
};
|
||||
|
||||
const useRejectFollowRequestMutation = (accountId: string) => {
|
||||
const client = useClient();
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ['accountsLists', 'followRequests', accountId],
|
||||
mutationFn: () => client.myAccount.rejectFollowRequest(accountId),
|
||||
onSettled: () => removeFollowRequest(accountId),
|
||||
});
|
||||
};
|
||||
|
||||
const prefetchFollowRequests = (client: PlApiClient) => queryClient.prefetchInfiniteQuery({
|
||||
queryKey: ['accountsLists', 'followRequests'],
|
||||
queryFn: ({ pageParam }) => pageParam.next?.() || client.myAccount.getFollowRequests().then(minifyAccountList),
|
||||
initialPageParam: { previous: null, next: null, items: [], partial: false } as PaginatedResponse<string>,
|
||||
});
|
||||
|
||||
export {
|
||||
appendFollowRequest,
|
||||
useFollowRequests,
|
||||
useFollowRequestsCount,
|
||||
useAcceptFollowRequestMutation,
|
||||
useRejectFollowRequestMutation,
|
||||
prefetchFollowRequests,
|
||||
};
|
||||
@@ -1,39 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { importEntities } from 'pl-fe/actions/importer';
|
||||
import { makePaginatedResponseQuery } from 'pl-fe/api/utils/make-paginated-response-query';
|
||||
import { minifyAccountList } from 'pl-fe/api/utils/minify-list';
|
||||
import { useAppDispatch } from 'pl-fe/hooks/use-app-dispatch';
|
||||
import { useClient } from 'pl-fe/hooks/use-client';
|
||||
|
||||
const queryKey = {
|
||||
getDislikedBy: 'statusDislikes',
|
||||
getFavouritedBy: 'statusFavourites',
|
||||
getRebloggedBy: 'statusReblogs',
|
||||
};
|
||||
|
||||
const makeUseStatusInteractions = (method: 'getDislikedBy' | 'getFavouritedBy' | 'getRebloggedBy') => makePaginatedResponseQuery(
|
||||
(statusId: string) => ['accountsLists', queryKey[method], statusId],
|
||||
(client, params) => client.statuses[method](...params).then(minifyAccountList),
|
||||
);
|
||||
|
||||
const useStatusDislikes = makeUseStatusInteractions('getDislikedBy');
|
||||
const useStatusFavourites = makeUseStatusInteractions('getFavouritedBy');
|
||||
const useStatusReblogs = makeUseStatusInteractions('getRebloggedBy');
|
||||
|
||||
const useStatusReactions = (statusId: string, emoji?: string) => {
|
||||
const client = useClient();
|
||||
const dispatch = useAppDispatch();
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['accountsLists', 'statusReactions', statusId, emoji],
|
||||
queryFn: () => client.statuses.getStatusReactions(statusId, emoji).then((reactions) => {
|
||||
dispatch(importEntities({ accounts: reactions.map(({ accounts }) => accounts).flat() }));
|
||||
|
||||
return reactions.map(({ accounts, ...reactions }) => reactions);
|
||||
}),
|
||||
placeholderData: (previousData) => previousData?.filter(({ name }) => name === emoji),
|
||||
});
|
||||
};
|
||||
|
||||
export { useStatusDislikes, useStatusFavourites, useStatusReactions, useStatusReblogs };
|
||||
@@ -8,7 +8,8 @@ import { useFeatures } from 'pl-fe/hooks/use-features';
|
||||
import { useLoggedIn } from 'pl-fe/hooks/use-logged-in';
|
||||
import { type Account, normalizeAccount } from 'pl-fe/normalizers/account';
|
||||
|
||||
import { useAccountScrobble } from './use-account-scrobble';
|
||||
import { useAccountScrobble } from '../../../queries/accounts/use-account-scrobble';
|
||||
|
||||
import { useRelationship } from './use-relationship';
|
||||
|
||||
import type { Account as BaseAccount } from 'pl-api';
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { useClient } from 'pl-fe/hooks/use-client';
|
||||
import { useFeatures } from 'pl-fe/hooks/use-features';
|
||||
|
||||
import type { Scrobble } from 'pl-api';
|
||||
|
||||
interface UseScrobblesOpts {
|
||||
enabled?: boolean;
|
||||
}
|
||||
|
||||
const useAccountScrobble = (accountId?: string, opts: UseScrobblesOpts = {}) => {
|
||||
const client = useClient();
|
||||
const features = useFeatures();
|
||||
const { enabled = false } = opts;
|
||||
|
||||
const { data: scrobble, ...result } = useQuery<Scrobble>({
|
||||
queryKey: ['scrobbles', accountId!],
|
||||
queryFn: async () => (await client.accounts.getScrobbles(accountId!, { limit: 1 })).items[0] || null,
|
||||
placeholderData: undefined,
|
||||
enabled: enabled && !!accountId && features.scrobbles,
|
||||
staleTime: 3 * 60 * 1000,
|
||||
});
|
||||
|
||||
return { scrobble, ...result };
|
||||
};
|
||||
|
||||
export { useAccountScrobble };
|
||||
@@ -9,7 +9,8 @@ import { useFeatures } from 'pl-fe/hooks/use-features';
|
||||
import { useLoggedIn } from 'pl-fe/hooks/use-logged-in';
|
||||
import { type Account, normalizeAccount } from 'pl-fe/normalizers/account';
|
||||
|
||||
import { useAccountScrobble } from './use-account-scrobble';
|
||||
import { useAccountScrobble } from '../../../queries/accounts/use-account-scrobble';
|
||||
|
||||
import { useRelationship } from './use-relationship';
|
||||
|
||||
import type { Account as BaseAccount } from 'pl-api';
|
||||
|
||||
@@ -10,7 +10,7 @@ import * as v from 'valibot';
|
||||
import { useClient } from 'pl-fe/hooks/use-client';
|
||||
import { queryClient } from 'pl-fe/queries/client';
|
||||
|
||||
import { useAnnouncements as useUserAnnouncements } from '../announcements/use-announcements';
|
||||
import { useAnnouncements as useUserAnnouncements } from '../../../queries/announcements/use-announcements';
|
||||
|
||||
const useAnnouncements = () => {
|
||||
const client = useClient();
|
||||
|
||||
@@ -1,92 +0,0 @@
|
||||
import { useMutation, useQuery } from '@tanstack/react-query';
|
||||
import { announcementReactionSchema, type AnnouncementReaction, type Announcement } from 'pl-api';
|
||||
import * as v from 'valibot';
|
||||
|
||||
import { useClient } from 'pl-fe/hooks/use-client';
|
||||
import { queryClient } from 'pl-fe/queries/client';
|
||||
|
||||
const updateReaction = (reaction: AnnouncementReaction, count: number, me?: boolean, overwrite?: boolean) => v.parse(announcementReactionSchema, {
|
||||
...reaction,
|
||||
me: typeof me === 'boolean' ? me : reaction.me,
|
||||
count: overwrite ? count : (reaction.count + count),
|
||||
});
|
||||
|
||||
const updateReactions = (reactions: AnnouncementReaction[], name: string, count: number, me?: boolean, overwrite?: boolean) => {
|
||||
const idx = reactions.findIndex(reaction => reaction.name === name);
|
||||
|
||||
if (idx > -1) {
|
||||
reactions = reactions.map(reaction => reaction.name === name ? updateReaction(reaction, count, me, overwrite) : reaction);
|
||||
}
|
||||
|
||||
return [...reactions, updateReaction(v.parse(announcementReactionSchema, { name }), count, me, overwrite)];
|
||||
};
|
||||
|
||||
const useAnnouncements = () => {
|
||||
const client = useClient();
|
||||
|
||||
const { data, ...result } = useQuery<ReadonlyArray<Announcement>>({
|
||||
queryKey: ['announcements'],
|
||||
queryFn: () => client.announcements.getAnnouncements(),
|
||||
placeholderData: [],
|
||||
});
|
||||
|
||||
const {
|
||||
mutate: addReaction,
|
||||
} = useMutation({
|
||||
mutationFn: ({ announcementId, name }: { announcementId: string; name: string }) =>
|
||||
client.announcements.addAnnouncementReaction(announcementId, name),
|
||||
retry: false,
|
||||
onMutate: ({ announcementId: id, name }) => {
|
||||
queryClient.setQueryData(['announcements'], (prevResult: Announcement[]) =>
|
||||
prevResult.map(value => value.id !== id ? value : {
|
||||
...value,
|
||||
reactions: updateReactions(value.reactions, name, 1, true),
|
||||
}),
|
||||
);
|
||||
},
|
||||
onError: (_, { announcementId: id, name }) => {
|
||||
queryClient.setQueryData(['announcements'], (prevResult: Announcement[]) =>
|
||||
prevResult.map(value => value.id !== id ? value : {
|
||||
...value,
|
||||
reactions: updateReactions(value.reactions, name, -1, false),
|
||||
}),
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
const {
|
||||
mutate: removeReaction,
|
||||
} = useMutation({
|
||||
mutationFn: ({ announcementId, name }: { announcementId: string; name: string }) =>
|
||||
client.announcements.deleteAnnouncementReaction(announcementId, name),
|
||||
retry: false,
|
||||
onMutate: ({ announcementId: id, name }) => {
|
||||
queryClient.setQueryData(['announcements'], (prevResult: Announcement[]) =>
|
||||
prevResult.map(value => value.id !== id ? value : {
|
||||
...value,
|
||||
reactions: updateReactions(value.reactions, name, -1, false),
|
||||
}),
|
||||
);
|
||||
},
|
||||
onError: (_, { announcementId: id, name }) => {
|
||||
queryClient.setQueryData(['announcements'], (prevResult: Announcement[]) =>
|
||||
prevResult.map(value => value.id !== id ? value : {
|
||||
...value,
|
||||
reactions: updateReactions(value.reactions, name, 1, true),
|
||||
}),
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
data: data ? [...data].sort(compareAnnouncements) : undefined,
|
||||
...result,
|
||||
addReaction,
|
||||
removeReaction,
|
||||
};
|
||||
};
|
||||
|
||||
const compareAnnouncements = (a: Announcement, b: Announcement): number =>
|
||||
new Date(a.starts_at || a.published_at).getTime() - new Date(b.starts_at || b.published_at).getTime();
|
||||
|
||||
export { updateReactions, useAnnouncements };
|
||||
@@ -1,40 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { useClient } from 'pl-fe/hooks/use-client';
|
||||
import { useFeatures } from 'pl-fe/hooks/use-features';
|
||||
import { useInstance } from 'pl-fe/hooks/use-instance';
|
||||
import { useLoggedIn } from 'pl-fe/hooks/use-logged-in';
|
||||
|
||||
const useTranslationLanguages = () => {
|
||||
const client = useClient();
|
||||
const { isLoggedIn } = useLoggedIn();
|
||||
const features = useFeatures();
|
||||
const instance = useInstance();
|
||||
|
||||
const getTranslationLanguages = async () => {
|
||||
const metadata = instance.pleroma.metadata;
|
||||
|
||||
if (metadata.translation.source_languages?.length) {
|
||||
return Object.fromEntries(metadata.translation.source_languages.map(source => [
|
||||
source,
|
||||
metadata.translation.target_languages!.filter(lang => lang !== source),
|
||||
]));
|
||||
}
|
||||
|
||||
return client.instance.getInstanceTranslationLanguages();
|
||||
};
|
||||
|
||||
const { data, ...result } = useQuery({
|
||||
queryKey: ['translationLanguages'],
|
||||
queryFn: getTranslationLanguages,
|
||||
placeholderData: {},
|
||||
enabled: isLoggedIn && features.translations,
|
||||
});
|
||||
|
||||
return {
|
||||
translationLanguages: data || {},
|
||||
...result,
|
||||
};
|
||||
};
|
||||
|
||||
export { useTranslationLanguages };
|
||||
@@ -1,19 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { staticFetch } from 'pl-fe/api';
|
||||
|
||||
const fetchAboutPage = async (slug: string, locale?: string) => {
|
||||
const filename = `${slug}${locale ? `.${locale}` : ''}.html`;
|
||||
|
||||
const { data } = await staticFetch(`/instance/about/${filename}`);
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
const useAboutPage = (slug = 'index', locale?: string) =>
|
||||
useQuery({
|
||||
queryKey: ['pl-fe', 'aboutPages', slug, locale],
|
||||
queryFn: () => fetchAboutPage(slug, locale),
|
||||
});
|
||||
|
||||
export { useAboutPage };
|
||||
@@ -1,16 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { useClient } from 'pl-fe/hooks/use-client';
|
||||
|
||||
const useSearchLocation = (query: string) => {
|
||||
const client = useClient();
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['search', 'location', query],
|
||||
queryFn: ({ signal }) => client.search.searchLocation(query, { signal }),
|
||||
gcTime: 60 * 1000,
|
||||
enabled: !!query.trim(),
|
||||
});
|
||||
};
|
||||
|
||||
export { useSearchLocation };
|
||||
@@ -1,104 +0,0 @@
|
||||
import { useInfiniteQuery } from '@tanstack/react-query';
|
||||
|
||||
import { importEntities } from 'pl-fe/actions/importer';
|
||||
import { useAppDispatch } from 'pl-fe/hooks/use-app-dispatch';
|
||||
import { useClient } from 'pl-fe/hooks/use-client';
|
||||
|
||||
import type { SearchParams } from 'pl-api';
|
||||
import type { PaginationParams } from 'pl-api/dist/params/common';
|
||||
|
||||
const useSearchAccounts = (
|
||||
query: string,
|
||||
params?: Omit<SearchParams, keyof PaginationParams | 'type' | 'offset'>,
|
||||
) => {
|
||||
const client = useClient();
|
||||
const dispatch = useAppDispatch();
|
||||
|
||||
return useInfiniteQuery({
|
||||
queryKey: ['search', 'accounts', query, params],
|
||||
queryFn: ({ pageParam: offset, signal }) => client.search.search(query!, {
|
||||
with_relationships: true,
|
||||
...params,
|
||||
offset,
|
||||
type: 'accounts',
|
||||
}, { signal }).then(({ accounts }) => {
|
||||
dispatch(importEntities({ accounts }));
|
||||
return accounts.map(({ id }) => id);
|
||||
}),
|
||||
enabled: !!query?.trim(),
|
||||
initialPageParam: 0,
|
||||
getNextPageParam: (_, allPages) => allPages.flat().length,
|
||||
select: (data) => data.pages.flat(),
|
||||
});
|
||||
};
|
||||
|
||||
const useSearchStatuses = (
|
||||
query: string,
|
||||
params?: Omit<SearchParams, keyof PaginationParams | 'type' | 'offset'>,
|
||||
) => {
|
||||
const client = useClient();
|
||||
const dispatch = useAppDispatch();
|
||||
|
||||
return useInfiniteQuery({
|
||||
queryKey: ['search', 'statuses', query, params],
|
||||
queryFn: ({ pageParam: offset, signal }) => client.search.search(query, {
|
||||
with_relationships: true,
|
||||
...params,
|
||||
offset,
|
||||
type: 'statuses',
|
||||
}, { signal }).then(({ statuses }) => {
|
||||
dispatch(importEntities({ statuses }));
|
||||
return statuses.map(({ id }) => id);
|
||||
}),
|
||||
enabled: !!query?.trim(),
|
||||
initialPageParam: 0,
|
||||
getNextPageParam: (_, allPages) => allPages.flat().length,
|
||||
select: (data) => data.pages.flat(),
|
||||
});
|
||||
};
|
||||
|
||||
const useSearchHashtags = (
|
||||
query: string,
|
||||
params?: Omit<SearchParams, keyof PaginationParams | 'type' | 'offset'>,
|
||||
) => {
|
||||
const client = useClient();
|
||||
|
||||
return useInfiniteQuery({
|
||||
queryKey: ['search', 'hashtags', query, params],
|
||||
queryFn: ({ pageParam: offset, signal }) => client.search.search(query, {
|
||||
...params,
|
||||
offset,
|
||||
type: 'hashtags',
|
||||
}, { signal }).then(({ hashtags }) => hashtags),
|
||||
enabled: !!query?.trim(),
|
||||
initialPageParam: 0,
|
||||
getNextPageParam: (_, allPages) => allPages.flat().length,
|
||||
select: (data) => data.pages.flat(),
|
||||
});
|
||||
};
|
||||
|
||||
const useSearchGroups = (
|
||||
query: string,
|
||||
params?: Omit<SearchParams, keyof PaginationParams | 'type' | 'offset'>,
|
||||
) => {
|
||||
const client = useClient();
|
||||
const dispatch = useAppDispatch();
|
||||
|
||||
return useInfiniteQuery({
|
||||
queryKey: ['search', 'groups', query, params],
|
||||
queryFn: ({ pageParam: offset, signal }) => client.search.search(query, {
|
||||
...params,
|
||||
offset,
|
||||
type: 'groups',
|
||||
}, { signal }).then(({ groups }) => {
|
||||
dispatch(importEntities({ groups }));
|
||||
return groups.map(({ id }) => id);
|
||||
}),
|
||||
enabled: !!query?.trim(),
|
||||
initialPageParam: 0,
|
||||
getNextPageParam: (_, allPages) => allPages.flat().length,
|
||||
select: (data) => data.pages.flat(),
|
||||
});
|
||||
};
|
||||
|
||||
export { useSearchAccounts, useSearchStatuses, useSearchHashtags, useSearchGroups };
|
||||
@@ -1,5 +0,0 @@
|
||||
import { useBookmarkFolders } from './use-bookmark-folders';
|
||||
|
||||
const useBookmarkFolder = (folderId?: string) => useBookmarkFolders((data) => folderId ? data.find(folder => folder.id === folderId) : undefined);
|
||||
|
||||
export { useBookmarkFolder };
|
||||
@@ -1,22 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { useClient } from 'pl-fe/hooks/use-client';
|
||||
import { useFeatures } from 'pl-fe/hooks/use-features';
|
||||
|
||||
import type { BookmarkFolder } from 'pl-api';
|
||||
|
||||
const useBookmarkFolders = <T>(
|
||||
select?: ((data: Array<BookmarkFolder>) => T),
|
||||
) => {
|
||||
const client = useClient();
|
||||
const features = useFeatures();
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['bookmarkFolders'],
|
||||
queryFn: () => client.myAccount.getBookmarkFolders(),
|
||||
enabled: features.bookmarkFolders,
|
||||
select,
|
||||
});
|
||||
};
|
||||
|
||||
export { useBookmarkFolders };
|
||||
@@ -1,21 +0,0 @@
|
||||
import { useMutation } from '@tanstack/react-query';
|
||||
|
||||
import { useClient } from 'pl-fe/hooks/use-client';
|
||||
import { queryClient } from 'pl-fe/queries/client';
|
||||
|
||||
interface CreateBookmarkFolderParams {
|
||||
name: string;
|
||||
emoji?: string;
|
||||
}
|
||||
|
||||
const useCreateBookmarkFolder = () => {
|
||||
const client = useClient();
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ['bookmarkFolders', 'create'],
|
||||
mutationFn: (params: CreateBookmarkFolderParams) => client.myAccount.createBookmarkFolder(params),
|
||||
onSettled: () => queryClient.invalidateQueries({ queryKey: ['bookmarkFolders'] }),
|
||||
});
|
||||
};
|
||||
|
||||
export { useCreateBookmarkFolder };
|
||||
@@ -1,16 +0,0 @@
|
||||
import { useMutation } from '@tanstack/react-query';
|
||||
|
||||
import { useClient } from 'pl-fe/hooks/use-client';
|
||||
import { queryClient } from 'pl-fe/queries/client';
|
||||
|
||||
const useDeleteBookmarkFolder = () => {
|
||||
const client = useClient();
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ['bookmarkFolders', 'delete'],
|
||||
mutationFn: (folderId: string) => client.myAccount.deleteBookmarkFolder(folderId),
|
||||
onSettled: () => queryClient.invalidateQueries({ queryKey: ['bookmarkFolders'] }),
|
||||
});
|
||||
};
|
||||
|
||||
export { useDeleteBookmarkFolder };
|
||||
@@ -1,85 +0,0 @@
|
||||
import { type InfiniteData, useInfiniteQuery, useMutation } from '@tanstack/react-query';
|
||||
|
||||
import { importEntities } from 'pl-fe/actions/importer';
|
||||
import { useAppDispatch } from 'pl-fe/hooks/use-app-dispatch';
|
||||
import { useClient } from 'pl-fe/hooks/use-client';
|
||||
import { useFeatures } from 'pl-fe/hooks/use-features';
|
||||
|
||||
import type { InteractionRequest, PaginatedResponse } from 'pl-api';
|
||||
import type { AppDispatch } from 'pl-fe/store';
|
||||
|
||||
const minifyInteractionRequest = ({ account, status, reply, ...interactionRequest }: InteractionRequest) => ({
|
||||
account_id: account.id,
|
||||
status_id: status?.id || null,
|
||||
reply_id: reply?.id || null,
|
||||
...interactionRequest,
|
||||
});
|
||||
|
||||
type MinifiedInteractionRequest = ReturnType<typeof minifyInteractionRequest>;
|
||||
|
||||
const minifyInteractionRequestsList = (dispatch: AppDispatch, { previous, next, items, ...response }: PaginatedResponse<InteractionRequest>): PaginatedResponse<MinifiedInteractionRequest> => {
|
||||
dispatch(importEntities({
|
||||
statuses: items.map(item => [item.status, item.reply]).flat(),
|
||||
}));
|
||||
|
||||
return {
|
||||
...response,
|
||||
previous: previous ? () => previous().then(response => minifyInteractionRequestsList(dispatch, response)) : null,
|
||||
next: next ? () => next().then(response => minifyInteractionRequestsList(dispatch, response)) : null,
|
||||
items: items.map(minifyInteractionRequest),
|
||||
};
|
||||
};
|
||||
|
||||
const useInteractionRequests = <T>(
|
||||
select?: ((data: InfiniteData<PaginatedResponse<MinifiedInteractionRequest>>) => T),
|
||||
) => {
|
||||
const client = useClient();
|
||||
const features = useFeatures();
|
||||
const dispatch = useAppDispatch();
|
||||
|
||||
return useInfiniteQuery({
|
||||
queryKey: ['interactionRequests'],
|
||||
queryFn: ({ pageParam }) => pageParam.next?.() || client.interactionRequests.getInteractionRequests().then(response => minifyInteractionRequestsList(dispatch, response)),
|
||||
initialPageParam: { previous: null, next: null, items: [], partial: false } as PaginatedResponse<MinifiedInteractionRequest>,
|
||||
getNextPageParam: (page) => page.next ? page : undefined,
|
||||
enabled: features.interactionRequests,
|
||||
select,
|
||||
});
|
||||
};
|
||||
|
||||
const useFlatInteractionRequests = () => useInteractionRequests(
|
||||
(data: InfiniteData<PaginatedResponse<MinifiedInteractionRequest>>) => data.pages.map(page => page.items).flat(),
|
||||
);
|
||||
|
||||
const useInteractionRequestsCount = () => useInteractionRequests(data => data.pages.map(({ items }) => items).flat().length);
|
||||
|
||||
const useAuthorizeInteractionRequestMutation = (requestId: string) => {
|
||||
const client = useClient();
|
||||
const { refetch } = useInteractionRequests();
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ['interactionRequests', requestId],
|
||||
mutationFn: () => client.interactionRequests.authorizeInteractionRequest(requestId),
|
||||
onSettled: () => refetch(),
|
||||
});
|
||||
};
|
||||
|
||||
const useRejectInteractionRequestMutation = (requestId: string) => {
|
||||
const client = useClient();
|
||||
const { refetch } = useInteractionRequests();
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ['interactionRequests', requestId],
|
||||
mutationFn: () => client.interactionRequests.rejectInteractionRequest(requestId),
|
||||
onSettled: () => refetch(),
|
||||
});
|
||||
};
|
||||
|
||||
export {
|
||||
useInteractionRequests,
|
||||
useInteractionRequestsCount,
|
||||
useFlatInteractionRequests,
|
||||
useAuthorizeInteractionRequestMutation,
|
||||
useRejectInteractionRequestMutation,
|
||||
type MinifiedInteractionRequest,
|
||||
};
|
||||
@@ -1,25 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { StatusEdit } from 'pl-api';
|
||||
|
||||
import { importEntities } from 'pl-fe/actions/importer';
|
||||
import { useAppDispatch } from 'pl-fe/hooks/use-app-dispatch';
|
||||
import { useClient } from 'pl-fe/hooks/use-client';
|
||||
|
||||
const minifyStatusEdit = ({ account, ...statusEdit }: StatusEdit) => ({
|
||||
account_id: account.id,
|
||||
...statusEdit,
|
||||
});
|
||||
|
||||
const useStatusHistory = (statusId: string) => {
|
||||
const client = useClient();
|
||||
const dispatch = useAppDispatch();
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['statuses', 'history', statusId],
|
||||
queryFn: () => client.statuses.getStatusHistory(statusId)
|
||||
.then(history => (dispatch(importEntities({ accounts: history.map(({ account }) => account) })), history))
|
||||
.then(history => history.map(minifyStatusEdit)),
|
||||
});
|
||||
};
|
||||
|
||||
export { useStatusHistory };
|
||||
@@ -1,9 +0,0 @@
|
||||
import { makePaginatedResponseQuery } from 'pl-fe/api/utils/make-paginated-response-query';
|
||||
import { minifyStatusList } from 'pl-fe/api/utils/minify-list';
|
||||
|
||||
const useStatusQuotes = makePaginatedResponseQuery(
|
||||
(statusId: string) => ['statusLists', 'quotes', statusId],
|
||||
(client, params) => client.statuses.getStatusQuotes(...params).then(minifyStatusList),
|
||||
);
|
||||
|
||||
export { useStatusQuotes };
|
||||
@@ -1,18 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { useClient } from 'pl-fe/hooks/use-client';
|
||||
|
||||
import type { Translation } from 'pl-api';
|
||||
|
||||
const useStatusTranslation = (statusId: string, targetLanguage?: string) => {
|
||||
const client = useClient();
|
||||
|
||||
return useQuery<Translation | false>({
|
||||
queryKey: ['statuses', 'translations', statusId, targetLanguage],
|
||||
queryFn: () => client.statuses.translateStatus(statusId, targetLanguage)
|
||||
.then(translation => translation).catch(() => false),
|
||||
enabled: !!targetLanguage,
|
||||
});
|
||||
};
|
||||
|
||||
export { useStatusTranslation };
|
||||
@@ -1,21 +0,0 @@
|
||||
import { useMutation } from '@tanstack/react-query';
|
||||
|
||||
import { useClient } from 'pl-fe/hooks/use-client';
|
||||
import { queryClient } from 'pl-fe/queries/client';
|
||||
|
||||
interface UpdateBookmarkFolderParams {
|
||||
name: string;
|
||||
emoji?: string;
|
||||
}
|
||||
|
||||
const useUpdateBookmarkFolder = (folderId: string) => {
|
||||
const client = useClient();
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ['bookmarkFolders', 'update', folderId],
|
||||
mutationFn: (params: UpdateBookmarkFolderParams) => client.myAccount.updateBookmarkFolder(folderId, params),
|
||||
onSettled: () => queryClient.invalidateQueries({ queryKey: ['bookmarkFolders'] }),
|
||||
});
|
||||
};
|
||||
|
||||
export { useUpdateBookmarkFolder };
|
||||
@@ -19,7 +19,7 @@ import { useSettingsStore } from 'pl-fe/stores/settings';
|
||||
import { getUnreadChatsCount, updateChatListItem } from 'pl-fe/utils/chats';
|
||||
import { play, soundCache } from 'pl-fe/utils/sounds';
|
||||
|
||||
import { updateReactions } from '../announcements/use-announcements';
|
||||
import { updateReactions } from '../../../queries/announcements/use-announcements';
|
||||
|
||||
import { useTimelineStream } from './use-timeline-stream';
|
||||
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { importEntities } from 'pl-fe/actions/importer';
|
||||
import { useAppDispatch } from 'pl-fe/hooks/use-app-dispatch';
|
||||
import { useClient } from 'pl-fe/hooks/use-client';
|
||||
import { useFeatures } from 'pl-fe/hooks/use-features';
|
||||
|
||||
import type { Suggestion } from 'pl-api';
|
||||
|
||||
type MinifiedSuggestion = Omit<Suggestion, 'account'> & { account_id: string };
|
||||
|
||||
const useSuggestedAccounts = () => {
|
||||
const client = useClient();
|
||||
const dispatch = useAppDispatch();
|
||||
const features = useFeatures();
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['suggestions'],
|
||||
queryFn: () => client.myAccount.getSuggestions().then((suggestions) => {
|
||||
dispatch(importEntities({ accounts: suggestions.map(({ account }) => account) }));
|
||||
return suggestions.map(({ account, ...suggestion }): MinifiedSuggestion => ({ account_id: account.id, ...suggestion }));
|
||||
}),
|
||||
enabled: features.suggestions || features.suggestionsV2,
|
||||
});
|
||||
};
|
||||
|
||||
export { useSuggestedAccounts, type MinifiedSuggestion };
|
||||
@@ -1,17 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { useClient } from 'pl-fe/hooks/use-client';
|
||||
import { useFeatures } from 'pl-fe/hooks/use-features';
|
||||
|
||||
const useTrendingLinks = () => {
|
||||
const client = useClient();
|
||||
const features = useFeatures();
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['trends', 'links'],
|
||||
queryFn: () => client.trends.getTrendingLinks(),
|
||||
enabled: features.trendingLinks,
|
||||
});
|
||||
};
|
||||
|
||||
export { useTrendingLinks };
|
||||
@@ -1,28 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { importEntities } from 'pl-fe/actions/importer';
|
||||
import { useAppDispatch } from 'pl-fe/hooks/use-app-dispatch';
|
||||
import { useClient } from 'pl-fe/hooks/use-client';
|
||||
import { useFeatures } from 'pl-fe/hooks/use-features';
|
||||
|
||||
const useTrendingStatuses = () => {
|
||||
const client = useClient();
|
||||
const dispatch = useAppDispatch();
|
||||
const features = useFeatures();
|
||||
|
||||
const fetchTrendingStatuses = async () => {
|
||||
const response = await client.trends.getTrendingStatuses();
|
||||
|
||||
dispatch(importEntities({ statuses: response }));
|
||||
|
||||
return response.map(({ id }) => id);
|
||||
};
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['trends', 'statuses'],
|
||||
queryFn: fetchTrendingStatuses,
|
||||
enabled: features.trendingStatuses,
|
||||
});
|
||||
};
|
||||
|
||||
export { useTrendingStatuses };
|
||||
Reference in New Issue
Block a user