Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(mobile): allow repeated incoming shares

tianma-if4793004e20c1
diff --git a/apps/mobile/app/index.tsx b/apps/mobile/app/index.tsx
index 10cc15de..e9792115 100644
--- a/apps/mobile/app/index.tsx
+++ b/apps/mobile/app/index.tsx
@@ -1,10 +1,11 @@
import * as SplashScreen from "expo-splash-screen";
import { useIncomingShare } from "expo-sharing";
import { useIsRestoring } from "@tanstack/react-query";
-import { lazy, Suspense, useEffect } from "react";
+import { lazy, Suspense, useCallback, useEffect } from "react";
import { ActivityIndicator, StyleSheet, View } from "react-native";
import { Text } from "../src/components/LocalizedText";
import { LoginScreen } from "../src/screens/LoginScreen";
+import { clearAndRefreshIncomingShare } from "../src/lib/incoming-share-lifecycle";
import { useSession } from "../src/lib/session";
import { markStartup } from "../src/lib/startup-performance";
import { resolveMobileThemeStyles, useMobileTheme } from "../src/lib/mobile-theme";
@@ -21,6 +22,7 @@ export default function IndexScreen() {
error: incomingShareError,
isResolving: isResolvingIncomingShare,
resolvedSharedPayloads,
+ refreshSharePayloads,
sharedPayloads,
} = useIncomingShare();
const incomingSharePayloads = resolvedSharedPayloads.length > 0
@@ -29,6 +31,9 @@ export default function IndexScreen() {
const isWaitingForBinaryShareResolution = !incomingShareError
&& resolvedSharedPayloads.length === 0
&& sharedPayloads.some((payload) => payload.shareType !== "text" && payload.shareType !== "url");
+ const handleIncomingShareHandled = useCallback(() => {
+ void clearAndRefreshIncomingShare(clearSharedPayloads, refreshSharePayloads);
+ }, [clearSharedPayloads, refreshSharePayloads]);
useEffect(() => {
if (!isLoading && !isRestoringCache) {
@@ -47,7 +52,7 @@ export default function IndexScreen() {
incomingShareError={incomingShareError}
incomingShareIsResolving={isResolvingIncomingShare || isWaitingForBinaryShareResolution}
incomingSharePayloads={incomingSharePayloads}
- onIncomingShareHandled={clearSharedPayloads}
+ onIncomingShareHandled={handleIncomingShareHandled}
/>
</Suspense>
) : (
diff --git a/apps/mobile/src/lib/incoming-share-lifecycle.test.ts b/apps/mobile/src/lib/incoming-share-lifecycle.test.ts
new file mode 100644
index 00000000..92bfdc70
--- /dev/null
+++ b/apps/mobile/src/lib/incoming-share-lifecycle.test.ts
@@ -0,0 +1,31 @@
+import { describe, expect, test } from "bun:test";
+import { clearAndRefreshIncomingShare } from "./incoming-share-lifecycle";
+
+describe("incoming share lifecycle", () => {
+ test("resets the observed payload so the same share can be received again", async () => {
+ const articleUrl = "https://mp.weixin.qq.com/s/repeated-article";
+ let nativePayload: string | null = articleUrl;
+ let observedPayload: string | null = articleUrl;
+ const calls: string[] = [];
+
+ await clearAndRefreshIncomingShare(
+ () => {
+ calls.push("clear");
+ nativePayload = null;
+ },
+ async () => {
+ calls.push("refresh");
+ observedPayload = nativePayload;
+ },
+ );
+
+ expect(calls).toEqual(["clear", "refresh"]);
+ expect(observedPayload).toBeNull();
+
+ nativePayload = articleUrl;
+ if (nativePayload !== observedPayload) {
+ observedPayload = nativePayload;
+ }
+ expect(observedPayload).toBe(articleUrl);
+ });
+});
diff --git a/apps/mobile/src/lib/incoming-share-lifecycle.ts b/apps/mobile/src/lib/incoming-share-lifecycle.ts
new file mode 100644
index 00000000..4c858e51
--- /dev/null
+++ b/apps/mobile/src/lib/incoming-share-lifecycle.ts
@@ -0,0 +1,10 @@
+export type ClearIncomingSharePayloads = () => void;
+export type RefreshIncomingSharePayloads = () => void | Promise<void>;
+
+export const clearAndRefreshIncomingShare = (
+ clearSharedPayloads: ClearIncomingSharePayloads,
+ refreshSharePayloads: RefreshIncomingSharePayloads,
+) => {
+ clearSharedPayloads();
+ return refreshSharePayloads();
+};