-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Modal builder + interaction token in session storage
- Loading branch information
1 parent
cfc6d8c
commit 0581ab8
Showing
3 changed files
with
183 additions
and
1 deletion.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import type { | ||
DisplayModalParamsType, | ||
FinalActionType, | ||
FinalModalStepType, | ||
LoginModalStepType, | ||
ModalRpcMetadata, | ||
ModalRpcStepsResultType, | ||
ModalStepTypes, | ||
NexusClient, | ||
OpenInteractionSessionModalStepType, | ||
SendTransactionModalStepType, | ||
} from "../../types"; | ||
import { displayModal } from "../displayModal"; | ||
|
||
/** | ||
* Simple modal builder params builder | ||
* @param client | ||
* @param metadata | ||
* @param login | ||
* @param openSession | ||
*/ | ||
export function modalBuilder( | ||
client: NexusClient, | ||
{ | ||
metadata, | ||
login, | ||
openSession, | ||
}: { | ||
metadata: ModalRpcMetadata; | ||
login?: LoginModalStepType["params"]; | ||
openSession?: OpenInteractionSessionModalStepType["params"]; | ||
} | ||
): ModalStepBuilder<[LoginModalStepType, OpenInteractionSessionModalStepType]> { | ||
// Build the initial modal params | ||
const baseParams: DisplayModalParamsType< | ||
[LoginModalStepType, OpenInteractionSessionModalStepType] | ||
> = { | ||
steps: { | ||
login: login ?? {}, | ||
openSession: openSession ?? {}, | ||
}, | ||
metadata, | ||
}; | ||
|
||
// Return the step builder | ||
return modalStepsBuilder(client, baseParams); | ||
} | ||
|
||
/** | ||
* Represent the type of the modal step builder | ||
*/ | ||
type ModalStepBuilder<Steps extends ModalStepTypes[]> = { | ||
params: DisplayModalParamsType<Steps>; | ||
sendTx: ( | ||
options: SendTransactionModalStepType["params"] | ||
) => ModalStepBuilder<[...Steps, SendTransactionModalStepType]>; | ||
reward: ( | ||
options?: Omit<FinalModalStepType["params"], "action"> | ||
) => ModalStepBuilder<[...Steps, FinalModalStepType]>; | ||
sharing: ( | ||
sharingOptions?: Extract< | ||
FinalActionType, | ||
{ key: "sharing" } | ||
>["options"], | ||
options?: Omit<FinalModalStepType["params"], "action"> | ||
) => ModalStepBuilder<[...Steps, FinalModalStepType]>; | ||
display: () => Promise<ModalRpcStepsResultType<Steps>>; | ||
}; | ||
|
||
/** | ||
* Build builder helping to add steps to the modal | ||
* @param client | ||
* @param params | ||
*/ | ||
function modalStepsBuilder<CurrentSteps extends ModalStepTypes[]>( | ||
client: NexusClient, | ||
params: DisplayModalParamsType<CurrentSteps> | ||
): ModalStepBuilder<CurrentSteps> { | ||
// Function add the send tx step | ||
function sendTx(options: SendTransactionModalStepType["params"]) { | ||
return modalStepsBuilder< | ||
[...CurrentSteps, SendTransactionModalStepType] | ||
>(client, { | ||
...params, | ||
steps: { | ||
...params.steps, | ||
sendTransaction: options, | ||
}, | ||
} as DisplayModalParamsType< | ||
[...CurrentSteps, SendTransactionModalStepType] | ||
>); | ||
} | ||
|
||
// Function to add a reward step at the end | ||
function reward(options?: Omit<FinalModalStepType["params"], "action">) { | ||
return modalStepsBuilder<[...CurrentSteps, FinalModalStepType]>( | ||
client, | ||
{ | ||
...params, | ||
steps: { | ||
...params.steps, | ||
final: { | ||
...options, | ||
action: { key: "reward" }, | ||
}, | ||
}, | ||
} as DisplayModalParamsType<[...CurrentSteps, FinalModalStepType]> | ||
); | ||
} | ||
|
||
// Function to add sharing step at the end | ||
function sharing( | ||
sharingOptions?: Extract< | ||
FinalActionType, | ||
{ key: "sharing" } | ||
>["options"], | ||
options?: Omit<FinalModalStepType["params"], "action"> | ||
) { | ||
return modalStepsBuilder<[...CurrentSteps, FinalModalStepType]>( | ||
client, | ||
{ | ||
...params, | ||
steps: { | ||
...params.steps, | ||
final: { | ||
...options, | ||
action: { key: "sharing", options: sharingOptions }, | ||
}, | ||
}, | ||
} as DisplayModalParamsType<[...CurrentSteps, FinalModalStepType]> | ||
); | ||
} | ||
|
||
// Function to display it | ||
async function display() { | ||
return await displayModal(client, params); | ||
} | ||
|
||
return { | ||
// Access current modal params | ||
params, | ||
// Function to add new steps | ||
sendTx, | ||
reward, | ||
sharing, | ||
// Display the modal | ||
display, | ||
}; | ||
} |