fix: link provider
This commit is contained in:
@@ -33,6 +33,92 @@ const UniversalLinkProvider = ({ children }) => {
|
||||
const [pendingNavigation, setPendingNavigation] = useState(null);
|
||||
const lastUrlRef = useRef(null);
|
||||
|
||||
const handleLinkRedirect = async (queryParams = {}) => {
|
||||
const schemeParam = queryParams?.scheme;
|
||||
const fallbackParam = queryParams?.fallback;
|
||||
|
||||
const scheme =
|
||||
typeof schemeParam === "string" && schemeParam.trim().length > 0
|
||||
? schemeParam.trim()
|
||||
: null;
|
||||
const fallback =
|
||||
typeof fallbackParam === "string" && fallbackParam.trim().length > 0
|
||||
? fallbackParam.trim()
|
||||
: null;
|
||||
|
||||
if (isWeb) {
|
||||
if (scheme) {
|
||||
let fallbackTimer = null;
|
||||
if (fallback) {
|
||||
fallbackTimer = window.setTimeout(() => {
|
||||
try {
|
||||
window.location.href = fallback;
|
||||
} catch (error) {
|
||||
console.error("link.redirect.fallback.error", error);
|
||||
}
|
||||
}, 1500);
|
||||
}
|
||||
|
||||
try {
|
||||
window.location.href = scheme;
|
||||
} catch (error) {
|
||||
console.error("link.redirect.scheme.error", error);
|
||||
if (fallbackTimer) window.clearTimeout(fallbackTimer);
|
||||
if (fallback) {
|
||||
try {
|
||||
window.location.href = fallback;
|
||||
} catch (fallbackError) {
|
||||
console.error("link.redirect.fallback.error", fallbackError);
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (fallback) {
|
||||
try {
|
||||
window.location.href = fallback;
|
||||
} catch (error) {
|
||||
console.error("link.redirect.fallback.error", error);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!scheme && fallback) {
|
||||
try {
|
||||
await Linking.openURL(fallback);
|
||||
} catch (error) {
|
||||
console.error("link.redirect.native.fallback.error", error);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!scheme) return;
|
||||
|
||||
try {
|
||||
const canOpen = await Linking.canOpenURL(scheme);
|
||||
if (canOpen) {
|
||||
await Linking.openURL(scheme);
|
||||
} else if (fallback) {
|
||||
await Linking.openURL(fallback);
|
||||
} else {
|
||||
await Linking.openURL(APP_STORE_FALLBACK_URL);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("link.redirect.native.scheme.error", error);
|
||||
try {
|
||||
if (fallback) {
|
||||
await Linking.openURL(fallback);
|
||||
} else {
|
||||
await Linking.openURL(APP_STORE_FALLBACK_URL);
|
||||
}
|
||||
} catch (fallbackError) {
|
||||
console.error("link.redirect.native.fallback.error", fallbackError);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
const handleDeepLink = async (event) => {
|
||||
// console.log("event", event);
|
||||
@@ -127,6 +213,11 @@ const UniversalLinkProvider = ({ children }) => {
|
||||
const normalizedPath = normalizePath(path);
|
||||
if (!normalizedPath) return;
|
||||
|
||||
if (normalizedPath.toLowerCase() === "link") {
|
||||
handleLinkRedirect(queryParams);
|
||||
return;
|
||||
}
|
||||
|
||||
const [resource, identifier] = normalizedPath.split("/").filter(Boolean);
|
||||
if (!resource || !identifier) return;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user