From 4c5885e46aaf5251710dc889790ddd8c77d3b34e Mon Sep 17 00:00:00 2001 From: hotwater Date: Fri, 16 Feb 2024 21:18:04 +0200 Subject: [PATCH 1/2] feat: account id persistance --- packages/react/package.json | 9 ++----- packages/react/src/contexts/account-id.ts | 33 ++++++++++++++++++++--- pnpm-lock.yaml | 23 +++++++--------- 3 files changed, 41 insertions(+), 24 deletions(-) diff --git a/packages/react/package.json b/packages/react/package.json index 42127d61..cfcd8f58 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -26,8 +26,7 @@ "dependencies": { "@abstract-money/core": "workspace:*", "@tanstack/react-query": "^4", - "dedent": "^0.7.0", - "zustand": "^4.4.6" + "dedent": "^0.7.0" }, "peerDependencies": { "@cosmjs/amino": ">=0.28", @@ -73,11 +72,7 @@ }, "./package.json": "./package.json" }, - "files": [ - "/utils", - "/hooks", - "/dist" - ], + "files": ["/utils", "/hooks", "/dist"], "simple-git-hooks": { "pre-commit": "pnpm format && pnpm lint:fix" }, diff --git a/packages/react/src/contexts/account-id.ts b/packages/react/src/contexts/account-id.ts index 27c05c7d..4916ad77 100644 --- a/packages/react/src/contexts/account-id.ts +++ b/packages/react/src/contexts/account-id.ts @@ -1,4 +1,8 @@ -import { AccountId } from '@abstract-money/core' +import { + AccountId, + accountIdToString, + stringToAccountId, +} from '@abstract-money/core' import dedent from 'dedent' import * as React from 'react' import { Prettify } from '../types/utils' @@ -10,6 +14,7 @@ const noop = () => { type AbstractAccountIdContext = { accountId: AccountId setAccountId: (accountId: AccountId) => void + persistKey?: string } type AbstractAccountIdContextWithPartialAccountId = Prettify< @@ -31,13 +36,33 @@ const Context = export function AbstractAccountIdProvider({ children, accountId: defaultAccountId, -}: React.PropsWithChildren<{ accountId: AccountId }>) { - const [accountId, setAccountId] = React.useState(defaultAccountId) + persistKey, +}: React.PropsWithChildren<{ accountId: AccountId; persistKey?: string }>) { + const [accountId, setAccountId_] = React.useState(() => { + if (typeof window === 'undefined') return defaultAccountId + if (persistKey) { + const item = window.localStorage.getItem(persistKey) + if (item) return stringToAccountId(item) + return defaultAccountId + } + return defaultAccountId + }) + + const setAccountId = React.useCallback<(accountId: AccountId) => void>( + (accountId) => { + setAccountId_(accountId) + if (typeof window === 'undefined' || !persistKey) return + + window.localStorage.setItem(persistKey, accountIdToString(accountId)) + }, + [], + ) + // Bailing out of using JSX // https://github.com/egoist/tsup/issues/390#issuecomment-933488738 return React.createElement(Context.Provider, { children, - value: { accountId, setAccountId }, + value: { accountId, persistKey, setAccountId }, }) } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2bf46dab..befccbf3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,5 +1,9 @@ lockfileVersion: '6.0' +settings: + autoInstallPeers: true + excludeLinksFromLockfile: false + importers: .: @@ -652,9 +656,6 @@ importers: typescript: specifier: '>=5.0.4' version: 5.3.2 - zustand: - specifier: ^4.4.6 - version: 4.4.6(@types/react@18.2.38)(immer@9.0.21)(react@18.2.0) devDependencies: '@cosmjs/amino': specifier: ^0.31.0 @@ -9269,7 +9270,7 @@ packages: resolution: {integrity: sha512-WIdaQ8uW1vIbYvNnAVunkC6yxTrneJC7VQ5UUQ0kuw8b0C0A39KTIpoQHCfc8tV7o9vF4niwRhdXEdfAgQEsQQ==} dependencies: cosmos-directory-types: 0.0.6 - node-fetch-native: 1.6.1 + node-fetch-native: 1.6.2 /cosmos-directory-types@0.0.6: resolution: {integrity: sha512-9qlQ3kTNTHvhYglTXSnllGqKhrtGB08Weatw56ZqV5OqcmjuZdlY9iMtD00odgQLTEpTSQQL3gFGuqTkGdIDPA==} @@ -13257,8 +13258,8 @@ packages: engines: {node: '>=10.5.0'} dev: false - /node-fetch-native@1.6.1: - resolution: {integrity: sha512-bW9T/uJDPAJB2YNYEpWzE54U5O3MQidXsOyTfnbKYtTtFexRvGzb1waphBN4ZwP6EcIvYYEOwW0b72BpAqydTw==} + /node-fetch-native@1.6.2: + resolution: {integrity: sha512-69mtXOFZ6hSkYiXAVB5SqaRvrbITC/NPyqv7yuu/qw0nmgPyYbIMYYNIDhNtwPrzk0ptrimrLz/hhjvm4w5Z+w==} /node-fetch@2.7.0: resolution: {integrity: sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==} @@ -13375,7 +13376,7 @@ packages: resolution: {integrity: sha512-s1ZCMmQWXy4b5K/TW9i/DtiN8Ku+xCiHcjQ6/J/nDdssirrQNOoB165Zu8EqLMA2lln1JUth9a0aW9Ap2ctrUg==} dependencies: destr: 2.0.2 - node-fetch-native: 1.6.1 + node-fetch-native: 1.6.2 ufo: 1.3.2 /on-exit-leak-free@0.2.0: @@ -15844,7 +15845,7 @@ packages: consola: 3.2.3 defu: 6.1.3 mime: 3.0.0 - node-fetch-native: 1.6.1 + node-fetch-native: 1.6.2 pathe: 1.1.1 /unicode-canonical-property-names-ecmascript@2.0.0: @@ -15948,7 +15949,7 @@ packages: listhen: 1.5.5 lru-cache: 10.1.0 mri: 1.2.0 - node-fetch-native: 1.6.1 + node-fetch-native: 1.6.2 ofetch: 1.3.3 ufo: 1.3.2 transitivePeerDependencies: @@ -16758,7 +16759,3 @@ packages: immer: 9.0.21 react: 18.2.0 use-sync-external-store: 1.2.0(react@18.2.0) - -settings: - autoInstallPeers: true - excludeLinksFromLockfile: false From f0d965e33176306bfb6e11d6d4c1924b5e960a5a Mon Sep 17 00:00:00 2001 From: hotwater Date: Fri, 16 Feb 2024 21:18:29 +0200 Subject: [PATCH 2/2] chore: changesets --- .changeset/healthy-teachers-tap.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/healthy-teachers-tap.md diff --git a/.changeset/healthy-teachers-tap.md b/.changeset/healthy-teachers-tap.md new file mode 100644 index 00000000..7bf6bc50 --- /dev/null +++ b/.changeset/healthy-teachers-tap.md @@ -0,0 +1,5 @@ +--- +"@abstract-money/react": patch +--- + +Add persistKey parameter to AbstractAccountIdProvider