Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/healthy-teachers-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@abstract-money/react": patch
---

Add persistKey parameter to AbstractAccountIdProvider
9 changes: 2 additions & 7 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
},
Expand Down
33 changes: 29 additions & 4 deletions packages/react/src/contexts/account-id.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -10,6 +14,7 @@ const noop = () => {
type AbstractAccountIdContext = {
accountId: AccountId
setAccountId: (accountId: AccountId) => void
persistKey?: string
}

type AbstractAccountIdContextWithPartialAccountId = Prettify<
Expand All @@ -31,13 +36,33 @@ const Context =
export function AbstractAccountIdProvider({
children,
accountId: defaultAccountId,
}: React.PropsWithChildren<{ accountId: AccountId }>) {
const [accountId, setAccountId] = React.useState<AccountId>(defaultAccountId)
persistKey,
}: React.PropsWithChildren<{ accountId: AccountId; persistKey?: string }>) {
const [accountId, setAccountId_] = React.useState<AccountId>(() => {
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 },
})
}

Expand Down
23 changes: 10 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.