From d6aa4bf388d10a7019c6b126d41737d3d77ed700 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Thu, 24 Oct 2024 23:46:22 +0200 Subject: [PATCH] pl-hooks: Updates, add useStatusTranslation query MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- .../hooks/accounts/use-account-relationship.ts | 2 +- .../pl-hooks/lib/hooks/accounts/use-account.ts | 2 +- .../lib/hooks/notifications/use-notification.ts | 4 ++-- packages/pl-hooks/lib/hooks/polls/use-poll.ts | 2 +- .../hooks/statuses/use-status-translation.ts | 17 +++++++++++++++++ .../pl-hooks/lib/hooks/statuses/use-status.ts | 8 ++++---- packages/pl-hooks/lib/importer.ts | 4 ++-- packages/pl-hooks/lib/main.ts | 3 +++ packages/pl-hooks/lib/normalizers/account.ts | 2 +- packages/pl-hooks/lib/normalizers/status.ts | 5 +---- 10 files changed, 33 insertions(+), 16 deletions(-) create mode 100644 packages/pl-hooks/lib/hooks/statuses/use-status-translation.ts diff --git a/packages/pl-hooks/lib/hooks/accounts/use-account-relationship.ts b/packages/pl-hooks/lib/hooks/accounts/use-account-relationship.ts index 4f99191f9f..4074b9ed21 100644 --- a/packages/pl-hooks/lib/hooks/accounts/use-account-relationship.ts +++ b/packages/pl-hooks/lib/hooks/accounts/use-account-relationship.ts @@ -7,7 +7,7 @@ const useAccountRelationship = (accountId?: string) => { const { client } = usePlHooksApiClient(); const queryClient = usePlHooksQueryClient(); - return useQuery({ + return useQuery({ queryKey: ['accounts', 'entities', accountId], queryFn: async () => (await client.accounts.getRelationships([accountId!]))[0], enabled: !!accountId, diff --git a/packages/pl-hooks/lib/hooks/accounts/use-account.ts b/packages/pl-hooks/lib/hooks/accounts/use-account.ts index 2973823a6e..980df85ae8 100644 --- a/packages/pl-hooks/lib/hooks/accounts/use-account.ts +++ b/packages/pl-hooks/lib/hooks/accounts/use-account.ts @@ -3,7 +3,7 @@ import { useQuery, UseQueryResult } from '@tanstack/react-query'; import { usePlHooksApiClient } from 'pl-hooks/contexts/api-client'; import { queryClient, usePlHooksQueryClient } from 'pl-hooks/contexts/query-client'; import { importEntities } from 'pl-hooks/importer'; -import { normalizeAccount, type Account as NormalizedAccount } from 'pl-hooks/normalizers/account'; +import { normalizeAccount, type NormalizedAccount } from 'pl-hooks/normalizers/account'; import { useAccountRelationship } from './use-account-relationship'; diff --git a/packages/pl-hooks/lib/hooks/notifications/use-notification.ts b/packages/pl-hooks/lib/hooks/notifications/use-notification.ts index 0d6d994fec..54292d5877 100644 --- a/packages/pl-hooks/lib/hooks/notifications/use-notification.ts +++ b/packages/pl-hooks/lib/hooks/notifications/use-notification.ts @@ -7,8 +7,8 @@ import { type NormalizedNotification, normalizeNotification } from 'pl-hooks/nor import { useAccount } from '../accounts/use-account'; import { useStatus } from '../statuses/use-status'; -import type { Account } from 'pl-hooks/normalizers/account'; -import type { Status } from 'pl-hooks/normalizers/status'; +import type { NormalizedAccount as Account } from 'pl-hooks/normalizers/account'; +import type { NormalizedStatus as Status } from 'pl-hooks/normalizers/status'; const getNotificationStatusId = (n: NormalizedNotification) => { if (['mention', 'status', 'reblog', 'favourite', 'poll', 'update', 'emoji_reaction', 'event_reminder', 'participation_accepted', 'participation_request'].includes(n.type)) diff --git a/packages/pl-hooks/lib/hooks/polls/use-poll.ts b/packages/pl-hooks/lib/hooks/polls/use-poll.ts index 2974281a17..3619344da3 100644 --- a/packages/pl-hooks/lib/hooks/polls/use-poll.ts +++ b/packages/pl-hooks/lib/hooks/polls/use-poll.ts @@ -7,7 +7,7 @@ const usePoll = (pollId?: string) => { const queryClient = usePlHooksQueryClient(); const { client } = usePlHooksApiClient(); - return useQuery({ + return useQuery({ queryKey: ['polls', 'entities', pollId], queryFn: () => client.polls.getPoll(pollId!), enabled: !!pollId, diff --git a/packages/pl-hooks/lib/hooks/statuses/use-status-translation.ts b/packages/pl-hooks/lib/hooks/statuses/use-status-translation.ts new file mode 100644 index 0000000000..1a079addef --- /dev/null +++ b/packages/pl-hooks/lib/hooks/statuses/use-status-translation.ts @@ -0,0 +1,17 @@ +import { useQuery } from '@tanstack/react-query'; + +import { usePlHooksApiClient } from 'pl-hooks/contexts/api-client'; +import { usePlHooksQueryClient } from 'pl-hooks/contexts/query-client'; + +const useStatusTranslation = (statusId: string, targetLanguage?: string) => { + const { client } = usePlHooksApiClient(); + const queryClient = usePlHooksQueryClient(); + + return useQuery({ + queryKey: ['statuses', 'translations', statusId, targetLanguage], + queryFn: () => client.statuses.translateStatus(statusId, targetLanguage), + enabled: !!targetLanguage, + }, queryClient); +}; + +export { useStatusTranslation }; diff --git a/packages/pl-hooks/lib/hooks/statuses/use-status.ts b/packages/pl-hooks/lib/hooks/statuses/use-status.ts index e8a6e1de3b..626cf36623 100644 --- a/packages/pl-hooks/lib/hooks/statuses/use-status.ts +++ b/packages/pl-hooks/lib/hooks/statuses/use-status.ts @@ -4,8 +4,8 @@ import { usePlHooksApiClient } from 'pl-hooks/contexts/api-client'; import { queryClient, usePlHooksQueryClient } from 'pl-hooks/contexts/query-client'; import { importEntities } from 'pl-hooks/importer'; import { usePoll } from 'pl-hooks/main'; -import { type Account, normalizeAccount } from 'pl-hooks/normalizers/account'; -import { type Status as NormalizedStatus, normalizeStatus } from 'pl-hooks/normalizers/status'; +import { normalizeAccount, type NormalizedAccount } from 'pl-hooks/normalizers/account'; +import { type NormalizedStatus, normalizeStatus } from 'pl-hooks/normalizers/status'; import type { Poll } from 'pl-api'; @@ -87,8 +87,8 @@ const importStatus = (status: NormalizedStatus) => { }; type Status = NormalizedStatus & { - account: Account; - accounts: Array; + account: NormalizedAccount; + accounts: Array; poll?: Poll; reblog?: Status; }; diff --git a/packages/pl-hooks/lib/importer.ts b/packages/pl-hooks/lib/importer.ts index cd1bb4131a..8296648874 100644 --- a/packages/pl-hooks/lib/importer.ts +++ b/packages/pl-hooks/lib/importer.ts @@ -1,7 +1,7 @@ import { queryClient } from 'pl-hooks/contexts/query-client'; import { type DeduplicatedNotification, type NormalizedNotification, normalizeNotification } from './normalizers/notification'; -import { normalizeStatus, type Status } from './normalizers/status'; +import { normalizeStatus, type NormalizedStatus } from './normalizers/status'; import type { Account as BaseAccount, @@ -32,7 +32,7 @@ const importRelationship = (relationship: BaseRelationship) => queryClient.setQu ['relationships', 'entities', relationship.id], relationship, ); -const importStatus = (status: BaseStatus) => queryClient.setQueryData( +const importStatus = (status: BaseStatus) => queryClient.setQueryData( ['statuses', 'entities', status.id], normalizeStatus(status), ); diff --git a/packages/pl-hooks/lib/main.ts b/packages/pl-hooks/lib/main.ts index 859227b4e4..db232ea0b6 100644 --- a/packages/pl-hooks/lib/main.ts +++ b/packages/pl-hooks/lib/main.ts @@ -12,5 +12,8 @@ export * from './hooks/notifications/use-notification'; export * from './hooks/notifications/use-notification-list'; export * from './hooks/polls/use-poll'; export * from './hooks/statuses/use-status'; +export * from './hooks/statuses/use-status-translation'; export * from './importer'; + +export type { NormalizedAccount } from './normalizers/account'; diff --git a/packages/pl-hooks/lib/normalizers/account.ts b/packages/pl-hooks/lib/normalizers/account.ts index 7b2cf921e9..77b5584e3f 100644 --- a/packages/pl-hooks/lib/normalizers/account.ts +++ b/packages/pl-hooks/lib/normalizers/account.ts @@ -7,4 +7,4 @@ const normalizeAccount = ({ moved, relationship, ...account }: BaseAccount) => ( type Account = ReturnType; -export { normalizeAccount, type Account }; +export { normalizeAccount, type Account as NormalizedAccount }; diff --git a/packages/pl-hooks/lib/normalizers/status.ts b/packages/pl-hooks/lib/normalizers/status.ts index 3010ac232d..8727dde335 100644 --- a/packages/pl-hooks/lib/normalizers/status.ts +++ b/packages/pl-hooks/lib/normalizers/status.ts @@ -55,14 +55,11 @@ const normalizeStatus = ({ account, accounts, reblog, poll, group, quote, ...sta reblog_id: reblog?.id || null, poll_id: poll?.id || null, group_id: group?.id || null, - translating: false, expectsCard: false, showFiltered: null as null | boolean, ...status, quote_id: quote?.id || status.quote_id || null, mentions, - expanded: null, - hidden: null, filtered: status.filtered?.map(result => result.filter.title), event, media_attachments, @@ -75,5 +72,5 @@ export { type StatusApprovalStatus, type StatusVisibility, normalizeStatus, - type Status, + type Status as NormalizedStatus, };