-
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 #3465 from quantified-uncertainty/workflow-improve…
…ments Workflow improvements
- Loading branch information
Showing
32 changed files
with
541 additions
and
251 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,72 @@ | ||
import "server-only"; | ||
import { Prisma } from "@prisma/client"; | ||
|
||
import { ClientWorkflow } from "@quri/squiggle-ai"; | ||
|
||
import { prisma } from "@/lib/server/prisma"; | ||
import { getSessionUserOrRedirect } from "@/users/auth"; | ||
import { Paginated } from "@/lib/types"; | ||
import { checkRootUser, getSessionUserOrRedirect } from "@/users/auth"; | ||
|
||
import { decodeDbWorkflowToClientWorkflow } from "./storage"; | ||
|
||
export async function loadWorkflows({ | ||
limit = 20, | ||
}: { | ||
limit?: number; | ||
} = {}) { | ||
export type AiWorkflow = { | ||
workflow: ClientWorkflow; | ||
author: { | ||
username: string; | ||
}; | ||
}; | ||
|
||
export async function loadWorkflows( | ||
params: { | ||
allUsers?: boolean; | ||
cursor?: string; | ||
limit?: number; | ||
} = {} | ||
): Promise<Paginated<AiWorkflow>> { | ||
const sessionUser = await getSessionUserOrRedirect(); | ||
|
||
const limit = params.limit ?? 20; | ||
|
||
const where: Prisma.AiWorkflowWhereInput = {}; | ||
if (params.allUsers) { | ||
console.log("loading all workflows"); | ||
await checkRootUser(); | ||
} else { | ||
where.user = { email: sessionUser.email }; | ||
} | ||
|
||
const rows = await prisma.aiWorkflow.findMany({ | ||
orderBy: { createdAt: "desc" }, | ||
where: { | ||
user: { email: sessionUser.email }, | ||
cursor: params.cursor ? { id: params.cursor } : undefined, | ||
where, | ||
include: { | ||
user: { | ||
select: { | ||
asOwner: { | ||
select: { | ||
slug: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
take: limit + 1, | ||
}); | ||
|
||
const workflows = rows.map((row) => decodeDbWorkflowToClientWorkflow(row)); | ||
// TODO - it would be good to preserve author information in the client, but this would require a new type (ClientWorkflowWithAuthor?) | ||
const workflows = rows.map((row) => ({ | ||
workflow: decodeDbWorkflowToClientWorkflow(row), | ||
author: { username: row.user.asOwner?.slug ?? "[unknown]" }, | ||
})); | ||
|
||
const nextCursor = workflows[workflows.length - 1]?.workflow.id; | ||
|
||
async function loadMore(limit: number) { | ||
"use server"; | ||
return loadWorkflows({ ...params, cursor: nextCursor, limit }); | ||
} | ||
|
||
return { | ||
workflows: limit ? workflows.slice(0, limit) : workflows, | ||
hasMore: limit ? workflows.length > limit : false, | ||
items: workflows.slice(0, limit), | ||
loadMore: workflows.length > limit ? loadMore : undefined, | ||
}; | ||
} |
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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.