mirror of
https://github.com/r2r90/canvas-label.git
synced 2026-01-25 05:32:08 +00:00
first commit
This commit is contained in:
99
src/components/canvas.tsx
Normal file
99
src/components/canvas.tsx
Normal file
@@ -0,0 +1,99 @@
|
||||
import {
|
||||
TransformableImage,
|
||||
TransformableImageProps,
|
||||
} from "@/components/transformable-image";
|
||||
|
||||
import type { KonvaEventObject } from "konva/lib/Node";
|
||||
import { ChangeEvent, useState } from "react";
|
||||
import { Layer, Stage } from "react-konva";
|
||||
|
||||
import { v1 } from "uuid";
|
||||
import TransformableText, {
|
||||
TransformableTextProps,
|
||||
} from "./transformable-text";
|
||||
|
||||
const Canvas = () => {
|
||||
const [selectedImageId, selectImage] = useState<string | null>(null);
|
||||
const [selectedTextId, selectText] = useState<string | null>(null);
|
||||
|
||||
const [images, setImages] = useState<TransformableImageProps["imageProps"][]>(
|
||||
[],
|
||||
);
|
||||
|
||||
const [texts, setTexts] = useState<TransformableTextProps["textProps"][]>([]);
|
||||
|
||||
const checkDeselect = (e: KonvaEventObject<MouseEvent>) => {
|
||||
// deselect when clicked on empty area
|
||||
const clickedOnEmpty = e.target === e.target.getStage();
|
||||
if (clickedOnEmpty) {
|
||||
selectImage(null);
|
||||
}
|
||||
};
|
||||
|
||||
const handleImageUploaded = (e: ChangeEvent<HTMLInputElement>) => {
|
||||
const file = e.target.files?.[0];
|
||||
if (!file) return;
|
||||
|
||||
const imageId = v1();
|
||||
const imageUrl = URL.createObjectURL(file);
|
||||
setImages((prev) => [...prev, { imageUrl, imageId }]);
|
||||
};
|
||||
|
||||
const handleTextAdd = (e: ChangeEvent<HTMLInputElement>) => {
|
||||
const text = e.target.value;
|
||||
const textId = v1();
|
||||
|
||||
setTexts((prev) => [...prev, { text, textId }]);
|
||||
};
|
||||
|
||||
/*console.log(texts, " ++++ ", images);*/
|
||||
|
||||
return (
|
||||
<main>
|
||||
<input type="file" onChange={handleImageUploaded} />
|
||||
<input
|
||||
type="text"
|
||||
onChange={handleTextAdd}
|
||||
placeholder="tape your text"
|
||||
/>
|
||||
<Stage width={window.innerWidth} height={window.innerHeight}>
|
||||
<Layer>
|
||||
{images.map((image) => {
|
||||
return (
|
||||
<TransformableImage
|
||||
onSelect={() => selectImage(image.imageId)}
|
||||
isSelected={image.imageId === selectedImageId}
|
||||
onChange={(newAttrs) => {
|
||||
setImages(
|
||||
images.map((i) =>
|
||||
i.imageId === image.imageId ? newAttrs : i,
|
||||
),
|
||||
);
|
||||
}}
|
||||
imageProps={image}
|
||||
key={image.imageId}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
{texts.map((text) => {
|
||||
return (
|
||||
<TransformableText
|
||||
onSelect={() => selectText(text.textId)}
|
||||
isSelected={text.textId === selectedTextId}
|
||||
onChange={(newAttrs) => {
|
||||
setTexts(
|
||||
texts.map((t) => (t.textId === text.textId ? newAttrs : t)),
|
||||
);
|
||||
}}
|
||||
textProps={text}
|
||||
key={text.textId}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</Layer>
|
||||
</Stage>
|
||||
</main>
|
||||
);
|
||||
};
|
||||
|
||||
export default Canvas;
|
||||
14
src/components/stage.tsx
Normal file
14
src/components/stage.tsx
Normal file
@@ -0,0 +1,14 @@
|
||||
import { type ComponentPropsWithoutRef } from "react";
|
||||
import { Stage as StageKonva } from "react-konva";
|
||||
|
||||
const Stage = ({
|
||||
children,
|
||||
...rest
|
||||
}: ComponentPropsWithoutRef<typeof StageKonva>) => {
|
||||
return (
|
||||
<StageKonva width={window.innerWidth} height={window.innerHeight} {...rest}>
|
||||
{children}
|
||||
</StageKonva>
|
||||
);
|
||||
};
|
||||
export default Stage;
|
||||
94
src/components/transformable-image.tsx
Normal file
94
src/components/transformable-image.tsx
Normal file
@@ -0,0 +1,94 @@
|
||||
import { type ImageConfig } from "konva/lib/shapes/Image";
|
||||
import { useRef, useEffect, type ElementRef } from "react";
|
||||
import { Transformer, Image } from "react-konva";
|
||||
import useImage from "use-image";
|
||||
|
||||
type TransformableImageConfig = Omit<ImageConfig, "image"> & {
|
||||
image?: ImageConfig["image"];
|
||||
imageUrl: string;
|
||||
id: string;
|
||||
};
|
||||
|
||||
export type TransformableImageProps = {
|
||||
imageProps: TransformableImageConfig;
|
||||
isSelected: boolean;
|
||||
onSelect: () => void;
|
||||
onChange: (newAttrs: TransformableImageConfig) => void;
|
||||
};
|
||||
|
||||
export const TransformableImage = ({
|
||||
imageProps,
|
||||
isSelected,
|
||||
onSelect,
|
||||
onChange,
|
||||
}: TransformableImageProps) => {
|
||||
const imageRef = useRef<ElementRef<typeof Image>>(null);
|
||||
const trRef = useRef<ElementRef<typeof Transformer>>(null);
|
||||
const [image] = useImage(imageProps.imageUrl);
|
||||
|
||||
console.log(imageProps, isSelected, onSelect, onChange, " IMAGE ");
|
||||
|
||||
useEffect(() => {
|
||||
if (!imageRef.current) return;
|
||||
|
||||
if (isSelected) {
|
||||
// we need to attach transformer manually
|
||||
trRef.current?.nodes([imageRef.current]);
|
||||
trRef.current?.getLayer()?.batchDraw();
|
||||
}
|
||||
}, [isSelected]);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Image
|
||||
alt={"canvas image"}
|
||||
onClick={onSelect}
|
||||
onTap={onSelect}
|
||||
ref={imageRef}
|
||||
{...imageProps}
|
||||
image={image}
|
||||
draggable
|
||||
onDragEnd={(e) => {
|
||||
onChange({
|
||||
...imageProps,
|
||||
image,
|
||||
x: e.target.x(),
|
||||
y: e.target.y(),
|
||||
});
|
||||
}}
|
||||
/* onTransformEnd={(e) => {
|
||||
const node = imageRef.current;
|
||||
if (!node) return;
|
||||
|
||||
const scaleX = node.scaleX();
|
||||
const scaleY = node.scaleY();
|
||||
|
||||
// we will reset it back
|
||||
node.scaleX(1);
|
||||
node.scaleY(1);
|
||||
onChange({
|
||||
...imageProps,
|
||||
image,
|
||||
x: node.x(),
|
||||
y: node.y(),
|
||||
// set minimal value
|
||||
width: Math.max(5, node.width() * scaleX),
|
||||
height: Math.max(node.height() * scaleY),
|
||||
});
|
||||
}}*/
|
||||
/>
|
||||
{isSelected && (
|
||||
<Transformer
|
||||
ref={trRef}
|
||||
boundBoxFunc={(oldBox, newBox) => {
|
||||
// limit resize
|
||||
if (newBox.width < 5 || newBox.height < 5) {
|
||||
return oldBox;
|
||||
}
|
||||
return newBox;
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
};
|
||||
90
src/components/transformable-text.tsx
Normal file
90
src/components/transformable-text.tsx
Normal file
@@ -0,0 +1,90 @@
|
||||
import { useRef, type ElementRef, useEffect } from "react";
|
||||
import { type TextConfig } from "konva/lib/shapes/Text";
|
||||
import { Text, Transformer } from "react-konva";
|
||||
|
||||
type TransformableTextConfig = Omit<TextConfig, "text"> & {
|
||||
text?: TextConfig["text"];
|
||||
id: string;
|
||||
};
|
||||
|
||||
export type TransformableTextProps = {
|
||||
textProps: TransformableTextConfig;
|
||||
isSelected: boolean;
|
||||
onSelect: () => void;
|
||||
onChange: (newAttrs: TransformableTextConfig) => void;
|
||||
};
|
||||
|
||||
const TransformableText = ({
|
||||
textProps,
|
||||
isSelected,
|
||||
onSelect,
|
||||
onChange,
|
||||
}: TransformableTextProps) => {
|
||||
const textRef = useRef<ElementRef<typeof Text>>(null);
|
||||
const trRef = useRef<ElementRef<typeof Transformer>>(null);
|
||||
const text = textProps.text;
|
||||
|
||||
useEffect(() => {
|
||||
if (!textRef.current) return;
|
||||
|
||||
if (isSelected) {
|
||||
// we need to attach transformer manually
|
||||
trRef.current?.nodes([textRef.current]);
|
||||
trRef.current?.getLayer()?.batchDraw();
|
||||
}
|
||||
}, [isSelected]);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Text
|
||||
onClick={onSelect}
|
||||
onTap={onSelect}
|
||||
ref={textRef}
|
||||
{...textProps}
|
||||
text={text}
|
||||
draggable
|
||||
onDragEnd={(e) => {
|
||||
onChange({
|
||||
...textProps,
|
||||
text,
|
||||
x: e.target.x(),
|
||||
y: e.target.y(),
|
||||
});
|
||||
}}
|
||||
/* onTransformEnd={(e) => {
|
||||
const node = textRef.current;
|
||||
if (!node) return;
|
||||
|
||||
const scaleX = node.scaleX();
|
||||
const scaleY = node.scaleY();
|
||||
|
||||
// we will reset it back
|
||||
node.scaleX(1);
|
||||
node.scaleY(1);
|
||||
onChange({
|
||||
...textProps,
|
||||
text,
|
||||
x: node.x(),
|
||||
y: node.y(),
|
||||
// set minimal value
|
||||
width: Math.max(5, node.width() * scaleX),
|
||||
height: Math.max(node.height() * scaleY),
|
||||
});
|
||||
}}*/
|
||||
/>
|
||||
{isSelected && (
|
||||
<Transformer
|
||||
ref={trRef}
|
||||
boundBoxFunc={(oldBox, newBox) => {
|
||||
// limit resize
|
||||
if (newBox.width < 5 || newBox.height < 5) {
|
||||
return oldBox;
|
||||
}
|
||||
return newBox;
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
};
|
||||
export default TransformableText;
|
||||
56
src/env.mjs
Normal file
56
src/env.mjs
Normal file
@@ -0,0 +1,56 @@
|
||||
import { createEnv } from "@t3-oss/env-nextjs";
|
||||
import { z } from "zod";
|
||||
|
||||
export const env = createEnv({
|
||||
/**
|
||||
* Specify your server-side environment variables schema here. This way you can ensure the app
|
||||
* isn't built with invalid env vars.
|
||||
*/
|
||||
server: {
|
||||
DATABASE_URL: z.string().url(),
|
||||
NODE_ENV: z
|
||||
.enum(["development", "test", "production"])
|
||||
.default("development"),
|
||||
NEXTAUTH_SECRET:
|
||||
process.env.NODE_ENV === "production"
|
||||
? z.string().min(1)
|
||||
: z.string().min(1).optional(),
|
||||
NEXTAUTH_URL: z.preprocess(
|
||||
// This makes Vercel deployments not fail if you don't set NEXTAUTH_URL
|
||||
// Since NextAuth.js automatically uses the VERCEL_URL if present.
|
||||
(str) => process.env.VERCEL_URL ?? str,
|
||||
// VERCEL_URL doesn't include `https` so it cant be validated as a URL
|
||||
process.env.VERCEL ? z.string().min(1) : z.string().url()
|
||||
),
|
||||
// Add `.min(1) on ID and SECRET if you want to make sure they're not empty
|
||||
DISCORD_CLIENT_ID: z.string(),
|
||||
DISCORD_CLIENT_SECRET: z.string(),
|
||||
},
|
||||
|
||||
/**
|
||||
* Specify your client-side environment variables schema here. This way you can ensure the app
|
||||
* isn't built with invalid env vars. To expose them to the client, prefix them with
|
||||
* `NEXT_PUBLIC_`.
|
||||
*/
|
||||
client: {
|
||||
// NEXT_PUBLIC_CLIENTVAR: z.string().min(1),
|
||||
},
|
||||
|
||||
/**
|
||||
* You can't destruct `process.env` as a regular object in the Next.js edge runtimes (e.g.
|
||||
* middlewares) or client-side so we need to destruct manually.
|
||||
*/
|
||||
runtimeEnv: {
|
||||
DATABASE_URL: process.env.DATABASE_URL,
|
||||
NODE_ENV: process.env.NODE_ENV,
|
||||
NEXTAUTH_SECRET: process.env.NEXTAUTH_SECRET,
|
||||
NEXTAUTH_URL: process.env.NEXTAUTH_URL,
|
||||
DISCORD_CLIENT_ID: process.env.DISCORD_CLIENT_ID,
|
||||
DISCORD_CLIENT_SECRET: process.env.DISCORD_CLIENT_SECRET,
|
||||
},
|
||||
/**
|
||||
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation.
|
||||
* This is especially useful for Docker builds.
|
||||
*/
|
||||
skipValidation: !!process.env.SKIP_ENV_VALIDATION,
|
||||
});
|
||||
6
src/lib/utils.ts
Normal file
6
src/lib/utils.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { type ClassValue, clsx } from "clsx"
|
||||
import { twMerge } from "tailwind-merge"
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs))
|
||||
}
|
||||
20
src/pages/_app.tsx
Normal file
20
src/pages/_app.tsx
Normal file
@@ -0,0 +1,20 @@
|
||||
import { type Session } from "next-auth";
|
||||
import { SessionProvider } from "next-auth/react";
|
||||
import { type AppType } from "next/app";
|
||||
|
||||
import { api } from "@/utils/api";
|
||||
|
||||
import "@/styles/globals.css";
|
||||
|
||||
const MyApp: AppType<{ session: Session | null }> = ({
|
||||
Component,
|
||||
pageProps: { session, ...pageProps },
|
||||
}) => {
|
||||
return (
|
||||
<SessionProvider session={session}>
|
||||
<Component {...pageProps} />
|
||||
</SessionProvider>
|
||||
);
|
||||
};
|
||||
|
||||
export default api.withTRPC(MyApp);
|
||||
5
src/pages/api/auth/[...nextauth].ts
Normal file
5
src/pages/api/auth/[...nextauth].ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import NextAuth from "next-auth";
|
||||
|
||||
import { authOptions } from "@/server/auth";
|
||||
|
||||
export default NextAuth(authOptions);
|
||||
19
src/pages/api/trpc/[trpc].ts
Normal file
19
src/pages/api/trpc/[trpc].ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { createNextApiHandler } from "@trpc/server/adapters/next";
|
||||
|
||||
import { env } from "@/env.mjs";
|
||||
import { appRouter } from "@/server/api/root";
|
||||
import { createTRPCContext } from "@/server/api/trpc";
|
||||
|
||||
// export API handler
|
||||
export default createNextApiHandler({
|
||||
router: appRouter,
|
||||
createContext: createTRPCContext,
|
||||
onError:
|
||||
env.NODE_ENV === "development"
|
||||
? ({ path, error }) => {
|
||||
console.error(
|
||||
`❌ tRPC failed on ${path ?? "<no-path>"}: ${error.message}`
|
||||
);
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
7
src/pages/index.tsx
Normal file
7
src/pages/index.tsx
Normal file
@@ -0,0 +1,7 @@
|
||||
import dynamic from "next/dynamic";
|
||||
|
||||
const Canvas = dynamic(() => import("../components/canvas"), { ssr: false });
|
||||
|
||||
export default function Home() {
|
||||
return <Canvas />;
|
||||
}
|
||||
14
src/server/api/root.ts
Normal file
14
src/server/api/root.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { exampleRouter } from "@/server/api/routers/example";
|
||||
import { createTRPCRouter } from "@/server/api/trpc";
|
||||
|
||||
/**
|
||||
* This is the primary router for your server.
|
||||
*
|
||||
* All routers added in /api/routers should be manually added here.
|
||||
*/
|
||||
export const appRouter = createTRPCRouter({
|
||||
example: exampleRouter,
|
||||
});
|
||||
|
||||
// export type definition of API
|
||||
export type AppRouter = typeof appRouter;
|
||||
25
src/server/api/routers/example.ts
Normal file
25
src/server/api/routers/example.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import {
|
||||
createTRPCRouter,
|
||||
protectedProcedure,
|
||||
publicProcedure,
|
||||
} from "@/server/api/trpc";
|
||||
|
||||
export const exampleRouter = createTRPCRouter({
|
||||
hello: publicProcedure
|
||||
.input(z.object({ text: z.string() }))
|
||||
.query(({ input }) => {
|
||||
return {
|
||||
greeting: `Hello ${input.text}`,
|
||||
};
|
||||
}),
|
||||
|
||||
getAll: publicProcedure.query(({ ctx }) => {
|
||||
return ctx.db.example.findMany();
|
||||
}),
|
||||
|
||||
getSecretMessage: protectedProcedure.query(() => {
|
||||
return "you can now see this secret message!";
|
||||
}),
|
||||
});
|
||||
131
src/server/api/trpc.ts
Normal file
131
src/server/api/trpc.ts
Normal file
@@ -0,0 +1,131 @@
|
||||
/**
|
||||
* YOU PROBABLY DON'T NEED TO EDIT THIS FILE, UNLESS:
|
||||
* 1. You want to modify request context (see Part 1).
|
||||
* 2. You want to create a new middleware or type of procedure (see Part 3).
|
||||
*
|
||||
* TL;DR - This is where all the tRPC server stuff is created and plugged in. The pieces you will
|
||||
* need to use are documented accordingly near the end.
|
||||
*/
|
||||
|
||||
import { initTRPC, TRPCError } from "@trpc/server";
|
||||
import { type CreateNextContextOptions } from "@trpc/server/adapters/next";
|
||||
import { type Session } from "next-auth";
|
||||
import superjson from "superjson";
|
||||
import { ZodError } from "zod";
|
||||
|
||||
import { getServerAuthSession } from "@/server/auth";
|
||||
import { db } from "@/server/db";
|
||||
|
||||
/**
|
||||
* 1. CONTEXT
|
||||
*
|
||||
* This section defines the "contexts" that are available in the backend API.
|
||||
*
|
||||
* These allow you to access things when processing a request, like the database, the session, etc.
|
||||
*/
|
||||
|
||||
interface CreateContextOptions {
|
||||
session: Session | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* This helper generates the "internals" for a tRPC context. If you need to use it, you can export
|
||||
* it from here.
|
||||
*
|
||||
* Examples of things you may need it for:
|
||||
* - testing, so we don't have to mock Next.js' req/res
|
||||
* - tRPC's `createSSGHelpers`, where we don't have req/res
|
||||
*
|
||||
* @see https://create.t3.gg/en/usage/trpc#-serverapitrpcts
|
||||
*/
|
||||
const createInnerTRPCContext = (opts: CreateContextOptions) => {
|
||||
return {
|
||||
session: opts.session,
|
||||
db,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* This is the actual context you will use in your router. It will be used to process every request
|
||||
* that goes through your tRPC endpoint.
|
||||
*
|
||||
* @see https://trpc.io/docs/context
|
||||
*/
|
||||
export const createTRPCContext = async (opts: CreateNextContextOptions) => {
|
||||
const { req, res } = opts;
|
||||
|
||||
// Get the session from the server using the getServerSession wrapper function
|
||||
const session = await getServerAuthSession({ req, res });
|
||||
|
||||
return createInnerTRPCContext({
|
||||
session,
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* 2. INITIALIZATION
|
||||
*
|
||||
* This is where the tRPC API is initialized, connecting the context and transformer. We also parse
|
||||
* ZodErrors so that you get typesafety on the frontend if your procedure fails due to validation
|
||||
* errors on the backend.
|
||||
*/
|
||||
|
||||
const t = initTRPC.context<typeof createTRPCContext>().create({
|
||||
transformer: superjson,
|
||||
errorFormatter({ shape, error }) {
|
||||
return {
|
||||
...shape,
|
||||
data: {
|
||||
...shape.data,
|
||||
zodError:
|
||||
error.cause instanceof ZodError ? error.cause.flatten() : null,
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* 3. ROUTER & PROCEDURE (THE IMPORTANT BIT)
|
||||
*
|
||||
* These are the pieces you use to build your tRPC API. You should import these a lot in the
|
||||
* "/src/server/api/routers" directory.
|
||||
*/
|
||||
|
||||
/**
|
||||
* This is how you create new routers and sub-routers in your tRPC API.
|
||||
*
|
||||
* @see https://trpc.io/docs/router
|
||||
*/
|
||||
export const createTRPCRouter = t.router;
|
||||
|
||||
/**
|
||||
* Public (unauthenticated) procedure
|
||||
*
|
||||
* This is the base piece you use to build new queries and mutations on your tRPC API. It does not
|
||||
* guarantee that a user querying is authorized, but you can still access user session data if they
|
||||
* are logged in.
|
||||
*/
|
||||
export const publicProcedure = t.procedure;
|
||||
|
||||
/** Reusable middleware that enforces users are logged in before running the procedure. */
|
||||
const enforceUserIsAuthed = t.middleware(({ ctx, next }) => {
|
||||
if (!ctx.session?.user) {
|
||||
throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||
}
|
||||
return next({
|
||||
ctx: {
|
||||
// infers the `session` as non-nullable
|
||||
session: { ...ctx.session, user: ctx.session.user },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Protected (authenticated) procedure
|
||||
*
|
||||
* If you want a query or mutation to ONLY be accessible to logged in users, use this. It verifies
|
||||
* the session is valid and guarantees `ctx.session.user` is not null.
|
||||
*
|
||||
* @see https://trpc.io/docs/procedures
|
||||
*/
|
||||
export const protectedProcedure = t.procedure.use(enforceUserIsAuthed);
|
||||
77
src/server/auth.ts
Normal file
77
src/server/auth.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import { PrismaAdapter } from "@next-auth/prisma-adapter";
|
||||
import { type GetServerSidePropsContext } from "next";
|
||||
import {
|
||||
getServerSession,
|
||||
type DefaultSession,
|
||||
type NextAuthOptions,
|
||||
} from "next-auth";
|
||||
import DiscordProvider from "next-auth/providers/discord";
|
||||
|
||||
import { env } from "@/env.mjs";
|
||||
import { db } from "@/server/db";
|
||||
|
||||
/**
|
||||
* Module augmentation for `next-auth` types. Allows us to add custom properties to the `session`
|
||||
* object and keep type safety.
|
||||
*
|
||||
* @see https://next-auth.js.org/getting-started/typescript#module-augmentation
|
||||
*/
|
||||
declare module "next-auth" {
|
||||
interface Session extends DefaultSession {
|
||||
user: DefaultSession["user"] & {
|
||||
id: string;
|
||||
// ...other properties
|
||||
// role: UserRole;
|
||||
};
|
||||
}
|
||||
|
||||
// interface User {
|
||||
// // ...other properties
|
||||
// // role: UserRole;
|
||||
// }
|
||||
}
|
||||
|
||||
/**
|
||||
* Options for NextAuth.js used to configure adapters, providers, callbacks, etc.
|
||||
*
|
||||
* @see https://next-auth.js.org/configuration/options
|
||||
*/
|
||||
export const authOptions: NextAuthOptions = {
|
||||
callbacks: {
|
||||
session: ({ session, user }) => ({
|
||||
...session,
|
||||
user: {
|
||||
...session.user,
|
||||
id: user.id,
|
||||
},
|
||||
}),
|
||||
},
|
||||
adapter: PrismaAdapter(db),
|
||||
providers: [
|
||||
DiscordProvider({
|
||||
clientId: env.DISCORD_CLIENT_ID,
|
||||
clientSecret: env.DISCORD_CLIENT_SECRET,
|
||||
}),
|
||||
/**
|
||||
* ...add more providers here.
|
||||
*
|
||||
* Most other providers require a bit more work than the Discord provider. For example, the
|
||||
* GitHub provider requires you to add the `refresh_token_expires_in` field to the Account
|
||||
* model. Refer to the NextAuth.js docs for the provider you want to use. Example:
|
||||
*
|
||||
* @see https://next-auth.js.org/providers/github
|
||||
*/
|
||||
],
|
||||
};
|
||||
|
||||
/**
|
||||
* Wrapper for `getServerSession` so that you don't need to import the `authOptions` in every file.
|
||||
*
|
||||
* @see https://next-auth.js.org/configuration/nextjs
|
||||
*/
|
||||
export const getServerAuthSession = (ctx: {
|
||||
req: GetServerSidePropsContext["req"];
|
||||
res: GetServerSidePropsContext["res"];
|
||||
}) => {
|
||||
return getServerSession(ctx.req, ctx.res, authOptions);
|
||||
};
|
||||
16
src/server/db.ts
Normal file
16
src/server/db.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
|
||||
import { env } from "@/env.mjs";
|
||||
|
||||
const globalForPrisma = globalThis as unknown as {
|
||||
prisma: PrismaClient | undefined;
|
||||
};
|
||||
|
||||
export const db =
|
||||
globalForPrisma.prisma ??
|
||||
new PrismaClient({
|
||||
log:
|
||||
env.NODE_ENV === "development" ? ["query", "error", "warn"] : ["error"],
|
||||
});
|
||||
|
||||
if (env.NODE_ENV !== "production") globalForPrisma.prisma = db;
|
||||
76
src/styles/globals.css
Normal file
76
src/styles/globals.css
Normal file
@@ -0,0 +1,76 @@
|
||||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
|
||||
@layer base {
|
||||
:root {
|
||||
--background: 0 0% 100%;
|
||||
--foreground: 222.2 84% 4.9%;
|
||||
|
||||
--card: 0 0% 100%;
|
||||
--card-foreground: 222.2 84% 4.9%;
|
||||
|
||||
--popover: 0 0% 100%;
|
||||
--popover-foreground: 222.2 84% 4.9%;
|
||||
|
||||
--primary: 222.2 47.4% 11.2%;
|
||||
--primary-foreground: 210 40% 98%;
|
||||
|
||||
--secondary: 210 40% 96.1%;
|
||||
--secondary-foreground: 222.2 47.4% 11.2%;
|
||||
|
||||
--muted: 210 40% 96.1%;
|
||||
--muted-foreground: 215.4 16.3% 46.9%;
|
||||
|
||||
--accent: 210 40% 96.1%;
|
||||
--accent-foreground: 222.2 47.4% 11.2%;
|
||||
|
||||
--destructive: 0 84.2% 60.2%;
|
||||
--destructive-foreground: 210 40% 98%;
|
||||
|
||||
--border: 214.3 31.8% 91.4%;
|
||||
--input: 214.3 31.8% 91.4%;
|
||||
--ring: 222.2 84% 4.9%;
|
||||
|
||||
--radius: 0.5rem;
|
||||
}
|
||||
|
||||
.dark {
|
||||
--background: 222.2 84% 4.9%;
|
||||
--foreground: 210 40% 98%;
|
||||
|
||||
--card: 222.2 84% 4.9%;
|
||||
--card-foreground: 210 40% 98%;
|
||||
|
||||
--popover: 222.2 84% 4.9%;
|
||||
--popover-foreground: 210 40% 98%;
|
||||
|
||||
--primary: 210 40% 98%;
|
||||
--primary-foreground: 222.2 47.4% 11.2%;
|
||||
|
||||
--secondary: 217.2 32.6% 17.5%;
|
||||
--secondary-foreground: 210 40% 98%;
|
||||
|
||||
--muted: 217.2 32.6% 17.5%;
|
||||
--muted-foreground: 215 20.2% 65.1%;
|
||||
|
||||
--accent: 217.2 32.6% 17.5%;
|
||||
--accent-foreground: 210 40% 98%;
|
||||
|
||||
--destructive: 0 62.8% 30.6%;
|
||||
--destructive-foreground: 210 40% 98%;
|
||||
|
||||
--border: 217.2 32.6% 17.5%;
|
||||
--input: 217.2 32.6% 17.5%;
|
||||
--ring: 212.7 26.8% 83.9%;
|
||||
}
|
||||
}
|
||||
|
||||
@layer base {
|
||||
* {
|
||||
@apply border-border;
|
||||
}
|
||||
body {
|
||||
@apply bg-background text-foreground;
|
||||
}
|
||||
}
|
||||
68
src/utils/api.ts
Normal file
68
src/utils/api.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* This is the client-side entrypoint for your tRPC API. It is used to create the `api` object which
|
||||
* contains the Next.js App-wrapper, as well as your type-safe React Query hooks.
|
||||
*
|
||||
* We also create a few inference helpers for input and output types.
|
||||
*/
|
||||
import { httpBatchLink, loggerLink } from "@trpc/client";
|
||||
import { createTRPCNext } from "@trpc/next";
|
||||
import { type inferRouterInputs, type inferRouterOutputs } from "@trpc/server";
|
||||
import superjson from "superjson";
|
||||
|
||||
import { type AppRouter } from "@/server/api/root";
|
||||
|
||||
const getBaseUrl = () => {
|
||||
if (typeof window !== "undefined") return ""; // browser should use relative url
|
||||
if (process.env.VERCEL_URL) return `https://${process.env.VERCEL_URL}`; // SSR should use vercel url
|
||||
return `http://localhost:${process.env.PORT ?? 3000}`; // dev SSR should use localhost
|
||||
};
|
||||
|
||||
/** A set of type-safe react-query hooks for your tRPC API. */
|
||||
export const api = createTRPCNext<AppRouter>({
|
||||
config() {
|
||||
return {
|
||||
/**
|
||||
* Transformer used for data de-serialization from the server.
|
||||
*
|
||||
* @see https://trpc.io/docs/data-transformers
|
||||
*/
|
||||
transformer: superjson,
|
||||
|
||||
/**
|
||||
* Links used to determine request flow from client to server.
|
||||
*
|
||||
* @see https://trpc.io/docs/links
|
||||
*/
|
||||
links: [
|
||||
loggerLink({
|
||||
enabled: (opts) =>
|
||||
process.env.NODE_ENV === "development" ||
|
||||
(opts.direction === "down" && opts.result instanceof Error),
|
||||
}),
|
||||
httpBatchLink({
|
||||
url: `${getBaseUrl()}/api/trpc`,
|
||||
}),
|
||||
],
|
||||
};
|
||||
},
|
||||
/**
|
||||
* Whether tRPC should await queries when server rendering pages.
|
||||
*
|
||||
* @see https://trpc.io/docs/nextjs#ssr-boolean-default-false
|
||||
*/
|
||||
ssr: false,
|
||||
});
|
||||
|
||||
/**
|
||||
* Inference helper for inputs.
|
||||
*
|
||||
* @example type HelloInput = RouterInputs['example']['hello']
|
||||
*/
|
||||
export type RouterInputs = inferRouterInputs<AppRouter>;
|
||||
|
||||
/**
|
||||
* Inference helper for outputs.
|
||||
*
|
||||
* @example type HelloOutput = RouterOutputs['example']['hello']
|
||||
*/
|
||||
export type RouterOutputs = inferRouterOutputs<AppRouter>;
|
||||
Reference in New Issue
Block a user