Merge pull request #821 from notmd/766_admin_enhancement

Allow to filter `user` by `display_name`
This commit is contained in:
Keith Stevens
2023-01-22 21:32:48 +09:00
committed by GitHub
9 changed files with 377 additions and 150 deletions
+2 -3
View File
@@ -3,7 +3,7 @@ import { useRouter } from "next/router";
import { useSession } from "next-auth/react";
import { useEffect } from "react";
import { getAdminLayout } from "src/components/Layout";
import UsersCell from "src/components/UsersCell";
import { UserTable } from "src/components/UserTable";
export { getDefaultStaticProps as getStaticProps } from "src/lib/default_static_props";
/**
@@ -28,7 +28,6 @@ const AdminIndex = () => {
}
router.push("/");
}, [router, session, status]);
return (
<>
<Head>
@@ -38,7 +37,7 @@ const AdminIndex = () => {
content="Conversational AI for everyone. An open source project to create a chat enabled GPT LLM run by LAION and contributors around the world."
/>
</Head>
<main className="oa-basic-theme">{status === "loading" ? "loading..." : <UsersCell />}</main>
<main>{status === "loading" ? "loading..." : <UserTable />}</main>
</>
);
};
+13 -4
View File
@@ -1,5 +1,5 @@
import { withRole } from "src/lib/auth";
import { oasstApiClient } from "src/lib/oasst_api_client";
import { FetchUsersParams, oasstApiClient } from "src/lib/oasst_api_client";
import prisma from "src/lib/prismadb";
/**
@@ -17,10 +17,16 @@ const PAGE_SIZE = 20;
* direction.
*/
const handler = withRole("admin", async (req, res) => {
const { cursor, direction } = req.query;
const { cursor, direction, searchDisplayName = "", sortKey = "username" } = req.query;
// First, get all the users according to the backend.
const all_users = await oasstApiClient.fetch_users(PAGE_SIZE, cursor as string, direction === "forward");
const { items: all_users, ...rest } = await oasstApiClient.fetch_users({
searchDisplayName: searchDisplayName as FetchUsersParams["searchDisplayName"],
direction: direction as FetchUsersParams["direction"],
limit: PAGE_SIZE,
cursor: cursor as FetchUsersParams["cursor"],
sortKey: sortKey === "username" || sortKey === "display_name" ? sortKey : undefined,
});
// Next, get all the users stored in the web's auth database to fetch their role.
const local_user_ids = all_users.map(({ id }) => id);
@@ -51,7 +57,10 @@ const handler = withRole("admin", async (req, res) => {
};
});
res.status(200).json(users);
res.status(200).json({
items: users,
...rest,
});
});
export default handler;