mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-08-01 11:18:41 +08:00
feat(i18n): 收口多语言功能业务代码
This commit is contained in:
@@ -2,19 +2,22 @@ import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { message } from 'antd';
|
||||
|
||||
import { DBCommitTransaction, DBRollbackTransaction } from '../../wailsjs/go/app/App';
|
||||
import { t as catalogTranslate } from '../i18n/catalog';
|
||||
import { useStore } from '../store';
|
||||
import { formatSqlExecutionError } from '../utils/sqlErrorSemantics';
|
||||
import type { PendingSqlEditorTransaction } from './QueryEditorTransactionToolbar';
|
||||
|
||||
type FinishSqlEditorTransactionAction = 'commit' | 'rollback';
|
||||
type FinishSqlEditorTransactionSource = 'manual' | 'auto';
|
||||
type TranslateParams = Record<string, string | number | boolean | null | undefined>;
|
||||
|
||||
type UseSqlEditorTransactionControllerOptions = {
|
||||
tabId: string;
|
||||
translate?: (key: string, params?: TranslateParams) => string;
|
||||
};
|
||||
|
||||
export const useSqlEditorTransactionController = ({
|
||||
tabId,
|
||||
translate,
|
||||
}: UseSqlEditorTransactionControllerOptions) => {
|
||||
const setSqlEditorPendingTransaction = useStore(state => state.setSqlEditorPendingTransaction);
|
||||
const [pendingSqlTransaction, setPendingSqlTransaction] = useState<PendingSqlEditorTransaction | null>(null);
|
||||
@@ -24,6 +27,19 @@ export const useSqlEditorTransactionController = ({
|
||||
const autoCommitCountdownRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
||||
const [autoCommitRemainingSeconds, setAutoCommitRemainingSeconds] = useState<number | null>(null);
|
||||
|
||||
const translateMessage = useCallback((key: string, params?: TranslateParams) => {
|
||||
return translate ? translate(key, params) : catalogTranslate('zh-CN', key, params);
|
||||
}, [translate]);
|
||||
|
||||
const rawErrorDetail = useCallback((error: unknown) => {
|
||||
if (typeof error === 'string' && error.trim()) return error;
|
||||
if (error instanceof Error && error.message.trim()) return error.message;
|
||||
const messageValue = (error as any)?.message;
|
||||
if (typeof messageValue === 'string' && messageValue.trim()) return messageValue;
|
||||
if (error !== undefined && error !== null) return String(error);
|
||||
return translateMessage('common.unknown');
|
||||
}, [translateMessage]);
|
||||
|
||||
const clearAutoCommitTimer = useCallback(() => {
|
||||
if (autoCommitTimerRef.current) {
|
||||
clearTimeout(autoCommitTimerRef.current);
|
||||
@@ -63,21 +79,33 @@ export const useSqlEditorTransactionController = ({
|
||||
: await DBRollbackTransaction(transaction.id);
|
||||
if (res?.success) {
|
||||
if (action === 'commit') {
|
||||
message.success(source === 'auto' ? 'SQL 事务已自动提交' : 'SQL 事务已提交');
|
||||
message.success(source === 'auto'
|
||||
? translateMessage('data_grid.message.auto_commit_success')
|
||||
: translateMessage('data_grid.message.transaction_committed'));
|
||||
} else {
|
||||
message.success('SQL 事务已回滚');
|
||||
message.success(translateMessage('data_grid.message.transaction_rolled_back'));
|
||||
}
|
||||
return;
|
||||
}
|
||||
const fallback = action === 'commit' ? '提交失败' : '回滚失败';
|
||||
message.error(`${source === 'auto' ? '自动提交失败' : fallback}: ${formatSqlExecutionError(res?.message || '未知错误')}`);
|
||||
const detail = rawErrorDetail(res?.message);
|
||||
const key = source === 'auto'
|
||||
? 'data_grid.message.auto_commit_failed'
|
||||
: action === 'commit'
|
||||
? 'data_grid.message.commit_failed'
|
||||
: 'data_grid.message.rollback_failed';
|
||||
message.error(translateMessage(key, { detail }));
|
||||
} catch (err: any) {
|
||||
const fallback = action === 'commit' ? '提交失败' : '回滚失败';
|
||||
message.error(`${source === 'auto' ? '自动提交失败' : fallback}: ${formatSqlExecutionError(err?.message || err || '未知错误')}`);
|
||||
const detail = rawErrorDetail(err);
|
||||
const key = source === 'auto'
|
||||
? 'data_grid.message.auto_commit_failed'
|
||||
: action === 'commit'
|
||||
? 'data_grid.message.commit_failed'
|
||||
: 'data_grid.message.rollback_failed';
|
||||
message.error(translateMessage(key, { detail }));
|
||||
} finally {
|
||||
finishingTransactionIdsRef.current.delete(transaction.id);
|
||||
}
|
||||
}, [clearAutoCommitTimer, updatePendingSqlTransaction]);
|
||||
}, [clearAutoCommitTimer, rawErrorDetail, translateMessage, updatePendingSqlTransaction]);
|
||||
|
||||
const activatePendingSqlTransaction = useCallback((transaction: PendingSqlEditorTransaction) => {
|
||||
clearAutoCommitTimer();
|
||||
|
||||
Reference in New Issue
Block a user