-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
91 feat add a persistence layer to store the address (#102)
* feat: persist address in local storage * feat: dapp kit context provider * feat: context object stored in local storage * fix: connected address badge and modal test * feat: not persistent context property * test: dapp kit context initialization
- Loading branch information
Showing
16 changed files
with
227 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<vwk-connect-button-with-modal mode="DARK"></vwk-connect-button-with-modal> | ||
<vwk-vechain-dapp-connect-kit mode="DARK"></vwk-vechain-dapp-connect-kit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/dapp-kit-ui/src/components/provider/dapp-kit-context-provider/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { provide } from '@lit/context'; | ||
import { LitElement, type TemplateResult, html } from 'lit'; | ||
import { customElement, property } from 'lit/decorators.js'; | ||
import { | ||
type DappKitContext, | ||
dappKitContext, | ||
getDappKitContext, | ||
} from '../dapp-kit-context'; | ||
|
||
@customElement('dapp-kit-context-provider') | ||
export class DappKitContextProvider extends LitElement { | ||
@provide({ context: dappKitContext }) | ||
@property({ attribute: false }) | ||
dappKitContext: DappKitContext = { | ||
options: { | ||
notPersistentContext: false, | ||
}, | ||
address: '', | ||
}; | ||
|
||
@property({ type: Boolean }) | ||
notPersistentContext = false; | ||
|
||
// Use the `connectedCallback` lifecycle hook to retrieve the stored address | ||
connectedCallback(): void { | ||
super.connectedCallback(); | ||
this.dappKitContext = getDappKitContext(this.notPersistentContext); | ||
} | ||
|
||
render(): TemplateResult { | ||
return html`<slot></slot>`; | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
'dapp-kit-context-provider': DappKitContextProvider; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
packages/dapp-kit-ui/src/components/provider/dapp-kit-context.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { createContext } from '@lit/context'; | ||
|
||
interface DappKitContextOptions { | ||
notPersistentContext: boolean; | ||
} | ||
|
||
interface DappKitContext { | ||
options: DappKitContextOptions; | ||
address: string; | ||
} | ||
|
||
const dappKitContext = createContext<DappKitContext>( | ||
Symbol('dapp-kit-context'), | ||
); | ||
|
||
const storeDappKitContext = function (context: DappKitContext): void { | ||
localStorage.setItem('dapp-kit-context-object', JSON.stringify(context)); | ||
}; | ||
|
||
const getDappKitContext = function ( | ||
notPersistentContext = false, | ||
): DappKitContext { | ||
const dappKitContextObject = localStorage.getItem( | ||
'dapp-kit-context-object', | ||
); | ||
|
||
if (notPersistentContext || !dappKitContextObject) { | ||
return { | ||
options: { | ||
notPersistentContext, | ||
}, | ||
address: '', | ||
}; | ||
} | ||
|
||
return JSON.parse(dappKitContextObject) as DappKitContext; | ||
}; | ||
|
||
export { | ||
dappKitContext, | ||
type DappKitContext, | ||
storeDappKitContext, | ||
getDappKitContext, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './dapp-kit-context'; | ||
export * from './dapp-kit-context-provider'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/dapp-kit-ui/src/components/vwk-vechain-dapp-connect-kit/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { LitElement, type TemplateResult, html } from 'lit'; | ||
import { customElement, property } from 'lit/decorators.js'; | ||
import type { Theme, ThemeMode } from '../../constants'; | ||
|
||
@customElement('vwk-vechain-dapp-connect-kit') | ||
export class VechainDappConnectKit extends LitElement { | ||
@property({ type: String }) | ||
mode: ThemeMode = 'LIGHT'; | ||
|
||
@property({ type: String }) | ||
theme: Theme = 'DEFAULT'; | ||
|
||
@property({ type: Boolean }) | ||
notPersistentContext = false; | ||
|
||
render(): TemplateResult { | ||
return html`<dapp-kit-context-provider | ||
?notPersistentContext=${this.notPersistentContext} | ||
><vwk-connect-button-with-modal | ||
mode="DARK" | ||
></vwk-connect-button-with-modal | ||
></dapp-kit-context-provider>`; | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
'vwk-vechain-dapp-connect-kit': VechainDappConnectKit; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/dapp-kit-ui/test/vechain-dapp-connect-kit.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { beforeEach, describe, expect, it } from 'vitest'; | ||
|
||
import { DAppKit, DappKitContextProvider, VechainDappConnectKit } from '../src'; | ||
import { elementQueries } from './helpers/element-queries'; | ||
|
||
describe('connect-button-with-modal', () => { | ||
beforeEach(() => { | ||
DAppKit.configure({ nodeUrl: 'https://mainnet.vechain.org/' }); | ||
}); | ||
|
||
it('Should callback with source when user clicks a wallet and should render the connected address badge once connected', async () => { | ||
const element: VechainDappConnectKit = window.document.createElement( | ||
'vwk-vechain-dapp-connect-kit', | ||
); | ||
|
||
window.document.body.appendChild(element); | ||
|
||
// testing the dapp kit context provider | ||
|
||
// set a not persistent context | ||
element.notPersistentContext = true; | ||
|
||
const dappKitContextProvider = | ||
(await elementQueries.getDappKitContextProvider()) as DappKitContextProvider; | ||
|
||
expect(dappKitContextProvider).toBeDefined(); | ||
|
||
// check if the context is not persistent | ||
expect(dappKitContextProvider.notPersistentContext).toBe(true); | ||
|
||
expect(dappKitContextProvider.dappKitContext).toBeDefined(); | ||
expect(dappKitContextProvider.dappKitContext.address).toBe(''); | ||
}); | ||
}); |
Oops, something went wrong.