From 4ccef31ce1b93d403b80507e2070cd9c809fc9cc Mon Sep 17 00:00:00 2001 From: Ryan Bantz Date: Sun, 9 Feb 2025 06:39:06 -0600 Subject: [PATCH] Fix pinned projects Fix pinned projects --- main.ts | 67 ++++++++++-------------------------------- ui/FuzzySelectModal.ts | 49 ++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 51 deletions(-) create mode 100644 ui/FuzzySelectModal.ts diff --git a/main.ts b/main.ts index b6bfdd3..b80d372 100644 --- a/main.ts +++ b/main.ts @@ -12,6 +12,7 @@ import { } from 'obsidian'; import { AsanaPluginSettings, DEFAULT_SETTINGS, AsanaSettingTab } from './settings/settings'; import { fetchAsanaWorkspaces, fetchAsanaProjects, fetchAsanaSections, createTaskInAsana } from './api/asanaApi'; +import { FuzzySelectModal } from './ui/FuzzySelectModal'; /** * Main plugin class. @@ -182,8 +183,22 @@ export default class AsanaPlugin extends Plugin { // Fetch projects using the refactored function const projects = await fetchAsanaProjects(workspace.gid, this.settings); - const projectOptions = projects.map((p: any) => ({ name: p.name, gid: p.gid })); + // Ensure pinned projects are sorted to the top + const pinnedProjectIds = new Set(this.settings.pinnedProjects); + // Separate pinned and non-pinned projects + const pinnedProjects = projects + .filter((p: any) => pinnedProjectIds.has(p.gid) || pinnedProjectIds.has(p.name)) + .map((p: any) => ({ name: p.name, gid: p.gid, isPinned: true })); + + const otherProjects = projects + .filter((p: any) => !pinnedProjectIds.has(p.gid) && !pinnedProjectIds.has(p.name)) + .map((p: any) => ({ name: p.name, gid: p.gid, isPinned: false })); + + // Combine pinned projects first, followed by the rest + const projectOptions = [...pinnedProjects, ...otherProjects]; + + // Prompt for project selection const project = await this.promptForSelection('Select Project', projectOptions); if (!project) { @@ -292,54 +307,4 @@ export default class AsanaPlugin extends Plugin { const completedLine = lineText.replace('- [ ]', '- [x]'); editor.setLine(cursor.line, completedLine); } -} - -/** - * Modal for selection prompts. - */ -class FuzzySelectModal extends FuzzySuggestModal<{ name: string; gid: string; isPinned?: boolean }> { - private resolve: (value: { name: string; gid: string } | null) => void; - private items: Array<{ name: string; gid: string; isPinned?: boolean }>; - private title: string; - private selectedItem: { name: string; gid: string } | null = null; - private resolved: boolean = false; // Tracks if resolve has been handled - - constructor( - app: App, - title: string, - items: Array<{ name: string; gid: string; isPinned?: boolean }>, - resolve: (value: { name: string; gid: string } | null) => void - ) { - super(app); - this.title = title; - this.items = items; - this.resolve = resolve; - } - - onOpen() { - super.onOpen(); - this.setTitle('herer is the titme'); - this.setPlaceholder(this.title); - // this.setInstructions('here is the instruction'); - } - - getItems(): Array<{ name: string; gid: string; isPinned?: boolean }> { - return this.items; - } - - getItemText(item: { name: string; gid: string; isPinned?: boolean }): string { - return item.isPinned ? `📌 ${item.name}` : item.name; - } - - // Should this be async or not? - onChooseItem(item: { name: string; gid: string }, evt: MouseEvent | KeyboardEvent) { - if (!this.resolved) { - console.log(`ITEM CHOSEN - ${item.name} (gid: ${item.gid})`); - this.selectedItem = item; - this.resolved = true; // Mark as resolved - this.resolve(item); // Resolve with the selected item - } else { - console.warn(`ITEM CHOSEN MULTIPLE TIMES - Ignoring extra selection: ${item.name}`); - } - } } \ No newline at end of file diff --git a/ui/FuzzySelectModal.ts b/ui/FuzzySelectModal.ts new file mode 100644 index 0000000..2d1561e --- /dev/null +++ b/ui/FuzzySelectModal.ts @@ -0,0 +1,49 @@ +import { App, FuzzySuggestModal } from 'obsidian'; + +/** + * A modal for selecting items with fuzzy search support. + */ +export class FuzzySelectModal extends FuzzySuggestModal<{ name: string; gid: string; isPinned?: boolean }> { + private resolve: (value: { name: string; gid: string } | null) => void; + private items: Array<{ name: string; gid: string; isPinned?: boolean }>; + private title: string; + private selectedItem: { name: string; gid: string } | null = null; + private resolved: boolean = false; // Prevents resolving multiple times + + constructor( + app: App, + title: string, + items: Array<{ name: string; gid: string; isPinned?: boolean }>, + resolve: (value: { name: string; gid: string } | null) => void + ) { + super(app); + this.title = title; + this.items = items; + this.resolve = resolve; + } + + onOpen() { + super.onOpen(); + this.setTitle(this.title); + this.setPlaceholder(this.title); + } + + getItems(): Array<{ name: string; gid: string; isPinned?: boolean }> { + return this.items; + } + + getItemText(item: { name: string; gid: string; isPinned?: boolean }): string { + return item.isPinned ? `📌 ${item.name.replace(/^📌 /, '')}` : item.name; + } + + onChooseItem(item: { name: string; gid: string }, evt: MouseEvent | KeyboardEvent) { + if (!this.resolved) { + console.log(`ITEM CHOSEN - ${item.name} (gid: ${item.gid})`); + this.selectedItem = item; + this.resolved = true; // Prevents multiple selections + this.resolve(item); + } else { + console.warn(`ITEM CHOSEN MULTIPLE TIMES - Ignoring extra selection: ${item.name}`); + } + } +} \ No newline at end of file