Initial commit

This commit is contained in:
pdmarf
2026-04-10 19:51:21 +01:00
commit bc383e4fd8
22 changed files with 2190 additions and 0 deletions

119
src/notion.ts Normal file
View File

@@ -0,0 +1,119 @@
const NOTION_BASE = "https://api.notion.com/v1";
export interface NotionProject {
id: string;
name: string;
}
function headers(token: string) {
return {
Authorization: `Bearer ${token}`,
"Notion-Version": "2022-06-28",
"Content-Type": "application/json",
};
}
const BOOK_COLOR: Record<string, string> = {
green: "📗", blue: "📘", orange: "📙", red: "📕",
yellow: "📒", pink: "📔", purple: "📓",
};
const COLOR_CIRCLE: Record<string, string> = {
green: "🟢", blue: "🔵", red: "🔴", orange: "🟠",
yellow: "🟡", purple: "🟣", pink: "🩷", brown: "🟤",
gray: "⚫", lightgray: "⬜", default: "⬜",
};
const ICON_NAME: Record<string, string> = {
graduate: "🎓", science: "🔬", gears: "⚙️", comment: "💬",
building: "🏢", chart: "📊", code: "💻", document: "📄",
flag: "🚩", globe: "🌐", home: "🏠", link: "🔗",
music: "🎵", person: "👤", phone: "📞", pin: "📌",
rocket: "🚀", star: "⭐", tag: "🏷️", target: "🎯",
};
function notionIconToEmoji(page: any): string {
const icon = page?.icon;
if (!icon) return "";
if (icon.type === "emoji") return icon.emoji as string;
if (icon.type === "icon") {
const { name, color } = icon.icon ?? {};
if (name === "book") return BOOK_COLOR[color] ?? "📚";
if (name === "skip-forward") return `${COLOR_CIRCLE[color] ?? ""}`.trimEnd();
return ICON_NAME[name] ?? "";
}
return "";
}
export async function fetchProjects(token: string, dbId: string): Promise<NotionProject[]> {
const resp = await fetch(`${NOTION_BASE}/databases/${dbId}/query`, {
method: "POST",
headers: headers(token),
body: JSON.stringify({
page_size: 100,
sorts: [{ property: "Project name", direction: "ascending" }],
}),
});
if (!resp.ok) throw new Error(`Notion error ${resp.status}: ${await resp.text()}`);
const data = (await resp.json()) as { results: unknown[] };
return data.results.map((page: any) => {
const titleArr: any[] = page.properties?.["Project name"]?.title ?? [];
const title = titleArr.map((t: any) => t.plain_text).join("").trim() || "Untitled";
const emoji = notionIconToEmoji(page);
return {
id: page.id as string,
name: emoji ? `${emoji} ${title}` : title,
};
});
}
export async function startTimer(
token: string,
timingDbId: string,
projectId: string,
projectName: string,
userId: string,
): Promise<string> {
const now = new Date().toISOString();
const date = new Date().toLocaleDateString("en-GB");
const resp = await fetch(`${NOTION_BASE}/pages`, {
method: "POST",
headers: headers(token),
body: JSON.stringify({
parent: { database_id: timingDbId },
icon: { type: "emoji", emoji: "⏱️" },
properties: {
Name: { title: [{ text: { content: "Timer Task" } }] },
"Start Time": { date: { start: now } },
Projects: { relation: [{ id: projectId }] },
Status: { status: { name: "Running" } },
Responsible: { people: [{ object: "user", id: userId }] },
},
}),
});
if (!resp.ok) throw new Error(`Failed to start timer: ${await resp.text()}`);
const data = (await resp.json()) as { id: string };
return data.id;
}
export async function stopTimer(token: string, entryId: string): Promise<void> {
const now = new Date().toISOString();
const resp = await fetch(`${NOTION_BASE}/pages/${entryId}`, {
method: "PATCH",
headers: headers(token),
body: JSON.stringify({
properties: {
"End Time": { date: { start: now } },
Status: { status: { name: "Stopped" } },
},
}),
});
if (!resp.ok) throw new Error(`Failed to stop timer: ${await resp.text()}`);
}