160 lines
5.7 KiB
Vue
160 lines
5.7 KiB
Vue
<script setup lang="ts">
|
|
import { computed, onMounted, onUnmounted, ref, watch } from "vue";
|
|
import { useRouter } from "vue-router";
|
|
import { Search } from "@lucide/vue";
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
import {
|
|
CommandDialog,
|
|
CommandEmpty,
|
|
CommandGroup,
|
|
CommandInput,
|
|
CommandItem,
|
|
CommandList,
|
|
} from "@/components/ui/command";
|
|
import { Kbd } from "@/components/ui/kbd";
|
|
import { api } from "@/lib/api";
|
|
import { errors } from "@/copy/errors";
|
|
import { navEntries } from "@/lib/nav";
|
|
import { statusLabel } from "@/lib/task-meta";
|
|
|
|
/**
|
|
* Jump to a page, a task or a workflow by name. Opened from the top bar, with
|
|
* Cmd/Ctrl+K, or with '/' when no field has focus. Tasks and workflows are
|
|
* read when the palette opens, so it never shows a stale list for long.
|
|
*/
|
|
const props = defineProps<{ hidden: string[] }>();
|
|
const emit = defineEmits<{ newTask: [] }>();
|
|
|
|
const router = useRouter();
|
|
const open = ref(false);
|
|
let openedPath = router.currentRoute.value.path;
|
|
function onCloseAutoFocus(event: Event): void {
|
|
if (router.currentRoute.value.path === openedPath) return;
|
|
event.preventDefault();
|
|
document.getElementById("main-content")?.focus({ preventScroll: true });
|
|
}
|
|
|
|
interface TaskEntry {
|
|
id: number | string;
|
|
title?: string;
|
|
status?: string;
|
|
}
|
|
|
|
const pages = computed(() => navEntries(props.hidden));
|
|
const tasks = ref<TaskEntry[]>([]);
|
|
const workflows = ref<{ id: string; name: string }[]>([]);
|
|
const shortcut = navigator.platform.startsWith("Mac") ? "⌘K" : "Ctrl K";
|
|
const showWorkflows = computed(() => !props.hidden.includes("/workflows"));
|
|
const showTasks = computed(() => !props.hidden.includes("/tasks"));
|
|
|
|
// A failed read shows as an error item with Retry in that group, not as a
|
|
// missing group: the pages still work either way.
|
|
const tasksError = ref(false);
|
|
const workflowsError = ref(false);
|
|
let entriesGeneration = 0;
|
|
async function loadEntries(): Promise<void> {
|
|
const current = ++entriesGeneration;
|
|
const [taskPage, workflowList] = await Promise.all([
|
|
showTasks.value ? api.taskPage<TaskEntry>({ limit: 100 }).catch(() => null) : { tasks: [] },
|
|
showWorkflows.value ? api.workflowList().catch(() => null) : [],
|
|
]);
|
|
if (current !== entriesGeneration) return;
|
|
tasksError.value = taskPage === null;
|
|
tasks.value = taskPage?.tasks ?? [];
|
|
workflowsError.value = workflowList === null;
|
|
workflows.value = (workflowList ?? []).map(({ id, name }) => ({ id, name }));
|
|
}
|
|
|
|
watch(open, (value) => {
|
|
if (value) { openedPath = router.currentRoute.value.path; void loadEntries(); }
|
|
});
|
|
|
|
function go(path: string): void {
|
|
open.value = false;
|
|
void router.push(path);
|
|
}
|
|
|
|
function newTask(): void {
|
|
open.value = false;
|
|
emit("newTask");
|
|
}
|
|
|
|
function onGlobalKeydown(event: KeyboardEvent): void {
|
|
if ((event.metaKey || event.ctrlKey) && event.key.toLowerCase() === "k") {
|
|
event.preventDefault();
|
|
open.value = !open.value;
|
|
}
|
|
}
|
|
|
|
onMounted(() => document.addEventListener("keydown", onGlobalKeydown));
|
|
onUnmounted(() => document.removeEventListener("keydown", onGlobalKeydown));
|
|
</script>
|
|
|
|
<template>
|
|
<Button
|
|
variant="outline"
|
|
size="sm"
|
|
class="text-fg-muted w-44 justify-start max-lg:w-auto"
|
|
aria-label="Jump to"
|
|
@click="open = true"
|
|
>
|
|
<Search data-icon="inline-start" />
|
|
<span class="max-lg:hidden">Jump to</span>
|
|
<Kbd class="ml-auto max-lg:hidden">{{ shortcut }}</Kbd>
|
|
</Button>
|
|
<CommandDialog @close-auto-focus="onCloseAutoFocus" v-model:open="open" title="Jump to" description="Find a page, task or workflow.">
|
|
<CommandInput placeholder="Find a page, task or workflow" />
|
|
<CommandList>
|
|
<CommandEmpty>No matches.</CommandEmpty>
|
|
<CommandGroup v-if="showWorkflows" heading="Actions">
|
|
<CommandItem value="New task" @select="newTask">New task</CommandItem>
|
|
</CommandGroup>
|
|
<CommandGroup heading="Views">
|
|
<CommandItem value="Tasks in progress" @select="go('/tasks?status=running')">Tasks in progress</CommandItem>
|
|
<CommandItem value="Tasks needing attention" @select="go('/tasks?status=needs_attention')">Tasks needing attention</CommandItem>
|
|
</CommandGroup>
|
|
<CommandGroup heading="Pages">
|
|
<CommandItem
|
|
v-for="entry in pages"
|
|
:key="entry.path"
|
|
:value="`page:${entry.path}`"
|
|
@select="go(entry.path)"
|
|
>
|
|
{{ entry.label }}
|
|
</CommandItem>
|
|
</CommandGroup>
|
|
<CommandGroup v-if="workflowsError" heading="Workflows">
|
|
<CommandItem value="workflows-error" @select="loadEntries">{{ errors.components.commandPalette.workflows }}</CommandItem>
|
|
</CommandGroup>
|
|
<CommandGroup v-else-if="workflows.length" heading="Workflows">
|
|
<CommandItem
|
|
v-for="workflow in workflows"
|
|
:key="workflow.id"
|
|
:value="`workflow:${workflow.name}`"
|
|
@select="go(`/workflows/${encodeURIComponent(workflow.id)}`)"
|
|
>
|
|
{{ workflow.name }}
|
|
</CommandItem>
|
|
</CommandGroup>
|
|
<CommandGroup v-if="tasksError" heading="Tasks">
|
|
<CommandItem value="tasks-error" @select="loadEntries">{{ errors.components.commandPalette.tasks }}</CommandItem>
|
|
</CommandGroup>
|
|
<CommandGroup v-else-if="tasks.length" heading="Tasks">
|
|
<CommandItem
|
|
v-for="task in tasks"
|
|
:key="task.id"
|
|
:value="`task:${task.id}`"
|
|
@select="go(`/tasks/${task.id}`)"
|
|
>
|
|
<span class="text-fg-muted shrink-0 tabular-nums">#{{ task.id }}</span>
|
|
<span class="truncate">{{ task.title }}</span>
|
|
<span v-if="task.status" class="text-fg-muted ml-auto shrink-0 text-caption">{{
|
|
statusLabel(task.status)
|
|
}}</span>
|
|
</CommandItem>
|
|
</CommandGroup>
|
|
</CommandList>
|
|
</CommandDialog>
|
|
</template>
|