Files
package-todo-react-runtime/src/todo-react.impl.ts
T

53 lines
1.9 KiB
TypeScript

import fs from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { derived, type RuntimeContext } from "@quixos/camino-package-runtime";
const dirname = path.dirname(fileURLToPath(import.meta.url));
const componentSourcePath = path.join(dirname, "todo-component.mjs");
export const source = async () => await fs.readFile(componentSourcePath, "utf8");
export const createComponent = (context: RuntimeContext) => context.objectId;
export const props = derived(async (context: RuntimeContext) => {
const tasks = await context.edge("port:todo-react:subject").resolve();
if (tasks.length !== 1) {
throw new Error(`Task component ${context.objectId} has ${tasks.length} subjects`);
}
const titlePort = context.state<string>("port:todo-react:title");
const statusPort = context.state<string>("port:todo-react:status");
const priorityPort = context.state<string>("port:todo-react:priority");
const notesPort = context.state<string>("port:todo-react:notes");
const dueDatePort = context.state<string>("port:todo-react:due-date");
const energyPort = context.state<string>("port:todo-react:energy");
const [title, status, priority, titleLive, statusLive, priorityLive, notesLive, dueDateLive, energyLive] =
await Promise.all([
titlePort.get(),
statusPort.get(),
priorityPort.get(),
titlePort.live(),
statusPort.live(),
priorityPort.live(),
notesPort.live(),
dueDatePort.live(),
energyPort.live(),
]);
const priorityText = priority && priority !== "NORMAL"
? ` ${priority.toLowerCase()}`
: "";
return {
task_id: tasks[0],
component_id: context.objectId,
title: titleLive,
status: statusLive,
priority: priorityLive,
notes: notesLive,
due_date: dueDateLive,
energy: energyLive,
display_label: `[${status ?? "OPEN"}]${priorityText} ${title ?? "Untitled task"}`,
};
});
export const stopWatch = () => null;