pl-fe: support batch translations on supported backends

Signed-off-by: nicole mikołajczyk <git@mkljczk.pl>
This commit is contained in:
nicole mikołajczyk
2025-10-23 19:55:23 +02:00
parent 72d86fd62d
commit a049b534a2
2 changed files with 15 additions and 2 deletions

View File

@ -9,8 +9,16 @@ const relationships = memoize((client: PlApiClient) => create({
scheduler: bufferScheduler(200),
}));
// TODO: proper multi-client support
const translations = memoize((lang: string, client: PlApiClient) => create({
fetcher: (ids: string[]) => client.statuses.translateStatuses(ids, lang),
resolver: keyResolver('id'),
scheduler: bufferScheduler(200),
}));
const batcher = {
relationships,
translations,
};
export { batcher };

View File

@ -1,16 +1,21 @@
import { useQuery } from '@tanstack/react-query';
import { batcher } from 'pl-fe/api/batcher';
import { useClient } from 'pl-fe/hooks/use-client';
import { useFeatures } from 'pl-fe/hooks/use-features';
import type { Translation } from 'pl-api';
const useStatusTranslation = (statusId: string, targetLanguage?: string) => {
const client = useClient();
const features = useFeatures();
return useQuery<Translation | false>({
queryKey: ['statuses', 'translations', statusId, targetLanguage],
queryFn: () => client.statuses.translateStatus(statusId, targetLanguage)
.then(translation => translation).catch(() => false),
queryFn: () => (features.lazyTranslations && targetLanguage
? batcher.translations(targetLanguage, client).fetch(statusId)
: client.statuses.translateStatus(statusId, targetLanguage))
.then(translation => translation || false).catch(() => false),
enabled: !!targetLanguage,
});
};