Skip to content

Conversation

dakshpareek
Copy link
Contributor

@dakshpareek dakshpareek commented Jan 3, 2025

This PR adds the ability to sort bookmarks by creation date in ascending or descending order.

Changes

  • Add sort order toggle in GlobalActions component
  • Implement ascending/descending sort functionality
  • Update translations for sorting feature in all supported languages
  • Add sort order icons and dropdown menu
  • Maintain sort preference in URL parameters

Screenshots

ScreenRecording2025-01-03at1 50 11PM-ezgif com-video-to-gif-converter

Testing Done

  • Verified sort order works in both directions
  • Confirmed sort preference persists across page reloads
  • Tested with different layouts (grid, list, etc.)
  • Verified translations work correctly

Notes

  • Sort preference is maintained in URL params for better sharing and persistence
  • Default sort order is newest first (descending)
  • Note: Sort order resets to default on page refresh, server-side persistence can be implemented in future enhancement

- Add sort order toggle in GlobalActions component
- Implement ascending/descending sort functionality
- Update translations for sorting feature in all languages
- Add sort order icons and dropdown menu
- Maintain sort preference in URL params
- Add sort order toggle in GlobalActions component
- Implement ascending/descending sort functionality
- Update translations for sorting feature in all languages
- Add sort order icons and dropdown menu
- Maintain sort preference in URL params during session

Note: Sort order resets to default on page refresh, server-side persistence can be implemented in future enhancement
@kamtschatka
Copy link
Contributor

there is the new feature of smart lists/filtering based on queries. My assumption was, that sorting would be part of the query language in the future, since your solution will not work with pagination i assume?

@dakshpareek
Copy link
Contributor Author

I do not have knowledge about smart lists/filtering. Is this upcoming feature?
My solution will work with pagination I assume.

@MohamedBassem
Copy link
Collaborator

MohamedBassem commented Jan 3, 2025

Hey, thanks for the PR! My plan (still haven't given it a lot of thought) is to keep sorting and smart search separate (the way this PR introduces it) because sorting is needed even when smart searching is not used. Will hopefully be able to review this PR over the weekend.

Copy link
Collaborator

@MohamedBassem MohamedBassem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks for the PR! Overall, the approach looks good to me. Left some comments, that we can merge the PR after they get addressed.

pnpm-lock.yaml Outdated
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you might be using a different pnpm version which introduced a lot of changes to the lock file. Those will probably need to be reverted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure.

@@ -161,6 +167,8 @@ export const zGetBookmarksRequestSchema = z.object({
// The value is currently not being used, but keeping it so that client can still set it to true for older
// servers.
useCursorV2: z.boolean().optional(),
sortBy: zSortBy.optional().default("createdAt"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that this is currently unused, should we just drop it for now to avoid confusion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, removed it.

@@ -12,6 +12,12 @@ export const enum BookmarkTypes {
UNKNOWN = "unknown",
}

export const zSortOrder = z.enum(["asc", "desc"]);
export type SortOrder = z.infer<typeof zSortOrder>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please follow the same style in the rest of the file. All zod inferred type start with the Z prefix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

export type SortOrder = z.infer<typeof zSortOrder>;

export const zSortBy = z.enum(["createdAt"]);
export type SortBy = z.infer<typeof zSortBy>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment about the prefix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this as well.

className="cursor-pointer justify-between"
onClick={() => updateSort("desc")}
>
<span>{t("actions.sort.newest_first")}</span>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's include some lucide sort icons here to make it look nicer: https://lucide.dev/icons/?search=sort

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added icon here. Let me know if you think some other icon should be there.

Comment on lines +735 to +740
.orderBy(
input.sortOrder === "asc"
? asc(bookmarks.createdAt)
: desc(bookmarks.createdAt),
desc(bookmarks.id),
),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that this order button is global, we'll need to modify the searchBookmarks endpoint as well, and modify apps/web/lib/hooks/bookmark-search.ts to pass the sorting order to it as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, search will also support sorting now.

@dakshpareek
Copy link
Contributor Author

Hi @MohamedBassem, thanks for reviewing PR.
I will look into these comments when I reach back home on Thursday after my vacation.

@dakshpareek dakshpareek closed this Jan 8, 2025
@dakshpareek dakshpareek force-pushed the feature/bookmark-sort-by-date branch from 839c1ed to 7daa87b Compare January 8, 2025 15:47
@dakshpareek dakshpareek reopened this Jan 9, 2025
- Implement global sort order functionality using a shared Zustand store (`useSortOrder` hook).
- Update `getBookmarks` and `searchBookmarks` endpoints to accept a `sortOrder` parameter.
- Refactor code to import `ZSortOrder` from shared types (`bookmarks.ts`), ensuring consistency across the codebase.
- Update components (`UpdatableBookmarksGrid`, `bookmark-search`) to use the shared `useSortOrder` hook.
- Remove unused `zSortBy` definition from `packages/shared/types/bookmarks.ts` to avoid confusion.
- Ensure consistent naming conventions by prefixing Zod inferred types with `Z`.
- Clean up code and address previous PR feedback comments.
@dakshpareek
Copy link
Contributor Author

Hello @MohamedBassem,
I have adjusted sorting related code according to your suggestions. Please take a look when you have time and let me know what do you think.

Thanks

@MohamedBassem MohamedBassem merged commit b6293d1 into karakeep-app:main Jan 12, 2025
4 checks passed
@MohamedBassem
Copy link
Collaborator

Thank you!

@dakshpareek
Copy link
Contributor Author

Hey @MohamedBassem btw when is next release planned?
Just wanted to know when I can view latest changed on my hoarder account. 😄

@MohamedBassem
Copy link
Collaborator

@dakshpareek Given that the last release was last week, I think it'll be sometime until the next one. Maybe 3 weeks or so until we accumulate enough features for the release. There hasn't been a lot of changes since the release, so the nightly build should be stable if you want to switch to it today and have your feature available there today :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants