|
|
@@ -12,7 +12,8 @@ import {
|
|
|
|
markAutoTranslateTried,
|
|
|
|
markAutoTranslateTried,
|
|
|
|
} from '../state/translation';
|
|
|
|
} from '../state/translation';
|
|
|
|
import { chromeTranslationEngine as engine } from '../utils/translation/chromeEngine';
|
|
|
|
import { chromeTranslationEngine as engine } from '../utils/translation/chromeEngine';
|
|
|
|
import { sameLanguage } from '../utils/translation/langUtils';
|
|
|
|
import { sameLanguage, translateSkipMessage } from '../utils/translation/langUtils';
|
|
|
|
|
|
|
|
import { toastQueueAtom } from '../state/toast';
|
|
|
|
|
|
|
|
|
|
|
|
export type MsgTranslationStatus =
|
|
|
|
export type MsgTranslationStatus =
|
|
|
|
| 'idle' // not translating (original shown)
|
|
|
|
| 'idle' // not translating (original shown)
|
|
|
@@ -52,6 +53,7 @@ export const useMessageTranslation = (eventId: string, text: string): MsgTransla
|
|
|
|
// Write-only: reading the whole cache array here (via useAtom) would
|
|
|
|
// Write-only: reading the whole cache array here (via useAtom) would
|
|
|
|
// resubscribe this message to every other message's writes. [Gitea #39]
|
|
|
|
// resubscribe this message to every other message's writes. [Gitea #39]
|
|
|
|
const setCache = useSetAtom(translationCacheAtom);
|
|
|
|
const setCache = useSetAtom(translationCacheAtom);
|
|
|
|
|
|
|
|
const setToast = useSetAtom(toastQueueAtom);
|
|
|
|
|
|
|
|
|
|
|
|
// The key includes a fingerprint of `text`, so an edit (same event id, new
|
|
|
|
// The key includes a fingerprint of `text`, so an edit (same event id, new
|
|
|
|
// body) misses the old entry and re-translates instead of showing stale
|
|
|
|
// body) misses the old entry and re-translates instead of showing stale
|
|
|
@@ -124,6 +126,16 @@ export const useMessageTranslation = (eventId: string, text: string): MsgTransla
|
|
|
|
if (!from || sameLanguage(from, targetLang)) {
|
|
|
|
if (!from || sameLanguage(from, targetLang)) {
|
|
|
|
setStatus('skipped');
|
|
|
|
setStatus('skipped');
|
|
|
|
setActive(false);
|
|
|
|
setActive(false);
|
|
|
|
|
|
|
|
// [Gitea #287] Say why nothing happened instead of silently undoing
|
|
|
|
|
|
|
|
// the click (no chip shows once translation is switched back off).
|
|
|
|
|
|
|
|
setToast({
|
|
|
|
|
|
|
|
id: `translate-skipped-${eventId}-${Date.now()}`,
|
|
|
|
|
|
|
|
displayName: 'Translate',
|
|
|
|
|
|
|
|
body: translateSkipMessage(from, targetLang),
|
|
|
|
|
|
|
|
roomName: '',
|
|
|
|
|
|
|
|
roomId: '',
|
|
|
|
|
|
|
|
onClick: () => undefined,
|
|
|
|
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setFromLang(from);
|
|
|
|
setFromLang(from);
|
|
|
@@ -154,7 +166,7 @@ export const useMessageTranslation = (eventId: string, text: string): MsgTransla
|
|
|
|
// Invalidate this run's writes if inputs change / unmount.
|
|
|
|
// Invalidate this run's writes if inputs change / unmount.
|
|
|
|
runIdRef.current += 1;
|
|
|
|
runIdRef.current += 1;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}, [active, cached, supported, text, targetLang, key, setActive, setCache]);
|
|
|
|
}, [active, cached, supported, text, targetLang, key, setActive, setCache, setToast, eventId]);
|
|
|
|
|
|
|
|
|
|
|
|
// When switched off, drop back to idle (keep the cached translation around so
|
|
|
|
// When switched off, drop back to idle (keep the cached translation around so
|
|
|
|
// toggling back on is instant).
|
|
|
|
// toggling back on is instant).
|
|
|
|