-
Notifications
You must be signed in to change notification settings - Fork 1
Add support for gnosis pay #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Joshua-onwuzu
wants to merge
7
commits into
fileverse-mod
Choose a base branch
from
add-support-for-gnosis-pay
base: fileverse-mod
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a7e976d
add support for gnosis pay
Joshua-onwuzu 2d260a1
lock
Joshua-onwuzu cb45476
refactor-code
Joshua-onwuzu c332edf
v5
Joshua-onwuzu b91a59e
updated gnosis pay ui
Joshua-onwuzu a20369f
updated gnosis pay copy and UI
Joshua-onwuzu 6519ca3
released @fileverse-dev/fortune-react@1.0.88-patch-10
Joshua-onwuzu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
1 change: 1 addition & 0 deletions
1
packages/react/src/components/SheetOverlay/FormulaHint/constants.ts
This file contains hidden or 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 @@ | ||
| export const GNOSIS_PAY_ACCESS = "GNOSIS_PAY_ACCESS"; |
This file contains hidden or 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
103 changes: 103 additions & 0 deletions
103
packages/react/src/components/SheetOverlay/FormulaHint/use-gnosis-pay.tsx
This file contains hidden or 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,103 @@ | ||
| import { useEffect, useRef, useState } from "react"; | ||
| import { | ||
| isGnosisPayAccessExpired, | ||
| // @ts-ignore | ||
| } from "@fileverse-dev/formulajs/crypto-constants"; | ||
| import { GNOSIS_PAY_ACCESS } from "./constants"; | ||
| import { formatTimeLeft, getJwtExpiry } from "./utils/utils"; | ||
|
|
||
| const useGnosisPay = (fn: any) => { | ||
| const gnosisTokenTokenIntervalRef = useRef<any>(null); | ||
| const [isLoading, setIsLoading] = useState(false); | ||
| const [hasGnosisPayToken, setHasGnosisPayToken] = useState(false); | ||
| const [timeLeft, setTimeLeft] = useState("00:00"); | ||
| const [accessTokenCreatedAt, setAccessTokenCreatedAt] = useState(0); | ||
| const isWrongGnosisPayConnector = | ||
| localStorage.getItem("LOGIN_METHOD") !== "walletAddress"; | ||
|
|
||
| const handleGnosisPayToken = (onDone?: () => void) => { | ||
| if (localStorage.getItem(GNOSIS_PAY_ACCESS)) { | ||
| const access = localStorage.getItem(GNOSIS_PAY_ACCESS) || ""; | ||
| if (!access || isGnosisPayAccessExpired(access)) { | ||
| if (hasGnosisPayToken) { | ||
| setHasGnosisPayToken(false); | ||
| } | ||
| if (accessTokenCreatedAt) { | ||
| setAccessTokenCreatedAt(0); | ||
| } | ||
| localStorage.removeItem(GNOSIS_PAY_ACCESS); | ||
| return; | ||
| } | ||
| setHasGnosisPayToken(!!access); | ||
| setAccessTokenCreatedAt(getJwtExpiry(access)); | ||
| onDone?.(); | ||
| } else { | ||
| const urlParams = new URLSearchParams(window.location.search); | ||
| const isRejected = urlParams.has("reject"); | ||
| if (isRejected) { | ||
| const url = new URL(window.location.href); | ||
| url.searchParams.delete("reject"); | ||
| window.history.replaceState({}, "", url.toString()); | ||
| onDone?.(); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| return () => { | ||
| if (gnosisTokenTokenIntervalRef.current) | ||
| clearInterval(gnosisTokenTokenIntervalRef.current); | ||
| }; | ||
| }, [gnosisTokenTokenIntervalRef]); | ||
|
|
||
| useEffect(() => { | ||
| if (accessTokenCreatedAt <= 0) return () => {}; | ||
|
|
||
| const interval = setInterval(() => { | ||
| const access = localStorage.getItem(GNOSIS_PAY_ACCESS) || ""; | ||
| const expiryTimestamp = getJwtExpiry(access) * 1000; | ||
| const newTimeLeft = expiryTimestamp - Date.now(); | ||
| setTimeLeft(formatTimeLeft(newTimeLeft)); | ||
| if ( | ||
| isGnosisPayAccessExpired(localStorage.getItem(GNOSIS_PAY_ACCESS)) || | ||
| !document.getElementById("gnosis-pay-area") | ||
| ) { | ||
| localStorage.removeItem(GNOSIS_PAY_ACCESS); | ||
| setHasGnosisPayToken(false); | ||
| setAccessTokenCreatedAt(0); | ||
| clearInterval(interval); | ||
| } | ||
| }, 1000); | ||
|
|
||
| return () => { | ||
| clearInterval(interval); | ||
| }; | ||
| }, [accessTokenCreatedAt, fn]); | ||
|
|
||
| const grantAccess = () => { | ||
| const button = document.getElementById("grant-gnosispay-access"); | ||
| if (!button) return; | ||
| button.click(); | ||
| setIsLoading(true); | ||
| const interval = setInterval(() => { | ||
| handleGnosisPayToken(() => { | ||
| clearInterval(interval); | ||
| setIsLoading(false); | ||
| }); | ||
| }, 5000); | ||
|
|
||
| gnosisTokenTokenIntervalRef.current = interval; | ||
| }; | ||
|
|
||
| return { | ||
| grantAccess, | ||
| handleGnosisPayToken, | ||
| hasGnosisPayToken, | ||
| isWrongGnosisPayConnector, | ||
| isLoading, | ||
| accessTokenCreatedAt, | ||
| timeLeft, | ||
| }; | ||
| }; | ||
|
|
||
| export default useGnosisPay; | ||
27 changes: 27 additions & 0 deletions
27
packages/react/src/components/SheetOverlay/FormulaHint/utils/utils.ts
This file contains hidden or 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,27 @@ | ||
| export function formatTimeLeft(msLeft: number): string { | ||
| const totalSeconds = Math.max(0, Math.floor(msLeft / 1000)); | ||
| const minutes = String(Math.floor(totalSeconds / 60)).padStart(2, "0"); | ||
| const seconds = String(totalSeconds % 60).padStart(2, "0"); | ||
| return `${minutes}:${seconds}`; | ||
| } | ||
| export function timeFromNowMessage(expiryStr: string): string { | ||
| if (!expiryStr) { | ||
| return "0 minute"; | ||
| } | ||
| const [mm] = expiryStr.split(":").map(Number); | ||
|
|
||
| return `${mm} minute${mm !== 1 ? "s" : ""}`; | ||
| } | ||
| export function getJwtExpiry(token: string): number { | ||
| try { | ||
| const payloadBase64 = token?.split(".")?.[1]; | ||
| if (!payloadBase64) return 0; | ||
|
|
||
| const payloadJson = atob(payloadBase64); | ||
| const payload = JSON.parse(payloadJson); | ||
|
|
||
| return typeof payload.exp === "number" ? payload.exp : 0; | ||
| } catch (error) { | ||
| return 0; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The token already contains an expiry field, use that instead