Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nalinbhardwaj committed Jan 21, 2024
1 parent f36e6cd commit 4a50f99
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
27 changes: 19 additions & 8 deletions apps/daimo-mobile/src/logic/deeplink.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { getInitialURL } from "expo-linking";
import { Platform } from "react-native";
import NfcManager, { Ndef } from "react-native-nfc-manager";
import NfcManager, { Ndef, NdefRecord } from "react-native-nfc-manager";

// User opened app via deeplink or scanning an NFC tag: return URL, otherwise null.
export async function getInitialURLOrTag() {
const url = await getInitialURL();
if (url) return url;
const deeplinkURL = await getInitialURL();
if (deeplinkURL) return deeplinkURL;

if (Platform.OS === "android") {
const tag = await NfcManager.getLaunchTagEvent();
if (tag && tag.ndefMessage.length > 0) {
console.log(`[DEEPLINK] got NFC tag ${JSON.stringify(tag)}`);
try {
const message = tag.ndefMessage[0];
const url = Ndef.uri.decodePayload(
message.payload as unknown as Uint8Array
);
console.log(`[DEEPLINK] decoded NFC tag payload: ${url}`);
return url;
if (isRecordUint8Array(message.payload) && isURIRecord(message)) {
const nfcURL = Ndef.uri.decodePayload(message.payload);
console.log(`[DEEPLINK] decoded NFC tag payload: ${nfcURL}`);
return nfcURL;
} else {
console.warn(`[DEEPLINK] NFC tag payload is not a well-formed URI`);
}
} catch (e) {
console.warn(
`[DEEPLINK] error decoding NFC tag payload, failing silently: ${e}`
Expand All @@ -27,3 +30,11 @@ export async function getInitialURLOrTag() {

return null;
}

function isURIRecord(record: NdefRecord) {
return Ndef.isType(record, Ndef.TNF_WELL_KNOWN, Ndef.RTD_URI);
}

function isRecordUint8Array(payload: unknown): payload is Uint8Array {
return true;
}
4 changes: 2 additions & 2 deletions apps/daimo-web/src/app/l/[[...slug]]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
DaimoRequestStatus,
assert,
daimoDomain,
daimoLinkBase,
daimoLinkBaseV2,
getAccountName,
parseDaimoLink,
} from "@daimo/common";
Expand Down Expand Up @@ -36,7 +36,7 @@ const defaultMeta = metadata("Daimo", "Payments on Ethereum");

function getUrl(props: LinkProps): string {
const path = (props.params.slug || []).join("/");
return `${daimoLinkBase}/${path}`;
return `${daimoLinkBaseV2}/${path}`;
}

export async function generateMetadata(props: LinkProps): Promise<Metadata> {
Expand Down
10 changes: 2 additions & 8 deletions apps/daimo-web/src/components/CallToAction.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
"use client";

import {
DaimoLinkStatus,
daimoLinkBase,
daimoLinkBaseFuture,
} from "@daimo/common";
import { DaimoLinkStatus, daimoLinkBase, daimoLinkBaseV2 } from "@daimo/common";
import { useEffect, useState } from "react";
import { useAccount } from "wagmi";

Expand All @@ -28,9 +24,7 @@ export function CallToAction({
// for ephemeral notes.
const { href } = window.location;
setDirectDeepLink(
href
.replace(daimoLinkBase, "daimo:/")
.replace(daimoLinkBaseFuture, "daimo:/")
href.replace(daimoLinkBase, "daimo:/").replace(daimoLinkBaseV2, "daimo:/")
);
}, [directDeepLink]);

Expand Down
4 changes: 2 additions & 2 deletions packages/daimo-common/src/daimoLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { BigIntStr, DollarStr, zDollarStr, zHex } from "./model";
export const daimoDomain =
process.env.NEXT_PUBLIC_DOMAIN || process.env.DAIMO_DOMAIN;

export const daimoLinkBaseFuture = daimoDomain
export const daimoLinkBaseV2 = daimoDomain
? `https://${daimoDomain}/l`
: "http://localhost:3001/l";

Expand Down Expand Up @@ -154,7 +154,7 @@ function parseDaimoLinkInner(link: string): DaimoLink | null {
let suffix: string | undefined;
const prefixes = [
`${daimoLinkBase}/`,
`${daimoLinkBaseFuture}/`, // New shorter link prefix
`${daimoLinkBaseV2}/`, // New shorter link prefix
"daimo://",
"https://daimo.xyz/link/", // Backcompat with old domain
];
Expand Down

0 comments on commit 4a50f99

Please sign in to comment.