wip hooks migration

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2024-10-05 00:13:47 +02:00
parent cf9f9137b0
commit eeb1e1370c
19 changed files with 101 additions and 50 deletions

View file

@ -85,6 +85,7 @@ const baseAccountSchema = z.object({
group: z.boolean().catch(false),
discoverable: z.boolean().catch(false),
noindex: z.boolean().nullable().catch(null),
moved: z.null().catch(null),
suspended: z.boolean().optional().catch(undefined),
limited: z.boolean().optional().catch(undefined),
created_at: z.string().datetime().catch(new Date().toUTCString()),
@ -127,13 +128,15 @@ const baseAccountSchema = z.object({
});
const accountWithMovedAccountSchema = baseAccountSchema.extend({
moved: baseAccountSchema.optional().catch(undefined),
moved: z.lazy((): typeof baseAccountSchema => accountWithMovedAccountSchema as any).nullable().catch(null),
});
/** @see {@link https://docs.joinmastodon.org/entities/Account/} */
const accountSchema = z.preprocess(preprocessAccount, accountWithMovedAccountSchema);
type Account = z.infer<typeof accountSchema>;
type Account = z.infer<typeof baseAccountSchema> & {
moved: Account | null;
};
const credentialAccountSchema = z.preprocess(preprocessAccount, accountWithMovedAccountSchema.extend({
source: z.object({

View file

@ -1,8 +1,8 @@
import { PLEROMA, type UpdateNotificationSettingsParams, type Account, type CreateAccountParams, type PaginatedResponse, type Relationship } from 'pl-api';
import { importEntities } from 'pl-fe/pl-hooks/importer';
import { getClient, type PlfeResponse } from 'pl-fe/api';
import { Entities } from 'pl-fe/entity-store/entities';
import { importEntities } from 'pl-fe/pl-hooks/importer';
import { selectAccount } from 'pl-fe/selectors';
import { isLoggedIn } from 'pl-fe/utils/auth';

View file

@ -1,6 +1,7 @@
import clsx from 'clsx';
import debounce from 'lodash/debounce';
import { useMarker, useUpdateMarkerMutation } from 'pl-fe/pl-hooks/hooks/markers/useMarkers';
import { useMarker } from 'pl-fe/pl-hooks/hooks/markers/useMarkers';
import { useUpdateMarkerMutation } from 'pl-fe/pl-hooks/hooks/markers/useUpdateMarkerMutation';
import { useNotificationList } from 'pl-fe/pl-hooks/hooks/notifications/useNotificationList';
import React, { useCallback, useEffect, useRef } from 'react';
import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
@ -150,7 +151,7 @@ const Notifications = () => {
placeholderCount={20}
onLoadMore={handleLoadOlder}
onScroll={handleScroll}
listClassName={clsx('black:divide-gray-800 dark:divide-primary-800 divide-y divide-solid divide-gray-200', {
listClassName={clsx('divide-y divide-solid divide-gray-200 black:divide-gray-800 dark:divide-primary-800', {
'animate-pulse': notifications.length === 0,
})}
>

View file

@ -1,5 +1,5 @@
import { useStatus } from 'pl-fe/pl-hooks/hooks/statuses/useStatus';
import React, { useEffect, useState } from 'react';
import React, { useEffect } from 'react';
import { defineMessages, useIntl } from 'react-intl';
import { Redirect } from 'react-router-dom';

View file

@ -37,7 +37,6 @@ const normalizeAccount = (account: BaseAccount) => {
const emojiMap = makeEmojiMap(account.emojis);
return {
mute_expires_at: null,
...account,
avatar: account.avatar || account.avatar_static || missingAvatar,
avatar_static: account.avatar_static || account.avatar || missingAvatar,

View file

@ -1 +0,0 @@
../../pl-hooks/lib

View file

@ -0,0 +1,42 @@
import { useQuery } from '@tanstack/react-query';
import { useRelationship } from 'pl-fe/api/hooks/accounts/useRelationship';
import { useAppSelector, useClient } from 'pl-fe/hooks';
interface UseAccountOpts {
withRelationship?: boolean;
withScrobble?: boolean;
withMoveTarget?: boolean;
}
const useAccount = (accountId?: string, opts: UseAccountOpts = {}) => {
const client = useClient();
const accountQuery = useQuery({
queryKey: ['accounts', 'entities', accountId],
queryFn: () => client.accounts.getAccount(accountId!)
.then(normalizeAccount)
.then(minifyAccount),
enabled: !!accountId,
});
const relationshipQuery = useRelationship(accountId, {
enabled: opts.withRelationship,
});
const movedQuery = useAccount(opts.withMoveTarget && accountQuery.data?.moved_id || undefined);
const data: Account | null = useAppSelector((state) => {
const account = accountQuery.data;
if (!account) return null;
return {
...account,
account,
relationship: relationshipQuery.relationship,
moved: movedQuery.data || null,
};
});
return { ...accountQuery, data };
};

View file

@ -0,0 +1,24 @@
import { useQuery } from '@tanstack/react-query';
import { useClient } from 'pl-fe/hooks';
import { queryClient } from 'pl-fe/queries/client';
import type { PlApiClient } from 'pl-api';
type Timeline = 'home' | 'notifications';
const useMarker = (timeline: Timeline) => {
const client = useClient();
return useQuery({
queryKey: ['markers', timeline],
queryFn: () => client.timelines.getMarkers([timeline]).then(markers => markers[timeline]),
});
};
const prefetchMarker = (client: PlApiClient, timeline: 'home' | 'notifications') =>
queryClient.prefetchQuery({
queryKey: ['markers', timeline],
queryFn: () => client.timelines.getMarkers([timeline]).then(markers => markers[timeline]),
});
export { useMarker, prefetchMarker, type Timeline };

View file

@ -0,0 +1,25 @@
import { useMutation } from '@tanstack/react-query';
import { useClient } from 'pl-fe/hooks';
import { queryClient } from 'pl-fe/queries/client';
import type { Timeline } from './useMarkers';
import type { Marker } from 'pl-api';
const useUpdateMarkerMutation = (timeline: Timeline) => {
const client = useClient();
return useMutation({
mutationFn: (lastReadId: string) => client.timelines.saveMarkers({
[timeline]: {
last_read_id: lastReadId,
},
}),
retry: false,
onMutate: (lastReadId) => queryClient.setQueryData<Marker>(['markers', timeline], (marker) => marker ? ({
...marker,
last_read_id: lastReadId,
}) : undefined),
});
};
export { useUpdateMarkerMutation };

View file

@ -1,42 +0,0 @@
import { useMutation, useQuery } from '@tanstack/react-query';
import { useClient } from 'pl-fe/hooks';
import { queryClient } from 'pl-fe/queries/client';
import type { Marker, PlApiClient } from 'pl-api';
type Timeline = 'home' | 'notifications';
const useMarker = (timeline: Timeline) => {
const client = useClient();
return useQuery({
queryKey: ['markers', timeline],
queryFn: () => client.timelines.getMarkers([timeline]).then(markers => markers[timeline]),
});
};
const useUpdateMarkerMutation = (timeline: Timeline) => {
const client = useClient();
return useMutation({
mutationFn: (lastReadId: string) => client.timelines.saveMarkers({
[timeline]: {
last_read_id: lastReadId,
},
}),
retry: false,
onMutate: (lastReadId) => queryClient.setQueryData<Marker>(['markers', timeline], (marker) => marker ? ({
...marker,
last_read_id: lastReadId,
}) : undefined),
});
};
const prefetchMarker = (client: PlApiClient, timeline: 'home' | 'notifications') =>
queryClient.prefetchQuery({
queryKey: ['markers', timeline],
queryFn: () => client.timelines.getMarkers([timeline]).then(markers => markers[timeline]),
});
export { useMarker, prefetchMarker, useUpdateMarkerMutation };