Skip to content

Commit

Permalink
sort lastClicked descending
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey committed Sep 7, 2023
1 parent 2bd53ae commit 5d519f2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 26 deletions.
14 changes: 6 additions & 8 deletions app/ui/number.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"use client";

import { ReactNode } from "react";
import { nFormatter } from "#/lib/utils";
import { nFormatter, timeAgo } from "#/lib/utils";
import Tooltip from "#/ui/tooltip";

export default function Number({
value,
unit = "clicks",
unit = "total clicks",
children,
lastClicked,
}: {
Expand All @@ -27,12 +27,10 @@ export default function Number({
</p>
{lastClicked && (
<p className="mt-1 text-xs text-gray-500">
Last clicked on{" "}
{new Date(lastClicked).toLocaleDateString("en-us", {
month: "short",
day: "numeric",
year: "numeric",
})}
Last clicked {timeAgo(lastClicked)}{" "}
{Date.now() - new Date(lastClicked).getTime() < 82800000
? "ago"
: ""}
</p>
)}
</div>
Expand Down
19 changes: 3 additions & 16 deletions components/app/links/link-sort.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@ const sortOptions = [
{
display: "Date Added",
slug: "createdAt",
order: "desc",
},
{
display: "Number of Clicks",
slug: "clicks",
order: "desc",
},
{
display: "Last Clicked",
slug: "lastClicked",
order: "asc",
},
];

Expand All @@ -36,7 +33,7 @@ export default function LinkSort() {
<Popover
content={
<div className="w-full p-2 md:w-48">
{sortOptions.map(({ display, slug, order }) => (
{sortOptions.map(({ display, slug }) => (
<button
key={slug}
onClick={() => {
Expand All @@ -59,13 +56,7 @@ export default function LinkSort() {
>
<IconMenu
text={display}
icon={
order === "desc" ? (
<SortDesc className="h-4 w-4" />
) : (
<SortAsc className="h-4 w-4" />
)
}
icon={<SortDesc className="h-4 w-4" />}
/>
{selectedSort.slug === slug && (
<Tick className="h-4 w-4" aria-hidden="true" />
Expand All @@ -85,11 +76,7 @@ export default function LinkSort() {
text={sort ? selectedSort.display : "Sort by"}
icon={
sort ? (
selectedSort.order === "desc" ? (
<SortDesc className="h-4 w-4" />
) : (
<SortAsc className="h-4 w-4" />
)
<SortDesc className="h-4 w-4" />
) : (
<Sort className="h-4 w-4 shrink-0" />
)
Expand Down
4 changes: 2 additions & 2 deletions lib/api/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function getLinksForProject({
domain?: string;
tagId?: string;
search?: string;
sort?: "createdAt" | "clicks" | "lastClicked"; // descending for "createdAt" and "clicks", ascending for "lastClicked"
sort?: "createdAt" | "clicks" | "lastClicked"; // descending for all
page?: string;
userId?: string | null;
showArchived?: boolean;
Expand Down Expand Up @@ -52,7 +52,7 @@ export async function getLinksForProject({
user: true,
},
orderBy: {
[sort]: sort === "lastClicked" ? "asc" : "desc",
[sort]: "desc",
},
take: 100,
...(page && {
Expand Down

0 comments on commit 5d519f2

Please sign in to comment.