Skip to content

Commit

Permalink
sort by last clicked beta
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey committed Sep 5, 2023
1 parent 418caba commit 6726294
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 9 deletions.
24 changes: 22 additions & 2 deletions app/ui/number.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,36 @@ export default function Number({
value,
unit = "clicks",
children,
lastClicked,
}: {
value?: number | null;
unit?: string;
children: ReactNode;
lastClicked?: Date | null;
}) {
if (!value || value < 1000) {
if ((!value || value < 1000) && !lastClicked) {
return children;
}
return (
<Tooltip content={`${nFormatter(value, { full: true })} ${unit}`}>
<Tooltip
content={
<div className="block max-w-xs px-4 py-2 text-center text-sm text-gray-700">
<p className="text-sm font-semibold text-gray-700">
{nFormatter(value || 0, { full: true })} {unit}
</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",
})}
</p>
)}
</div>
}
>
{children}
</Tooltip>
);
Expand Down
3 changes: 2 additions & 1 deletion components/app/links/link-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default function LinkCard({
url,
rewrite,
createdAt,
lastClicked,
archived,
tagId,
comments,
Expand Down Expand Up @@ -364,7 +365,7 @@ export default function LinkCard({
</div>

<div className="flex items-center space-x-2">
<Number value={clicks}>
<Number value={clicks} lastClicked={lastClicked}>
<Link
onClick={(e) => {
e.stopPropagation();
Expand Down
34 changes: 30 additions & 4 deletions components/app/links/link-sort.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@ import { useRouter } from "next/router";
import { useMemo, useState } from "react";
import IconMenu from "@/components/shared/icon-menu";
import { Sort, Tick } from "@/components/shared/icons";
import { ChevronDown, SortDesc } from "lucide-react";
import { ChevronDown, SortAsc, SortDesc } from "lucide-react";
import Popover from "#/ui/popover";

const sortOptions = [
{
display: "Date Added",
slug: "createdAt",
order: "desc",
},
{
display: "Number of Clicks",
slug: "clicks",
order: "desc",
},
// {
// display: "Last Clicked",
// slug: "lastClicked",
// order: "asc",
// },
];

export default function LinkSort() {
Expand All @@ -29,7 +36,7 @@ export default function LinkSort() {
<Popover
content={
<div className="w-full p-2 md:w-48">
{sortOptions.map(({ display, slug }) => (
{sortOptions.map(({ display, slug, order }) => (
<button
key={slug}
onClick={() => {
Expand All @@ -52,7 +59,13 @@ export default function LinkSort() {
>
<IconMenu
text={display}
icon={<SortDesc className="h-4 w-4" />}
icon={
order === "desc" ? (
<SortDesc className="h-4 w-4" />
) : (
<SortAsc className="h-4 w-4" />
)
}
/>
{selectedSort.slug === slug && (
<Tick className="h-4 w-4" aria-hidden="true" />
Expand All @@ -68,7 +81,20 @@ export default function LinkSort() {
onClick={() => setOpenPopover(!openPopover)}
className="flex w-48 items-center justify-between space-x-2 rounded-md bg-white px-3 py-2.5 shadow transition-all duration-75 hover:shadow-md active:scale-95"
>
<IconMenu text="Sort by" icon={<Sort className="h-4 w-4 shrink-0" />} />
<IconMenu
text={sort ? selectedSort.display : "Sort by"}
icon={
sort ? (
selectedSort.order === "desc" ? (
<SortDesc className="h-4 w-4" />
) : (
<SortAsc className="h-4 w-4" />
)
) : (
<Sort className="h-4 w-4 shrink-0" />
)
}
/>
<ChevronDown
className={`h-5 w-5 text-gray-400 ${
openPopover ? "rotate-180 transform" : ""
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"; // always descending for both
sort?: "createdAt" | "clicks" | "lastClicked"; // descending for "createdAt" and "clicks", ascending for "lastClicked"
page?: string;
userId?: string | null;
showArchived?: boolean;
Expand Down Expand Up @@ -52,7 +52,7 @@ export async function getLinksForProject({
user: true,
},
orderBy: {
[sort]: "desc",
[sort]: sort === "lastClicked" ? "asc" : "desc",
},
take: 100,
...(page && {
Expand Down
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ model Link {
@@index(password)
@@index(createdAt(sort: Desc))
@@index(clicks(sort: Desc))
@@index(lastClicked(sort: Asc))
@@index(userId)
@@fulltext([key, url])
}
Expand Down

0 comments on commit 6726294

Please sign in to comment.