aboutsummaryrefslogtreecommitdiff
path: root/apps/raycast-extension/src/add-memory.tsx
blob: 87fb853f5cafd8a24786b90f9124b28c77885224 (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
import {
  Form,
  ActionPanel,
  Action,
  showToast,
  Toast,
  useNavigation,
} from "@raycast/api";
import { useEffect, useState } from "react";
import {
  addMemory,
  fetchProjects,
  checkApiConnection,
  type Project,
} from "./api";

interface FormValues {
  content: string;
  project: string;
}

export default function Command() {
  const [projects, setProjects] = useState<Project[]>([]);
  const [isLoading, setIsLoading] = useState(true);
  const [isSubmitting, setIsSubmitting] = useState(false);
  const { pop } = useNavigation();

  useEffect(() => {
    async function loadProjects() {
      try {
        setIsLoading(true);
        const isConnected = await checkApiConnection();
        if (!isConnected) {
          return;
        }

        const fetchedProjects = await fetchProjects();
        setProjects(fetchedProjects);
      } catch (error) {
        console.error("Failed to load projects:", error);
      } finally {
        setIsLoading(false);
      }
    }

    loadProjects();
  }, []);

  async function handleSubmit(values: FormValues) {
    if (!values.content.trim()) {
      await showToast({
        style: Toast.Style.Failure,
        title: "Content Required",
        message: "Please enter some content for the memory",
      });
      return;
    }

    try {
      setIsSubmitting(true);

      const containerTags = values.project ? [values.project] : undefined;

      await addMemory({
        content: values.content.trim(),
        containerTags,
      });

      pop();
    } catch (error) {
      console.error("Failed to add memory:", error);
    } finally {
      setIsSubmitting(false);
    }
  }

  return (
    <Form
      isLoading={isLoading || isSubmitting}
      actions={
        <ActionPanel>
          <Action.SubmitForm title="Add Memory" onSubmit={handleSubmit} />
        </ActionPanel>
      }
    >
      <Form.TextArea
        id="content"
        title="Content"
        placeholder="Enter the memory content..."
        info="The main content of your memory. This is required."
      />
      <Form.Separator />
      <Form.Dropdown
        id="project"
        title="Project"
        info="Select a project to organize this memory"
        storeValue
      >
        <Form.Dropdown.Item value="" title="No Project" />
        {projects.map((project) => (
          <Form.Dropdown.Item
            key={project.id}
            value={project.containerTag}
            title={project.name}
          />
        ))}
      </Form.Dropdown>
    </Form>
  );
}