The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Fixed mobile view for filters
Steven Teyc48bb2ea3c9f
diff --git a/components/app/links/link-card.tsx b/components/app/links/link-card.tsxindex a1f648cff6..9f85d80c82 100644--- a/components/app/links/link-card.tsx+++ b/components/app/links/link-card.tsx@@ -62,7 +62,7 @@ export default function LinkCard({ props }: { props: LinkProps }) {const expired = expiresAt && new Date() > new Date(expiresAt);return (- <div className="relative bg-white p-4 rounded-lg shadow hover:shadow-md transition-all">+ <div className="relative bg-white p-3 pr-1 sm:p-4 rounded-lg shadow hover:shadow-md transition-all"><LinkQRModal /><AddEditLinkModal /><ArchiveLinkModal />@@ -76,23 +76,33 @@ export default function LinkCard({ props }: { props: LinkProps }) {)}</div><li className="relative flex justify-between items-center">- <div className="relative flex items-center space-x-4 shrink">+ <div className="relative flex items-center space-x-2 sm:space-x-4 shrink"><BlurImagesrc={`https://www.google.com/s2/favicons?sz=64&domain_url=${apexDomain}`}alt={apexDomain}- className="w-10 h-10 rounded-full"+ className="w-8 h-8 sm:w-10 sm:h-10 rounded-full"width={20}height={20}/><div><div className="flex items-center space-x-2 max-w-fit"><a- className="text-blue-800 text-sm sm:text-base font-semibold truncate w-32 sm:w-full"+ className="text-blue-800 text-sm sm:text-base font-semibold truncate w-24 sm:w-full"href={linkConstructor({ key, domain })}target="_blank"rel="noreferrer">- {linkConstructor({ key, domain, pretty: true })}+ <span className="hidden sm:block">+ {linkConstructor({ key, domain, pretty: true })}+ </span>+ <span className="sm:hidden">+ {linkConstructor({+ key,+ domain,+ pretty: true,+ noDomain: true,+ })}+ </span></a><CopyButton url={linkConstructor({ key, domain })} /><button@@ -116,7 +126,7 @@ export default function LinkCard({ props }: { props: LinkProps }) {</a></Link></div>- <h3 className="text-sm font-medium text-gray-700 truncate max-w-xs md:max-w-md lg:max-w-2xl xl:max-w-3xl">+ <h3 className="text-sm font-medium text-gray-700 truncate max-w-[200px] md:max-w-md lg:max-w-2xl xl:max-w-3xl">{url}</h3></div>@@ -126,7 +136,7 @@ export default function LinkCard({ props }: { props: LinkProps }) {<p className="text-sm hidden sm:block text-gray-500 whitespace-nowrap mr-3">Added {timeAgo(createdAt)}</p>- <p className="text-sm sm:hidden text-gray-500 whitespace-nowrap mr-3">+ <p className="text-sm sm:hidden text-gray-500 whitespace-nowrap mr-1">{timeAgo(createdAt, true)}</p><Popoverdiff --git a/components/app/links/link-filters/index.tsx b/components/app/links/link-filters/index.tsxindex 92ab663391..1bffb040dc 100644--- a/components/app/links/link-filters/index.tsx+++ b/components/app/links/link-filters/index.tsx@@ -1,19 +1,59 @@import { useRouter } from "next/router";+import { useState } from "react";+import { AnimatePresence, motion } from "framer-motion";+import { ChevronDown, Filter } from "@/components/shared/icons";+import { AnimationSettings } from "../../modals/add-edit-link-modal/advanced-settings";+import LinkSort from "./link-sort";import StatusFilter from "./status-filter";import UserFilter from "./user-filter";export default function LinkFilters() {const router = useRouter();const { slug } = router.query as { slug?: string };+ const [showFilters, setShowFilters] = useState(false);return (- <div className="my-5 flex justify-end">- <StatusFilter />- {slug && (- <>- <div className="w-4" />- <UserFilter />- </>- )}- </div>+ <>+ {/* Mobile filters */}+ <button+ onClick={() => setShowFilters(!showFilters)}+ className="flex justify-between items-center space-x-2 sm:hidden bg-white w-full my-5 px-3 py-2.5 rounded-md shadow hover:shadow-md active:scale-95 transition-all duration-75"+ >+ <div className="flex items-center space-x-2 text-gray-700">+ <Filter className="h-4 w-4 shrink-0" />+ <p className="text-sm">Filters</p>+ </div>+ <ChevronDown+ className={`w-5 h-5 text-gray-400 ${+ showFilters ? "transform rotate-180" : ""+ } transition-all duration-75`}+ />+ </button>+ <AnimatePresence>+ {showFilters && (+ <motion.div+ className="border-t border-gray-200 w-full flex flex-col space-y-2 py-5"+ {...AnimationSettings}+ >+ <LinkSort />+ <StatusFilter />+ {slug && <UserFilter />}+ </motion.div>+ )}+ </AnimatePresence>++ {/* Desktop filters */}+ <div className="w-full my-5 hidden sm:flex justify-end">+ <LinkSort />+ {/* Weird workaround because if I put `space-x-2` in the parent it misaligns the popover dropdown */}+ <div className="w-4" />+ <StatusFilter />+ {slug && (+ <>+ <div className="w-4" />+ <UserFilter />+ </>+ )}+ </div>+ </>);}diff --git a/components/app/links/link-filters/link-sort.tsx b/components/app/links/link-filters/link-sort.tsxnew file mode 100644index 0000000000..70f0ba8628--- /dev/null+++ b/components/app/links/link-filters/link-sort.tsx@@ -0,0 +1,87 @@+import { useRouter } from "next/router";+import { useMemo, useState } from "react";+import BlurImage from "@/components/shared/blur-image";+import {+ ChevronDown,+ Sort,+ SortDesc,+ Tick,+ X,+} from "@/components/shared/icons";+import Popover from "@/components/shared/popover";++const sortOptions = [+ {+ display: "Date Added",+ slug: "createdAt",+ },+ {+ display: "Number of Clicks",+ slug: "clicks",+ },+];++export default function LinkSort() {+ const [openPopover, setOpenPopover] = useState(false);+ const router = useRouter();+ const { sort } = router.query as { sort?: string };++ const selectedSort = useMemo(() => {+ return sortOptions.find((s) => s.slug === sort) || sortOptions[0];+ }, [sort]);++ return (+ <Popover+ content={+ <div className="w-full md:w-48 p-2">+ {sortOptions.map(({ display, slug }) => (+ <button+ key={slug}+ onClick={() => {+ let newQuery;+ newQuery = {+ ...router.query,+ sort: slug,+ };+ if (slug === "createdAt") {+ delete newQuery.sort;+ }+ const { slug: omit, ...finalQuery } = newQuery;+ router.push({+ pathname: `/${router.query.slug}`,+ query: finalQuery,+ });+ }}+ className="flex items-center justify-between space-x-2 px-1 py-2 w-full rounded-md hover:bg-gray-100 active:bg-gray-200"+ >+ <div className="flex items-center justify-start space-x-2">+ <SortDesc className="w-4 h-4" />+ <p className="text-sm text-gray-700">{display}</p>+ </div>+ {selectedSort.slug === slug && (+ <Tick className="h-4 w-4" aria-hidden="true" />+ )}+ </button>+ ))}+ </div>+ }+ openPopover={openPopover}+ setOpenPopover={setOpenPopover}+ >+ <button+ onClick={() => setOpenPopover(!openPopover)}+ className="flex justify-between items-center space-x-2 bg-white w-full sm:w-48 px-3 py-2.5 rounded-md shadow hover:shadow-md active:scale-95 transition-all duration-75"+ >+ <div className="flex items-center space-x-2 text-gray-700">+ <Sort className="h-4 w-4 shrink-0" />+ <p className="text-sm">Sort by</p>+ </div>+ <ChevronDown+ className={`w-5 h-5 text-gray-400 ${+ openPopover ? "transform rotate-180" : ""+ } transition-all duration-75`}+ />+ </button>+ </Popover>+ );+}diff --git a/components/app/links/link-filters/status-filter.tsx b/components/app/links/link-filters/status-filter.tsxindex 16436ad9b1..752b3c1976 100644--- a/components/app/links/link-filters/status-filter.tsx+++ b/components/app/links/link-filters/status-filter.tsx@@ -1,5 +1,5 @@import { useRouter } from "next/router";-import { useState } from "react";+import { useMemo, useState } from "react";import { ChevronDown, Tick } from "@/components/shared/icons";import Popover from "@/components/shared/popover";@@ -39,11 +39,15 @@ export default function StatusFilter() {const [openPopover, setOpenPopover] = useState(false);const router = useRouter();const { status } = router.query as { status?: string };- const selectedStatus = !status- ? ["active"]- : status === "all" || status === "none"- ? ["active", "expired", "archived"]- : status.split(",");+ const selectedStatus = useMemo(() => {+ if (!status) {+ return ["active"];+ } else if (status === "all" || status === "none") {+ return ["active", "expired", "archived"];+ } else {+ return status.split(",");+ }+ }, [status]);return (<Popover@@ -98,7 +102,7 @@ export default function StatusFilter() {><buttononClick={() => setOpenPopover(!openPopover)}- className="flex justify-between items-center space-x-2 bg-white w-44 px-3 py-2.5 rounded-md shadow hover:shadow-md active:scale-95 transition-all duration-75"+ className="flex justify-between items-center space-x-2 bg-white w-full sm:w-44 px-3 py-2.5 rounded-md shadow hover:shadow-md active:scale-95 transition-all duration-75"><div className="flex items-center space-x-2 text-gray-700"><div className="flex -space-x-1">diff --git a/components/app/links/link-filters/user-filter.tsx b/components/app/links/link-filters/user-filter.tsxindex 170bf3d3d4..61a28952a2 100644--- a/components/app/links/link-filters/user-filter.tsx+++ b/components/app/links/link-filters/user-filter.tsx@@ -64,7 +64,7 @@ export default function UserFilter() {><buttononClick={() => setOpenPopover(!openPopover)}- className="flex justify-between items-center space-x-2 bg-white w-56 px-3 py-2.5 rounded-md shadow hover:shadow-md active:scale-95 transition-all duration-75"+ className="flex justify-between items-center space-x-1 bg-white w-full md:w-56 px-3 py-2 rounded-md shadow hover:shadow-md active:scale-95 transition-all duration-75"><div className="flex items-center space-x-2 text-gray-700 w-44"><Search className="h-4 w-4 shrink-0" />@@ -86,6 +86,7 @@ export default function UserFilter() {query: finalQuery,});}}+ className="p-1 rounded-full hover:bg-gray-100 active:bg-gray-200 transition-all duration-75"><X className="h-4 w-4" /></div>diff --git a/components/app/modals/archive-link-modal.tsx b/components/app/modals/archive-link-modal.tsxindex b5adf7181c..62c1ea0d7f 100644--- a/components/app/modals/archive-link-modal.tsx+++ b/components/app/modals/archive-link-modal.tsx@@ -75,7 +75,6 @@ function ArchiveLinkModal({},).then(async (res) => {setArchiving(false);- console.log(res.status, "mutating");if (res.status === 200) {mutate(domaindiff --git a/components/shared/icons/index.tsx b/components/shared/icons/index.tsxindex e05b789cac..6562751032 100644--- a/components/shared/icons/index.tsx+++ b/components/shared/icons/index.tsx@@ -34,3 +34,5 @@ export { default as ThreeDots } from "./three-dots";export { default as Random } from "./random";export { default as Filter } from "./filter";export { default as Search } from "./search";+export { default as Sort } from "./sort";+export { default as SortDesc } from "./sort-desc";diff --git a/components/shared/icons/sort-desc.tsx b/components/shared/icons/sort-desc.tsxnew file mode 100644index 0000000000..bd4b96adc2--- /dev/null+++ b/components/shared/icons/sort-desc.tsx@@ -0,0 +1,22 @@+export default function SortDesc({ className }: { className: string }) {+ return (+ <svg+ fill="none"+ shapeRendering="geometricPrecision"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ viewBox="0 0 24 24"+ width="24"+ height="24"+ className={className}+ >+ <path d="M11 5h10"></path>+ <path d="M11 9h7"></path>+ <path d="M11 13h4"></path>+ <path d="m3 17 3 3 3-3"></path>+ <path d="M6 18V4"></path>+ </svg>+ );+}diff --git a/components/shared/icons/sort.tsx b/components/shared/icons/sort.tsxnew file mode 100644index 0000000000..04e008a81d--- /dev/null+++ b/components/shared/icons/sort.tsx@@ -0,0 +1,18 @@+export default function Sort({ className }: { className: string }) {+ return (+ <svg+ fill="none"+ shapeRendering="geometricPrecision"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ viewBox="0 0 24 24"+ width="24"+ height="24"+ className={className}+ >+ <path d="M15 18H3M21 6H3M17 12H3" />+ </svg>+ );+}diff --git a/components/shared/popover.tsx b/components/shared/popover.tsxindex cdc135b83d..9841bb29d2 100644--- a/components/shared/popover.tsx+++ b/components/shared/popover.tsx@@ -43,14 +43,14 @@ export default function Popover({return (<>- <div className="sm:hidden">{children}</div>+ <div className="md:hidden">{children}</div><AnimatePresence>{openPopover && (<><motion.divref={mobileTooltipRef}key="mobile-tooltip"- className="group fixed bottom-0 inset-x-0 z-40 w-screen sm:hidden cursor-grab active:cursor-grabbing"+ className="group fixed bottom-0 inset-x-0 z-40 w-screen md:hidden cursor-grab active:cursor-grabbing"initial={{ y: "100%" }}animate={{y: openPopover ? 0 : "100%",@@ -76,7 +76,7 @@ export default function Popover({</motion.div><motion.divkey="mobile-tooltip-backdrop"- className="fixed inset-0 z-30 bg-gray-100 bg-opacity-10 backdrop-blur sm:hidden"+ className="fixed inset-0 z-30 bg-gray-100 bg-opacity-10 backdrop-blur md:hidden"initial={{ opacity: 0 }}animate={{ opacity: 1 }}exit={{ opacity: 0 }}@@ -90,13 +90,13 @@ export default function Popover({open={width > 640 && openPopover}onOpenChange={width > 640 && setOpenPopover}>- <PopoverPrimitive.Trigger className="hidden sm:inline-flex" asChild>+ <PopoverPrimitive.Trigger className="hidden md:inline-flex" asChild>{children}</PopoverPrimitive.Trigger><PopoverPrimitive.ContentsideOffset={4}align={align}- className="hidden sm:block animate-slide-up-fade items-center rounded-md overflow-hidden z-20 bg-white border border-gray-200 drop-shadow-lg"+ className="hidden md:block animate-slide-up-fade items-center rounded-md overflow-hidden z-20 bg-white border border-gray-200 drop-shadow-lg"><PopoverPrimitive.Arrow className="fill-current text-white" />{content}diff --git a/lib/api/links.ts b/lib/api/links.tsindex 13cf1d56b5..d06a888d96 100644--- a/lib/api/links.ts+++ b/lib/api/links.ts@@ -57,12 +57,12 @@ const getFiltersFromStatus = (status: string) => {export async function getLinksForProject({domain,status = "active",- orderBy = "createdAt",+ sort = "createdAt",userId,}: {domain: string;status?: string;- orderBy?: "createdAt" | "clicks"; // always descending for both+ sort?: "createdAt" | "clicks"; // always descending for bothuserId?: string;}): Promise<LinkProps[]> {const filters = getFiltersFromStatus(status);@@ -73,7 +73,7 @@ export async function getLinksForProject({...(userId && { userId }),},orderBy: {- [orderBy]: "desc",+ [sort]: "desc",},take: 100,});diff --git a/lib/utils.ts b/lib/utils.tsindex 28f94cafcc..965c32c3da 100644--- a/lib/utils.ts+++ b/lib/utils.ts@@ -60,16 +60,19 @@ export function linkConstructor({domain = "dub.sh",localhost,pretty,+ noDomain,}: {key: string;domain?: string;localhost?: boolean;pretty?: boolean;+ noDomain?: boolean;}) {const link = `${localhost ? "http://localhost:3000" : `https://${domain}`}/${key}`;+ if (noDomain) return `/${key}`;return pretty ? link.replace(/^https?:\/\//, "") : link;}diff --git a/pages/api/projects/[slug]/domains/[domain]/links/index.ts b/pages/api/projects/[slug]/domains/[domain]/links/index.tsindex b8731f5f89..cb1de12180 100644--- a/pages/api/projects/[slug]/domains/[domain]/links/index.ts+++ b/pages/api/projects/[slug]/domains/[domain]/links/index.ts@@ -6,14 +6,16 @@ export default withProjectAuth(async (req: NextApiRequest, res: NextApiResponse, _, session) => {// GET /api/projects/[slug]/domains/[domain]/links - Get all links for a projectif (req.method === "GET") {- const { domain, status, userId } = req.query as {+ const { domain, status, sort, userId } = req.query as {domain: string;status?: string;+ sort?: "createdAt" | "clicks";userId?: string;};const response = await getLinksForProject({domain,status,+ sort,userId,});return res.status(200).json(response);diff --git a/prisma/schema.prisma b/prisma/schema.prismaindex c943e442fa..e9ec94eef5 100644--- a/prisma/schema.prisma+++ b/prisma/schema.prisma@@ -149,12 +149,12 @@ model Link {@@unique([domain, key])// indices for links per domain/project- @@index([domain, archived, createdAt(sort: Desc)])- @@index([domain, archived, clicks(sort: Desc)])+ @@index([domain, archived, expiresAt, createdAt(sort: Desc)])+ @@index([domain, archived, expiresAt, clicks(sort: Desc)])// indices for links per domain/project specific to a user- @@index([domain, archived, userId, createdAt(sort: Desc)])- @@index([domain, archived, userId, clicks(sort: Desc)])+ @@index([domain, archived, expiresAt, userId, createdAt(sort: Desc)])+ @@index([domain, archived, expiresAt, userId, clicks(sort: Desc)])// TODO: might wanna add indices for utm_source, utm_medium, utm_campaign}\ No newline at end of file