Apply some changes from hooks-migration branch

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2024-11-05 16:12:16 +01:00
parent 91a4950b7b
commit e62e09e387
5 changed files with 31 additions and 20 deletions

View file

@ -119,7 +119,7 @@ const baseAccountSchema = v.object({
following_count: v.fallback(v.number(), 0),
roles: filteredArray(roleSchema),
fqn: v.fallback(v.nullable(v.string()), null),
fqn: v.string(),
ap_id: v.fallback(v.nullable(v.string()), null),
background_image: v.fallback(v.nullable(v.string()), null),
relationship: v.fallback(v.optional(relationshipSchema), undefined),

View file

@ -3,13 +3,16 @@ import { useQuery } from '@tanstack/react-query';
import { usePlHooksApiClient } from 'pl-hooks/contexts/api-client';
import { usePlHooksQueryClient } from 'pl-hooks/contexts/query-client';
import type { Translation } from 'pl-api';
const useStatusTranslation = (statusId: string, targetLanguage?: string) => {
const { client } = usePlHooksApiClient();
const queryClient = usePlHooksQueryClient();
return useQuery({
return useQuery<Translation | false>({
queryKey: ['statuses', 'translations', statusId, targetLanguage],
queryFn: () => client.statuses.translateStatus(statusId, targetLanguage),
queryFn: () => client.statuses.translateStatus(statusId, targetLanguage)
.then(translation => translation).catch(() => false),
enabled: !!targetLanguage,
}, queryClient);
};

View file

@ -38,13 +38,15 @@ const importStatus = (status: BaseStatus) => queryClient.setQueryData<Normalized
const isEmpty = (object: Record<string, any>) => !Object.values(object).some(value => value);
type OptionalArray<T> = Array<T | undefined | null>;
const importEntities = (entities: {
accounts?: Array<BaseAccount>;
groups?: Array<BaseGroup>;
notifications?: Array<DeduplicatedNotification>;
polls?: Array<BasePoll>;
statuses?: Array<BaseStatus>;
relationships?: Array<BaseRelationship>;
accounts?: OptionalArray<BaseAccount>;
groups?: OptionalArray<BaseGroup>;
notifications?: OptionalArray<DeduplicatedNotification>;
polls?: OptionalArray<BasePoll>;
statuses?: OptionalArray<BaseStatus>;
relationships?: OptionalArray<BaseRelationship>;
}, options = {
withParents: true,
}) => {
@ -90,14 +92,14 @@ const importEntities = (entities: {
};
if (options.withParents) {
entities.groups?.forEach(group => groups[group.id] = group);
entities.polls?.forEach(poll => polls[poll.id] = poll);
entities.relationships?.forEach(relationship => relationships[relationship.id] = relationship);
entities.groups?.forEach(group => group && (groups[group.id] = group));
entities.polls?.forEach(poll => poll && (polls[poll.id] = poll));
entities.relationships?.forEach(relationship => relationship && (relationships[relationship.id] = relationship));
}
entities.accounts?.forEach((account) => processAccount(account, options.withParents));
entities.notifications?.forEach((notification) => processNotification(notification, options.withParents));
entities.statuses?.forEach((status) => processStatus(status, options.withParents));
entities.accounts?.forEach((account) => account && processAccount(account, options.withParents));
entities.notifications?.forEach((notification) => notification && processNotification(notification, options.withParents));
entities.statuses?.forEach((status) => status && processStatus(status, options.withParents));
if (!isEmpty(accounts)) Object.values(accounts).forEach(importAccount);
if (!isEmpty(groups)) Object.values(groups).forEach(importGroup);

View file

@ -35,7 +35,7 @@
"dependencies": {
"@tanstack/react-query": "^5.59.16",
"lodash": "^4.17.21",
"pl-api": "^0.1.5",
"pl-api": "^0.1.9",
"react": "^18.3.1",
"valibot": "^0.42.1"
},

View file

@ -1891,6 +1891,11 @@ lodash.merge@^4.6.2:
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==
lodash.omit@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.omit/-/lodash.omit-4.5.0.tgz#6eb19ae5a1ee1dd9df0b969e66ce0b7fa30b5e60"
integrity sha512-XeqSp49hNGmlkj2EJlfrQFIzQ6lXdNro9sddtQzcJY8QaoC2GO0DT7xaIokHeyM+mIT0mPMlPvkYzg2xCuHdZg==
lodash.pick@^4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/lodash.pick/-/lodash.pick-4.4.0.tgz#52f05610fff9ded422611441ed1fc123a03001b3"
@ -2148,13 +2153,14 @@ pkg-types@^1.0.3, pkg-types@^1.1.1:
mlly "^1.7.1"
pathe "^1.1.2"
pl-api@^0.1.5:
version "0.1.5"
resolved "https://registry.yarnpkg.com/pl-api/-/pl-api-0.1.5.tgz#373d58fc40ae23b12c6d20def8d3332112e2dd93"
integrity sha512-IMcwANPtTRMv+tTLW4ic/o/usYIHeapR+aOx5YhmmVQsXTiVUZQtCVLmrfIrqMnyXm8PHvE/arzzozRFU8+GXQ==
pl-api@^0.1.9:
version "0.1.9"
resolved "https://registry.yarnpkg.com/pl-api/-/pl-api-0.1.9.tgz#250aacb4533de0acbe0aa487c2810806a2d99415"
integrity sha512-5r+TZXbEBU9BednnYXxdI1KgnvRSy/lhSLLU8ZsmMgapy+IyxG2yGmxSc7j0NuC59kBYxlKKwaRgdRvwclQOaQ==
dependencies:
blurhash "^2.0.5"
http-link-header "^1.1.3"
lodash.omit "^4.5.0"
lodash.pick "^4.4.0"
object-to-formdata "^4.5.1"
query-string "^9.1.0"