-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3421 from quantified-uncertainty/ai-pagination
/ai pagination
- Loading branch information
Showing
9 changed files
with
177 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -1,24 +1,24 @@ | ||
import { ClientWorkflow } from "@quri/squiggle-ai"; | ||
import { z } from "zod"; | ||
|
||
import { prisma } from "@/prisma"; | ||
import { getUserOrRedirect } from "@/server/helpers"; | ||
import { numberInString } from "@/lib/zodUtils"; | ||
import { loadWorkflows } from "@/server/ai/data"; | ||
|
||
import { decodeDbWorkflowToClientWorkflow } from "../../server/ai/storage"; | ||
import { AiDashboard } from "./AiDashboard"; | ||
|
||
export default async function AiPage() { | ||
const user = await getUserOrRedirect(); | ||
export default async function SessionsPage({ | ||
searchParams, | ||
}: { | ||
searchParams: { [key: string]: string | string[] | undefined }; | ||
}) { | ||
const { limit } = z | ||
.object({ | ||
limit: numberInString.optional(), | ||
}) | ||
.parse(searchParams); | ||
|
||
const rows = await prisma.aiWorkflow.findMany({ | ||
orderBy: { createdAt: "desc" }, | ||
where: { | ||
user: { email: user.email }, | ||
}, | ||
}); | ||
const { workflows, hasMore } = await loadWorkflows({ limit }); | ||
|
||
const workflows: ClientWorkflow[] = rows.map((row) => | ||
decodeDbWorkflowToClientWorkflow(row) | ||
return ( | ||
<AiDashboard initialWorkflows={workflows} hasMoreWorkflows={hasMore} /> | ||
); | ||
|
||
return <AiDashboard initialWorkflows={workflows} />; | ||
} |
This file contains 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 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,29 @@ | ||
import { FC } from "react"; | ||
|
||
import { useUpdateSearchParams } from "@/hooks/useUpdateSearchParams"; | ||
|
||
import { LoadMore } from "./LoadMore"; | ||
|
||
type Props = { | ||
param?: string; | ||
}; | ||
|
||
export const LoadMoreViaSearchParam: FC<Props> = ({ param = "limit" }) => { | ||
const updateSearchParams = useUpdateSearchParams(); | ||
|
||
const action = (count: number) => { | ||
updateSearchParams( | ||
(params) => { | ||
if (params.get(param)) { | ||
const oldValue = parseInt(params.get(param) as string); | ||
params.set(param, String(oldValue + count)); | ||
} else { | ||
params.set(param, String(count)); | ||
} | ||
}, | ||
{ mode: "replace", scroll: false } | ||
); | ||
}; | ||
|
||
return <LoadMore loadNext={action} />; | ||
}; |
This file contains 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,29 @@ | ||
import { usePathname, useRouter, useSearchParams } from "next/navigation"; | ||
import { useCallback } from "react"; | ||
|
||
export function useUpdateSearchParams() { | ||
const router = useRouter(); | ||
const pathname = usePathname(); | ||
const searchParams = useSearchParams(); | ||
|
||
const updateSearchParams = useCallback( | ||
( | ||
update: (params: URLSearchParams) => void, | ||
{ | ||
mode = "push", | ||
scroll = true, | ||
}: { | ||
mode?: "push" | "replace"; | ||
scroll?: boolean; | ||
} = {} | ||
) => { | ||
const currentParams = new URLSearchParams(searchParams.toString()); | ||
update(currentParams); | ||
const method = mode === "push" ? router.push : router.replace; | ||
method(`${pathname}?${currentParams}`, { scroll }); | ||
}, | ||
[pathname, searchParams, router] | ||
); | ||
|
||
return updateSearchParams; | ||
} |
This file contains 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,18 @@ | ||
import { z } from "zod"; | ||
|
||
export const numberInString = z.string().transform((val, ctx) => { | ||
const parsed = parseInt(val); | ||
if (isNaN(parsed)) { | ||
ctx.addIssue({ | ||
code: z.ZodIssueCode.custom, | ||
message: "Not a number", | ||
}); | ||
|
||
// This is a special symbol you can use to | ||
// return early from the transform function. | ||
// It has type `never` so it does not affect the | ||
// inferred return type. | ||
return z.NEVER; | ||
} | ||
return parsed; | ||
}); |
This file contains 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,29 @@ | ||
import "server-only"; | ||
|
||
import { prisma } from "@/prisma"; | ||
|
||
import { getUserOrRedirect } from "../helpers"; | ||
import { decodeDbWorkflowToClientWorkflow } from "./storage"; | ||
|
||
export async function loadWorkflows({ | ||
limit = 20, | ||
}: { | ||
limit?: number; | ||
} = {}) { | ||
const user = await getUserOrRedirect(); | ||
|
||
const rows = await prisma.aiWorkflow.findMany({ | ||
orderBy: { createdAt: "desc" }, | ||
where: { | ||
user: { email: user.email }, | ||
}, | ||
take: limit + 1, | ||
}); | ||
|
||
const workflows = rows.map((row) => decodeDbWorkflowToClientWorkflow(row)); | ||
|
||
return { | ||
workflows: limit ? workflows.slice(0, limit) : workflows, | ||
hasMore: limit ? workflows.length > limit : false, | ||
}; | ||
} |