marcusolsson/obsidian-projects

More options for date type fields in Filters

Closed this issue · 7 comments

What would you like to be added?

First off, I'm loving the plugin and thank you for all the work done so far. There is one feature that I think will be useful to organize tasks, hope it's not already on the Roadmap and I missed it.

The feature is to have more options in the Filters and Colors for date type fields. Currently, there's only "is empty" and "is not empty" for fields with dates, but I think it will be useful to also have something like "less than", "greater than", "equal" to X. The X might be a hardcoded option like "Today", "2 weeks from now" etc. or like calculated field, like "today()" or " today() - 14d".

Why is this needed?

It will allow to create different views related to dates, like due tasks and not completed, incoming tasks in X amount of time, currently running tasks etc.

This would be a great addition! 👍

It looks like this would involve something like adding a dateFns to filter-functions.ts.

Would something like the below work?

export const dateFns: Record<
  DateFilterOperator,
  (left: string, right?: string) => boolean
> = {
  eq: (left, right) => left === right,
  neq: (left, right) => left !== right,
  lt: (left, right) => (right ? left < right : false),
  gt: (left, right) => (right ? left > right : false),
  lte: (left, right) => (right ? left <= right : false),
  gte: (left, right) => (right ? left >= right : false),
};

Theoretically, since dates are just strings like 2023-05-25 and 2023-05-26, I think the above would function just fine. Maybe I'm missing something obvious, though?

I think it would work, though I'd probably prefer to convert the date to a proper date object, rather than comparing the formatted string.

While the plugin only supports YYYY-MM-DD right now, we probably want to support more formats down the road.

I'm focusing on mobile support right now, but after that I'll work on better filtering capabilities (including this one).

I messed around with this, and it seems simple enough, but I'm stuck trying to figure out how to create a case "Date" in matchesCondition.

CleanShot 2023-05-25 at 13 03 56

I imagine I'm missing something naive so I'll pause this for now. alas!

convert the date to a proper date object, rather than comparing the formatted string

@marcusolsson Would you do this in matchesCondition, under case "string"? e.g., some kind of if statement that interprets the string, and if it looks like a date string, convert it, then compare?

I believe the type of a date is object, so it would probably be better to add an if statement below the switch statement, using isDate():

if (isDate(value)) {
  // ...
}

Got it, I think! But, damnit, now I see I actually do need to deal with Svelte in FilterSettings.svelte to do this.

Harumph. That's a steep learning curve for me.

Is it worth submitting a PR for the, like, twenty lines I've added, for further pointers? I'd love to see this implemented but I'm having trouble gauging how hard it would be for me to do it. Hmm...