aboutsummaryrefslogtreecommitdiffstats
path: root/apps/mobile/app/dashboard/add-note.tsx
blob: 8aa319364ad595abc9273606bd691d213ee3e9a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { useEffect, useState } from "react";
import { Text, View } from "react-native";
import { Stack, useLocalSearchParams, useRouter } from "expo-router";
import { Button } from "@/components/ui/Button";
import { Input } from "@/components/ui/Input";
import { api } from "@/lib/trpc";

import {
  useCreateBookmark,
  useUpdateBookmarkText,
} from "@hoarder/shared-react/hooks/bookmarks";
import { BookmarkTypes } from "@hoarder/shared/types/bookmarks";

export default function AddNote() {
  const { bookmarkId } = useLocalSearchParams();
  if (bookmarkId && typeof bookmarkId !== "string") {
    throw new Error("Unexpected param type");
  }

  const isEditing = !!bookmarkId;

  const [text, setText] = useState("");
  const [error, setError] = useState<string | undefined>();
  const router = useRouter();

  const { data: bookmark } = api.bookmarks.getBookmark.useQuery(
    { bookmarkId: bookmarkId! },
    {
      enabled: !!bookmarkId,
    },
  );

  useEffect(() => {
    if (bookmark) {
      if (bookmark.content.type !== BookmarkTypes.TEXT) {
        throw new Error("Wrong content type rendered");
      }
      setText(bookmark.content.text);
    }
  }, [bookmark]);

  const onSuccess = () => {
    if (router.canGoBack()) {
      router.replace("./");
    } else {
      router.replace("dashboard");
    }
  };

  const { mutate: createBookmark } = useCreateBookmark({
    onSuccess,
    onError: (e) => {
      let message;
      if (e.data?.zodError) {
        const zodError = e.data.zodError;
        message = JSON.stringify(zodError);
      } else {
        message = `Something went wrong: ${e.message}`;
      }
      setError(message);
    },
  });

  const { mutate: updateBookmark } = useUpdateBookmarkText({
    onSuccess,
    onError: (e) => {
      let message;
      if (e.data?.zodError) {
        const zodError = e.data.zodError;
        message = JSON.stringify(zodError);
      } else {
        message = `Something went wrong: ${e.message}`;
      }
      setError(message);
    },
  });

  const mutate = (text: string) => {
    if (isEditing) {
      updateBookmark({
        bookmarkId,
        text,
      });
    } else {
      createBookmark({ type: BookmarkTypes.TEXT, text });
    }
  };

  return (
    <View className="flex gap-2 p-4">
      <Stack.Screen
        options={{
          title: isEditing ? "Edit Note" : "Add Note",
        }}
      />
      {error && (
        <Text className="w-full text-center text-red-500">{error}</Text>
      )}
      <Input
        value={text}
        onChangeText={setText}
        multiline
        numberOfLines={8}
        placeholder="What's on your mind?"
        autoFocus
        textAlignVertical="top"
      />
      <Button
        onPress={() => mutate(text)}
        label={isEditing ? "Save" : "Add Note"}
      />
    </View>
  );
}