2025-07-16 16:05:37 +00:00
|
|
|
import { useState, useCallback } from "react";
|
|
|
|
|
import { TFile } from "obsidian";
|
|
|
|
|
import { NoteStatus } from "@/types/noteStatus";
|
|
|
|
|
import {
|
|
|
|
|
BaseNoteStatusService,
|
|
|
|
|
NoteStatusService,
|
|
|
|
|
} from "@/core/noteStatusService";
|
2025-11-21 17:42:55 +00:00
|
|
|
import { getAllFrontmatterKeys } from "@/core/statusKeyHelpers";
|
2025-07-16 16:05:37 +00:00
|
|
|
|
|
|
|
|
interface CurrentNoteInfo {
|
|
|
|
|
file: TFile | null;
|
|
|
|
|
statuses: Record<string, NoteStatus[]>;
|
|
|
|
|
lastModified: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const useCurrentNote = () => {
|
|
|
|
|
const [currentNote, setCurrentNote] = useState<CurrentNoteInfo>({
|
|
|
|
|
file: null,
|
|
|
|
|
statuses: {},
|
|
|
|
|
lastModified: 0,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const updateCurrentNote = useCallback(() => {
|
|
|
|
|
const activeFile = BaseNoteStatusService.app.workspace.getActiveFile();
|
|
|
|
|
|
|
|
|
|
if (!activeFile) {
|
|
|
|
|
setCurrentNote({ file: null, statuses: {}, lastModified: 0 });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const noteStatusService = new NoteStatusService(activeFile);
|
|
|
|
|
noteStatusService.populateStatuses();
|
|
|
|
|
|
2025-11-21 17:42:55 +00:00
|
|
|
const statusMetadataKeys = getAllFrontmatterKeys();
|
2025-11-21 17:21:16 +00:00
|
|
|
const statusesByKey: Record<string, NoteStatus[]> = {};
|
|
|
|
|
statusMetadataKeys.forEach((key) => {
|
|
|
|
|
const statuses = noteStatusService.getStatusesForKey(key);
|
|
|
|
|
if (statuses.length) {
|
|
|
|
|
statusesByKey[key] = statuses;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-16 16:05:37 +00:00
|
|
|
setCurrentNote({
|
|
|
|
|
file: activeFile,
|
2025-11-21 17:21:16 +00:00
|
|
|
statuses: statusesByKey,
|
2025-07-16 16:05:37 +00:00
|
|
|
lastModified: activeFile.stat.mtime,
|
|
|
|
|
});
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
currentNote,
|
|
|
|
|
updateCurrentNote,
|
|
|
|
|
};
|
|
|
|
|
};
|