hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { useNavigate } from "@tanstack/solid-router"; |
| 2 | import { For, Show, createEffect, createMemo, createSignal, onCleanup, onMount } from "solid-js"; |
| 3 | import { createResource } from "@/lib/create-resource"; |
| 4 | import { Dialog, DialogContent, DialogDescription, DialogTitle } from "@/components/ui/dialog"; |
| 5 | import { |
| 6 | IconBook, |
| 7 | IconCalendar, |
| 8 | IconDownload, |
| 9 | IconExam, |
| 10 | IconGlobe, |
| 11 | IconHomework, |
| 12 | IconLogout, |
| 13 | IconMoon, |
| 14 | IconNote, |
| 15 | IconSearch, |
| 16 | IconSparkles, |
| 17 | IconSun, |
| 18 | IconUsers, |
| 19 | IconX, |
| 20 | } from "@/components/ui/icons"; |
| 21 | import { getUserSearch } from "@/api/users"; |
| 22 | import { |
| 23 | COMMAND_RECORD_KINDS, |
| 24 | COMMAND_RECORD_MIN_QUERY, |
| 25 | COMMAND_RECORD_ROUTES, |
| 26 | filterCommandRecords, |
| 27 | loadCommandRecords, |
| 28 | type CommandRecord, |
| 29 | type CommandRecordKind, |
| 30 | } from "@/lib/command-search"; |
| 31 | import { courseKindLabel } from "@/lib/course-kind"; |
| 32 | import { formatDate, formatDateTime } from "@/lib/format"; |
| 33 | import type { MessageKey } from "@/i18n/messages"; |
| 34 | import { HOME_ITEM, visibleNavGroups } from "@/components/layout/nav-items"; |
| 35 | import { createStudentClassLabels } from "@/lib/student-classes"; |
| 36 | import { hasMinRole } from "@/lib/roles"; |
| 37 | import { useAuth } from "@/stores/auth-context"; |
| 38 | import { useModules } from "@/stores/modules-context"; |
| 39 | import { usePreferences, useT } from "@/stores/preferences-context"; |
| 40 | import { cn } from "@/lib/cn"; |
| 41 | import { matchesSearch } from "@/lib/search-text"; |
| 42 | |
| 43 | export type CommandPaletteProps = { |
| 44 | open: boolean; |
| 45 | onOpenChange: (open: boolean) => void; |
| 46 | onOpenCelebi?: () => void; |
| 47 | onOpenProfile?: () => void; |
| 48 | }; |
| 49 | |
| 50 | type CommandCategory = "people" | CommandRecordKind | "actions" | "pages" | "system"; |
| 51 | |
| 52 | const RECORD_GROUP_KEY: Record<CommandRecordKind, MessageKey> = { |
| 53 | course: "command.group.courses", |
| 54 | exam: "command.group.exams", |
| 55 | homework: "command.group.homework", |
| 56 | event: "command.group.events", |
| 57 | }; |
| 58 | |
| 59 | const RECORD_ICON: Record<CommandRecordKind, typeof IconSearch> = { |
| 60 | course: IconBook, |
| 61 | exam: IconExam, |
| 62 | homework: IconHomework, |
| 63 | event: IconCalendar, |
| 64 | }; |
| 65 | |
| 66 | type CommandItem = { |
| 67 | id: string; |
| 68 | category: CommandCategory; |
| 69 | categoryLabel: string; |
| 70 | title: string; |
| 71 | description?: string; |
| 72 | keywords?: string; |
| 73 | icon: typeof IconSearch; |
| 74 | shortcut?: string; |
| 75 | roleBadge?: string; |
| 76 | onSelect: () => void; |
| 77 | }; |
| 78 | |
| 79 | export function CommandPalette(props: CommandPaletteProps) { |
| 80 | const auth = useAuth(); |
| 81 | const prefs = usePreferences(); |
| 82 | const t = useT(); |
| 83 | const navigate = useNavigate(); |
| 84 | const modules = useModules(); |
| 85 | |
| 86 | const [query, setQuery] = createSignal(""); |
| 87 | const [selectedIndex, setSelectedIndex] = createSignal(0); |
| 88 | let inputRef: HTMLInputElement | undefined; |
| 89 | let listRef: HTMLDivElement | undefined; |
| 90 | |
| 91 | const role = () => auth.user()?.role; |
| 92 | |
| 93 | // Student lookup moved here from the dashboard hero. `/users/search` is the |
| 94 | // app's only search endpoint and the user detail page is admin-only, so the |
| 95 | // "people" section exists for admins alone. |
| 96 | const [peopleQuery, setPeopleQuery] = createSignal(""); |
| 97 | let peopleDebounce: ReturnType<typeof setTimeout> | undefined; |
| 98 | createEffect(() => { |
| 99 | const q = query().trim(); |
| 100 | clearTimeout(peopleDebounce); |
| 101 | peopleDebounce = setTimeout(() => setPeopleQuery(q.length >= 2 ? q : ""), 250); |
| 102 | }); |
| 103 | onCleanup(() => clearTimeout(peopleDebounce)); |
| 104 | const [people] = createResource( |
| 105 | () => (props.open && role() === "admin" && peopleQuery() ? peopleQuery() : null), |
| 106 | (q) => getUserSearch(q, undefined, "student", { limit: 6 }), |
| 107 | ); |
| 108 | // Two students called "Ayşe Yılmaz" are only told apart by their class, so |
| 109 | // each visible match is resolved to one (see `student-classes`). |
| 110 | const classLabels = createStudentClassLabels(() => (people.latest?.items ?? []).map((person) => person.id)); |
| 111 | |
| 112 | // Courses, exams, homework and events the user's own list pages show. Only |
| 113 | // kinds whose list page is in this user's nav are searched, read once per |
| 114 | // open (cached briefly across reopens) and filtered client-side per keystroke. |
| 115 | const recordKinds = createMemo<CommandRecordKind[]>(() => { |
| 116 | const routes = new Set( |
| 117 | visibleNavGroups(role(), modules.enabled()).flatMap((group) => group.items.map((item) => item.to)), |
| 118 | ); |
| 119 | return COMMAND_RECORD_KINDS.filter((kind) => routes.has(COMMAND_RECORD_ROUTES[kind])); |
| 120 | }); |
| 121 | const [records] = createResource( |
| 122 | () => { |
| 123 | const userRole = role(); |
| 124 | const kinds = recordKinds(); |
| 125 | return props.open && userRole && kinds.length > 0 ? { role: userRole, kinds } : null; |
| 126 | }, |
| 127 | (source) => loadCommandRecords(source.role, source.kinds), |
| 128 | ); |
| 129 | const recordDescription = (record: CommandRecord): string | undefined => { |
| 130 | const locale = prefs.locale(); |
| 131 | switch (record.kind) { |
| 132 | case "course": |
| 133 | return record.courseKind ? courseKindLabel(record.courseKind, t) : undefined; |
| 134 | case "exam": |
| 135 | return [record.context, record.at != null ? formatDateTime(record.at, locale) : null].filter(Boolean).join(" · ") || undefined; |
| 136 | case "homework": |
| 137 | return [record.context, record.at != null ? t("command.homeworkDue", { date: formatDate(record.at, locale) }) : null] |
| 138 | .filter(Boolean) |
| 139 | .join(" · ") || undefined; |
| 140 | case "event": |
| 141 | return record.at != null ? formatDateTime(record.at, locale) : undefined; |
| 142 | } |
| 143 | }; |
| 144 | const openRecord = (record: CommandRecord) => { |
| 145 | const params = { id: record.id }; |
| 146 | if (record.kind === "course") void navigate({ to: "/courses/$id", params }); |
| 147 | else if (record.kind === "exam") void navigate({ to: "/exams/$id", params }); |
| 148 | else if (record.kind === "homework") void navigate({ to: "/homework/$id", params }); |
| 149 | else void navigate({ to: "/events/$id", params }); |
| 150 | }; |
| 151 | const recordsLoading = () => |
| 152 | records.loading && query().trim().length >= COMMAND_RECORD_MIN_QUERY && records.latest === undefined; |
| 153 | |
| 154 | const items = createMemo<CommandItem[]>(() => { |
| 155 | const list: CommandItem[] = []; |
| 156 | const userRole = role(); |
| 157 | const isTeacherPlus = hasMinRole(userRole, "teacher"); |
| 158 | const isStudent = userRole === "student"; |
| 159 | |
| 160 | const mod = "Ctrl/Cmd + "; |
| 161 | |
| 162 | // 1. Quick Actions (Hızlı İşlemler) |
| 163 | const actionsCategoryLabel = t("command.group.actions"); |
| 164 | |
| 165 | if (isTeacherPlus) { |
| 166 | list.push({ |
| 167 | id: "action-create-homework", |
| 168 | category: "actions", |
| 169 | categoryLabel: actionsCategoryLabel, |
| 170 | title: t("command.action.createHomework"), |
| 171 | description: t("command.action.createHomeworkDesc"), |
| 172 | keywords: "homework odev ekle olustur ders", |
| 173 | icon: IconHomework, |
| 174 | shortcut: `${mod}H`, |
| 175 | roleBadge: t("role.teacher"), |
| 176 | onSelect: () => void navigate({ to: "/homework", search: { action: "new" } as any }), |
| 177 | }); |
| 178 | |
| 179 | list.push({ |
| 180 | id: "action-create-course", |
| 181 | category: "actions", |
| 182 | categoryLabel: actionsCategoryLabel, |
| 183 | title: t("command.action.createCourse"), |
| 184 | description: t("command.action.createCourseDesc"), |
| 185 | keywords: "course ders kurs olustur egitim", |
| 186 | icon: IconBook, |
| 187 | shortcut: `${mod}C`, |
| 188 | roleBadge: t("role.teacher"), |
| 189 | onSelect: () => void navigate({ to: "/courses", search: { action: "new" } as any }), |
| 190 | }); |
| 191 | |
| 192 | list.push({ |
| 193 | id: "action-create-event", |
| 194 | category: "actions", |
| 195 | categoryLabel: actionsCategoryLabel, |
| 196 | title: t("command.action.createEvent"), |
| 197 | description: t("command.action.createEventDesc"), |
| 198 | keywords: "event etkinlik planla takvim oturum", |
| 199 | icon: IconCalendar, |
| 200 | shortcut: `${mod}E`, |
| 201 | roleBadge: t("role.teacher"), |
| 202 | onSelect: () => void navigate({ to: "/events", search: { action: "new" } as any }), |
| 203 | }); |
| 204 | |
| 205 | list.push({ |
| 206 | id: "action-create-exam", |
| 207 | category: "actions", |
| 208 | categoryLabel: actionsCategoryLabel, |
| 209 | title: t("command.action.createExam"), |
| 210 | description: t("command.action.createExamDesc"), |
| 211 | keywords: "exam sinav test hazirla olustur", |
| 212 | icon: IconExam, |
| 213 | shortcut: `${mod}X`, |
| 214 | roleBadge: t("role.teacher"), |
| 215 | onSelect: () => void navigate({ to: "/exams", search: { action: "new" } as any }), |
| 216 | }); |
| 217 | } |
| 218 | |
| 219 | // Common Quick Actions for all roles |
| 220 | list.push({ |
| 221 | id: "action-create-note", |
| 222 | category: "actions", |
| 223 | categoryLabel: actionsCategoryLabel, |
| 224 | title: t("command.action.createNote"), |
| 225 | description: t("command.action.createNoteDesc"), |
| 226 | keywords: "note not ekle ders notu kaydet", |
| 227 | icon: IconNote, |
| 228 | shortcut: `${mod}N`, |
| 229 | onSelect: () => void navigate({ to: "/notes/new" }), |
| 230 | }); |
| 231 | |
| 232 | list.push({ |
| 233 | id: "action-import-note", |
| 234 | category: "actions", |
| 235 | categoryLabel: actionsCategoryLabel, |
| 236 | title: t("command.action.importNote"), |
| 237 | description: t("command.action.importNoteDesc"), |
| 238 | keywords: "import ice aktar pdf txt markdown assistant", |
| 239 | icon: IconDownload, |
| 240 | onSelect: () => void navigate({ to: "/notes", search: { action: "import" } as any }), |
| 241 | }); |
| 242 | |
| 243 | list.push({ |
| 244 | id: "action-ask-question", |
| 245 | category: "actions", |
| 246 | categoryLabel: actionsCategoryLabel, |
| 247 | title: t("command.action.askQuestion"), |
| 248 | description: t("command.action.askQuestionDesc"), |
| 249 | keywords: "question soru sor havuz ekle topluluk", |
| 250 | icon: IconExam, |
| 251 | shortcut: `${mod}Q`, |
| 252 | roleBadge: isStudent ? t("role.student") : undefined, |
| 253 | onSelect: () => void navigate({ to: "/questions", search: { action: "new" } as any }), |
| 254 | }); |
| 255 | |
| 256 | if (props.onOpenCelebi) { |
| 257 | list.push({ |
| 258 | id: "action-ask-celebi", |
| 259 | category: "actions", |
| 260 | categoryLabel: actionsCategoryLabel, |
| 261 | title: t("command.action.askCelebi"), |
| 262 | description: t("command.action.askCelebiDesc"), |
| 263 | keywords: "celebi ai yapay zeka sohbet asistan yardım", |
| 264 | icon: IconSparkles, |
| 265 | onSelect: () => props.onOpenCelebi?.(), |
| 266 | }); |
| 267 | } |
| 268 | |
| 269 | if (props.onOpenProfile) { |
| 270 | list.push({ |
| 271 | id: "action-my-profile", |
| 272 | category: "actions", |
| 273 | categoryLabel: actionsCategoryLabel, |
| 274 | title: t("command.action.myProfile"), |
| 275 | description: t("command.action.myProfileDesc"), |
| 276 | keywords: "profile profil hesabim kullanici sifre", |
| 277 | icon: IconUsers, |
| 278 | onSelect: () => props.onOpenProfile?.(), |
| 279 | }); |
| 280 | } |
| 281 | |
| 282 | // 2. Navigation Pages (Sayfalar & Gezinme) |
| 283 | const pagesCategoryLabel = t("command.group.navigation"); |
| 284 | |
| 285 | list.push({ |
| 286 | id: "page-home", |
| 287 | category: "pages", |
| 288 | categoryLabel: pagesCategoryLabel, |
| 289 | title: t(HOME_ITEM.labelKey), |
| 290 | description: t("app.name"), |
| 291 | keywords: "home anasayfa kontrol paneli dashboard", |
| 292 | icon: HOME_ITEM.Icon, |
| 293 | onSelect: () => void navigate({ to: HOME_ITEM.to }), |
| 294 | }); |
| 295 | |
| 296 | for (const group of visibleNavGroups(userRole, modules.enabled())) { |
| 297 | const groupName = t(group.labelKey); |
| 298 | for (const item of group.items) { |
| 299 | // Çelebi and the account-settings entries open an existing shell |
| 300 | // surface rather than a route — the palette already offers both |
| 301 | // above, wired to the panel/dialog directly. |
| 302 | if (item.action) continue; |
| 303 | list.push({ |
| 304 | id: `page-${item.to}`, |
| 305 | category: "pages", |
| 306 | categoryLabel: pagesCategoryLabel, |
| 307 | title: t(item.labelKey), |
| 308 | description: groupName, |
| 309 | keywords: `${item.to} ${t(item.labelKey)} ${groupName}`, |
| 310 | icon: item.Icon, |
| 311 | onSelect: () => void navigate({ to: item.to, search: item.search as never }), |
| 312 | }); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | // 3. System Preferences & Account (Sistem & Tercihler) |
| 317 | const systemCategoryLabel = t("command.group.system"); |
| 318 | |
| 319 | const isDark = prefs.theme() === "dark"; |
| 320 | list.push({ |
| 321 | id: "system-theme-toggle", |
| 322 | category: "system", |
| 323 | categoryLabel: systemCategoryLabel, |
| 324 | title: isDark ? t("command.action.themeLight") : t("command.action.themeDark"), |
| 325 | description: isDark ? t("command.action.themeLightDesc") : t("command.action.themeDarkDesc"), |
| 326 | keywords: "theme tema koyu acik dark light mode", |
| 327 | icon: isDark ? IconSun : IconMoon, |
| 328 | onSelect: () => prefs.setTheme(isDark ? "light" : "dark"), |
| 329 | }); |
| 330 | |
| 331 | const isTr = prefs.locale() === "tr"; |
| 332 | list.push({ |
| 333 | id: "system-lang-toggle", |
| 334 | category: "system", |
| 335 | categoryLabel: systemCategoryLabel, |
| 336 | title: isTr ? t("command.action.langEn") : t("command.action.langTr"), |
| 337 | description: isTr ? t("command.action.langEnDesc") : t("command.action.langTrDesc"), |
| 338 | keywords: "language dil turkce english tr en", |
| 339 | icon: IconGlobe, |
| 340 | onSelect: () => prefs.setLocale(isTr ? "en" : "tr"), |
| 341 | }); |
| 342 | |
| 343 | list.push({ |
| 344 | id: "system-logout", |
| 345 | category: "system", |
| 346 | categoryLabel: systemCategoryLabel, |
| 347 | title: t("command.action.logout"), |
| 348 | description: t("command.action.logoutDesc"), |
| 349 | keywords: "logout cıkıs oturum kapat ayrıl", |
| 350 | icon: IconLogout, |
| 351 | onSelect: async () => { |
| 352 | await auth.logout(); |
| 353 | void navigate({ to: "/login" }); |
| 354 | }, |
| 355 | }); |
| 356 | |
| 357 | return list; |
| 358 | }); |
| 359 | |
| 360 | const filteredItems = createMemo(() => { |
| 361 | const q = query().trim(); |
| 362 | if (!q) return items(); |
| 363 | const peopleLabel = t("command.group.people"); |
| 364 | const found: CommandItem[] = (people.latest?.items ?? []).map((person) => ({ |
| 365 | id: `person-${person.id}`, |
| 366 | category: "people", |
| 367 | categoryLabel: peopleLabel, |
| 368 | title: person.display_name || person.username, |
| 369 | description: classLabels()[person.id] ? `${classLabels()[person.id]} · ${person.username}` : person.username, |
| 370 | icon: IconUsers, |
| 371 | // Same destination as a row on the students list. |
| 372 | onSelect: () => void navigate({ to: "/profile/$userId", params: { userId: person.id } }), |
| 373 | })); |
| 374 | const matchedRecords: CommandItem[] = filterCommandRecords(records.latest ?? [], q).map((record) => ({ |
| 375 | id: `${record.kind}-${record.id}`, |
| 376 | category: record.kind, |
| 377 | categoryLabel: t(RECORD_GROUP_KEY[record.kind]), |
| 378 | title: record.title, |
| 379 | description: recordDescription(record), |
| 380 | icon: RECORD_ICON[record.kind], |
| 381 | onSelect: () => openRecord(record), |
| 382 | })); |
| 383 | return [...found, ...matchedRecords, ...items().filter((item) => |
| 384 | matchesSearch(q, item.title, item.description, item.keywords, item.categoryLabel), |
| 385 | )]; |
| 386 | }); |
| 387 | |
| 388 | // Group items by category while preserving single flat index order for keyboard navigation |
| 389 | const groupedItems = createMemo(() => { |
| 390 | const list = filteredItems(); |
| 391 | const categories: { category: CommandCategory; label: string; items: { item: CommandItem; globalIndex: number }[] }[] = []; |
| 392 | |
| 393 | const map = new Map<CommandCategory, { item: CommandItem; globalIndex: number }[]>(); |
| 394 | |
| 395 | list.forEach((item, idx) => { |
| 396 | if (!map.has(item.category)) { |
| 397 | map.set(item.category, []); |
| 398 | } |
| 399 | map.get(item.category)!.push({ item, globalIndex: idx }); |
| 400 | }); |
| 401 | |
| 402 | const order: CommandCategory[] = ["people", ...COMMAND_RECORD_KINDS, "actions", "pages", "system"]; |
| 403 | for (const cat of order) { |
| 404 | const itemsInCat = map.get(cat); |
| 405 | if (itemsInCat && itemsInCat.length > 0) { |
| 406 | categories.push({ |
| 407 | category: cat, |
| 408 | label: itemsInCat[0].item.categoryLabel, |
| 409 | items: itemsInCat, |
| 410 | }); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | return categories; |
| 415 | }); |
| 416 | |
| 417 | const executeItem = (item: CommandItem) => { |
| 418 | props.onOpenChange(false); |
| 419 | item.onSelect(); |
| 420 | }; |
| 421 | |
| 422 | createEffect(() => { |
| 423 | if (!props.open) return; |
| 424 | setQuery(""); |
| 425 | setSelectedIndex(0); |
| 426 | }); |
| 427 | |
| 428 | // Reset selected index when query changes |
| 429 | createEffect(() => { |
| 430 | query(); |
| 431 | setSelectedIndex(0); |
| 432 | }); |
| 433 | |
| 434 | // Auto scroll active item into view |
| 435 | createEffect(() => { |
| 436 | const idx = selectedIndex(); |
| 437 | if (!listRef) return; |
| 438 | const activeEl = listRef.querySelector(`[data-index="${idx}"]`); |
| 439 | if (activeEl) { |
| 440 | activeEl.scrollIntoView({ block: "nearest" }); |
| 441 | } |
| 442 | }); |
| 443 | |
| 444 | const handleKeyDown = (event: KeyboardEvent) => { |
| 445 | const list = filteredItems(); |
| 446 | if (!list.length) return; |
| 447 | |
| 448 | if (event.key === "ArrowDown") { |
| 449 | event.preventDefault(); |
| 450 | setSelectedIndex((prev) => (prev + 1) % list.length); |
| 451 | } else if (event.key === "ArrowUp") { |
| 452 | event.preventDefault(); |
| 453 | setSelectedIndex((prev) => (prev - 1 + list.length) % list.length); |
| 454 | } else if (event.key === "Enter") { |
| 455 | event.preventDefault(); |
| 456 | const current = list[selectedIndex()]; |
| 457 | if (current) executeItem(current); |
| 458 | } |
| 459 | }; |
| 460 | |
| 461 | onMount(() => { |
| 462 | const onGlobalKeyDown = (event: KeyboardEvent) => { |
| 463 | if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "k") { |
| 464 | event.preventDefault(); |
| 465 | if (auth.user()) props.onOpenChange(!props.open); |
| 466 | } |
| 467 | if (event.key === "Escape" && props.open) props.onOpenChange(false); |
| 468 | }; |
| 469 | window.addEventListener("keydown", onGlobalKeyDown); |
| 470 | onCleanup(() => window.removeEventListener("keydown", onGlobalKeyDown)); |
| 471 | }); |
| 472 | |
| 473 | return ( |
| 474 | <Dialog open={props.open} onOpenChange={props.onOpenChange}> |
| 475 | <DialogContent |
| 476 | position="top" |
| 477 | class="max-h-[min(85vh,38rem)] max-w-2xl overflow-hidden rounded-xl p-0 shadow-2xl border border-black/8 dark:border-white/12 bg-popover" |
| 478 | // The palette draws its own labelled close control: the dialog's |
| 479 | // icon-only × sat beside the clear-search × and read as the same button. |
| 480 | closeButton={false} |
| 481 | // The dialog's focus trap parks focus on the panel itself when it |
| 482 | // opens, which used to swallow a focus() queued from an effect — the |
| 483 | // palette opened with nothing focused and the first keystroke went |
| 484 | // nowhere. Taking the initial focus over hands it to the query field. |
| 485 | onOpenAutoFocus={(event) => { |
| 486 | event.preventDefault(); |
| 487 | inputRef?.focus(); |
| 488 | }} |
| 489 | > |
| 490 | <DialogTitle class="sr-only">{t("dashboard.commandCenter")}</DialogTitle> |
| 491 | <DialogDescription class="sr-only">{t("common.searchPlaceholder")}</DialogDescription> |
| 492 | |
| 493 | {/* Search Bar Header */} |
| 494 | <div class="flex items-center gap-2.5 border-b border-border/80 px-4 py-3 bg-muted/20"> |
| 495 | <IconSearch class="h-4 w-4 text-muted-foreground shrink-0" /> |
| 496 | <input |
| 497 | ref={inputRef} |
| 498 | type="text" |
| 499 | value={query()} |
| 500 | placeholder={t("common.searchPlaceholder")} |
| 501 | class="flex-1 h-9 w-full bg-transparent px-1 text-base font-medium text-foreground outline-hidden border-none shadow-none focus:outline-hidden focus:ring-0 placeholder:text-muted-foreground/60" |
| 502 | onInput={(event) => setQuery(event.currentTarget.value)} |
| 503 | onKeyDown={handleKeyDown} |
| 504 | // Combobox pattern: focus stays in the field while arrow keys move |
| 505 | // an active option that assistive tech follows. |
| 506 | role="combobox" |
| 507 | aria-label={t("common.searchPlaceholder")} |
| 508 | aria-expanded="true" |
| 509 | aria-controls="command-palette-list" |
| 510 | aria-autocomplete="list" |
| 511 | aria-activedescendant={filteredItems().length > 0 ? `command-item-${selectedIndex()}` : undefined} |
| 512 | /> |
| 513 | <Show when={query()}> |
| 514 | <button |
| 515 | type="button" |
| 516 | class="flex h-10 w-10 shrink-0 items-center justify-center rounded-md text-muted-foreground hover:bg-muted sm:h-6 sm:w-6 hover:text-foreground transition-colors" |
| 517 | onClick={() => { |
| 518 | setQuery(""); |
| 519 | inputRef?.focus(); |
| 520 | }} |
| 521 | title={t("common.clearSearch")} |
| 522 | aria-label={t("common.clearSearch")} |
| 523 | > |
| 524 | <IconX class="h-3.5 w-3.5" /> |
| 525 | </button> |
| 526 | <span role="status" class="shrink-0 whitespace-nowrap rounded-md bg-secondary px-2 py-0.5 text-xs font-semibold text-muted-foreground"> |
| 527 | {t("command.resultCount", { count: filteredItems().length })} |
| 528 | </span> |
| 529 | </Show> |
| 530 | <button |
| 531 | type="button" |
| 532 | class="ml-1 inline-flex h-10 shrink-0 items-center gap-1.5 rounded-md sm:h-7 border border-border/80 bg-background px-2 text-xs font-medium text-muted-foreground transition-colors hover:bg-muted hover:text-foreground" |
| 533 | onClick={() => props.onOpenChange(false)} |
| 534 | aria-label={t("common.close")} |
| 535 | title={t("common.close")} |
| 536 | > |
| 537 | {/* Keyboards get the key to press, touch screens the word. */} |
| 538 | <kbd class="hidden font-mono text-[11px] font-semibold sm:inline">Esc</kbd> |
| 539 | <span class="sm:hidden">{t("common.close")}</span> |
| 540 | </button> |
| 541 | </div> |
| 542 | |
| 543 | {/* What the field accepts — the palette takes names, pages and |
| 544 | actions, and an empty box says none of that on its own. */} |
| 545 | <Show when={!query()}> |
| 546 | <p class="border-b border-border/60 bg-muted/10 px-4 py-2 text-[11px] leading-snug text-muted-foreground"> |
| 547 | {role() === "admin" ? t("search.hint.commandAdmin") : t("search.hint.command")} |
| 548 | <Show when={recordKinds().length > 0}> |
| 549 | {" "} |
| 550 | {t("search.hint.commandRecords", { |
| 551 | kinds: recordKinds() |
| 552 | .map((kind) => t(RECORD_GROUP_KEY[kind]).toLocaleLowerCase(prefs.locale() === "tr" ? "tr-TR" : "en-US")) |
| 553 | .join(", "), |
| 554 | })} |
| 555 | </Show> |
| 556 | </p> |
| 557 | </Show> |
| 558 | <Show when={recordsLoading()}> |
| 559 | <p role="status" class="border-b border-border/60 px-4 py-2 text-[11px] text-muted-foreground"> |
| 560 | {t("command.recordsLoading")} |
| 561 | </p> |
| 562 | </Show> |
| 563 | |
| 564 | {/* Command Items List */} |
| 565 | <div |
| 566 | ref={listRef} |
| 567 | id="command-palette-list" |
| 568 | role="listbox" |
| 569 | aria-label={t("dashboard.commandCenter")} |
| 570 | class="max-h-[min(65vh,28rem)] overflow-y-auto p-2 space-y-4" |
| 571 | > |
| 572 | <Show |
| 573 | when={filteredItems().length > 0} |
| 574 | fallback={<p class="px-3 py-12 text-center text-sm text-muted-foreground">{t("common.noResults")}</p>} |
| 575 | > |
| 576 | <For each={groupedItems()}> |
| 577 | {(group, groupIndex) => ( |
| 578 | <div class="space-y-1" role="group" aria-labelledby={`command-group-${groupIndex()}`}> |
| 579 | <div id={`command-group-${groupIndex()}`} class="px-3 py-1 text-xs font-semibold uppercase tracking-wider text-muted-foreground/70"> |
| 580 | {group.label} |
| 581 | </div> |
| 582 | <For each={group.items}> |
| 583 | {({ item, globalIndex }) => { |
| 584 | const isSelected = () => selectedIndex() === globalIndex; |
| 585 | const ItemIcon = item.icon; |
| 586 | return ( |
| 587 | <button |
| 588 | type="button" |
| 589 | id={`command-item-${globalIndex}`} |
| 590 | role="option" |
| 591 | aria-selected={isSelected()} |
| 592 | tabIndex={-1} |
| 593 | data-index={globalIndex} |
| 594 | onClick={() => executeItem(item)} |
| 595 | onMouseEnter={() => setSelectedIndex(globalIndex)} |
| 596 | class={cn( |
| 597 | "flex w-full min-w-0 items-center gap-3 rounded-xl px-3 py-2.5 text-left outline-hidden transition-colors duration-150", |
| 598 | isSelected() |
| 599 | ? "bg-accent text-accent-foreground font-medium shadow-xs" |
| 600 | : "text-foreground hover:bg-secondary/60", |
| 601 | )} |
| 602 | > |
| 603 | <div |
| 604 | class={cn( |
| 605 | "flex h-8 w-8 shrink-0 items-center justify-center rounded-lg transition-colors", |
| 606 | isSelected() |
| 607 | ? "bg-primary text-primary-foreground" |
| 608 | : "bg-muted text-muted-foreground", |
| 609 | )} |
| 610 | > |
| 611 | <ItemIcon class="h-4 w-4" /> |
| 612 | </div> |
| 613 | |
| 614 | <div class="min-w-0 flex-1"> |
| 615 | <div class="flex items-center gap-2"> |
| 616 | <span class="truncate text-sm font-medium">{item.title}</span> |
| 617 | <Show when={item.roleBadge}> |
| 618 | <span class="rounded-full bg-secondary border border-border px-2 py-px text-[11px] font-semibold text-muted-foreground"> |
| 619 | {item.roleBadge} |
| 620 | </span> |
| 621 | </Show> |
| 622 | </div> |
| 623 | <Show when={item.description}> |
| 624 | <p class="truncate text-xs text-muted-foreground">{item.description}</p> |
| 625 | </Show> |
| 626 | </div> |
| 627 | |
| 628 | <Show when={item.shortcut}> |
| 629 | <kbd class="hidden shrink-0 rounded-md border border-border/80 bg-muted/60 px-2 py-0.5 font-mono text-[11px] font-semibold text-muted-foreground sm:inline-block"> |
| 630 | {item.shortcut} |
| 631 | </kbd> |
| 632 | </Show> |
| 633 | </button> |
| 634 | ); |
| 635 | }} |
| 636 | </For> |
| 637 | </div> |
| 638 | )} |
| 639 | </For> |
| 640 | </Show> |
| 641 | </div> |
| 642 | |
| 643 | {/* Command Palette Footer — keyboard-only guidance, so it is hidden on |
| 644 | phones where there are no arrow, enter or escape keys to press. */} |
| 645 | <div class="hidden items-center justify-between border-t border-border/60 bg-muted/30 px-4 py-2 text-xs text-muted-foreground sm:flex"> |
| 646 | <span class="truncate">{t("command.shortcutHint")}</span> |
| 647 | <div class="flex shrink-0 items-center gap-2 [&_kbd]:font-mono"> |
| 648 | <kbd class="rounded border border-border bg-background px-1.5 py-0.5 text-[11px] font-semibold">↑↓</kbd> |
| 649 | <span>{t("command.key.navigate")}</span> |
| 650 | <kbd class="rounded border border-border bg-background px-1.5 py-0.5 text-[11px] font-semibold">↵</kbd> |
| 651 | <span>{t("command.key.select")}</span> |
| 652 | <kbd class="rounded border border-border bg-background px-1.5 py-0.5 text-[11px] font-semibold">Esc</kbd> |
| 653 | <span>{t("command.key.close")}</span> |
| 654 | </div> |
| 655 | </div> |
| 656 | </DialogContent> |
| 657 | </Dialog> |
| 658 | ); |
| 659 | } |