-
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.
author in new AiWorkflow type; refactor load more; show user icon on …
…not-owned workflows
- Loading branch information
Showing
11 changed files
with
169 additions
and
107 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,36 +1,72 @@ | ||
import { Prisma } from "@prisma/client"; | ||
|
||
import { ClientWorkflow } from "@quri/squiggle-ai"; | ||
|
||
import { prisma } from "@/lib/server/prisma"; | ||
import { Paginated } from "@/lib/types"; | ||
import { checkRootUser, getSessionUserOrRedirect } from "@/users/auth"; | ||
|
||
import { decodeDbWorkflowToClientWorkflow } from "./storage"; | ||
|
||
export async function loadWorkflows({ | ||
limit = 20, | ||
allUsers = false, | ||
}: { | ||
limit?: number; | ||
allUsers?: boolean; | ||
} = {}) { | ||
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 (allUsers) { | ||
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" }, | ||
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
Oops, something went wrong.