refactor: code refactor

This commit is contained in:
Andrea
2023-03-14 13:54:52 +01:00
parent 6d3a6c2155
commit 84ed5530ca
19 changed files with 1666 additions and 1023 deletions

View File

@@ -1,27 +0,0 @@
import { db } from "~/utils/db.server";
export const findPosts = async () => {
return db.post.findMany({
where: {
isPublic: true,
},
select: {
id: true,
title: true,
description: true,
createdAt: true,
},
orderBy: {
createdAt: "desc",
},
});
};
export const findPost = async (id: string) => {
return db.post.findFirst({
where: {
id,
isPublic: true,
},
});
};

View File

@@ -3,7 +3,6 @@ import { useLoaderData } from "@remix-run/react";
import { getMDXComponent } from "mdx-bundler/client";
import React from "react";
import { Title } from "~/components/Title";
import { findPost } from "~/models/posts.server";
import { getMdxFile } from "~/utils/posts.server";
export const handle = {
@@ -12,27 +11,24 @@ export const handle = {
};
export const loader = async ({ params }: LoaderArgs) => {
const id = params.id;
if (id == null) {
const name = params.name;
if (name == null) {
throw new Response(null, { status: 400 });
}
const post = await findPost(id);
if (post == null) {
throw new Response(null, { status: 404 });
}
const { code } = await getMdxFile(post.fileName);
return { post, code };
return getMdxFile(name);
};
export default function () {
const { post, code } = useLoaderData<typeof loader>();
const {
code,
frontmatter: { title },
} = useLoaderData<typeof loader>();
const MdxComponent = React.useMemo(() => getMDXComponent(code), [code]);
return (
<>
<Title>{post.title}</Title>
<Title>{title}</Title>
<div className="m-3 mt-10 xl:w-1/2 prose dark:prose-invert prose-a:no-underline prose-a:font-bold">
<MdxComponent />
</div>

View File

@@ -1,23 +1,23 @@
import { Link } from "@remix-run/react";
type PostProps = {
id: string;
filename: string;
title: string;
createdAt: string;
publishedAt: string;
description: string | null;
};
export function Post({ title, description, id, createdAt }: PostProps) {
export function Post({ title, description, filename, publishedAt }: PostProps) {
return (
<div className="mx-5 md:mx-0 md:w-1/2 lg:w-1/3">
<div className="p-5 border-gray-600 border-2 rounded-lg">
<Link to={id}>
<Link to={filename}>
<div className="text-center font-bold">
<span className="text-[#ffff00] hover:text-[#e6c2bf] md:text-2xl">
{title}
</span>
<span className="ml-2">{`(${new Date(
createdAt
publishedAt
).toLocaleDateString()})`}</span>
</div>
{description && (

View File

@@ -1,8 +1,8 @@
import { useLoaderData } from "@remix-run/react";
import { Title } from "~/components/Title";
import { findPosts } from "~/models/posts.server";
import { EmptyState } from "../components/EmptyState";
import { Post } from "../components/Post";
import { findPosts } from "~/utils/posts.server";
import { EmptyState } from "./EmptyState";
import { Post } from "./Post";
export const handle = {
to: "/",

View File

@@ -1,5 +1,5 @@
import { Link, Outlet } from "@remix-run/react";
import { useMatch } from "~/hooks/useMatch";
import { useMatch } from "./useMatch";
export default function () {
const { handle } = useMatch();

3
app/tailwind.css Normal file
View File

@@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

View File

@@ -1,21 +0,0 @@
import { PrismaClient } from "@prisma/client";
export let db: PrismaClient;
declare global {
var __db: PrismaClient | undefined;
}
// this is needed because in development we don't want to restart
// the server with every change, but we want to make sure we don't
// create a new connection to the DB with every change either.
if (process.env.NODE_ENV === "production") {
db = new PrismaClient();
db.$connect();
} else {
if (!global.__db) {
global.__db = new PrismaClient();
global.__db.$connect();
}
db = global.__db;
}

View File

@@ -1,12 +1,17 @@
import { remarkCodeHike } from "@code-hike/mdx";
import { readFile } from "fs/promises";
import { readdir, readFile } from "fs/promises";
import { bundleMDX } from "mdx-bundler";
import codeHikeTheme from "shiki/themes/one-dark-pro.json";
export const getMdxFile = async (fileName: string) => {
const source = await readFile(`posts/${fileName}`);
return bundleMDX({
source: source.toString(),
type FrontMatter = {
title: string;
description: string;
publishedAt: string;
};
export const getMdxFile = async (file: string) => {
return bundleMDX<FrontMatter>({
source: (await readFile(`./posts/${file}.mdx`)).toString(),
mdxOptions() {
return {
remarkPlugins: [
@@ -24,3 +29,41 @@ export const getMdxFile = async (fileName: string) => {
},
});
};
export const findPosts = async () => {
const files = await readdir("posts");
const posts: (FrontMatter & {
filename: string;
})[] = [];
for (const file of files.filter((file) => file.endsWith(".mdx"))) {
const { frontmatter } = await bundleMDX({
source: (await readFile(`./posts/${file}`)).toString(),
mdxOptions() {
return {
remarkPlugins: [
[
remarkCodeHike,
{
theme: codeHikeTheme,
lineNumbers: true,
showCopyButton: true,
autoImport: true,
},
],
],
};
},
});
posts.push({
filename: file.replace(".mdx", ""),
description: frontmatter.description,
title: frontmatter.title,
publishedAt: frontmatter.publishedAt,
});
}
return posts.sort((a, b) =>
new Date(a.publishedAt) > new Date(b.publishedAt) ? -1 : 1
);
};