migrate snack bar to composition API and typescript

This commit is contained in:
MaysWind
2025-01-04 18:52:21 +08:00
parent 229d9c76c3
commit 5eec635146
2 changed files with 40 additions and 38 deletions
+30 -37
View File
@@ -8,43 +8,36 @@
</v-snackbar> </v-snackbar>
</template> </template>
<script> <script setup lang="ts">
export default { import { type Ref, ref, watch } from 'vue';
props: [
'show',
'message'
],
emits: [
'update:show'
],
expose: [
'showMessage',
'showError'
],
data() {
const self = this;
return { import { useI18n } from '@/lib/i18n.js';
showState: self.show,
messageContent: self.message, const emit = defineEmits<{
resolve: null, (e: 'update:show', value: boolean): void
reject: null }>();
}
}, const { tt, te } = useI18n();
watch: {
'showState': function (newValue) { const showState: Ref<boolean> = ref(false);
this.$emit('update:show', newValue); const messageContent: Ref<string> = ref('');
}
}, function showMessage(message: string, options: Record<string, unknown>): void {
methods: { showState.value = true;
showMessage(message, options) { messageContent.value = tt(message, options);
this.showState = true;
this.messageContent = this.$t(message, options);
},
showError(error) {
this.showState = true;
this.messageContent = this.$tError(error.message || error);
}
}
} }
function showError(error: string | { message: string }): void {
showState.value = true;
messageContent.value = te(error.message || error);
}
watch(showState, (newValue) => {
emit('update:show', newValue);
});
defineExpose({
showMessage,
showError
});
</script> </script>
+10 -1
View File
@@ -1,8 +1,9 @@
import { useI18n as useVueI18n } from 'vue-i18n';
import moment from 'moment-timezone'; import moment from 'moment-timezone';
import { defaultLanguage, allLanguages } from '@/locales/index.ts'; import { defaultLanguage, allLanguages } from '@/locales/index.ts';
import { Month, WeekDay, MeridiemIndicator, LongDateFormat, ShortDateFormat, LongTimeFormat, ShortTimeFormat, DateRangeScene, DateRange, LANGUAGE_DEFAULT_DATE_TIME_FORMAT_VALUE } from '@/core/datetime.ts'; import { Month, WeekDay, MeridiemIndicator, LongDateFormat, ShortDateFormat, LongTimeFormat, ShortTimeFormat, DateRange, LANGUAGE_DEFAULT_DATE_TIME_FORMAT_VALUE } from '@/core/datetime.ts';
import { TimezoneTypeForStatistics } from '@/core/timezone.ts'; import { TimezoneTypeForStatistics } from '@/core/timezone.ts';
import { DecimalSeparator, DigitGroupingSymbol, DigitGroupingType } from '@/core/numeral.ts'; import { DecimalSeparator, DigitGroupingSymbol, DigitGroupingType } from '@/core/numeral.ts';
import { CurrencyDisplayType, CurrencySortingType } from '@/core/currency.ts'; import { CurrencyDisplayType, CurrencySortingType } from '@/core/currency.ts';
@@ -1747,8 +1748,16 @@ export function i18nFunctions(i18nGlobal) {
getCategorizedAccountsWithDisplayBalance: (allVisibleAccounts, showAccountBalance, defaultCurrency, settingsStore, userStore, exchangeRatesStore) => getCategorizedAccountsWithDisplayBalance(allVisibleAccounts, showAccountBalance, defaultCurrency, userStore, settingsStore, exchangeRatesStore, i18nGlobal.t), getCategorizedAccountsWithDisplayBalance: (allVisibleAccounts, showAccountBalance, defaultCurrency, settingsStore, userStore, exchangeRatesStore) => getCategorizedAccountsWithDisplayBalance(allVisibleAccounts, showAccountBalance, defaultCurrency, userStore, settingsStore, exchangeRatesStore, i18nGlobal.t),
getServerTipContent: (tipConfig) => getServerTipContent(tipConfig, i18nGlobal), getServerTipContent: (tipConfig) => getServerTipContent(tipConfig, i18nGlobal),
joinMultiText: (textArray) => joinMultiText(textArray, i18nGlobal.t), joinMultiText: (textArray) => joinMultiText(textArray, i18nGlobal.t),
tt: (...args) => i18nGlobal.t(...args),
ti: (text, isTranslate) => translateIf(text, isTranslate, i18nGlobal.t),
te: (message) => translateError(message, i18nGlobal.t),
setLanguage: (locale, force) => setLanguage(i18nGlobal, locale, force), setLanguage: (locale, force) => setLanguage(i18nGlobal, locale, force),
setTimeZone: (timezone) => setTimeZone(timezone), setTimeZone: (timezone) => setTimeZone(timezone),
initLocale: (lastUserLanguage, timezone) => initLocale(i18nGlobal, lastUserLanguage, timezone) initLocale: (lastUserLanguage, timezone) => initLocale(i18nGlobal, lastUserLanguage, timezone)
}; };
} }
export function useI18n() {
const i18nGlobal = useVueI18n();
return i18nFunctions(i18nGlobal);
}