|
| 1 | +import path from "path" |
| 2 | +import { createMemo } from "solid-js" |
| 3 | +import { DialogSelect } from "../ui/dialog-select" |
| 4 | +import { useDialog } from "../ui/dialog" |
| 5 | +import { useData } from "../context/data" |
| 6 | +import { useRoute } from "../context/route" |
| 7 | +import { abbreviateHome } from "../runtime" |
| 8 | +import { useTuiPaths } from "../context/runtime" |
| 9 | +import { useLocation } from "../context/location" |
| 10 | +import { useToast } from "../ui/toast" |
| 11 | + |
| 12 | +export function DialogProject() { |
| 13 | + const dialog = useDialog() |
| 14 | + const data = useData() |
| 15 | + const route = useRoute() |
| 16 | + const paths = useTuiPaths() |
| 17 | + const location = useLocation() |
| 18 | + const toast = useToast() |
| 19 | + |
| 20 | + data.project.invalidate() |
| 21 | + void data.project.sync().catch(toast.error) |
| 22 | + |
| 23 | + const current = () => location.current?.project |
| 24 | + |
| 25 | + const options = createMemo(() => { |
| 26 | + const seen = new Set<string>() |
| 27 | + return data.project |
| 28 | + .list() |
| 29 | + .filter((project) => { |
| 30 | + if (project.canonical === "/" || seen.has(project.canonical)) return false |
| 31 | + seen.add(project.canonical) |
| 32 | + return true |
| 33 | + }) |
| 34 | + .toSorted((a, b) => { |
| 35 | + if (a.id === current()?.id) return -1 |
| 36 | + if (b.id === current()?.id) return 1 |
| 37 | + return 0 |
| 38 | + }) |
| 39 | + .map((project) => ({ |
| 40 | + title: project.name ?? path.basename(project.canonical), |
| 41 | + description: abbreviateHome(project.canonical, paths.home), |
| 42 | + value: project.canonical, |
| 43 | + category: project.id === current()?.id ? "Current" : "Projects", |
| 44 | + })) |
| 45 | + }) |
| 46 | + |
| 47 | + return ( |
| 48 | + <DialogSelect |
| 49 | + title="Switch project" |
| 50 | + placeholder="Search projects…" |
| 51 | + options={options()} |
| 52 | + current={current()?.canonical} |
| 53 | + emptyView={ |
| 54 | + <box paddingLeft={4} paddingRight={4} paddingTop={1}> |
| 55 | + <text>No projects found</text> |
| 56 | + </box> |
| 57 | + } |
| 58 | + onSelect={(option) => { |
| 59 | + dialog.clear() |
| 60 | + if (option.value === current()?.canonical) return |
| 61 | + const target = { directory: option.value } |
| 62 | + route.navigate({ type: "home", location: target }) |
| 63 | + location.set(target) |
| 64 | + }} |
| 65 | + /> |
| 66 | + ) |
| 67 | +} |
0 commit comments