From d26aea88c9e44ffdf11c518754ae3cece96463d8 Mon Sep 17 00:00:00 2001 From: BBK Date: Wed, 15 May 2024 18:05:13 +0800 Subject: [PATCH 1/2] fix: sender error after account switch (#297) * Sender error after account switch * fix: typo --- src-tauri/src/commands/mod.rs | 2 +- src-tauri/src/commands/tx.rs | 4 ++-- src-tauri/src/commands/wallets.rs | 8 ++++---- src-tauri/src/key_manager.rs | 7 +++++-- src-tauri/src/main.rs | 16 ++++++++-------- src/components/settings/UpdateNetwork.svelte | 1 + src/components/txs/Transactions.svelte | 6 ++++-- src/components/wallet/AccountsList.svelte | 19 +++++++++---------- src/components/wallet/Keygen.svelte | 1 - src/modules/accountActions.ts | 14 +++----------- src/modules/accountTransactions.ts | 1 + 11 files changed, 38 insertions(+), 41 deletions(-) diff --git a/src-tauri/src/commands/mod.rs b/src-tauri/src/commands/mod.rs index 4169960f..1dd3c1e2 100644 --- a/src-tauri/src/commands/mod.rs +++ b/src-tauri/src/commands/mod.rs @@ -1,5 +1,5 @@ pub(crate) mod app_version; -pub(crate) mod mining; +// pub(crate) mod mining; pub(crate) mod networks; pub(crate) mod preferences; pub(crate) mod query; diff --git a/src-tauri/src/commands/tx.rs b/src-tauri/src/commands/tx.rs index cce919f7..917b6d3c 100644 --- a/src-tauri/src/commands/tx.rs +++ b/src-tauri/src/commands/tx.rs @@ -30,8 +30,8 @@ pub async fn coin_transfer( }; let mut config = get_cfg()?; - inject_private_key_to_cfg(&mut config)?; - let mut sender = Sender::from_app_cfg(&config, None, legacy).await?; + inject_private_key_to_cfg(&mut config, _sender)?; + let mut sender = Sender::from_app_cfg(&config, Some(_sender.to_string()), legacy).await?; sender .transfer(receiver_account, amount as f64, false) .await?; diff --git a/src-tauri/src/commands/wallets.rs b/src-tauri/src/commands/wallets.rs index e8f5b61f..0af5cae4 100644 --- a/src-tauri/src/commands/wallets.rs +++ b/src-tauri/src/commands/wallets.rs @@ -210,7 +210,7 @@ pub async fn get_originating_address( } } -/// Switch tx profiles, change 0L.toml to use selected account +/// Switch tx profiles, change libra.yaml to use selected account #[tauri::command(async)] // IMPORTANT: don't return the profile, since it has keys pub async fn switch_profile(account: AccountAddress) -> Result { @@ -266,11 +266,11 @@ async fn test_fetch_originating() { } #[tauri::command(async)] -pub async fn set_slow_wallet(legacy: bool) -> Result<(), CarpeError> { +pub async fn set_slow_wallet(legacy: bool, _sender: AccountAddress) -> Result<(), CarpeError> { // NOTE: unsure Serde was catching all cases check serialization let mut config = get_cfg()?; - inject_private_key_to_cfg(&mut config)?; - let mut sender = Sender::from_app_cfg(&config, None, legacy).await?; + inject_private_key_to_cfg(&mut config, _sender)?; + let mut sender = Sender::from_app_cfg(&config, Some(_sender.to_string()), legacy).await?; let t = SetSlowTx {}; t.run(&mut sender).await?; diff --git a/src-tauri/src/key_manager.rs b/src-tauri/src/key_manager.rs index ce2c5bd0..0e6a9b24 100644 --- a/src-tauri/src/key_manager.rs +++ b/src-tauri/src/key_manager.rs @@ -75,9 +75,12 @@ pub fn get_keypair( /// insert the public key into the AppCfg temporarily so that we don't need /// to prompt user for mnemonic. // NOTE to future devs: DANGER: make sure this is never called in a flow that uses save_file(). The upstream prevents the key from serializing, but it should be guarded here as well. -pub fn inject_private_key_to_cfg(app_cfg_mut: &mut AppCfg) -> anyhow::Result<(), CarpeError> { +pub fn inject_private_key_to_cfg( + app_cfg_mut: &mut AppCfg, + account: AccountAddress, +) -> anyhow::Result<(), CarpeError> { // gets the default profile - let profile = app_cfg_mut.get_profile_mut(None)?; + let profile = app_cfg_mut.get_profile_mut(Some(account.to_string()))?; let pri_key = get_private_key(&profile.account).map_err(|e| CarpeError { category: ErrorCat::Configs, uid: E_KEY_NOT_REGISTERED, diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index b349ba8f..5ef9fe03 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -114,14 +114,14 @@ async fn main() { commands::tx::coin_transfer, // claim_make_whole, //////// Tower //////// - commands::query::get_onchain_tower_state, - commands::mining::miner_once, - commands::mining::start_backlog_sender_listener, - commands::mining::get_local_height, - commands::mining::get_epoch_rules, - commands::mining::submit_backlog, - commands::mining::get_last_local_proof, - commands::mining::debug_highest_proof_path, + // commands::query::get_onchain_tower_state, + // commands::mining::miner_once, + // commands::mining::start_backlog_sender_listener, + // commands::mining::get_local_height, + // commands::mining::get_epoch_rules, + // commands::mining::submit_backlog, + // commands::mining::get_last_local_proof, + // commands::mining::debug_highest_proof_path, // submit_proof_zero, //////// Preferences //////// diff --git a/src/components/settings/UpdateNetwork.svelte b/src/components/settings/UpdateNetwork.svelte index 63cc8c84..a54e6459 100644 --- a/src/components/settings/UpdateNetwork.svelte +++ b/src/components/settings/UpdateNetwork.svelte @@ -48,6 +48,7 @@ .then((res: NetworkPlaylist) => { network_profile.set(res); notify_success("Network Settings Updated"); + check_sync(); }) .catch((error) => { raise_error(error as CarpeError, false, "forceUpstream"); diff --git a/src/components/txs/Transactions.svelte b/src/components/txs/Transactions.svelte index da712e06..d88708fe 100644 --- a/src/components/txs/Transactions.svelte +++ b/src/components/txs/Transactions.svelte @@ -10,6 +10,7 @@ import type { CarpeProfile } from '../../modules/accounts' import { raise_error } from '../../modules/carpeError' import CantStart from '../miner/cards/CantStart.svelte' + import { refreshAccounts } from '../../modules/accountActions' const errorDic = { '120127': $_('txs.transfer.error_slow_wallet'), @@ -46,8 +47,8 @@ account && amount > unscaledCoins(account.balance) ? $_('txs.transfer.error_amount_greater_than_balance') : receiver && receiver.toUpperCase() == account.account.toUpperCase() - ? $_('txs.transfer.error_receiver_equals_sender') - : '' + ? $_('txs.transfer.error_receiver_equals_sender') + : '' onDestroy(async () => { unsubs && unsubs() @@ -68,6 +69,7 @@ waitingTxs = false amount = 0 receiver = null + refreshAccounts() }) .catch((error) => { responses.set(JSON.stringify(error)) diff --git a/src/components/wallet/AccountsList.svelte b/src/components/wallet/AccountsList.svelte index 4beac967..ebc8b552 100644 --- a/src/components/wallet/AccountsList.svelte +++ b/src/components/wallet/AccountsList.svelte @@ -4,7 +4,6 @@ import Icons from 'uikit/dist/js/uikit-icons' import { allAccounts, formatAccount, signingAccount } from '../../modules/accounts' import { printCoins, unscaledCoins } from '../../modules/coinHelpers' - import { minerLoopEnabled } from '../../modules/miner' import { connected } from '../../modules/networks' import { setAccount } from '../../modules/accountActions' import Actions from './Actions.svelte' @@ -113,17 +112,17 @@ {#each $allAccounts as a} - - + + {#if a.account == $signingAccount.account} - - +
+
+ +
+ {#if loading} +
+ +
+ {/if} +
- + + - {#if loading} -
- -
- {/if} + +
+
+

+ {$_('txs.set_wallet_type.warning')} +

+
- - - - -
-
-

- {$_('txs.set_wallet_type.warning')} -

- -
-
+ {/if} + {/await} diff --git a/src/components/wallet/AccountNote.svelte b/src/components/wallet/AccountNote.svelte new file mode 100644 index 00000000..b94855e3 --- /dev/null +++ b/src/components/wallet/AccountNote.svelte @@ -0,0 +1,49 @@ + + + + + diff --git a/src/components/wallet/AccountsList.svelte b/src/components/wallet/AccountsList.svelte index ebc8b552..c1f97b72 100644 --- a/src/components/wallet/AccountsList.svelte +++ b/src/components/wallet/AccountsList.svelte @@ -8,64 +8,88 @@ import { setAccount } from '../../modules/accountActions' import Actions from './Actions.svelte' import Copy from '../../components/layout/Copy.svelte' + import { preferences, setAccountsListPreference } from '../../modules/user_preferences' UIkit.use(Icons) let showOptions = false + let showNoteColumn = false + $: showNoteColumn = $allAccounts.some( + (account) => account.note !== null && account.note.trim() !== '', + ) + const toggleOptions = () => { - showOptions ? (showOptions = false) : (showOptions = true) + showOptions = !showOptions } - signingAccount.subscribe(() => { + function selectAddress(address) { showOptions = false - }) + setAccount(address) + } - let sortOrder = 'asc' // Initial sort order ('asc' for ascending, 'desc' for descending) - let sortColumn = null // Default column for initial sorting + // Subscribe to the preferences store + let sortOrder = 'asc' + let sortColumn = null + $: if ($preferences.accounts_list_sort_column && $preferences.accounts_list_sort_order) { + sortColumn = $preferences.accounts_list_sort_column + sortOrder = $preferences.accounts_list_sort_order + } - // Function to sort accounts based on the column header clicked + // Update preferences when user clicks to sort function sortAccounts(column) { - if (sortColumn === column) { - sortOrder = sortOrder === 'asc' ? 'desc' : 'asc' - } else { - sortColumn = column - sortOrder = 'desc' - } + const newSortOrder = sortColumn === column && sortOrder === 'asc' ? 'desc' : 'asc' + + // Set sortColumn and sortOrder immediately for local update + sortColumn = column + sortOrder = newSortOrder privateSortAccounts(column) + setAccountsListPreference(column, sortOrder) } + // Private function to sort accounts function privateSortAccounts(column) { $allAccounts = $allAccounts.slice().sort((a, b) => { - // Access nested properties for 'unlocked' and 'balance' let valA = column === 'balance' ? a.balance.total : column === 'unlocked' ? a.balance.unlocked - : formatAccount(a.account) + : column === 'note' + ? a.note || '' + : formatAccount(a.account) let valB = column === 'balance' ? b.balance.total : column === 'unlocked' ? b.balance.unlocked - : formatAccount(b.account) + : column === 'note' + ? b.note || '' + : formatAccount(b.account) - // Handle case-insensitive sorting for string types if (typeof valA === 'string') valA = valA.toLowerCase() if (typeof valB === 'string') valB = valB.toLowerCase() - if (sortOrder === 'asc') { - return valA > valB ? 1 : -1 - } else { - return valA < valB ? 1 : -1 + if (valA === valB) { + // Desempate usando formatAccount + let secondaryValA = formatAccount(a.account).toLowerCase() + let secondaryValB = formatAccount(b.account).toLowerCase() + return sortOrder === 'asc' + ? secondaryValA > secondaryValB + ? 1 + : -1 + : secondaryValA < secondaryValB + ? 1 + : -1 } + + // Ordenação principal + return sortOrder === 'asc' ? (valA > valB ? 1 : -1) : valA < valB ? 1 : -1 }) } - $: if (sortColumn && $allAccounts.length > 0) { - privateSortAccounts(sortColumn) - } + // Re-sort when preferences change + $: sortColumn && $allAccounts.length > 0 && privateSortAccounts(sortColumn)
@@ -73,9 +97,23 @@ - + {/if} - {#each $allAccounts as a} - - + {#if showNoteColumn} + + {/if} - -
+ + {#if showNoteColumn} + sortAccounts('note')} + > + {$_('wallet.account_list.note')} + {#if sortColumn === 'note'} + + {/if} + sortAccounts('account')} > {$_('wallet.account_list.address')} @@ -87,7 +125,7 @@ {/if} sortAccounts('unlocked')} > {$_('wallet.account_list.unlocked')} @@ -98,7 +136,10 @@ /> {/if} sortAccounts('balance')}> + sortAccounts('balance')} + > {$_('wallet.account_list.balance')} {#if sortColumn === 'balance'}
+ {#if a.account == $signingAccount.account} - - {a.note ? a.note : ''} setAccount(a.account)} + on:click={() => selectAddress(a.account)} class="uk-transition-toggle uk-table-shrink" - style="cursor:grab" + style="cursor: grab" >
- {formatAccount(a.account)}{formatAccount(a.account)}
{printCoins(a.balance.unlocked)} + {#if a.on_chain != null && a.on_chain == false} {$_('wallet.account_list.account_on_chain')} {:else if a.on_chain}
{#if unscaledCoins(a.balance) < 1} - - +
{$_('wallet.account_list.message')}
{/if} - {printCoins(a.balance.total)}
{:else if a.balance == null} @@ -171,10 +211,9 @@
{/if} - - {#if showOptions} - - {/if} +
+ +
diff --git a/src/components/wallet/Actions.svelte b/src/components/wallet/Actions.svelte index 15855399..bd17ad16 100644 --- a/src/components/wallet/Actions.svelte +++ b/src/components/wallet/Actions.svelte @@ -1,27 +1,18 @@
- - -
- - {#await is_slow_wallet($signingAccount.account) then res} - {#if res} -

{$_('wallet.settings.slow_label')}

-

{$_('wallet.settings.slow_info')}

- {:else if !$signingAccount.watch_only && $signingAccount.on_chain } - - {/if} - {/await} + + +
diff --git a/src/components/wallet/Info.svelte b/src/components/wallet/Info.svelte index 5d13d50b..862f9063 100644 --- a/src/components/wallet/Info.svelte +++ b/src/components/wallet/Info.svelte @@ -1,8 +1,8 @@
@@ -12,11 +12,11 @@

{$_('wallet.account_list.address')}: {$signingAccount.account} - - +

-

{$_('wallet.account_list.authkey')}: - {$signingAccount.auth_key} - +

+ {$_('wallet.account_list.authkey')}: + {$signingAccount.auth_key} +

diff --git a/src/components/wallet/Wallet.svelte b/src/components/wallet/Wallet.svelte index afe35c67..913d70ae 100644 --- a/src/components/wallet/Wallet.svelte +++ b/src/components/wallet/Wallet.svelte @@ -27,14 +27,18 @@ {$_('wallet.wallet')} -
- {$_('wallet.account_list.balance')}: {printCoins($totalBalance.total)} 1} +
- {$_('wallet.account_list.unlocked')}: {printCoins($totalBalance.unlocked)} -
+ + {$_('wallet.account_list.balance')}: {printCoins($totalBalance.total)} + + + {$_('wallet.account_list.unlocked')}: {printCoins($totalBalance.unlocked)} + +
+ {/if} diff --git a/src/lang/locales/ar.json b/src/lang/locales/ar.json index 76b52c01..09ed7cfd 100644 --- a/src/lang/locales/ar.json +++ b/src/lang/locales/ar.json @@ -44,6 +44,7 @@ "onboard_key": "ONBOARD KEY" }, "account_list": { + "note": "Note", "nickname": "Nickname", "address": "Address", "authkey": "Authkey", @@ -55,6 +56,12 @@ "message": " Your balance will go down for every transaction you send, including mining.", "info_title": "Account Info" }, + "account_note": { + "title": "Account Note", + "description": "A note to help you identify your account:", + "placeholder": "Enter your note here...", + "btn_save": "Save Note" + }, "keygen": { "title": "Create New Account", "description": "After you generate an account and secret phrase, you'll need someone to send one 0L coin to that account for it to be created on chain.", diff --git a/src/lang/locales/de.json b/src/lang/locales/de.json index 4fbdd970..58e7b282 100644 --- a/src/lang/locales/de.json +++ b/src/lang/locales/de.json @@ -44,6 +44,7 @@ "onboard_key": "ONBOARD KEY" }, "account_list": { + "note": "Notiz", "nickname": "Spitzname", "address": "Adresse", "authkey": "Authkey", @@ -55,6 +56,12 @@ "message": " Dein Guthaben verringert sich bei jeder Transaktion, die du sendest, einschließlich Mining.", "info_title": "Konto-Informationen" }, + "account_note": { + "title": "Kontonotiz", + "description": "Eine Notiz, um Ihnen zu helfen, Ihr Konto zu identifizieren:", + "placeholder": "Geben Sie hier Ihre Notiz ein...", + "btn_save": "Notiz speichern" + }, "keygen": { "title": "Neues Konto anlegen", "description": "Nachdem du ein Konto und eine geheime Phrase erstellt hast, muss jemand einen 0L-Coin an dieses Konto senden, damit es auf der Chain erstellt wird.", diff --git a/src/lang/locales/en.json b/src/lang/locales/en.json index 9130d937..8267f22b 100644 --- a/src/lang/locales/en.json +++ b/src/lang/locales/en.json @@ -44,6 +44,7 @@ "onboard_key": "ADDRESS" }, "account_list": { + "note": "Note", "nickname": "Nickname", "address": "Address", "authkey": "Authkey", @@ -55,6 +56,12 @@ "message": "Your balance will go down for every transaction you send, including mining.", "info_title": "Account Info" }, + "account_note": { + "title": "Account Note", + "description": "A note to help you identify your account:", + "placeholder": "Enter your note here...", + "btn_save": "Save Note" + }, "keygen": { "title": "Create New Account", "description": "After you generate an account and secret phrase, you'll need someone to send one 0L coin to that account for it to be created on chain.", diff --git a/src/lang/locales/es.json b/src/lang/locales/es.json index b722e847..18f1fb1b 100644 --- a/src/lang/locales/es.json +++ b/src/lang/locales/es.json @@ -44,6 +44,7 @@ "onboard_key": "ONBOARD KEY" }, "account_list": { + "note": "Nota", "nickname": "Nickname", "address": "Address", "authkey": "Authkey", @@ -55,6 +56,12 @@ "message": " Your balance will go down for every transaction you send, including mining.", "info_title": "Account Info" }, + "account_note": { + "title": "Nota de cuenta", + "description": "Una nota para ayudarte a identificar tu cuenta:", + "placeholder": "Ingresa tu nota aquí...", + "btn_save": "Guardar nota" + }, "keygen": { "title": "Create New Account", "description": "After you generate an account and secret phrase, you'll need someone to send one 0L coin to that account for it to be created on chain.", diff --git a/src/lang/locales/fr.json b/src/lang/locales/fr.json index 9b54b4c0..fe0c0162 100644 --- a/src/lang/locales/fr.json +++ b/src/lang/locales/fr.json @@ -44,6 +44,7 @@ "onboard_key": "ONBOARD KEY" }, "account_list": { + "note": "Note", "nickname": "Pseudo", "address": "Adresse", "authkey": "Authkey", @@ -55,6 +56,12 @@ "message": " Votre solde diminuera pour toutes transactions effectuées, y compris lorsque vous minez.", "info_title": "Informations sur le compte" }, + "account_note": { + "title": "Note du compte", + "description": "Une note pour vous aider à identifier votre compte :", + "placeholder": "Entrez votre note ici...", + "btn_save": "Sauvegarder la note" + }, "keygen": { "title": "Creer un nouveau compte", "description": "Aprés avoir généré votre compte et phrase secrète, vous aurez besoin que quelqu'un envoie un Coin 0L à ce compte pour qu'il soit mis sur la chaîne.", diff --git a/src/lang/locales/it.json b/src/lang/locales/it.json index 56d48839..f9f2902d 100644 --- a/src/lang/locales/it.json +++ b/src/lang/locales/it.json @@ -44,6 +44,7 @@ "onboard_key": "ONBOARD KEY" }, "account_list": { + "note": "Nota", "nickname": "Nickname", "address": "Address", "authkey": "Authkey", @@ -55,6 +56,12 @@ "message": " Your balance will go down for every transaction you send, including mining.", "info_title": "Account Info" }, + "account_note": { + "title": "Nota dell'account", + "description": "Una nota per aiutarti a identificare il tuo account:", + "placeholder": "Inserisci qui la tua nota...", + "btn_save": "Salva nota" + }, "keygen": { "title": "Create New Account", "description": "After you generate an account and secret phrase, you'll need someone to send one 0L coin to that account for it to be created on chain.", diff --git a/src/lang/locales/pt.json b/src/lang/locales/pt.json index a6cc6340..c98f8b64 100644 --- a/src/lang/locales/pt.json +++ b/src/lang/locales/pt.json @@ -44,6 +44,7 @@ "onboard_key": "CHAVE DE INTEGRAÇÃO" }, "account_list": { + "note": "Nota", "nickname": "Apelido", "address": "Conta", "authkey": "Chave de autenticação", @@ -55,6 +56,12 @@ "message": "Seu saldo diminuirá para cada transação que você enviar, incluindo as de mineração.", "info_title": "Informações sobre a conta" }, + "account_note": { + "title": "Nota da Conta", + "description": "Uma nota para ajudá-lo a identificar sua conta:", + "placeholder": "Digite sua nota aqui...", + "btn_save": "Salvar Nota" + }, "keygen": { "title": "Criar nova conta", "description": "Depois de gerar uma conta e a frase secreta, você precisará que alguém envie uma moeda do 0L para esta conta para que ela seja criada na rede.", diff --git a/src/lang/locales/zh_cn.json b/src/lang/locales/zh_cn.json index 18f00d6d..11896c7d 100644 --- a/src/lang/locales/zh_cn.json +++ b/src/lang/locales/zh_cn.json @@ -44,6 +44,7 @@ "onboard_key": "激活码(AuthKey)" }, "account_list": { + "note": "笔记", "nickname": "昵称", "address": "地址", "authkey": "激活码", @@ -55,6 +56,12 @@ "message": " 所有发送的交易(包括:挖矿)都会消耗一些余额作为手续费。", "info_title": "账户信息" }, + "account_note": { + "title": "账户备注", + "description": "一条备注,帮助您识别您的账户:", + "placeholder": "在此输入您的备注...", + "btn_save": "保存备注" + }, "keygen": { "title": "创建新账号", "description": "创建账户后,您的账户需要收到来自链上的任意一个钱包至少1个0L代币,才能够被激活上链。", diff --git a/src/modules/accountActions.ts b/src/modules/accountActions.ts index 2f9176e7..e28111b3 100644 --- a/src/modules/accountActions.ts +++ b/src/modules/accountActions.ts @@ -54,7 +54,7 @@ export const getDefaultProfile = async () => { export const getAccounts = async () => { // first make sure we don't have empty accounts - invoke('get_all_accounts') + invoke('get_all_accounts_with_notes') .then((result: CarpeProfile[]) => { const watchList = get(watchAccounts) result.map((item) => { @@ -64,7 +64,7 @@ export const getAccounts = async () => { }) allAccounts.set(result) }) - .catch((e) => raise_error(e, true, 'get_all_accounts')) + .catch((e) => raise_error(e, true, 'get_all_accounts_with_notes')) } export const refreshAccounts = async () => { @@ -448,3 +448,26 @@ async function onAccountAdd(res: CarpeProfile) { await refreshAccounts().finally(() => navigate('wallet')) } + +export const associateNoteWithAccount = async (account, note) => { + // update allAccounts set account with the new note + const list = get(allAccounts) + const updatedList = list.map((item) => { + if (item.account === account) { + item.note = note + } + return item + }) + allAccounts.set(updatedList) + + // update the backend + try { + const result = await invoke('associate_note_with_account', { account, note }) + refreshAccounts() + notify_success('Note successfully associated with account') + return result + } catch (e) { + raise_error(e, true, 'associateNoteWithAccount') + notify_error('Failed to associate note with account') + } +} diff --git a/src/modules/accounts.ts b/src/modules/accounts.ts index a5b29668..bfd4e765 100644 --- a/src/modules/accounts.ts +++ b/src/modules/accounts.ts @@ -8,6 +8,7 @@ export interface CarpeProfile { balance?: SlowWalletBalance locale?: string // TODO: refactor, tauri now offers locale of the OS watch_only?: boolean + note: string } export interface SlowWalletBalance { unlocked: number @@ -22,6 +23,7 @@ export const new_account = (account: string, authkey: string, nickname: string): on_chain: false, balance: { unlocked: 0, total: 0 }, watch_only: false, + note: null, } } diff --git a/src/modules/user_preferences.ts b/src/modules/user_preferences.ts new file mode 100644 index 00000000..73d8dd2b --- /dev/null +++ b/src/modules/user_preferences.ts @@ -0,0 +1,42 @@ +import { writable } from 'svelte/store' +import { invoke } from '@tauri-apps/api/tauri' + +// Define the interface for UserPreferences +interface UserPreferences { + accounts_list_sort_column?: string + accounts_list_sort_order?: string +} + +// Create a writable store to hold the preferences +export const preferences = writable({}) + +// Function to load preferences from the backend +export async function loadUserPreferences() { + try { + const prefs = await invoke('get_user_preferences') + preferences.set(prefs) + } catch (error) { + console.error('Failed to load user preferences:', error) + // Optionally set defaults or handle errors in a specific way + preferences.set({}) + } +} + +// Function to save preferences to the backend +export async function setAccountsListPreference(sortColumn: string, sortOrder: string) { + try { + await invoke('set_accounts_list_preference', { sortColumn, sortOrder }) + // Update the local store after successful backend update + preferences.update((prefs) => ({ + ...prefs, + accounts_list_sort_column: sortColumn, + accounts_list_sort_order: sortOrder, + })) + } catch (error) { + console.error('Failed to set accounts list preference:', error) + // Handle errors, possibly revert to old values or show user feedback + } +} + +// Initial load of preferences +loadUserPreferences()