aboutsummaryrefslogtreecommitdiff
path: root/apps/web/components/new/space-selector.tsx
blob: 68c615a570b055d6f970f0c6ab3bc2b74ce55e22 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"use client"

import { useState, useMemo } from "react"
import { cn } from "@lib/utils"
import { dmSansClassName } from "@/lib/fonts"
import { $fetch } from "@repo/lib/api"
import { DEFAULT_PROJECT_ID } from "@repo/lib/constants"
import { useQuery } from "@tanstack/react-query"
import { ChevronsLeftRight, Plus } from "lucide-react"
import type { Project } from "@repo/lib/types"
import { CreateProjectDialog } from "@/components/create-project-dialog"
import {
	DropdownMenu,
	DropdownMenuContent,
	DropdownMenuItem,
	DropdownMenuTrigger,
} from "@ui/components/dropdown-menu"

export interface SpaceSelectorProps {
	value: string
	onValueChange: (containerTag: string) => void
	variant?: "default" | "insideOut"
	showChevron?: boolean
	triggerClassName?: string
	contentClassName?: string
	showNewSpace?: boolean
}

const triggerVariants = {
	default: "px-3 py-2 rounded-md hover:bg-white/5",
	insideOut: "px-3 py-2 rounded-full bg-[#0D121A] shadow-inside-out",
}

export function SpaceSelector({
	value,
	onValueChange,
	variant = "default",
	showChevron = false,
	triggerClassName,
	contentClassName,
	showNewSpace = true,
}: SpaceSelectorProps) {
	const [isOpen, setIsOpen] = useState(false)
	const [showCreateDialog, setShowCreateDialog] = useState(false)

	const { data: projects = [], isLoading } = useQuery({
		queryKey: ["projects"],
		queryFn: async () => {
			const response = await $fetch("@get/projects")

			if (response.error) {
				throw new Error(response.error?.message || "Failed to load projects")
			}

			return response.data?.projects || []
		},
		staleTime: 30 * 1000,
	})

	const selectedProjectName = useMemo(() => {
		if (value === DEFAULT_PROJECT_ID) return "My Space"
		const found = projects.find((p: Project) => p.containerTag === value)
		return found?.name ?? value
	}, [projects, value])

	const handleSelect = (containerTag: string) => {
		onValueChange(containerTag)
		setIsOpen(false)
	}

	const handleNewSpace = () => {
		setIsOpen(false)
		setShowCreateDialog(true)
	}

	return (
		<>
			<DropdownMenu open={isOpen} onOpenChange={setIsOpen}>
				<DropdownMenuTrigger asChild>
					<button
						type="button"
						className={cn(
							"flex items-center gap-2 cursor-pointer transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/50",
							triggerVariants[variant],
							dmSansClassName(),
							triggerClassName,
						)}
					>
						<span className="text-sm font-bold tracking-[-0.98px]">📁</span>
						<span className="text-sm font-medium text-white">
							{isLoading ? "..." : selectedProjectName}
						</span>
						{showChevron && (
							<ChevronsLeftRight className="size-4 rotate-90 text-white/70" />
						)}
					</button>
				</DropdownMenuTrigger>
				<DropdownMenuContent
					align="start"
					className={cn(
						"min-w-[200px] p-3 rounded-[11px] border border-[#2E3033] shadow-[0px_1.5px_20px_0px_rgba(0,0,0,0.65)]",
						dmSansClassName(),
						contentClassName,
					)}
					style={{
						background: "linear-gradient(180deg, #0A0E14 0%, #05070A 100%)",
					}}
				>
					<div className="flex flex-col gap-3">
						<div className="flex flex-col">
							{/* Default Project */}
							<DropdownMenuItem
								onClick={() => handleSelect(DEFAULT_PROJECT_ID)}
								className={cn(
									"flex items-center gap-2 px-3 py-2 rounded-md cursor-pointer text-white text-sm font-medium",
									value === DEFAULT_PROJECT_ID
										? "bg-[#161E2B] border border-[rgba(115,115,115,0.1)]"
										: "opacity-50 hover:opacity-100 hover:bg-[#161E2B]/50",
								)}
							>
								<span className="font-bold tracking-[-0.98px]">📁</span>
								<span>My Space</span>
							</DropdownMenuItem>

							{/* User Projects */}
							{projects
								.filter((p: Project) => p.containerTag !== DEFAULT_PROJECT_ID)
								.map((project: Project) => (
									<DropdownMenuItem
										key={project.id}
										onClick={() => handleSelect(project.containerTag)}
										className={cn(
											"flex items-center gap-2 px-3 py-2 rounded-md cursor-pointer text-white text-sm font-medium",
											value === project.containerTag
												? "bg-[#161E2B] border border-[rgba(115,115,115,0.1)]"
												: "opacity-50 hover:opacity-100 hover:bg-[#161E2B]/50",
										)}
									>
										<span className="font-bold tracking-[-0.98px]">📁</span>
										<span className="truncate">{project.name}</span>
									</DropdownMenuItem>
								))}
						</div>

						{showNewSpace && (
							<button
								type="button"
								onClick={handleNewSpace}
								className="flex items-center justify-center gap-2 px-3 py-2 rounded-md cursor-pointer text-white text-sm font-medium border border-[#161F2C] hover:bg-[#0D121A]/80 transition-colors"
								style={{
									background:
										"linear-gradient(180deg, #0D121A 0%, #000000 100%)",
								}}
							>
								<Plus className="size-4" />
								<span>New Space</span>
							</button>
						)}
					</div>
				</DropdownMenuContent>
			</DropdownMenu>

			<CreateProjectDialog
				open={showCreateDialog}
				onOpenChange={setShowCreateDialog}
			/>
		</>
	)
}