Files
gods-eye-view/src/voice/chatPreferences.js
T
German Khokhlov e66b99c8ff Fix the typed AI chat translations
The quick commands "Reset to globe view" and "Show earthquakes" reached t()
through a .map(), so the catalog check never saw them and every language
showed them in English. They are now marked with msg() and translated.

Review of the 71 strings from #1507 in all eleven languages:
- zh-Hant is regenerated from zh-CN with OpenCC s2tw, as its guide says;
  40 entries had Taiwan vocabulary instead.
- ar "Dock to the right" names the left side, where RTL puts that rail.
- ja "What am I looking at?" no longer asks what the assistant sees.
- Provider Settings, dock, provider, live and map use each glossary's terms.
- de drops the du-form; ja drops spaces between Japanese and Latin text.
- Calques, register and the example commands read naturally.
2026-11-04 17:53:23 +04:00

114 lines
3.4 KiB
JavaScript

import { msg, t } from '../i18n/index.js';
/**
* Chat suggestions and the one chat preference (quick commands), written to
* the voice control presenter. Recent commands and autocomplete are always on;
* command history is deliberately transient.
*/
export function bindChatPreferences({ ui, getDraft, setDraft }) {
const key = 'gev.chat.preferences';
let quick = true;
let recent = [];
let historyIndex = -1;
let saved = '';
const resetHistory = () => {
historyIndex = -1;
saved = '';
};
const quickCommands = () =>
[
msg('Fly to Tokyo'),
msg('Show live flights'),
msg('Reset to globe view'),
msg('Show earthquakes'),
].map((text) => t(text));
try {
const stored = JSON.parse(localStorage.getItem(key));
if (typeof stored?.quick === 'boolean') quick = stored.quick;
} catch {
/* Storage is optional. */
}
function render() {
const draft = getDraft();
if (historyIndex >= 0 && draft !== recent[historyIndex]) resetHistory();
const query = draft.trim().toLowerCase();
const groups = [];
if (query) {
const candidates = [...recent, ...(quick ? quickCommands() : [])];
groups.push({
label: t('Suggestions'),
items: [...new Set(candidates)]
.filter(
(text) =>
text.toLowerCase().includes(query) &&
text.toLowerCase() !== query,
)
.slice(0, 4),
});
} else {
groups.push({ label: t('Recent'), items: recent.slice(0, 3) });
if (quick)
groups.push({
label: t('Quick commands'),
items: quickCommands().slice(0, 3),
});
}
ui.update('chat', {
quickChecked: quick,
suggestions: groups.filter((group) => group.items.length),
});
}
const offLocale = ui.on('locale', render);
render();
return {
render,
setQuick(value) {
quick = Boolean(value);
try {
localStorage.setItem(key, JSON.stringify({ quick }));
} catch {
/* Storage is optional. */
}
render();
},
remember(text) {
resetHistory();
recent = [text, ...recent.filter((entry) => entry !== text)].slice(0, 8);
render();
},
/**
* Up and Down recall earlier messages from the start of the input, and
* restore the unsent draft. Returns whether the key was used.
* @param {KeyboardEvent} event
*/
keydown(event) {
const input = event.target;
if (
!['ArrowUp', 'ArrowDown'].includes(event.key) ||
event.isComposing ||
event.altKey ||
event.ctrlKey ||
event.metaKey ||
event.shiftKey ||
input?.selectionStart !== input?.selectionEnd ||
!recent.length
)
return false;
const up = event.key === 'ArrowUp';
// Keep history navigation at the start of recalled text. Moving the
// caret into a message returns arrow keys to native textarea editing.
if (input.selectionStart !== 0 || (!up && historyIndex < 0)) return false;
event.preventDefault();
if (up) {
if (historyIndex < 0) saved = getDraft();
historyIndex = Math.min(historyIndex + 1, recent.length - 1);
} else historyIndex--;
const next = historyIndex < 0 ? saved : recent[historyIndex];
setDraft(next, { caret: historyIndex < 0 ? next.length : 0 });
render();
return true;
},
destroy: offLocale,
};
}