From 55928ac2523a732861f58687b656a1de40c2d1cc Mon Sep 17 00:00:00 2001 From: ekzyis Date: Fri, 5 Jul 2024 08:22:47 +0200 Subject: [PATCH] Save order as priority --- components/wallet-card.js | 8 ++-- components/wallet/index.js | 21 ++++++++++- pages/settings/wallets/index.js | 65 ++++++++++++++++++++++----------- styles/wallet.module.css | 1 - 4 files changed, 67 insertions(+), 28 deletions(-) diff --git a/components/wallet-card.js b/components/wallet-card.js index dd3209e0..66b494c7 100644 --- a/components/wallet-card.js +++ b/components/wallet-card.js @@ -3,10 +3,10 @@ import styles from '@/styles/wallet.module.css' import Plug from '@/svgs/plug.svg' import Gear from '@/svgs/settings-5-fill.svg' import Link from 'next/link' -import { useWallet, Status } from './wallet' +import { Status } from './wallet' -export default function WalletCard ({ name, title, badges, status }) { - const wallet = useWallet(name) +export default function WalletCard ({ wallet }) { + const { card: { title, badges } } = wallet let indicator = styles.disabled switch (wallet.status) { @@ -39,7 +39,7 @@ export default function WalletCard ({ name, title, badges, status }) { )} - + {wallet.isConfigured ? <>configure diff --git a/components/wallet/index.js b/components/wallet/index.js index f9a87fbd..afe6ab08 100644 --- a/components/wallet/index.js +++ b/components/wallet/index.js @@ -32,6 +32,10 @@ export function useWallet (name) { const [config, saveConfig, clearConfig] = useConfig(wallet) const _isConfigured = isConfigured({ ...wallet, config }) + const status = (config?.enabled || config?.priority) ? Status.Enabled : Status.Initialized + const enabled = status === Status.Enabled + const priority = config?.priority + const sendPayment = useCallback(async (bolt11) => { const hash = bolt11Tags(bolt11).payment_hash logger.info('sending payment:', `payment_hash=${hash}`) @@ -55,6 +59,12 @@ export function useWallet (name) { logger.info('wallet disabled') }, [name, me, logger]) + const setPriority = useCallback(async (priority) => { + if (_isConfigured && priority !== config.priority) { + await saveConfig({ ...config, priority }) + } + }, [wallet, config, logger]) + const save = useCallback(async (newConfig) => { try { // validate should log custom INFO and OK message @@ -91,8 +101,11 @@ export function useWallet (name) { delete: delete_, enable, disable, + setPriority, isConfigured: _isConfigured, - status: config?.enabled || config?.priority ? Status.Enabled : Status.Initialized, + status, + enabled, + priority, logger } } @@ -149,7 +162,7 @@ function useServerConfig (wallet) { const walletId = data?.walletByType?.id const serverConfig = { id: walletId, priority: data?.walletByType?.priority, ...data?.walletByType?.wallet } const autowithdrawSettings = autowithdrawInitial({ me, priority: serverConfig?.priority }) - const config = { ...serverConfig, autowithdrawSettings } + const config = { ...serverConfig, ...autowithdrawSettings } const saveConfig = useCallback(async ({ autoWithdrawThreshold, @@ -210,6 +223,10 @@ export function getEnabledWallet (me) { }) } +export function useWallets () { + return WALLET_DEFS.map(def => useWallet(def.name)) +} + function getStorageKey (name, me) { let storageKey = `wallet:${name}` if (me) { diff --git a/pages/settings/wallets/index.js b/pages/settings/wallets/index.js index 308f6b3d..9951a5d2 100644 --- a/pages/settings/wallets/index.js +++ b/pages/settings/wallets/index.js @@ -3,7 +3,7 @@ import Layout from '@/components/layout' import styles from '@/styles/wallet.module.css' import { WALLETS as WALLETS_QUERY } from '@/fragments/wallet' import Link from 'next/link' -import { WALLET_DEFS } from '@/components/wallet' +import { useWallets } from '@/components/wallet' import { useState } from 'react' import dynamic from 'next/dynamic' @@ -12,7 +12,8 @@ const WalletCard = dynamic(() => import('@/components/wallet-card'), { ssr: fals export const getServerSideProps = getGetServerSideProps({ query: WALLETS_QUERY, authRequired: true }) export default function Wallet ({ ssrData }) { - const [wallets, setWallets] = useState(WALLET_DEFS) + const wallets = useWallets() + const [sourceIndex, setSourceIndex] = useState() const [targetIndex, setTargetIndex] = useState() @@ -29,20 +30,25 @@ export default function Wallet ({ ssrData }) { setTargetIndex(i) } - const onDragEnd = (e) => { + const onDragEnd = async (e) => { setSourceIndex(null) setTargetIndex(null) + if (sourceIndex === targetIndex) return - setWallets(wallets => { - const copy = [...wallets] - const [source] = copy.splice(sourceIndex, 1) - const newTargetIndex = sourceIndex < targetIndex ? targetIndex - 1 : targetIndex - const append = sourceIndex < targetIndex + const newOrder = [...wallets] - copy.splice(newTargetIndex + (append ? 1 : 0), 0, source) - return copy - }) + const [source] = newOrder.splice(sourceIndex, 1) + const newTargetIndex = sourceIndex < targetIndex ? targetIndex - 1 : targetIndex + const append = sourceIndex < targetIndex + + newOrder.splice(newTargetIndex + (append ? 1 : 0), 0, source) + + await Promise.all( + newOrder.map((w, i) => + w.setPriority(i).catch(console.error) + ) + ) } return ( @@ -57,17 +63,34 @@ export default function Wallet ({ ssrData }) {
{wallets - .map((def, i) => -
- -
+ .sort((w1, w2) => { + if (!w2.isConfigured || !w2.enabled) { + return -1 + } + return w1.priority - w2.priority + }) + .map((w, i) => { + const draggable = w.isConfigured + return ( +
+ +
+ ) + } )} +
diff --git a/styles/wallet.module.css b/styles/wallet.module.css index e29e5ae3..b4efe607 100644 --- a/styles/wallet.module.css +++ b/styles/wallet.module.css @@ -18,7 +18,6 @@ .card { width: 160px; height: 180px; - cursor: move; } .badge {