aboutsummaryrefslogtreecommitdiff
path: root/apps/raycast-extension/src/add-memory.tsx
blob: 812c1c4a2c6daf2b4ccd09c8959e031ed963be9a (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 {
	Form,
	ActionPanel,
	Action,
	showToast,
	Toast,
	useNavigation,
	Icon,
	getSelectedText,
} from "@raycast/api"
import { useState, useEffect } from "react"
import { addMemory, fetchProjects } from "./api"
import { usePromise } from "@raycast/utils"
import { withSupermemory } from "./withSupermemory"

interface FormValues {
	content: string
	project: string
}

export default withSupermemory(Command)
function Command() {
	const [isSubmitting, setIsSubmitting] = useState(false)
	const [initialContent, setInitialContent] = useState("")
	const { pop } = useNavigation()

	const { isLoading, data: projects = [] } = usePromise(fetchProjects)

	useEffect(() => {
		async function loadSelectedText() {
			try {
				const selectedText = await getSelectedText()
				if (selectedText) {
					setInitialContent(selectedText)
				}
			} catch {
				// No text selected or error getting selected text - silently fail
				// User can still manually enter content
			}
		}

		loadSelectedText()
	}, [])

	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={
				!isLoading && (
					<ActionPanel>
						<Action.SubmitForm
							icon={Icon.Plus}
							title="Add Memory"
							onSubmit={handleSubmit}
						/>
					</ActionPanel>
				)
			}
		>
			<Form.TextArea
				id="content"
				title="Content"
				value={initialContent}
				placeholder="Enter the memory content..."
				info="The main content of your memory. This is required."
				onChange={(value) => setInitialContent(value)}
			/>
			<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>
	)
}