75 lines
2.1 KiB
TypeScript
75 lines
2.1 KiB
TypeScript
import { App, TFile } from "obsidian";
|
|
import { Task, Category } from "./types";
|
|
|
|
export function formatDuration(milliseconds: number): string {
|
|
const totalSeconds = Math.floor(milliseconds / 1000);
|
|
const hours = Math.floor(totalSeconds / 3600);
|
|
const minutes = Math.floor((totalSeconds % 3600) / 60);
|
|
const seconds = totalSeconds % 60;
|
|
|
|
if (hours > 0) {
|
|
return `${hours}h ${minutes}m ${seconds}s`;
|
|
} else if (minutes > 0) {
|
|
return `${minutes}m ${seconds}s`;
|
|
} else {
|
|
return `${seconds}s`;
|
|
}
|
|
}
|
|
|
|
export function generateUniqueId(): string {
|
|
return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
}
|
|
|
|
async function getDailyNote(app: App): Promise<TFile> {
|
|
const { vault } = app;
|
|
|
|
// @ts-ignore - Access internal daily notes API
|
|
const dailyNotesPlugin = app.internalPlugins.plugins["daily-notes"];
|
|
|
|
if (!dailyNotesPlugin || !dailyNotesPlugin.enabled) {
|
|
throw new Error("Daily notes plugin is not enabled. Please enable it in Settings → Core plugins.");
|
|
}
|
|
|
|
// @ts-ignore - Access daily notes interface
|
|
const { createDailyNote, getDailyNote, getAllDailyNotes } = app.internalPlugins.plugins["daily-notes"].instance;
|
|
|
|
// @ts-ignore
|
|
const dailyNotes = getAllDailyNotes();
|
|
// @ts-ignore
|
|
const today = window.moment();
|
|
// @ts-ignore
|
|
let dailyNote = getDailyNote(today, dailyNotes);
|
|
|
|
if (!dailyNote) {
|
|
// @ts-ignore
|
|
dailyNote = await createDailyNote(today);
|
|
}
|
|
|
|
return dailyNote;
|
|
}
|
|
|
|
export async function logTaskToDailyNote(
|
|
app: App,
|
|
task: Task,
|
|
category: Category | undefined,
|
|
format: string
|
|
): Promise<void> {
|
|
try {
|
|
const dailyNote = await getDailyNote(app);
|
|
|
|
const duration = formatDuration(task.totalElapsed);
|
|
const logEntry = format
|
|
.replace("{{title}}", task.title)
|
|
.replace("{{duration}}", duration)
|
|
.replace("{{emoji}}", category?.emoji || "")
|
|
.replace("{{category}}", category?.name || "");
|
|
|
|
const content = await app.vault.read(dailyNote);
|
|
const newContent = content ? `${content}\n${logEntry}` : logEntry;
|
|
await app.vault.modify(dailyNote, newContent);
|
|
} catch (error) {
|
|
console.error("Failed to log task to daily note:", error);
|
|
throw error;
|
|
}
|
|
}
|