aboutsummaryrefslogtreecommitdiff
path: root/apps/raycast-extension/src/search-projects.tsx
blob: 02b423d5216b60802956a8cf822ed10238bd136f (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
import {
	ActionPanel,
	List,
	Action,
	Icon,
	Form,
	useNavigation,
} from "@raycast/api"
import { useState } from "react"
import { fetchProjects, addProject } from "./api"
import {
	FormValidation,
	showFailureToast,
	useCachedPromise,
	useForm,
} from "@raycast/utils"
import { withSupermemory } from "./withSupermemory"

export default withSupermemory(Command)

function Command() {
	const { isLoading, data: projects, mutate } = useCachedPromise(fetchProjects)

	return (
		<List isLoading={isLoading} searchBarPlaceholder="Search your projects">
			{!isLoading && !projects?.length ? (
				<List.EmptyView
					title="No Projects Found"
					actions={
						<ActionPanel>
							<Action.Push
								icon={Icon.Plus}
								title="Create Project"
								target={<CreateProject />}
								onPop={mutate}
							/>
						</ActionPanel>
					}
				/>
			) : (
				projects?.map((project) => (
					<List.Item
						key={project.id}
						icon={Icon.Folder}
						title={project.name}
						subtitle={project.description}
						accessories={[{ tag: project.containerTag }]}
						actions={
							<ActionPanel>
								<Action.Push
									icon={Icon.Plus}
									title="Create Project"
									target={<CreateProject />}
									onPop={mutate}
								/>
							</ActionPanel>
						}
					/>
				))
			)}
		</List>
	)
}

function CreateProject() {
	const { pop } = useNavigation()
	const [isLoading, setIsLoading] = useState(false)
	const { handleSubmit, itemProps } = useForm<{ name: string }>({
		async onSubmit(values) {
			setIsLoading(true)
			try {
				await addProject(values)
				pop()
			} catch (error) {
				await showFailureToast(error, { title: "Failed to add project" })
			} finally {
				setIsLoading(false)
			}
		},
		validation: {
			name: FormValidation.Required,
		},
	})
	return (
		<Form
			navigationTitle="Search Projects / Add"
			isLoading={isLoading}
			actions={
				<ActionPanel>
					<Action.SubmitForm
						icon={Icon.Plus}
						title="Create Project"
						onSubmit={handleSubmit}
					/>
				</ActionPanel>
			}
		>
			<Form.TextField
				title="Name"
				placeholder="My Awesome Project"
				info="This will help you organize your memories"
				{...itemProps.name}
			/>
		</Form>
	)
}