From 61e852d963fb04b13b7bda39830e208a648b3545 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Wed, 13 Mar 2024 16:51:42 +0000 Subject: [PATCH] fix(mobile): Multiple attempts to stop the duplication sharing --- packages/mobile/app/_layout.tsx | 10 ++++++++-- packages/mobile/app/sharing.tsx | 8 +++++++- packages/mobile/lib/last-shared-intent.ts | 15 +++++++++++++++ packages/mobile/package.json | 3 ++- pnpm-lock.yaml | 3 +++ 5 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 packages/mobile/lib/last-shared-intent.ts diff --git a/packages/mobile/app/_layout.tsx b/packages/mobile/app/_layout.tsx index 561e911e..c4298ec4 100644 --- a/packages/mobile/app/_layout.tsx +++ b/packages/mobile/app/_layout.tsx @@ -9,16 +9,22 @@ import { useEffect } from "react"; import { View } from "react-native"; import { Providers } from "@/lib/providers"; +import { useLastSharedIntent } from "@/lib/last-shared-intent"; export default function RootLayout() { const router = useRouter(); const { hasShareIntent, shareIntent, resetShareIntent } = useShareIntent(); + const lastSharedIntent = useLastSharedIntent(); + useEffect(() => { - if (hasShareIntent) { + const intentJson = JSON.stringify(shareIntent); + if (hasShareIntent && !lastSharedIntent.isPreviouslyShared(intentJson)) { + // TODO: Remove once https://github.com/achorein/expo-share-intent/issues/14 is fixed + lastSharedIntent.setIntent(intentJson); router.replace({ pathname: "sharing", - params: { shareIntent: JSON.stringify(shareIntent) }, + params: { shareIntent: intentJson }, }); resetShareIntent(); } diff --git a/packages/mobile/app/sharing.tsx b/packages/mobile/app/sharing.tsx index 59c6fc25..64bbd933 100644 --- a/packages/mobile/app/sharing.tsx +++ b/packages/mobile/app/sharing.tsx @@ -1,5 +1,5 @@ import { Link, useLocalSearchParams, useRouter } from "expo-router"; -import { ShareIntent } from "expo-share-intent"; +import { ShareIntent, useShareIntent } from "expo-share-intent"; import { useEffect, useMemo, useState } from "react"; import { View, Text } from "react-native"; import { z } from "zod"; @@ -12,6 +12,9 @@ type Mode = | { type: "error" }; function SaveBookmark({ setMode }: { setMode: (mode: Mode) => void }) { + // Desperate attempt to fix sharing duplication + const { hasShareIntent, resetShareIntent } = useShareIntent(); + const params = useLocalSearchParams(); const shareIntent = useMemo(() => { @@ -36,6 +39,9 @@ function SaveBookmark({ setMode }: { setMode: (mode: Mode) => void }) { mutate({ type: "text", text: shareIntent.text }); } } + if (hasShareIntent) { + resetShareIntent(); + } }, []); const { mutate, isPending } = api.bookmarks.createBookmark.useMutation({ diff --git a/packages/mobile/lib/last-shared-intent.ts b/packages/mobile/lib/last-shared-intent.ts new file mode 100644 index 00000000..951bcf74 --- /dev/null +++ b/packages/mobile/lib/last-shared-intent.ts @@ -0,0 +1,15 @@ +import { create } from "zustand"; + +interface LastSharedIntent { + lastIntent: string; + setIntent: (intent: string) => void; + isPreviouslyShared: (intent: string) => boolean; +} + +export const useLastSharedIntent = create((set, get) => ({ + lastIntent: "", + setIntent: (intent: string) => set({ lastIntent: intent }), + isPreviouslyShared: (intent: string) => { + return get().lastIntent === intent; + }, +})); diff --git a/packages/mobile/package.json b/packages/mobile/package.json index 9a9198c7..1298b8db 100644 --- a/packages/mobile/package.json +++ b/packages/mobile/package.json @@ -36,7 +36,8 @@ "react-native-svg": "^15.1.0", "tailwind-merge": "^2.2.1", "use-debounce": "^10.0.0", - "zod": "^3.22.4" + "zod": "^3.22.4", + "zustand": "^4.5.1" }, "devDependencies": { "@babel/core": "^7.20.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 835ad5f5..3b80328c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -274,6 +274,9 @@ importers: zod: specifier: ^3.22.4 version: 3.22.4 + zustand: + specifier: ^4.5.1 + version: 4.5.1(@types/react@18.2.58)(react@18.2.0) devDependencies: '@babel/core': specifier: ^7.20.0