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

145
src/plugin.ts Normal file
View File

@@ -0,0 +1,145 @@
import streamDeck, {
action,
KeyDownEvent,
PropertyInspectorDidAppearEvent,
SingletonAction,
WillAppearEvent,
} from "@elgato/streamdeck";
import { fetchProjects, startTimer, stopTimer } from "./notion.js";
interface GlobalSettings {
notionToken: string;
timingDbId: string;
projectsDbId: string;
userId: string;
}
interface TimerSettings {
projectId: string;
projectName: string;
activeEntryId: string | null;
}
const HARDCODED = {
timingDbId: "2ec93117da3a80799ee2cf91244ee264",
projectsDbId: "cd65c760-0f0a-4d6e-a262-d572c9e31585",
};
async function getGlobal(): Promise<GlobalSettings> {
const stored = await streamDeck.settings.getGlobalSettings<GlobalSettings>();
return { ...stored, ...HARDCODED };
}
function isConfigured(g: GlobalSettings): boolean {
return !!(g.notionToken && g.userId);
}
// Strip leading emoji characters so the button title shows just the project name
function buttonTitle(projectName: string): string {
return projectName.replace(/^[\p{Extended_Pictographic}\uFE0F\s]+/u, "").trim();
}
@action({ UUID: "com.pdma.notion-timer.toggle" })
class TimerToggle extends SingletonAction<TimerSettings> {
private settingsCache = new Map<string, TimerSettings>();
async onWillAppear(ev: WillAppearEvent<TimerSettings>): Promise<void> {
this.settingsCache.set(ev.action.id, ev.payload.settings);
const { activeEntryId, projectName } = ev.payload.settings;
const title = buttonTitle(projectName || "");
if (activeEntryId) {
await Promise.all([ev.action.setState(1), ev.action.setTitle(`${title}`)]);
} else {
await Promise.all([ev.action.setState(0), ev.action.setTitle(title)]);
}
}
async onPropertyInspectorDidAppear(ev: PropertyInspectorDidAppearEvent<TimerSettings>): Promise<void> {
try {
const global = await getGlobal();
if (!isConfigured(global)) {
await streamDeck.ui.sendToPropertyInspector({ event: "projects", data: [], error: "Configure Notion credentials in plugin settings first." });
return;
}
const projects = await fetchProjects(global.notionToken, global.projectsDbId);
await streamDeck.ui.sendToPropertyInspector({ event: "projects", data: projects });
} catch (err) {
streamDeck.logger.error("Failed to fetch projects:", err);
await streamDeck.ui.sendToPropertyInspector({ event: "projects", data: [], error: String(err) });
}
}
async onKeyDown(ev: KeyDownEvent<TimerSettings>): Promise<void> {
this.settingsCache.set(ev.action.id, ev.payload.settings);
const global = await getGlobal();
const { projectId, projectName, activeEntryId } = ev.payload.settings;
const title = buttonTitle(projectName || "");
if (!isConfigured(global)) {
await ev.action.showAlert();
return;
}
if (!projectId) {
await ev.action.showAlert();
return;
}
try {
if (activeEntryId) {
await stopTimer(global.notionToken, activeEntryId);
const stopped = { ...ev.payload.settings, activeEntryId: null };
await ev.action.setSettings(stopped);
this.settingsCache.set(ev.action.id, stopped);
await ev.action.setState(0);
await ev.action.setTitle(title);
await ev.action.showOk();
} else {
// Stop any other running timer first
for (const other of this.actions) {
if (other.id === ev.action.id) continue;
const otherSettings = this.settingsCache.get(other.id);
if (otherSettings?.activeEntryId) {
await stopTimer(global.notionToken, otherSettings.activeEntryId);
const stopped = { ...otherSettings, activeEntryId: null };
await other.setSettings(stopped);
this.settingsCache.set(other.id, stopped);
await other.setState(0);
await other.setTitle(buttonTitle(otherSettings.projectName || ""));
}
}
const entryId = await startTimer(
global.notionToken,
global.timingDbId,
projectId,
projectName,
global.userId,
);
const started = { ...ev.payload.settings, activeEntryId: entryId };
await ev.action.setSettings(started);
this.settingsCache.set(ev.action.id, started);
await ev.action.setState(1);
await ev.action.setTitle(`${title}`);
await ev.action.showOk();
}
} catch (err) {
streamDeck.logger.error("Timer toggle failed:", err);
await ev.action.showAlert();
}
}
}
const timerAction = new TimerToggle();
streamDeck.actions.registerAction(timerAction);
// v2 requires using streamDeck.ui.onSendToPlugin — the SingletonAction method does not fire
streamDeck.ui.onSendToPlugin<{ event: string; settings?: TimerSettings }>(async (ev) => {
if (ev.payload.event === "saveSettings" && ev.payload.settings) {
await ev.action.setSettings(ev.payload.settings);
const title = buttonTitle(ev.payload.settings.projectName || "");
if (title) await ev.action.setTitle(title);
}
});
streamDeck.connect();