Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat: Caps page pagination

Richie McIlroy3d2ab1f1a1d4
diff --git a/apps/web/app/dashboard/caps/Caps.tsx b/apps/web/app/dashboard/caps/Caps.tsx
index dda276680..cb50de91b 100644
--- a/apps/web/app/dashboard/caps/Caps.tsx
+++ b/apps/web/app/dashboard/caps/Caps.tsx
@@ -2,7 +2,7 @@
import { Button } from "@cap/ui";
import moment from "moment";
import { VideoThumbnail } from "@/components/VideoThumbnail";
-import { useRouter } from "next/navigation";
+import { useRouter, useSearchParams } from "next/navigation";
import toast from "react-hot-toast";
import {
EyeIcon,
@@ -13,6 +13,15 @@ import {
} from "lucide-react";
import { useEffect, useState } from "react";
import { useSharedContext } from "@/app/dashboard/_components/DynamicSharedLayout";
+import {
+ Pagination,
+ PaginationContent,
+ PaginationEllipsis,
+ PaginationItem,
+ PaginationLink,
+ PaginationNext,
+ PaginationPrevious,
+} from "@cap/ui";
type videoData = {
id: string;
@@ -23,10 +32,25 @@ type videoData = {
totalReactions: number;
}[];
-export const Caps = ({ data }: { data: videoData }) => {
+export const Caps = ({ data, count }: { data: videoData; count: number }) => {
const { push } = useRouter();
+ const params = useSearchParams();
+ const page = Number(params.get("page")) || 1;
+ console.log("page: ", page);
const [analytics, setAnalytics] = useState<Record<string, number>>({});
const { user } = useSharedContext();
+ const limit = 16;
+ const totalPages = Math.ceil(count / limit);
+
+ const nextPage = () => {
+ push(`/dashboard/caps?page=${page + 1}`);
+ };
+
+ const prevPage = () => {
+ if (page > 1) {
+ push(`/dashboard/caps?page=${page - 1}`);
+ }
+ };
useEffect(() => {
const fetchAnalytics = async () => {
@@ -162,6 +186,64 @@ export const Caps = ({ data }: { data: videoData }) => {
</div>
</>
)}
+ <div className="pt-8">
+ <Pagination>
+ <PaginationContent>
+ <PaginationItem>
+ <PaginationPrevious
+ href={
+ process.env.NEXT_PUBLIC_URL +
+ `/dashboard/caps?page=${page === 1 ? page : page - 1}`
+ }
+ />
+ </PaginationItem>
+ <PaginationItem>
+ <PaginationLink
+ href={process.env.NEXT_PUBLIC_URL + `/dashboard/caps?page=1`}
+ isActive={page === 1}
+ >
+ 1
+ </PaginationLink>
+ </PaginationItem>
+ {page !== 1 && (
+ <PaginationItem>
+ <PaginationLink
+ href={
+ process.env.NEXT_PUBLIC_URL + `/dashboard/caps?page=${page}`
+ }
+ isActive={true}
+ >
+ {page}
+ </PaginationLink>
+ </PaginationItem>
+ )}
+ {totalPages > page + 1 && (
+ <PaginationItem>
+ <PaginationLink
+ href={
+ process.env.NEXT_PUBLIC_URL +
+ `/dashboard/caps?page=${page + 1}`
+ }
+ isActive={page === page + 1}
+ >
+ {page + 1}
+ </PaginationLink>
+ </PaginationItem>
+ )}
+ {page > 2 && <PaginationEllipsis />}
+ <PaginationItem>
+ <PaginationNext
+ href={
+ process.env.NEXT_PUBLIC_URL +
+ `/dashboard/caps?page=${
+ page === totalPages ? page : page + 1
+ }`
+ }
+ />
+ </PaginationItem>
+ </PaginationContent>
+ </Pagination>
+ </div>
</div>
);
};
diff --git a/apps/web/app/dashboard/caps/page.tsx b/apps/web/app/dashboard/caps/page.tsx
index 3bc4d3c8d..02598a6ac 100644
--- a/apps/web/app/dashboard/caps/page.tsx
+++ b/apps/web/app/dashboard/caps/page.tsx
@@ -1,16 +1,24 @@
import { Caps } from "./Caps";
import { db } from "@cap/database";
import { comments, videos } from "@cap/database/schema";
-import { desc, eq, sql } from "drizzle-orm";
+import { desc, eq, sql, count } from "drizzle-orm";
import { getCurrentUser } from "@cap/database/auth/session";
import { redirect } from "next/navigation";
export const revalidate = 0;
-export default async function CapsPage() {
+export default async function CapsPage({
+ searchParams,
+}: {
+ searchParams: { [key: string]: string | string[] | undefined };
+}) {
+ const page = Number(searchParams.page) || 1;
+ const limit = Number(searchParams.limit) || 16;
const user = await getCurrentUser();
const userId = user?.id as string;
+ console.log("page: ", page);
+
if (
user !== null &&
(user.name === null ||
@@ -21,6 +29,15 @@ export default async function CapsPage() {
return redirect("/onboarding");
}
+ const offset = (page - 1) * limit;
+
+ const totalCountResult = await db
+ .select({ count: count() })
+ .from(videos)
+ .where(eq(videos.ownerId, userId));
+
+ const totalCount = totalCountResult[0]?.count || 0;
+
const videoData = await db
.select({
id: videos.id,
@@ -34,7 +51,9 @@ export default async function CapsPage() {
.leftJoin(comments, eq(videos.id, comments.videoId))
.where(eq(videos.ownerId, userId))
.groupBy(videos.id, videos.ownerId, videos.name, videos.createdAt)
- .orderBy(desc(videos.createdAt));
+ .orderBy(desc(videos.createdAt))
+ .limit(limit)
+ .offset(offset);
- return <Caps data={videoData} />;
+ return <Caps data={videoData} count={totalCount} />;
}
diff --git a/apps/web/app/record/Record.tsx b/apps/web/app/record/Record.tsx
index daf53f646..1ee95466b 100644
--- a/apps/web/app/record/Record.tsx
+++ b/apps/web/app/record/Record.tsx
@@ -695,7 +695,7 @@ export const Record = ({
const videoId = await getLatestVideoId();
- if (segmentIndex === 1) {
+ if (segmentIndex === 0) {
const video = document.createElement("video");
video.src = URL.createObjectURL(videoSegment);
video.muted = true;
diff --git a/packages/ui/src/components/Button.tsx b/packages/ui/src/components/Button.tsx
index a27313bcd..3c46e3e6e 100644
--- a/packages/ui/src/components/Button.tsx
+++ b/packages/ui/src/components/Button.tsx
@@ -15,6 +15,7 @@ const buttonVariants = cva(
"group relative isolate font-medium inline-flex items-center justify-center overflow-hidden text-sm transition duration-300 ease-[cubic-bezier(0.4,0.36,0,1)] before:duration-300 before:ease-[cubic-bezier(0.4,0.36,0,1)] before:transtion-opacity rounded-full ring-1 before:pointer-events-none before:absolute before:inset-0 before:-z-10 before:rounded-full before:bg-gradient-to-b before:from-white/20 before:opacity-50 hover:before:opacity-100 after:pointer-events-none after:absolute after:inset-0 after:-z-10 after:rounded-full after:bg-gradient-to-b after:from-white/10 after:from-[46%] after:to-[54%] after:mix-blend-overlay dark:ring-0 bg-red-900 ring-2 text-white ring-900",
outline:
"default-button group relative isolate font-medium inline-flex items-center justify-center overflow-hidden text-sm transition duration-300 ease-[cubic-bezier(0.4,0.36,0,1)] before:duration-300 before:ease-[cubic-bezier(0.4,0.36,0,1)] before:transtion-opacity rounded-full ring-1 before:pointer-events-none before:absolute before:inset-0 before:-z-10 before:rounded-full before:bg-gradient-to-b before:from-white/20 before:opacity-50 hover:before:opacity-100 after:pointer-events-none after:absolute after:inset-0 after:-z-10 after:rounded-full after:bg-gradient-to-b after:from-white/10 after:from-[46%] after:to-[54%] after:mix-blend-overlay dark:ring-0 bg-white ring-2 text-primary ring-primary",
+ white: "bg-white text-primary ring-0",
secondary: "bg-secondary text-white hover:bg-secondary-1",
},
size: {
diff --git a/packages/ui/src/components/Pagination.tsx b/packages/ui/src/components/Pagination.tsx
new file mode 100644
index 000000000..d18a4e419
--- /dev/null
+++ b/packages/ui/src/components/Pagination.tsx
@@ -0,0 +1,120 @@
+import * as React from "react";
+import { ChevronLeft, ChevronRight, MoreHorizontal } from "lucide-react";
+
+import { classNames } from "@cap/utils";
+import { ButtonProps, buttonVariants } from "./Button";
+
+const Pagination = ({ className, ...props }: React.ComponentProps<"nav">) => (
+ <nav
+ role="navigation"
+ aria-label="pagination"
+ className={classNames("mx-auto flex w-full justify-center", className)}
+ {...props}
+ />
+);
+Pagination.displayName = "Pagination";
+
+const PaginationContent = React.forwardRef<
+ HTMLUListElement,
+ React.ComponentProps<"ul">
+>(({ className, ...props }, ref) => (
+ <ul
+ ref={ref}
+ className={classNames("flex flex-row items-center gap-1", className)}
+ {...props}
+ />
+));
+PaginationContent.displayName = "PaginationContent";
+
+const PaginationItem = React.forwardRef<
+ HTMLLIElement,
+ React.ComponentProps<"li">
+>(({ className, ...props }, ref) => (
+ <li ref={ref} className={classNames("", className)} {...props} />
+));
+PaginationItem.displayName = "PaginationItem";
+
+type PaginationLinkProps = {
+ isActive?: boolean;
+} & Pick<ButtonProps, "size"> &
+ React.ComponentProps<"a">;
+
+const PaginationLink = ({
+ className,
+ isActive,
+ size = "icon",
+ ...props
+}: PaginationLinkProps) => (
+ <a
+ aria-current={isActive ? "page" : undefined}
+ className={classNames(
+ buttonVariants({
+ variant: isActive ? "default" : "white",
+ size,
+ }),
+ className
+ )}
+ {...props}
+ />
+);
+PaginationLink.displayName = "PaginationLink";
+
+const PaginationPrevious = ({
+ className,
+ ...props
+}: React.ComponentProps<typeof PaginationLink>) => (
+ <PaginationLink
+ aria-label="Go to previous page"
+ size="default"
+ className={classNames("gap-1 pl-2.5", className)}
+ {...props}
+ >
+ <ChevronLeft className="h-4 w-4" />
+ <span>Previous</span>
+ </PaginationLink>
+);
+PaginationPrevious.displayName = "PaginationPrevious";
+
+const PaginationNext = ({
+ className,
+ ...props
+}: React.ComponentProps<typeof PaginationLink>) => (
+ <PaginationLink
+ aria-label="Go to next page"
+ size="default"
+ className={classNames("gap-1 pr-2.5", className)}
+ {...props}
+ >
+ <span>Next</span>
+ <ChevronRight className="h-4 w-4" />
+ </PaginationLink>
+);
+PaginationNext.displayName = "PaginationNext";
+
+const PaginationEllipsis = ({
+ className,
+ ...props
+}: React.ComponentProps<"span">) => (
+ <span
+ aria-hidden
+ className={classNames(
+ "flex h-9 w-9 items-center justify-center",
+ className
+ )}
+ {...props}
+ >
+ <MoreHorizontal className="h-4 w-4" />
+ <span className="sr-only">More pages</span>
+ </span>
+);
+PaginationEllipsis.displayName = "PaginationEllipsis";
+
+export {
+ Pagination,
+ PaginationContent,
+ PaginationEllipsis,
+ PaginationItem,
+ PaginationLink,
+ PaginationNext,
+ PaginationPrevious,
+};
diff --git a/packages/ui/src/index.tsx b/packages/ui/src/index.tsx
index ff53dcb45..d1397ab3b 100644
--- a/packages/ui/src/index.tsx
+++ b/packages/ui/src/index.tsx
@@ -18,6 +18,7 @@ export * from "./components/Popover";
export * from "./components/Card";
export * from "./components/NavigationMenu";
export * from "./components/Dropdown";
+export * from "./components/Pagination";
// Forms & Inputs
export * from "./components/input/Form";