Skip to content

Commit

Permalink
feat(translation): move to pinia
Browse files Browse the repository at this point in the history
Signed-off-by: Grigory Vodyanov <[email protected]>
  • Loading branch information
GVodyanov committed Jan 11, 2025
1 parent 58d2fd4 commit 1157541
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 135 deletions.
9 changes: 5 additions & 4 deletions src/components/TranslationModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ import { showError, showSuccess } from '@nextcloud/dialogs'
import { NcButton, NcDialog, NcLoadingIcon, NcRichText, NcSelect } from '@nextcloud/vue'

import { translateText } from '../service/translationService.js'
import { mapGetters } from 'vuex'
import { mapState } from 'pinia'
import useMainStore from '../store/mainStore.js'

export default {
name: 'TranslationModal',
Expand Down Expand Up @@ -116,9 +117,9 @@ export default {
},

computed: {
...mapGetters({
availableInputLanguages: 'getTranslationInputLanguages',
availableOutputLanguages: 'getTranslationOutputLanguages',
...mapState(useMainStore, {
availableInputLanguages: 'translationInputLanguages',
availableOutputLanguages: 'translationOutputLanguages',
}),

userLanguage() {
Expand Down
6 changes: 6 additions & 0 deletions src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
import { fixAccountId } from './service/AccountService.js'
import { loadState } from '@nextcloud/initial-state'
import { fetchAvailableLanguages } from './service/translationService.js'

import useOutboxStore from './store/outboxStore.js'
import useMainStore from './store/mainStore.js'
Expand Down Expand Up @@ -125,4 +126,9 @@ export default function initAfterAppCreation() {
const outboxStore = useOutboxStore()
outboxMessages.forEach(message => outboxStore.addMessageMutation({ message }))

const llmTranslationEnabled = loadState('mail', 'llm_translation_enabled', false)
mainStore.isTranslationEnabled = llmTranslationEnabled
if (llmTranslationEnabled) {
fetchAvailableLanguages().then(() => {})
}
}
124 changes: 0 additions & 124 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ import vToolTip from 'v-tooltip'
import App from './App.vue'
import Nextcloud from './mixins/Nextcloud.js'
import router from './router.js'
import store from './store/index.js'
import { fixAccountId } from './service/AccountService.js'
import { loadState } from '@nextcloud/initial-state'
import { createPinia, PiniaVuePlugin } from 'pinia'
import useOutboxStore from './store/outboxStore.js'
import { fetchAvailableLanguages } from './service/translationService.js'

// eslint-disable-next-line camelcase
__webpack_nonce__ = btoa(getRequestToken())
Expand All @@ -35,132 +30,13 @@ Vue.mixin(Nextcloud)
Vue.use(VueShortKey, { prevent: ['input', 'div', 'textarea'] })
Vue.use(vToolTip)

const getPreferenceFromPage = (key) => {
const elem = document.getElementById(key)
if (!elem) {
return
}
return elem.value
}

registerDavProperty('nc:share-attributes', { nc: 'http://nextcloud.org/ns' })

store.commit('savePreference', {
key: 'debug',
value: loadState('mail', 'debug', false),
})
store.commit('savePreference', {
key: 'ncVersion',
value: loadState('mail', 'ncVersion'),
})

store.commit('savePreference', {
key: 'sort-order',
value: loadState('mail', 'sort-order', 'newest'),
})

store.commit('savePreference', {
key: 'attachment-size-limit',
value: Number.parseInt(getPreferenceFromPage('attachment-size-limit'), 10),
})
store.commit('savePreference', {
key: 'version',
value: getPreferenceFromPage('config-installed-version'),
})
store.commit('savePreference', {
key: 'external-avatars',
value: getPreferenceFromPage('external-avatars'),
})
store.commit('savePreference', {
key: 'collect-data',
value: getPreferenceFromPage('collect-data'),
})
store.commit('savePreference', {
key: 'search-priority-body',
value: getPreferenceFromPage('search-priority-body'),
})
const startMailboxId = getPreferenceFromPage('start-mailbox-id')
store.commit('savePreference', {
key: 'start-mailbox-id',
value: startMailboxId ? parseInt(startMailboxId, 10) : null,
})
store.commit('savePreference', {
key: 'tag-classified-messages',
value: getPreferenceFromPage('tag-classified-messages'),
})
store.commit('savePreference', {
key: 'allow-new-accounts',
value: loadState('mail', 'allow-new-accounts', true),
})
store.commit('savePreference', {
key: 'password-is-unavailable',
value: loadState('mail', 'password-is-unavailable', false),
})
store.commit('savePreference', {
key: 'layout-mode',
value: getPreferenceFromPage('layout-mode'),
})
store.commit('savePreference', {
key: 'follow-up-reminders',
value: getPreferenceFromPage('follow-up-reminders'),
})
store.commit('savePreference', {
key: 'internal-addresses',
value: loadState('mail', 'internal-addresses', false),
})

const accountSettings = loadState('mail', 'account-settings')
const accounts = loadState('mail', 'accounts', [])
const internalAddressesList = loadState('mail', 'internal-addresses-list', [])
const tags = loadState('mail', 'tags', [])
const outboxMessages = loadState('mail', 'outbox-messages')
const disableScheduledSend = loadState('mail', 'disable-scheduled-send')
const disableSnooze = loadState('mail', 'disable-snooze')
const googleOauthUrl = loadState('mail', 'google-oauth-url', null)
const microsoftOauthUrl = loadState('mail', 'microsoft-oauth-url', null)
const followUpFeatureAvailable = loadState('mail', 'llm_followup_available', false)

accounts.map(fixAccountId).forEach((account) => {
const settings = accountSettings.find(settings => settings.accountId === account.id)
if (settings) {
delete settings.accountId
Object.entries(settings).forEach(([key, value]) => {
store.commit('setAccountSetting', {
accountId: account.id,
key,
value,
})
})
}
store.commit('addAccount', { ...account, ...settings })
})

tags.forEach(tag => store.commit('addTag', { tag }))
internalAddressesList.forEach(internalAddress => store.commit('addInternalAddress', internalAddress))

store.commit('setScheduledSendingDisabled', disableScheduledSend)
store.commit('setSnoozeDisabled', disableSnooze)
store.commit('setGoogleOauthUrl', googleOauthUrl)
store.commit('setMicrosoftOauthUrl', microsoftOauthUrl)
store.commit('setFollowUpFeatureAvailable', followUpFeatureAvailable)

const smimeCertificates = loadState('mail', 'smime-certificates', [])
store.commit('setSmimeCertificates', smimeCertificates)

const llmTranslationEnabled = loadState('mail', 'llm_translation_enabled', false)
store.commit('enableTranslation', llmTranslationEnabled)
if (llmTranslationEnabled) {
fetchAvailableLanguages()
}
/* eslint-disable vue/match-component-file-name */
export default new Vue({
el: '#content',
name: 'Mail',
router,
store,
pinia,
render: (h) => h(App),
})

const outboxStore = useOutboxStore()
outboxMessages.forEach(message => outboxStore.addMessageMutation({ message }))
7 changes: 4 additions & 3 deletions src/service/translationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@

import axios from '@nextcloud/axios'
import { generateOcsUrl } from '@nextcloud/router'
import store from '../store/index.js'
import useMainStore from '../store/mainStore.js'

const fetchAvailableLanguages = async function() {
const mainStore = useMainStore()
try {
const response = await axios.get(generateOcsUrl('taskprocessing/tasktypes'))
const inputLanguages = response.data.ocs.data.types['core:text2text:translate'].inputShapeEnumValues[0]
const outputLanguages = response.data.ocs.data.types['core:text2text:translate'].inputShapeEnumValues[1]
store.commit('setTranslationInputLanguages', inputLanguages)
store.commit('setTranslationOutputLanguages', outputLanguages)
mainStore.translationInputLanguages = inputLanguages
mainStore.translationOutputLanguages = outputLanguages
} catch (e) {
console.error('Failed to fetch available languages', e)
}
Expand Down
3 changes: 3 additions & 0 deletions src/store/mainStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ export default defineStore('main', {
internalAddress: [],
hasCurrentUserPrincipalAndCollections: false,
showAccountSettings: null,
isTranslationEnabled: false,
translationInputLanguages: [],
translationOutputLanguages: [],
}
},
getters: {
Expand Down
8 changes: 4 additions & 4 deletions src/store/mainStore/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ export default function mainStoreActions() {
mailbox: updated,
})

this.syncEnvelopes({
await this.syncEnvelopes({
accountId,
mailboxId,
})
Expand Down Expand Up @@ -812,7 +812,7 @@ export default function mainStoreActions() {
})
})
},
syncEnvelopes({
async syncEnvelopes({
mailboxId,
query,
init = false,
Expand Down Expand Up @@ -912,15 +912,15 @@ export default function mainStoreActions() {
})
.catch((error) => {
return matchError(error, {
[SyncIncompleteError.getName()]() {
[SyncIncompleteError.getName()]: () => {
console.warn(`(initial) sync of mailbox ${mailboxId} (${query}) is incomplete, retriggering`)
return this.syncEnvelopes({
mailboxId,
query,
init,
})
},
[MailboxLockedError.getName()](error) {
[MailboxLockedError.getName()]: (error) => {
if (init) {
logger.info('Sync failed because the mailbox is locked, stopping here because this is an initial sync', { error })
throw error
Expand Down

0 comments on commit 1157541

Please sign in to comment.