Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade next-admin to 3.2.5 #110

Merged
merged 5 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@heroicons/react": "^2.0.16",
"@next-auth/prisma-adapter": "^1.0.5",
"@postlight/parser": "^2.2.3",
"@premieroctet/next-admin": "^3.0.0",
"@premieroctet/next-admin": "^3.2.5",
"@prisma/client": "^4.11.0",
"@radix-ui/react-dialog": "^1.0.3",
"@radix-ui/react-navigation-menu": "^1.1.2",
Expand Down
11 changes: 9 additions & 2 deletions src/actions/nextadmin.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
'use server';
import client from '@/lib/db';
import { options } from '@/utils/nextadmin';
import { ActionParams } from '@premieroctet/next-admin';
import { submitForm } from '@premieroctet/next-admin/dist/actions';
import { ActionParams, ModelName } from '@premieroctet/next-admin';
import { deleteResourceItems, submitForm } from '@premieroctet/next-admin/dist/actions';

export const submitFormAction = async (
params: ActionParams,
formData: FormData
) => {
return submitForm({ ...params, options, prisma: client }, formData);
};

export const deleteItem = async (
model: ModelName,
ids: string[] | number[]
) => {
return deleteResourceItems(client, model, ids);
};
116 changes: 30 additions & 86 deletions src/app/(admin)/admin/[[...nextadmin]]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,22 @@
import { authOptions } from '@/pages/api/auth/[...nextauth]';
import client from '@/lib/db';
import { getServerSession } from 'next-auth';
import { redirect } from 'next/navigation';
import { NextAdmin } from '@premieroctet/next-admin';
import { getPropsFromParams } from '@premieroctet/next-admin/dist/appRouter';
import '@/theme/admin.css';
import '@premieroctet/next-admin/dist/styles.css';
import schema from '../../../../../prisma/json-schema/json-schema.json';
import { options } from '@/utils/nextadmin';
import { submitFormAction } from '@/actions/nextadmin';
import { deleteItem, submitFormAction } from '@/actions/nextadmin';
import Dashboard, { DashboardProps } from '@/components/admin/Dashboard';
import {
linksByDay,
linksByDomain,
newDigestByMonth,
newUsersByMonth,
} from '@/lib/adminQueries';
import { Digest, Team } from '@prisma/client';
import { Card, Col, Grid, Metric, Text } from '@tremor/react';
import DataOverTime from '@/components/admin/widgets/DataOverTime';
import LinksByWebsite from '@/components/admin/widgets/LinksByWebsite';
import LinksOverTime from '@/components/admin/widgets/LinksOverTime';
import client from '@/lib/db';
import { authOptions } from '@/pages/api/auth/[...nextauth]';
import '@/theme/admin.css';
import { options } from '@/utils/nextadmin';
import { NextAdmin } from '@premieroctet/next-admin';
import { getPropsFromParams } from '@premieroctet/next-admin/dist/appRouter';
import '@premieroctet/next-admin/dist/styles.css';
import { getServerSession } from 'next-auth';
import { redirect } from 'next/navigation';
import schema from '../../../../../prisma/json-schema/json-schema.json';

type DashboardProps = {
newUsersByMonth: Awaited<ReturnType<typeof newUsersByMonth>>;
newDigestByMonth: Awaited<ReturnType<typeof newDigestByMonth>>;
linksByDomain: Awaited<ReturnType<typeof linksByDomain>>;
linksCount: number;
latestTeam: Team | null;
latestDigest: Digest | null;
linksByDay: Awaited<ReturnType<typeof linksByDay>>;
};

export default async function AdminPage({
params,
Expand All @@ -52,76 +39,33 @@ export default async function AdminPage({
prisma: client,
schema,
action: submitFormAction,
deleteAction: deleteItem
});

const dashboardProps: DashboardProps | undefined = !params.nextadmin
? {
newUsersByMonth: await newUsersByMonth(),
newDigestByMonth: await newDigestByMonth(),
linksByDomain: await linksByDomain(),
linksByDay: await linksByDay(),
linksCount: await client.link.count(),
latestDigest: await client.digest.findFirst({
where: { publishedAt: { not: null } },
orderBy: { createdAt: 'desc' },
include: {
team: true,
},
}),
latestTeam: await client.team.findFirst({
orderBy: { createdAt: 'desc' },
}),
}
newUsersByMonth: await newUsersByMonth(),
newDigestByMonth: await newDigestByMonth(),
linksByDomain: await linksByDomain(),
linksByDay: await linksByDay(),
linksCount: await client.link.count(),
latestDigest: await client.digest.findFirst({
where: { publishedAt: { not: null } },
orderBy: { createdAt: 'desc' },
include: {
team: true,
},
}),
latestTeam: await client.team.findFirst({
orderBy: { createdAt: 'desc' },
}),
}
: undefined;

return (
<NextAdmin
{...props}
dashboard={
dashboardProps && (
<Grid numItems={1} numItemsSm={2} numItemsLg={3} className="gap-2">
<Col numColSpan={1} numColSpanLg={2}>
<DataOverTime
data={{
newUsersByMonth: dashboardProps?.newUsersByMonth,
newDigestByMonth: dashboardProps?.newDigestByMonth,
}}
/>
</Col>
<LinksByWebsite data={dashboardProps.linksByDomain} />
<Col>
<Card>
<Text>Dernier digest publié</Text>
<Metric>
<a
// @ts-expect-error
href={`${dashboardProps.latestDigest?.team?.slug}/${dashboardProps.latestDigest?.slug}`}
target="_blank"
>
{dashboardProps.latestDigest?.title} par {/* @ts-ignore */}
{dashboardProps.latestDigest?.team.name}
</a>
</Metric>
</Card>
</Col>
<Card>
<Text>Dernière team créée</Text>
<Metric>
<a href={`${dashboardProps.latestTeam?.slug}`} target="_blank">
{dashboardProps.latestTeam?.name}
</a>
</Metric>
</Card>
<Card>
<Text>Total des liens</Text>
<Metric>{dashboardProps?.linksCount}</Metric>
</Card>
<Col numColSpan={3} numColSpanLg={3}>
<LinksOverTime data={{ linksByDay: dashboardProps.linksByDay }} />
</Col>
</Grid>
)
}
dashboard={dashboardProps && <Dashboard {...dashboardProps} />}
/>
);
}
65 changes: 65 additions & 0 deletions src/components/admin/Dashboard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { linksByDay, linksByDomain, newDigestByMonth, newUsersByMonth } from "@/lib/adminQueries";
import { Digest, Team } from "@prisma/client";
import { Card, Col, Grid, Metric, Text } from "@tremor/react";
import DataOverTime from "./widgets/DataOverTime";
import LinksByWebsite from "./widgets/LinksByWebsite";
import LinksOverTime from "./widgets/LinksOverTime";


export type DashboardProps = {
newUsersByMonth: Awaited<ReturnType<typeof newUsersByMonth>>;
newDigestByMonth: Awaited<ReturnType<typeof newDigestByMonth>>;
linksByDomain: Awaited<ReturnType<typeof linksByDomain>>;
linksCount: number;
latestTeam: Team | null;
latestDigest: Digest | null;
linksByDay: Awaited<ReturnType<typeof linksByDay>>;
};

const Dashboard = (dashboardProps: DashboardProps) => {
return (
<Grid numItems={1} numItemsSm={2} numItemsLg={3} className="gap-2">
<Col numColSpan={1} numColSpanLg={2}>
<DataOverTime
data={{
newUsersByMonth: dashboardProps?.newUsersByMonth,
newDigestByMonth: dashboardProps?.newDigestByMonth,
}}
/>
</Col>
<LinksByWebsite data={dashboardProps.linksByDomain} />
<Col>
<Card>
<Text>Dernier digest publié</Text>
<Metric>
<a
// @ts-expect-error
href={`${dashboardProps.latestDigest?.team?.slug}/${dashboardProps.latestDigest?.slug}`}
target="_blank"
>
{dashboardProps.latestDigest?.title} par {/* @ts-ignore */}
{dashboardProps.latestDigest?.team.name}
</a>
</Metric>
</Card>
</Col>
<Card>
<Text>Dernière team créée</Text>
<Metric>
<a href={`${dashboardProps.latestTeam?.slug}`} target="_blank">
{dashboardProps.latestTeam?.name}
</a>
</Metric>
</Card>
<Card>
<Text>Total des liens</Text>
<Metric>{dashboardProps?.linksCount}</Metric>
</Card>
<Col numColSpan={3} numColSpanLg={3}>
<LinksOverTime data={{ linksByDay: dashboardProps.linksByDay }} />
</Col>
</Grid>
)
}

export default Dashboard
79 changes: 78 additions & 1 deletion src/utils/nextadmin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,105 @@ export const options: NextAdminOptions = {
basePath: '/admin',
model: {
User: {
aliases: {
emailVerified: 'Email verified date',
defaultTeam: 'Default team',
},
toString: (user) => user.email ?? user.id,
list: {
display: ['id', 'email', 'name', 'role', 'createdAt'],
search: ['email', 'name'],
search: ['id', 'email', 'name'],
fields: {
createdAt: {
formatter: (date) => new Date(date).toLocaleString('fr'),
},
}
},
edit: {
display: ['id', 'createdAt', 'email', 'name', 'emailVerified', 'accounts', 'role', 'defaultTeam'],
fields: {
defaultTeam: {
optionFormatter: (team) => team?.name!
},
},
},
},
Team: {
aliases: {
bookmarks: 'Links',
createdAt: 'Created at',
slackToken: 'Slack token',
slackTeamId: 'Slack team',
typefullyToken: 'Typefully token',
apiKey: 'API key',
nextSuggestedDigestTitle: 'Next suggested digest title',
subscriptionId: 'Subscription',
},
list: {
display: ['id', 'name', 'memberships', 'createdAt'],
search: ['name'],
fields: {
createdAt: {
formatter: (date) => new Date(date).toLocaleString('fr'),
}
}
},
edit: {
fields: {
memberships: {
optionFormatter: (membership) => membership?.user?.email ?? `User ${membership?.user?.id?.slice(0, 5)}...`
},
bookmarks: {
optionFormatter: (bookmark) => bookmark.link.title ?? `Bookmark ${bookmark?.id?.slice(0, 5)}...`
},
color: {
format: 'color'
},
apiKey: {
format: 'password'
}
}
},
},
Link: {
aliases: {
bookmark: 'Teams',
},
list: {
display: ['id', 'title', 'description', 'url'],
search: ['url'],
},
edit: {
fields: {
url: {
format: 'uri'
},
bookmark: {
optionFormatter: (bookmark) => bookmark.team.name ?? `Bookmark ${bookmark?.id?.slice(0, 5)}...`
},
}
},
},
Digest: {
toString: (digest) => digest.title ?? digest.id,
list: {
display: ['id', 'title', 'description', 'isFeatured', 'createdAt'],
fields: {
createdAt: {
formatter: (date) => new Date(date).toLocaleString('fr'),
}
}
},
edit: {
fields: {
team: {
optionFormatter: (team) => team?.name!
},
digestBlocks: {
optionFormatter: (digestBlock) => digestBlock.title ?? `DigestBlock ${digestBlock?.id?.slice(0, 5)}...`
},
}
}
},
},
};
2 changes: 1 addition & 1 deletion tailwind.admin.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module.exports = {
content: [
'./node_modules/@premieroctet/next-admin/src/**/*.{js,ts,jsx,tsx}',
'./node_modules/@premieroctet/next-admin/dist/**/*.{js,ts,jsx,tsx}',
'./node_modules/@tremor/**/*.{js,ts,jsx,tsx}',
'./src/components/admin/**/*.{js,ts,jsx,tsx}',
],
Expand Down
Loading
Loading