Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix: confirm shortcut reset and refresh translations (#5295)
Jiayuan Zhang6aed9b40ee7a
diff --git a/packages/core/i18n/provider.test.tsx b/packages/core/i18n/provider.test.tsxnew file mode 100644index 0000000000..cb21da4fa3--- /dev/null+++ b/packages/core/i18n/provider.test.tsx@@ -0,0 +1,50 @@+// @vitest-environment jsdom++import { cleanup, render, screen, waitFor } from "@testing-library/react";+import { afterEach, describe, expect, it } from "vitest";+import { useTranslation } from "react-i18next";+import { I18nProvider } from "./provider";+import type { LocaleResources } from "./types";++afterEach(cleanup);++function DialogTitle() {+ const { t } = useTranslation("settings");+ return <span>{t("shortcuts.reset_confirm.title")}</span>;+}++describe("I18nProvider", () => {+ it("rebuilds its i18n instance when resources change", async () => {+ const initialResources: Record<string, LocaleResources> = {+ en: { settings: { shortcuts: { title: "Keyboard Shortcuts" } } },+ };+ const updatedResources: Record<string, LocaleResources> = {+ en: {+ settings: {+ shortcuts: {+ title: "Keyboard Shortcuts",+ reset_confirm: { title: "Restore all shortcut defaults?" },+ },+ },+ },+ };++ const { rerender } = render(+ <I18nProvider locale="en" resources={initialResources}>+ <DialogTitle />+ </I18nProvider>,+ );+ expect(screen.queryByText("shortcuts.reset_confirm.title")).not.toBeNull();++ rerender(+ <I18nProvider locale="en" resources={updatedResources}>+ <DialogTitle />+ </I18nProvider>,+ );+ await waitFor(() => {+ expect(+ screen.queryByText("Restore all shortcut defaults?"),+ ).not.toBeNull();+ });+ });+});diff --git a/packages/core/i18n/provider.tsx b/packages/core/i18n/provider.tsxindex bf824a91c0..e9fd954fb7 100644--- a/packages/core/i18n/provider.tsx+++ b/packages/core/i18n/provider.tsx@@ -1,6 +1,6 @@"use client";-import { useState, type ReactNode } from "react";+import { useEffect, useRef, useState, type ReactNode } from "react";import { I18nextProvider } from "react-i18next";import { createI18n } from "./create-i18n";import type { LocaleResources, SupportedLocale } from "./types";@@ -16,9 +16,39 @@ export function I18nProvider({resources,children,}: I18nProviderProps) {- // Lazy init via useState so the instance survives re-renders.- // Locale + resources are determined at boot and never change at runtime —- // language switching goes through window.location.reload().const [instance] = useState(() => createI18n(locale, resources));+ const previous = useRef({ locale, resources });++ // Fast Refresh can update locale JSON without remounting this provider.+ // Synchronize changed bundles into the live instance, then emit the event+ // react-i18next subscribes to so newly added translations replace raw keys.+ useEffect(() => {+ if (+ previous.current.locale === locale &&+ previous.current.resources === resources+ ) {+ return;+ }++ for (const [language, namespaces] of Object.entries(resources)) {+ for (const [namespace, bundle] of Object.entries(namespaces)) {+ instance.addResourceBundle(+ language,+ namespace,+ bundle,+ true,+ true,+ );+ }+ }++ if (instance.language !== locale) {+ void instance.changeLanguage(locale);+ } else {+ instance.emit("languageChanged", locale);+ }+ previous.current = { locale, resources };+ }, [instance, locale, resources]);+return <I18nextProvider i18n={instance}>{children}</I18nextProvider>;}diff --git a/packages/views/locales/en/settings.json b/packages/views/locales/en/settings.jsonindex 856372a176..85dba20fb1 100644--- a/packages/views/locales/en/settings.json+++ b/packages/views/locales/en/settings.json@@ -37,6 +37,12 @@"description": "Customize product actions on this device. Click a shortcut, then press the new key combination.","search_placeholder": "Search actions...","reset_all": "Restore defaults",+ "reset_confirm": {+ "title": "Restore all shortcut defaults?",+ "description": "This will remove every custom shortcut and restore the defaults on this device.",+ "cancel": "Cancel",+ "confirm": "Restore all defaults"+ },"reset": "Reset","disable": "Disable","reset_action": "Reset {{action}}",diff --git a/packages/views/locales/ja/settings.json b/packages/views/locales/ja/settings.jsonindex a268fbec5c..4d31b2c5be 100644--- a/packages/views/locales/ja/settings.json+++ b/packages/views/locales/ja/settings.json@@ -37,6 +37,12 @@"description": "このデバイスで製品操作のショートカットをカスタマイズします。ショートカットをクリックして、新しいキーの組み合わせを押してください。","search_placeholder": "操作を検索...","reset_all": "デフォルトに戻す",+ "reset_confirm": {+ "title": "すべてのショートカットをデフォルトに戻しますか?",+ "description": "このデバイスのカスタムショートカットをすべて削除し、デフォルト設定に戻します。",+ "cancel": "キャンセル",+ "confirm": "すべてデフォルトに戻す"+ },"reset": "リセット","disable": "無効にする","reset_action": "{{action}}をリセット",diff --git a/packages/views/locales/ko/settings.json b/packages/views/locales/ko/settings.jsonindex 9749b37738..94b7e50f47 100644--- a/packages/views/locales/ko/settings.json+++ b/packages/views/locales/ko/settings.json@@ -37,6 +37,12 @@"description": "이 기기에서 제품 동작의 단축키를 설정합니다. 단축키를 클릭한 다음 새 키 조합을 누르세요.","search_placeholder": "동작 검색...","reset_all": "기본값 복원",+ "reset_confirm": {+ "title": "모든 단축키를 기본값으로 복원할까요?",+ "description": "이 기기의 모든 사용자 지정 단축키를 삭제하고 기본 설정으로 복원합니다.",+ "cancel": "취소",+ "confirm": "모두 기본값으로 복원"+ },"reset": "재설정","disable": "사용 안 함","reset_action": "{{action}} 재설정",diff --git a/packages/views/locales/zh-Hans/settings.json b/packages/views/locales/zh-Hans/settings.jsonindex 9fa864d497..cd5ee1a1ea 100644--- a/packages/views/locales/zh-Hans/settings.json+++ b/packages/views/locales/zh-Hans/settings.json@@ -37,6 +37,12 @@"description": "自定义当前设备上的产品操作。点击一个快捷键,然后按下新的组合键。","search_placeholder": "搜索操作...","reset_all": "恢复默认设置",+ "reset_confirm": {+ "title": "恢复所有快捷键的默认设置?",+ "description": "这会清除当前设备上的所有自定义快捷键,并恢复默认设置。",+ "cancel": "取消",+ "confirm": "恢复全部默认设置"+ },"reset": "重置","disable": "停用","reset_action": "重置「{{action}}」",diff --git a/packages/views/settings/components/keyboard-shortcuts-tab.test.tsx b/packages/views/settings/components/keyboard-shortcuts-tab.test.tsxindex a465baf582..9c8f3f191e 100644--- a/packages/views/settings/components/keyboard-shortcuts-tab.test.tsx+++ b/packages/views/settings/components/keyboard-shortcuts-tab.test.tsx@@ -136,4 +136,35 @@ describe("KeyboardShortcutsTab", () => {);expect(getShortcut("createIssue")).toEqual(createShortcutChord("C"));});++ it("confirms before restoring all shortcut defaults", () => {+ useShortcutStore.getState().setShortcut(+ "openSearch",+ createShortcutChord("J", { primary: true }),+ );+ renderWithI18n(<KeyboardShortcutsTab />);++ fireEvent.click(screen.getByRole("button", { name: "Restore defaults" }));+ expect(screen.getByRole("alertdialog")).toHaveTextContent(+ "This will remove every custom shortcut and restore the defaults on this device.",+ );+ expect(getShortcut("openSearch")).toEqual(+ createShortcutChord("J", { primary: true }),+ );++ fireEvent.click(screen.getByRole("button", { name: "Cancel" }));+ expect(screen.queryByRole("alertdialog")).not.toBeInTheDocument();+ expect(getShortcut("openSearch")).toEqual(+ createShortcutChord("J", { primary: true }),+ );++ fireEvent.click(screen.getByRole("button", { name: "Restore defaults" }));+ fireEvent.click(+ screen.getByRole("button", { name: "Restore all defaults" }),+ );+ expect(screen.queryByRole("alertdialog")).not.toBeInTheDocument();+ expect(getShortcut("openSearch")).toEqual(+ createShortcutChord("K", { primary: true }),+ );+ });});diff --git a/packages/views/settings/components/keyboard-shortcuts-tab.tsx b/packages/views/settings/components/keyboard-shortcuts-tab.tsxindex c6e7224508..4db3f21528 100644--- a/packages/views/settings/components/keyboard-shortcuts-tab.tsx+++ b/packages/views/settings/components/keyboard-shortcuts-tab.tsx@@ -4,6 +4,16 @@ import { useMemo, useState } from "react";import { Keyboard, RotateCcw, Search, X } from "lucide-react";import { Button } from "@multica/ui/components/ui/button";import { Input } from "@multica/ui/components/ui/input";+import {+ AlertDialog,+ AlertDialogAction,+ AlertDialogCancel,+ AlertDialogContent,+ AlertDialogDescription,+ AlertDialogFooter,+ AlertDialogHeader,+ AlertDialogTitle,+} from "@multica/ui/components/ui/alert-dialog";import { cn } from "@multica/ui/lib/utils";import {findShortcutConflict,@@ -42,6 +52,7 @@ export function KeyboardShortcutsTab() {const [query, setQuery] = useState("");const [recording, setRecording] = useState<ShortcutActionId | null>(null);const [captureError, setCaptureError] = useState<CaptureError>(null);+ const [resetConfirmOpen, setResetConfirmOpen] = useState(false);const overrides = useShortcutStore((state) => state.overrides);const setShortcut = useShortcutStore((state) => state.setShortcut);const resetShortcut = useShortcutStore((state) => state.resetShortcut);@@ -119,11 +130,7 @@ export function KeyboardShortcutsTab() {<Buttonvariant="outline"size="sm"- onClick={() => {- resetAll();- setCaptureError(null);- setRecording(null);- }}+ onClick={() => setResetConfirmOpen(true)}disabled={Object.keys(overrides).length === 0}><RotateCcw className="size-3.5" />@@ -195,6 +202,35 @@ export function KeyboardShortcutsTab() {<FixedShortcutRow label={t(($) => $.shortcuts.fixed.close_dialog)} shortcut={createShortcutChord("Escape")} /></SettingsCard></SettingsSection>++ <AlertDialog open={resetConfirmOpen} onOpenChange={setResetConfirmOpen}>+ <AlertDialogContent>+ <AlertDialogHeader>+ <AlertDialogTitle>+ {t(($) => $.shortcuts.reset_confirm.title)}+ </AlertDialogTitle>+ <AlertDialogDescription>+ {t(($) => $.shortcuts.reset_confirm.description)}+ </AlertDialogDescription>+ </AlertDialogHeader>+ <AlertDialogFooter>+ <AlertDialogCancel>+ {t(($) => $.shortcuts.reset_confirm.cancel)}+ </AlertDialogCancel>+ <AlertDialogAction+ variant="destructive"+ onClick={() => {+ resetAll();+ setCaptureError(null);+ setRecording(null);+ setResetConfirmOpen(false);+ }}+ >+ {t(($) => $.shortcuts.reset_confirm.confirm)}+ </AlertDialogAction>+ </AlertDialogFooter>+ </AlertDialogContent>+ </AlertDialog></SettingsTab>);}