-
Notifications
You must be signed in to change notification settings - Fork 11
add sorting to active users #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
WalkthroughThe ACTIVEUSERS command handler is updated with a new sort parameter to enable sorting active users by text in ascending, descending, or no particular order. Internal logic refactored from direct formatting to a collect-sort-format workflow, with help text updated to document the optional sort option. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/utils/core.ts (1)
2309-2313: Guard againstparseNlpDatereturning null/undefined before.valueOf()If the NLP expression cannot be parsed,
parseNlpDate(...)may return a falsy value; calling.valueOf()on that will throw and break the workflow instead of failing gracefully.You can mirror the
DATEhandler pattern by first resolving the date and only then calling.valueOf():- const timestamp = - parseNlpDate(nlp, getDateBasisDate()).valueOf() || - // chrono fails basic parsing requiring forward date if ambiguous - // https://github.com/wanasit/chrono/commit/4f264a9f21fbd04eb740bf48f5616f6e6e0e78b7 - parseNlpDate(`in ${nlp}`, getDateBasisDate()).valueOf(); + const parsedDate = + parseNlpDate(nlp, getDateBasisDate()) || + // chrono fails basic parsing requiring forward date if ambiguous + // https://github.com/wanasit/chrono/commit/4f264a9f21fbd04eb740bf48f5616f6e6e0e78b7 + parseNlpDate(`in ${nlp}`, getDateBasisDate()); + if (!parsedDate) { + return `Could not return a valid date with text "${nlp}"`; + } + const timestamp = parsedDate.valueOf();This keeps behavior predictable even for bad NLP input.
🧹 Nitpick comments (1)
src/utils/core.ts (1)
2317-2341: ACTIVEUSERS result shaping and sorting: consider uniqueness, normalization, and minor ergonomicsFunctionally this works, but a few improvements are worth considering:
- Potential duplicate users
The Datalog query returns one row per qualifying block, so a user who edited many blocks aftertimestampwill appear multiple times inactiveUsers. If the intended semantics are “unique active users”, consider de‑duplicating byuid:- const activeUsers = ( - window.roamAlphaAPI.data.fast.q(`...`) as [PullBlock, PullBlock][] - ).map(([user]) => ({ - text: user[":node/title"] || user[":block/uid"] || "", - uid: user[":block/uid"], - })); + const activeUsersMap = new Map<string, { text: string; uid: string }>(); + ( + window.roamAlphaAPI.data.fast.q(`...`) as PullBlock[][] + ).forEach(([user]) => { + const uid = user[":block/uid"] as string; + const text = (user[":node/title"] as string) || uid || ""; + if (uid && !activeUsersMap.has(uid)) { + activeUsersMap.set(uid, { text, uid }); + } + }); + const activeUsers = Array.from(activeUsersMap.values());
- Normalize
sortand avoid in‑place mutation / repeated formatter creation
To be more forgiving (ascvsASC) and a bit cleaner:- const sortedUsers = - sort === "NONE" - ? activeUsers - : activeUsers.sort( - sort === "DESC" - ? (a, b) => b.text.localeCompare(a.text) - : (a, b) => a.text.localeCompare(b.text) - ); - return sortedUsers.map((user) => getFormatter(format)(user)); + const normalizedSort = (sort || "NONE").toUpperCase(); + const formatter = getFormatter(format); + const sortedUsers = + normalizedSort === "NONE" + ? activeUsers + : [...activeUsers].sort( + normalizedSort === "DESC" + ? (a, b) => b.text.localeCompare(a.text) + : (a, b) => a.text.localeCompare(b.text) + ); + return sortedUsers.map((user) => formatter(user));These changes are optional but would make the command more robust and predictable.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/utils/core.ts(2 hunks)
🔇 Additional comments (1)
src/utils/core.ts (1)
2303-2308: ACTIVEUSERS: new sort parameter and help text look consistentThe added
sortargument (with default"NONE") matches the updated help text and is backward compatible for existing calls that only pass 0–2 arguments. No issues here.
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.