Polished timer and UI
This commit is contained in:
73
utils.ts
73
utils.ts
@@ -1,4 +1,4 @@
|
||||
import { App, TFile } from "obsidian";
|
||||
import { App, TFile, Notice, moment } from "obsidian";
|
||||
import { Task, Category } from "./types";
|
||||
|
||||
export function formatDuration(milliseconds: number): string {
|
||||
@@ -20,32 +20,64 @@ export function generateUniqueId(): string {
|
||||
return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
||||
}
|
||||
|
||||
async function getDailyNote(app: App): Promise<TFile> {
|
||||
async function getDailyNote(app: App): Promise<TFile | null> {
|
||||
const { vault } = app;
|
||||
|
||||
// @ts-ignore - Access internal daily notes API
|
||||
// @ts-ignore - Access internal plugins
|
||||
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.");
|
||||
new Notice("Daily notes plugin is not enabled. Please enable it in Settings → Core plugins.");
|
||||
return null;
|
||||
}
|
||||
|
||||
// @ts-ignore - Access daily notes interface
|
||||
const { createDailyNote, getDailyNote, getAllDailyNotes } = app.internalPlugins.plugins["daily-notes"].instance;
|
||||
// @ts-ignore - Access the daily notes instance
|
||||
const dailyNotesInterface = dailyNotesPlugin.instance;
|
||||
|
||||
// @ts-ignore
|
||||
const dailyNotes = getAllDailyNotes();
|
||||
// @ts-ignore
|
||||
const today = window.moment();
|
||||
// @ts-ignore
|
||||
let dailyNote = getDailyNote(today, dailyNotes);
|
||||
if (!dailyNotesInterface) {
|
||||
new Notice("Could not access daily notes interface");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!dailyNote) {
|
||||
try {
|
||||
// @ts-ignore
|
||||
dailyNote = await createDailyNote(today);
|
||||
}
|
||||
const { format, folder, template } = dailyNotesInterface.options || {};
|
||||
|
||||
return dailyNote;
|
||||
const dateFormat = format || "YYYY-MM-DD";
|
||||
const dailyNotesFolder = folder || "";
|
||||
const today = moment().format(dateFormat);
|
||||
|
||||
const fileName = `${today}.md`;
|
||||
const filePath = dailyNotesFolder ? `${dailyNotesFolder}/${fileName}` : fileName;
|
||||
|
||||
let dailyNote = vault.getAbstractFileByPath(filePath);
|
||||
|
||||
if (!dailyNote || !(dailyNote instanceof TFile)) {
|
||||
if (dailyNotesFolder && !vault.getAbstractFileByPath(dailyNotesFolder)) {
|
||||
await vault.createFolder(dailyNotesFolder);
|
||||
}
|
||||
|
||||
let initialContent = "";
|
||||
if (template) {
|
||||
const templateFile = vault.getAbstractFileByPath(template);
|
||||
if (templateFile instanceof TFile) {
|
||||
initialContent = await vault.read(templateFile);
|
||||
}
|
||||
}
|
||||
|
||||
dailyNote = await vault.create(filePath, initialContent);
|
||||
}
|
||||
|
||||
if (dailyNote instanceof TFile) {
|
||||
return dailyNote;
|
||||
}
|
||||
|
||||
return null;
|
||||
} catch (error) {
|
||||
console.error("Error getting daily note:", error);
|
||||
new Notice("Error accessing daily note: " + error.message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function logTaskToDailyNote(
|
||||
@@ -57,6 +89,11 @@ export async function logTaskToDailyNote(
|
||||
try {
|
||||
const dailyNote = await getDailyNote(app);
|
||||
|
||||
if (!dailyNote) {
|
||||
new Notice("Could not find or create daily note");
|
||||
return;
|
||||
}
|
||||
|
||||
const duration = formatDuration(task.totalElapsed);
|
||||
const logEntry = format
|
||||
.replace("{{title}}", task.title)
|
||||
@@ -67,8 +104,10 @@ export async function logTaskToDailyNote(
|
||||
const content = await app.vault.read(dailyNote);
|
||||
const newContent = content ? `${content}\n${logEntry}` : logEntry;
|
||||
await app.vault.modify(dailyNote, newContent);
|
||||
|
||||
new Notice("Task logged to daily note");
|
||||
} catch (error) {
|
||||
console.error("Failed to log task to daily note:", error);
|
||||
throw error;
|
||||
new Notice("Failed to log task: " + error.message);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user