Repository navigation
[resources] Add sort by date/relevance dropdown to Resources page #8073
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
Open
akshatsinghai6682-sketch
wants to merge
8
commits into
layer5io:master
Choose a base branch
from
akshatsinghai6682-sketch:sort-resources-feature
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e3e6c6f
[resources] Add sort by date/relevance dropdown to Resources page
akshatsinghai6682-sketch 91612aa
Merge branch 'master' into sort-resources-feature
akshatsinghai6682-sketch 0cba95a
fix(resources): reset sort when search query changes
akshatsinghai6682-sketch 04268a9
[resources] Add sort and pagination tests and remove formatting noise
akshatsinghai6682-sketch 367c761
Update unit test command to include new test file
akshatsinghai6682-sketch 8923075
Merge branch 'master' into sort-resources-feature
akshatsinghai6682-sketch bb0e38d
fix(resources): clear sort choice when the search query changes
akshatsinghai6682-sketch 3681de8
Update DataWrapper.js
akshatsinghai6682-sketch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| const getTime = (node) => { | ||
| const t = new Date(node.fields?.dateForSort).getTime(); | ||
| return Number.isNaN(t) || t <= 0 ? null : t; | ||
| }; | ||
|
|
||
| const sortResources = (nodes, sortOption) => { | ||
| if (sortOption === "relevance") return nodes; | ||
|
|
||
| const direction = sortOption === "oldest" ? 1 : -1; | ||
| return nodes.slice().sort((a, b) => { | ||
| const ta = getTime(a); | ||
| const tb = getTime(b); | ||
| if (ta === null && tb === null) return 0; | ||
| if (ta === null) return 1; | ||
| if (tb === null) return -1; | ||
| return direction * (ta - tb); | ||
| }); | ||
| }; | ||
|
|
||
| const paginateResources = (items, currentPage, postsPerPage) => { | ||
| if (!(postsPerPage > 0)) return items; | ||
| const last = currentPage * postsPerPage; | ||
| return items.slice(last - postsPerPage, last); | ||
| }; | ||
|
|
||
| module.exports = { getTime, sortResources, paginateResources }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| const test = require("node:test"); | ||
| const assert = require("node:assert/strict"); | ||
| const { sortResources, paginateResources } = require("./resource-sort"); | ||
|
|
||
| const node = (id, dateForSort) => ({ id, fields: { dateForSort } }); | ||
|
|
||
| const mixed = [ | ||
| node("b", "2024-02-01T00:00:00.000Z"), | ||
| node("missing", undefined), | ||
| node("c", "2025-03-01T00:00:00.000Z"), | ||
| node("invalid", "not-a-date"), | ||
| node("a", "2023-01-01T00:00:00.000Z"), | ||
| ]; | ||
|
|
||
| const ids = (list) => list.map((n) => n.id); | ||
|
|
||
| test("latest sorts newest first", () => { | ||
| assert.deepEqual(ids(sortResources(mixed, "latest")).slice(0, 3), ["c", "b", "a"]); | ||
| }); | ||
|
|
||
| test("oldest sorts oldest first", () => { | ||
| assert.deepEqual(ids(sortResources(mixed, "oldest")).slice(0, 3), ["a", "b", "c"]); | ||
| }); | ||
|
|
||
| test("missing or invalid dates are always last", () => { | ||
| for (const option of ["latest", "oldest"]) { | ||
| const tail = ids(sortResources(mixed, option)).slice(3); | ||
| assert.deepEqual(tail.sort(), ["invalid", "missing"]); | ||
| } | ||
| }); | ||
|
|
||
| test("relevance keeps the incoming order", () => { | ||
| assert.deepEqual(ids(sortResources(mixed, "relevance")), ids(mixed)); | ||
| }); | ||
|
|
||
| test("sorting does not mutate the input", () => { | ||
| const before = ids(mixed); | ||
| sortResources(mixed, "latest"); | ||
| assert.deepEqual(ids(mixed), before); | ||
| }); | ||
|
|
||
| const items = [1, 2, 3, 4, 5, 6, 7]; | ||
|
|
||
| test("pagination returns the right slice for each page", () => { | ||
| assert.deepEqual(paginateResources(items, 1, 3), [1, 2, 3]); | ||
| assert.deepEqual(paginateResources(items, 2, 3), [4, 5, 6]); | ||
| assert.deepEqual(paginateResources(items, 3, 3), [7]); | ||
| }); | ||
|
|
||
| test("pagination returns everything when postsPerPage is 0", () => { | ||
| assert.deepEqual(paginateResources(items, 1, 0), items); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.