Skip to content

Commit

Permalink
fix(mobile): Multiple attempts to stop the duplication sharing
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedBassem committed Mar 13, 2024
1 parent a80869c commit 61e852d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
10 changes: 8 additions & 2 deletions packages/mobile/app/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
8 changes: 7 additions & 1 deletion packages/mobile/app/sharing.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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(() => {
Expand All @@ -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({
Expand Down
15 changes: 15 additions & 0 deletions packages/mobile/lib/last-shared-intent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { create } from "zustand";

interface LastSharedIntent {
lastIntent: string;
setIntent: (intent: string) => void;
isPreviouslyShared: (intent: string) => boolean;
}

export const useLastSharedIntent = create<LastSharedIntent>((set, get) => ({
lastIntent: "",
setIntent: (intent: string) => set({ lastIntent: intent }),
isPreviouslyShared: (intent: string) => {
return get().lastIntent === intent;
},
}));
3 changes: 2 additions & 1 deletion packages/mobile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 61e852d

Please sign in to comment.