Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

3.5 KB
Raw Blame
import React, { useEffect, useState } from "react";
import {
  View,
  Text,
  ActivityIndicator,
  Alert,
  TouchableOpacity,
} from "react-native";
import { Redirect, useRouter } from "expo-router";
import useAuthStore from "@/store/auth";
import useDataStore from "@/store/data";
import { Check } from "lucide-react-native";
import { useAddLink } from "@linkwarden/router/links";
import { rawTheme, ThemeName } from "@/lib/colors";
import { useColorScheme } from "nativewind";
import { SheetManager } from "react-native-actions-sheet";
import { LinkIncludingShortenedCollectionAndTags } from "@linkwarden/types/global";
import { SafeAreaView } from "react-native-safe-area-context";

let pendingSubmission: { url: string; promise: Promise<any> } | null = null;

export default function IncomingScreen() {
  const { auth } = useAuthStore();
  const router = useRouter();
  const { data, updateData } = useDataStore();
  const addLink = useAddLink({ auth });
  const { colorScheme } = useColorScheme();
  const [showSuccess, setShowSuccess] = useState(false);
  const [link, setLink] = useState<LinkIncludingShortenedCollectionAndTags>();

  useEffect(() => {
    const url = data.shareIntent.url;

    if (!url) {
      pendingSubmission = null;
      return;
    }

    if (auth.status !== "authenticated") return;

    if (pendingSubmission?.url !== url) {
      pendingSubmission = {
        url,
        promise: addLink.mutateAsync({
          url,
          collection: { id: data.preferredCollection?.id },
        }),
      };
    }

    let ignore = false;

    pendingSubmission.promise.then(
      (e) => {
        if (ignore) return;

        setLink(e as unknown as LinkIncludingShortenedCollectionAndTags);
        setShowSuccess(true);
        setTimeout(() => {
          pendingSubmission = null;
          updateData({
            shareIntent: {
              hasShareIntent: false,
              url: "",
            },
          });
          router.replace("/dashboard");
        }, 1500);
      },
      (error) => {
        pendingSubmission = null;

        if (ignore) return;

        Alert.alert("Error", "There was an error adding the link.");
        console.error("Error adding link:", error);
      }
    );

    return () => {
      ignore = true;
    };
  }, [auth.status, data.shareIntent.url]);

  if (auth.status === "unauthenticated") return <Redirect href="/" />;

  return (
    <SafeAreaView className="flex-1 bg-base-100">
      <View className="flex-1 items-center justify-center">
        {data?.shareIntent.url && showSuccess && link ? (
          <>
            <Check
              size={140}
              className="mb-3 text-base-content"
              color={rawTheme[colorScheme as ThemeName].primary}
            />
            <Text className="text-2xl font-semibold text-base-content">
              Link Saved!
            </Text>
            <TouchableOpacity
              className="w-fit mx-auto mt-5"
              onPress={() =>
                SheetManager.show("edit-link-sheet", {
                  payload: {
                    link: link,
                  },
                })
              }
            >
              <Text className="text-neutral text-center w-fit">Edit Link</Text>
            </TouchableOpacity>
          </>
        ) : (
          <>
            <ActivityIndicator size="large" />
            <Text className="mt-3 text-base text-base-content opacity-70">
              One sec…
            </Text>
          </>
        )}
      </View>
    </SafeAreaView>
  );
}