Skip to content

Commit

Permalink
touchup code
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink committed Nov 15, 2024
1 parent ad0475b commit 1e8de39
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 49 deletions.
5 changes: 5 additions & 0 deletions .changeset/rare-apes-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@onflow/typedefs": patch
---

Fix CurrentUser services type
1 change: 1 addition & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions packages/fcl-wc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@babel/plugin-transform-react-jsx": "^7.25.9",
"@babel/preset-typescript": "^7.25.7",
"@onflow/fcl-bundle": "1.5.1-alpha.0",
"@onflow/typedefs": "^1.4.0-alpha.1",
"eslint": "^8.57.1",
"eslint-plugin-jsdoc": "^46.10.1",
"jest": "^29.7.0",
Expand Down
46 changes: 35 additions & 11 deletions packages/fcl-wc/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import {
import {SignClient} from "@walletconnect/sign-client/dist/types/client"
import {createSessionProposal, request} from "./session"
import {ModalCtrlState} from "@walletconnect/modal-core/dist/_types/src/types/controllerTypes"
import {showWcRequestNotification} from "./ui/notifications"
import {showNotification} from "./ui/notifications"
import type {FclWalletConnectConfig} from "./fcl-wc"
import mobileIcon from "./ui/assets/mobile.png"
import { CurrentUser, Service } from "@onflow/typedefs"

type WalletConnectModalType = import("@walletconnect/modal").WalletConnectModal

Expand Down Expand Up @@ -114,15 +116,11 @@ const makeExec = (
openDeeplink(appLink)
}

// Show notification to the user
let dismissNotification = () => {}
if (showNotifications) {
const {dismiss} = showWcRequestNotification({
user,
service,
})
dismissNotification = dismiss
}
// Show notification to the user if enabled
const notification = showNotifications ? showWcRequestNotification({
user,
service,
}) : null

// Make request to the WalletConnect client and return the result
return await request({
Expand All @@ -131,7 +129,7 @@ const makeExec = (
session,
client,
abortSignal,
}).finally(dismissNotification)
}).finally(() => notification?.dismiss())

function validateAppLink({uid}: {uid: string}) {
if (!(uid && /^(ftp|http|https):\/\/[^ "]+$/.test(uid))) {
Expand Down Expand Up @@ -258,3 +256,29 @@ function connectWc(
}
}
}

/**
* Show a notification for a WalletConnect request.
* @param service - The service that is requesting the user's attention.
* @param user - The user that is being requested to sign a transaction.
* @returns A close function to dismiss the notification.
*/
export function showWcRequestNotification({
service,
user,
}: {
service: Service
user: CurrentUser
}) {
const authnService = user?.services?.find((s: any) => s.type === "authn")
const walletProvider = authnService?.provider

return showNotification({
title: walletProvider?.name || "Mobile Wallet",
message: isMobile()
? "Tap to view request in app"
: "Pending request on your mobile device",
icon: walletProvider?.icon || mobileIcon,
onClick: service.uid ? () => openDeeplink(service.uid!) : undefined,
})
}
6 changes: 0 additions & 6 deletions packages/fcl-wc/src/ui/constants.ts

This file was deleted.

33 changes: 2 additions & 31 deletions packages/fcl-wc/src/ui/notifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {render} from "preact"
import styles from "./styles.css"
import {Notification} from "./components/Notification"
import {NotificationInfo} from "../types/types"
import {isMobile, openDeeplink} from "../utils"
import mobileIcon from "./assets/mobile.png"

let renderRoot: HTMLElement | null = null
let id = 0
Expand Down Expand Up @@ -34,7 +32,7 @@ function createRenderRoot() {
/**
* Show a notification to the user. Only one notification can be shown at a time and will replace any existing notification.
*/
function showNotification({
export function showNotification({
title,
message,
icon,
Expand Down Expand Up @@ -68,31 +66,4 @@ function showNotification({
return {
dismiss,
}
}

/**
* Show a notification for a WalletConnect request.
* @param service - The service that is requesting the user's attention.
* @param user - The user that is being requested to sign a transaction.
* @param openDeeplink - A function to open the wallet app deeplink.
* @returns A close function to dismiss the notification.
*/
export function showWcRequestNotification({
service,
user,
}: {
service: any
user: any
}) {
const authnService = user?.services?.find((s: any) => s.type === "authn")
const walletProvider = authnService?.provider

return showNotification({
title: walletProvider?.name || "Mobile Wallet",
message: isMobile()
? "Tap to view request in app"
: "Pending request on your mobile device",
icon: walletProvider?.icon || mobileIcon,
onClick: service.uid ? () => openDeeplink(service.uid) : undefined,
})
}
}
2 changes: 1 addition & 1 deletion packages/typedefs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export type CurrentUser = {
/**
* - A list of trusted services that express ways of interacting with the current user's identity
*/
services: Array<object>
services: Array<Service>
}
export type Event = {
/**
Expand Down

0 comments on commit 1e8de39

Please sign in to comment.