Skip to content

Commit

Permalink
fix: improve linking
Browse files Browse the repository at this point in the history
  • Loading branch information
reneaaron committed Sep 17, 2024
1 parent 4f05700 commit 45138f9
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 19 deletions.
31 changes: 16 additions & 15 deletions hooks/useHandleLinking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,34 @@ import * as Linking from "expo-linking";
import { router, useRootNavigationState } from "expo-router";
import React from "react";

const SUPPORTED_SCHEMES = ["lightning:", "bitcoin:", "alby:"];
const SUPPORTED_SCHEMES = ["lightning:", "bitcoin:", "alby:", "exp:"];

export function useHandleLinking() {
const rootNavigationState = useRootNavigationState();
let url = Linking.useURL();
let hasNavigationState = !!rootNavigationState?.key;
const url = Linking.useURL();
const hasNavigationState = !!rootNavigationState?.key;

React.useEffect(() => {
if (!hasNavigationState) {
if (!hasNavigationState || !url) {
return;
}
console.log("Received linking URL", url);

console.log("useHandleLinking", url);

for (const scheme of SUPPORTED_SCHEMES) {
if (url?.startsWith(scheme)) {
console.log("Linking URL matched scheme", url, scheme);
if (url.startsWith(scheme + "//")) {
url = url.replace(scheme + "//", scheme);
}
if (url.startsWith(scheme)) {
let currentUrl = url.startsWith(scheme + "//")
? url.replace(scheme + "//", scheme)
: url;
currentUrl = currentUrl.replace("exp:127.0.0.1:8081/--/", "lightning:");

console.log("navigating to send screen", currentUrl);

// TODO: it should not always navigate to send,
// but that's the only linking functionality supported right now
router.dismissAll();
router.navigate({
// Instead of dismissing all screens, we'll use replace to avoid navigation stack issues
router.replace({
pathname: "/send",
params: {
url,
url: currentUrl,
},
});
break;
Expand Down
3 changes: 2 additions & 1 deletion pages/Wildcard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export function Wildcard() {
}}
/>
<Loading />
<Text>Loading {pathname}</Text>
<Text>Loading</Text>
<Text className="text-muted-foreground">{pathname}</Text>
</View>
);
}
2 changes: 1 addition & 1 deletion pages/send/LNURLPay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export function LNURLPay() {
To
</Text>
<Text className="text-center text-foreground text-2xl font-medium2">
{originalText}
{originalText.toLowerCase().replace("lightning:", "")}
</Text>
</View>
</View>
Expand Down
4 changes: 2 additions & 2 deletions pages/send/PaymentSuccess.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function PaymentSuccess() {
<Screen
title="Success"
/>
<View className="flex-1 justify-center items-center gap-8">
<View className="flex-1 justify-center items-center gap-8 p-6">
<Paid />
<View className="flex flex-col items-center gap-2 -mt-24">
<View className="flex flex-row items-end justify-center">
Expand All @@ -36,7 +36,7 @@ export function PaymentSuccess() {
Sent to
</Text>
<Text className="text-foreground text-center text-2xl font-medium2">
{originalText}
{originalText.toLowerCase().replace("lightning:", "")}
</Text>
</View>
}
Expand Down
2 changes: 2 additions & 0 deletions pages/send/Send.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export function Send() {
loadPayment(keyboardText);
}

// TODO: Add a property for the human readable version of the url
// and use it across different send / receive screens (e.g. without "lightning:")
async function loadPayment(text: string) {
if (!text) {
errorToast(new Error("Your clipboard is empty."));
Expand Down

0 comments on commit 45138f9

Please sign in to comment.