chore(repo): initialize planarchy workspace
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useRef, useEffect, useMemo } from "react";
|
||||
import { trpc } from "~/lib/trpc/client.js";
|
||||
import { useDebounce } from "~/hooks/useDebounce.js";
|
||||
import type { ProjectStatus } from "@planarchy/shared";
|
||||
|
||||
interface ProjectComboboxProps {
|
||||
value: string | null;
|
||||
onChange: (id: string | null) => void;
|
||||
placeholder?: string;
|
||||
disabled?: boolean;
|
||||
status?: ProjectStatus;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export function ProjectCombobox({
|
||||
value,
|
||||
onChange,
|
||||
placeholder = "Search project…",
|
||||
disabled = false,
|
||||
status,
|
||||
className = "",
|
||||
}: ProjectComboboxProps) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [search, setSearch] = useState("");
|
||||
const debouncedSearch = useDebounce(search, 300);
|
||||
const containerRef = useRef<HTMLDivElement>(null);
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
const { data } = trpc.project.list.useQuery(
|
||||
{ search: debouncedSearch || undefined, limit: 15, ...(status ? { status } : {}) },
|
||||
{ enabled: open, staleTime: 30_000 },
|
||||
);
|
||||
|
||||
const projects = data?.projects ?? [];
|
||||
|
||||
const { data: allData } = trpc.project.list.useQuery(
|
||||
{ limit: 500 },
|
||||
{ enabled: !!value && !open, staleTime: 60_000 },
|
||||
);
|
||||
|
||||
const selectedLabel = useMemo(() => {
|
||||
if (!value) return "";
|
||||
const fromOpen = projects.find((p) => p.id === value);
|
||||
if (fromOpen) return `${fromOpen.shortCode} — ${fromOpen.name}`;
|
||||
const fromAll = allData?.projects.find((p) => p.id === value);
|
||||
if (fromAll) return `${fromAll.shortCode} — ${fromAll.name}`;
|
||||
return value;
|
||||
}, [value, projects, allData]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return;
|
||||
function handleClick(e: MouseEvent) {
|
||||
if (containerRef.current && !containerRef.current.contains(e.target as Node)) {
|
||||
setOpen(false);
|
||||
setSearch("");
|
||||
}
|
||||
}
|
||||
document.addEventListener("mousedown", handleClick);
|
||||
return () => document.removeEventListener("mousedown", handleClick);
|
||||
}, [open]);
|
||||
|
||||
function handleOpen() {
|
||||
if (disabled) return;
|
||||
setOpen(true);
|
||||
setSearch("");
|
||||
setTimeout(() => inputRef.current?.focus(), 0);
|
||||
}
|
||||
|
||||
function select(id: string | null) {
|
||||
onChange(id);
|
||||
setOpen(false);
|
||||
setSearch("");
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={`relative ${className}`} ref={containerRef}>
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleOpen}
|
||||
disabled={disabled}
|
||||
className={`w-full text-left px-3 py-2 border border-gray-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-brand-500 bg-white disabled:opacity-50 disabled:cursor-not-allowed ${
|
||||
open ? "border-brand-500 ring-2 ring-brand-500" : "hover:border-gray-400"
|
||||
}`}
|
||||
>
|
||||
<span className={selectedLabel ? "text-gray-900" : "text-gray-400"}>
|
||||
{selectedLabel || placeholder}
|
||||
</span>
|
||||
{value && !disabled && (
|
||||
<span
|
||||
role="button"
|
||||
tabIndex={0}
|
||||
onMouseDown={(e) => { e.stopPropagation(); select(null); }}
|
||||
onKeyDown={(e) => { if (e.key === "Enter") select(null); }}
|
||||
className="absolute right-2 top-1/2 -translate-y-1/2 text-gray-400 hover:text-gray-600 text-lg leading-none"
|
||||
aria-label="Clear"
|
||||
>
|
||||
×
|
||||
</span>
|
||||
)}
|
||||
</button>
|
||||
|
||||
{open && (
|
||||
<div className="absolute left-0 right-0 top-full mt-1 z-50 bg-white border border-gray-200 rounded-xl shadow-xl overflow-hidden">
|
||||
<div className="p-2 border-b border-gray-100">
|
||||
<input
|
||||
ref={inputRef}
|
||||
type="text"
|
||||
value={search}
|
||||
onChange={(e) => setSearch(e.target.value)}
|
||||
placeholder="Type to search…"
|
||||
className="w-full px-2 py-1 text-sm border-0 outline-none"
|
||||
/>
|
||||
</div>
|
||||
<ul className="max-h-52 overflow-y-auto py-1">
|
||||
{projects.length === 0 ? (
|
||||
<li className="px-3 py-2 text-sm text-gray-400">No results</li>
|
||||
) : (
|
||||
projects.map((p) => (
|
||||
<li key={p.id}>
|
||||
<button
|
||||
type="button"
|
||||
onMouseDown={() => select(p.id)}
|
||||
className={`w-full text-left px-3 py-1.5 text-sm hover:bg-brand-50 ${
|
||||
p.id === value ? "bg-brand-50 text-brand-700 font-medium" : "text-gray-700"
|
||||
}`}
|
||||
>
|
||||
<span className="font-medium text-xs text-gray-400 mr-1.5">{p.shortCode}</span>
|
||||
<span>{p.name}</span>
|
||||
</button>
|
||||
</li>
|
||||
))
|
||||
)}
|
||||
</ul>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user