Export template source from 1227e3672a5a24b7331567fc3f431e422e1513c1:quixos-instance/packages/todo-runtime
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
import type { Contexts } from "./gen/qx.js";
|
||||
|
||||
export const complete = async ({ ports }: Contexts["complete"]) => {
|
||||
const [status, repeatDays, dueDate] = await Promise.all([
|
||||
ports.data["status.get"](),
|
||||
ports.data["repeatDays.get"](),
|
||||
ports.data["dueDate.get"](),
|
||||
]);
|
||||
if (status === "ARCHIVED") throw new Error("Restore this task before completing it");
|
||||
if (status === "DONE") return "DONE";
|
||||
if (!Number.isInteger(repeatDays) || repeatDays < 0 || repeatDays > 36500)
|
||||
throw new Error("Repeat interval must be between 0 and 36500 whole days");
|
||||
const now = Date.now();
|
||||
const completion = await ports.completion.construct({ completedAt: now, scheduledDate: dueDate });
|
||||
await ports.data["completions.connect"](completion);
|
||||
if (repeatDays > 0) {
|
||||
const next = new Date(now);
|
||||
next.setUTCDate(next.getUTCDate() + repeatDays);
|
||||
await ports.data["dueDate.set"](next.toISOString().slice(0, 10));
|
||||
return "OPEN";
|
||||
}
|
||||
await ports.data["status.set"]("DONE");
|
||||
return "DONE";
|
||||
};
|
||||
|
||||
export const createCompletion = async ({ ports, input, objectId }: Contexts["createCompletion"]) => {
|
||||
if (!Number.isFinite(input.completedAt)) throw new Error("Invalid completion time");
|
||||
await ports.data["completedAt.set"](input.completedAt);
|
||||
await ports.data["scheduledDate.set"](input.scheduledDate);
|
||||
return objectId;
|
||||
};
|
||||
export const reopen = async ({ ports }: Contexts["reopen"]) => {
|
||||
await ports.data["status.set"]("OPEN");
|
||||
return "OPEN";
|
||||
};
|
||||
export const archive = async ({ ports }: Contexts["archive"]) => {
|
||||
await ports.data["status.set"]("ARCHIVED");
|
||||
return "ARCHIVED";
|
||||
};
|
||||
export const displayLabel = {
|
||||
kind: "derived" as const,
|
||||
get: async ({ ports }: Contexts["displayLabelGet"]) => {
|
||||
const [title, status, priority] = await Promise.all([
|
||||
ports.data["title.get"](),
|
||||
ports.data["status.get"](),
|
||||
ports.data["priority.get"](),
|
||||
]);
|
||||
const priorityText = priority && priority !== "NORMAL" ? ` ${priority.toLowerCase()}` : "";
|
||||
return `[${status || "OPEN"}]${priorityText} ${title || "Untitled task"}`;
|
||||
},
|
||||
};
|
||||
export const searchDocument = {
|
||||
kind: "derived" as const,
|
||||
get: async ({ ports }: Contexts["searchDocumentGet"]) => {
|
||||
const [title, status, priority, dueDate] = await Promise.all([
|
||||
ports.data["title.get"](),
|
||||
ports.data["status.get"](),
|
||||
ports.data["priority.get"](),
|
||||
ports.data["dueDate.get"](),
|
||||
]);
|
||||
return {
|
||||
title: title || "Untitled task",
|
||||
subtitle: `Task · ${status || "OPEN"}${dueDate ? ` · due ${dueDate}` : ""}`,
|
||||
keywords: ["task", status || "open", priority || "normal", dueDate || ""],
|
||||
};
|
||||
},
|
||||
};
|
||||
export const createTask = async ({ ports, input, objectId }: Contexts["createTask"]) => {
|
||||
await ports.data["title.set"](input.trim() || "Untitled task");
|
||||
return objectId;
|
||||
};
|
||||
export const stopWatch = () => null;
|
||||
Reference in New Issue
Block a user