-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc32065
commit 4ce10d3
Showing
9 changed files
with
956 additions
and
362 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,25 +1,56 @@ | ||
import { AppKitSmartSessionControllerClient } from '../src/client.js' | ||
import { useState, useEffect, useCallback } from 'react' | ||
import { SmartSessionController } from '../src/core/controller/SmartSessionController.js' | ||
import type { | ||
SmartSessionGrantPermissionsRequest, | ||
SmartSessionGrantPermissionsResponse | ||
} from '../src/core/utils/TypeUtils.js' | ||
export { | ||
SmartSessionController, | ||
type SmartSessionControllerClient | ||
} from '../src/core/controller/SmartSessionController.js' | ||
export { SmartSessionController } from '../src/core/controller/SmartSessionController.js' | ||
export * from '../src/core/utils/TypeUtils.js' | ||
import { AppKitSmartSessionControllerClient } from '../src/client.js' | ||
|
||
export type { AppKitSmartSessionControllerClient } | ||
|
||
// -- Hooks ------------------------------------------------------------------- | ||
export function useSmartSession() { | ||
async function grantPermissions( | ||
request: SmartSessionGrantPermissionsRequest | ||
): Promise<SmartSessionGrantPermissionsResponse> { | ||
const appkitSmartSessionControllerClient = new AppKitSmartSessionControllerClient() | ||
return await appkitSmartSessionControllerClient.grantPermissions(request) | ||
} | ||
export const useSmartSession = () => { | ||
// Local state to store the latest smart session state | ||
const [permissions, setPermissions] = useState(SmartSessionController.state.permissions) | ||
const [permissionsContext, setPermissionsContext] = useState( | ||
SmartSessionController.state.permissionsContext | ||
) | ||
|
||
// Grant permissions method | ||
const grantPermissions = useCallback( | ||
async ( | ||
smartSessionGrantPermissionsRequest: SmartSessionGrantPermissionsRequest | ||
): Promise<SmartSessionGrantPermissionsResponse> => { | ||
try { | ||
const response = await SmartSessionController.grantPermissions( | ||
smartSessionGrantPermissionsRequest | ||
) | ||
return response | ||
} catch (error) { | ||
console.error('Error granting permissions:', error) | ||
throw error | ||
} | ||
}, | ||
[] | ||
) | ||
|
||
// Subscribe to the SmartSessionController state and update local state when it changes | ||
useEffect(() => { | ||
const unsubscribe = SmartSessionController.subscribe(newState => { | ||
setPermissions(newState.permissions) | ||
setPermissionsContext(newState.permissionsContext) | ||
}) | ||
|
||
// Cleanup subscription on unmount | ||
return () => unsubscribe() | ||
}, []) | ||
|
||
// Return values and methods to use in components | ||
return { | ||
permissions, | ||
permissionsContext, | ||
grantPermissions | ||
} | ||
} |
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
59 changes: 25 additions & 34 deletions
59
packages/experimental/smart-session/src/core/controller/SmartSessionController.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 |
---|---|---|
@@ -1,58 +1,49 @@ | ||
import type { ChainAdapter } from '@reown/appkit-core' | ||
import { subscribeKey as subKey } from 'valtio/vanilla/utils' | ||
import { proxy, ref, subscribe as sub } from 'valtio/vanilla' | ||
import { proxy, subscribe as sub } from 'valtio/vanilla' | ||
import type { | ||
SmartSessionClientMethods, | ||
SmartSessionGrantPermissionsRequest, | ||
SmartSessionGrantPermissionsResponse | ||
} from '../utils/TypeUtils' | ||
import { AppKitSmartSessionControllerClient } from '../../client' | ||
|
||
// -- Types --------------------------------------------- // | ||
export interface SmartSessionControllerClient extends SmartSessionClientMethods { | ||
chainAdapter: ChainAdapter | ||
export interface SmartSessionControllerState { | ||
permissionsContext?: SmartSessionGrantPermissionsResponse['context'] | ||
permissions?: SmartSessionGrantPermissionsResponse['permissions'] | ||
} | ||
|
||
export interface SmartSessionControllerClientState { | ||
_client?: SmartSessionControllerClient | ||
} | ||
|
||
type StateKey = keyof SmartSessionControllerClientState | ||
export type StateKey = keyof SmartSessionControllerState | ||
|
||
// -- State --------------------------------------------- // | ||
const state = proxy<SmartSessionControllerClientState>({ | ||
// status: 'uninitialized' | ||
// -- State --------------------------------------------------------- // | ||
const state = proxy<SmartSessionControllerState>({ | ||
permissions: undefined, | ||
permissionsContext: undefined | ||
}) | ||
|
||
// -- Controller ---------------------------------------- // | ||
export const SmartSessionController = { | ||
state, | ||
|
||
subscribeKey<K extends StateKey>( | ||
key: K, | ||
callback: (value: SmartSessionControllerClientState[K]) => void | ||
) { | ||
return subKey(state, key, callback) | ||
}, | ||
async grantPermissions( | ||
smartSessionGrantPermissionsRequest: SmartSessionGrantPermissionsRequest | ||
): Promise<SmartSessionGrantPermissionsResponse> { | ||
const service = new AppKitSmartSessionControllerClient() | ||
const response = await service.grantPermissions(smartSessionGrantPermissionsRequest) | ||
|
||
subscribe(callback: (newState: SmartSessionControllerClientState) => void) { | ||
return sub(state, () => callback(state)) | ||
}, | ||
this.setPermissions(response.permissions) | ||
this.setPermissionsContext(response.context) | ||
|
||
_getClient() { | ||
if (!state._client) { | ||
throw new Error('SmartSessionControllerClientState client not set') | ||
} | ||
return response | ||
}, | ||
|
||
return state._client | ||
subscribe(callback: (newState: SmartSessionControllerState) => void) { | ||
return sub(state, () => callback(state)) | ||
}, | ||
|
||
setSmartSessionControllerClient(client: SmartSessionControllerClient) { | ||
state._client = ref(client) | ||
setPermissions(permissions: SmartSessionGrantPermissionsResponse['permissions']) { | ||
state.permissions = permissions | ||
}, | ||
|
||
grantPermissions( | ||
request: SmartSessionGrantPermissionsRequest | ||
): Promise<SmartSessionGrantPermissionsResponse> { | ||
return this._getClient().grantPermissions(request) | ||
setPermissionsContext(context: SmartSessionGrantPermissionsResponse['context']) { | ||
state.permissionsContext = context | ||
} | ||
} |
Oops, something went wrong.