pl-fe: support batch translations on supported backends
Signed-off-by: nicole mikołajczyk <git@mkljczk.pl>
This commit is contained in:
@ -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 };
|
||||
|
||||
@ -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,
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user