Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

Make the markdown component reusable (#172)

Sean Boult18ad952ef6e4
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 523977cb..9e0c2818 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -6,6 +6,7 @@ env:
DATABASE_URL: 'mysql://fake.com'
GITHUB_ID: 'xd'
GITHUB_SECRET: 'lmao'
+ NEXTAUTH_SECRET: 'ligma'
jobs:
build:
diff --git a/src/app/create/page.tsx b/src/app/create/page.tsx
index ba7dc428..69e177a7 100644
--- a/src/app/create/page.tsx
+++ b/src/app/create/page.tsx
@@ -17,7 +17,7 @@ import ExploreCardInputs from '../../components/create/explore-card-inputs';
import { useTheme } from 'next-themes';
-import MDEditor from '@uiw/react-md-editor';
+import { RichMarkdownEditor } from '~/components/ui/rich-markdown-editor';
export default function CreateChallenge() {
const createChallengeStore = useCreateChallengeStore();
@@ -145,19 +145,7 @@ export default function CreateChallenge() {
<Markdown>{description}</Markdown>
</div>
) : (
- // TODO: move out to customized component
- <MDEditor
- height="100%"
- className="flex-1"
- value={description}
- // non-split-screen by default
- preview="edit"
- // removes resize handle on bottom right
- visibleDragbar={false}
- // @ts-ignore
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
- onChange={(ev) => setDescription(ev)}
- />
+ <RichMarkdownEditor onChange={(val) => setDescription(val)} value={description} />
)}
</TabsContent>
</Tabs>
diff --git a/src/components/admin/admin.actions.tsx b/src/components/admin/admin.actions.tsx
index 99a6fe51..85fec4e6 100644
--- a/src/components/admin/admin.actions.tsx
+++ b/src/components/admin/admin.actions.tsx
@@ -9,7 +9,7 @@ export type AdminReportDetails = Awaited<ReturnType<typeof getChallengeReports>>
* The function fetches all the reports along
* with challenge and the user.
*/
-export function getChallengeReports() {
+export async function getChallengeReports() {
return prisma.challengeReport.findMany({
include: {
challenge: {
diff --git a/src/components/admin/reports.tsx b/src/components/admin/reports.tsx
index 17c0f5f2..0e0dc554 100644
--- a/src/components/admin/reports.tsx
+++ b/src/components/admin/reports.tsx
@@ -36,9 +36,9 @@ export const ReportDetails = ({ data }: ReportsProps) => {
const [selectedIssue, setSelectedIssue] = useState<AdminReportDetails[number]>();
const [banReason, setBanReason] = useState('');
const handledReports = data.filter((report) => report.moderatorId === session.data?.user.id);
- // todo: will add them to a dropdown later.
- const [isDismissedHidden, setIsDismissedHidden] = useState(false);
- const [isClearedHidden, setIsClearedHidden] = useState(false);
+ // TODO: will add them to a dropdown later.
+ const [isDismissedHidden, _setIsDismissedHidden] = useState(false);
+ const [isClearedHidden, _setIsClearedHidden] = useState(false);
// Functions
async function handleUserBan() {
diff --git a/src/components/challenge/comments/comment.tsx b/src/components/challenge/comments/comment.tsx
index 99da799d..3d4058c6 100644
--- a/src/components/challenge/comments/comment.tsx
+++ b/src/components/challenge/comments/comment.tsx
@@ -3,7 +3,7 @@ import { Delete } from 'lucide-react';
import { useState } from 'react';
import { useForm } from 'react-hook-form';
import { z } from 'zod';
-import { ChallengeRouteData } from '~/app/challenge/[id]/getChallengeRouteData';
+import type { ChallengeRouteData } from '~/app/challenge/[id]/getChallengeRouteData';
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '~/components/ui/dialog';
import { getRelativeTime } from '~/utils/relativeTime';
import { Button } from '../../ui/button';
diff --git a/src/components/challenge/solutions/solution-editor.tsx b/src/components/challenge/solutions/solution-editor.tsx
index 1e2ec5e0..79a63a03 100644
--- a/src/components/challenge/solutions/solution-editor.tsx
+++ b/src/components/challenge/solutions/solution-editor.tsx
@@ -10,58 +10,8 @@ import { type ChallengeRouteData } from '~/app/challenge/[id]/getChallengeRouteD
import { useSession } from 'next-auth/react';
import { useTheme } from 'next-themes';
import { useRouter } from 'next/navigation';
-import { useContext } from 'react';
-import MDEditor, { commands, type ICommand, EditorContext } from '@uiw/react-md-editor';
-
-const PreviewToggle = () => {
- console.warn(commands);
- const { preview, dispatch } = useContext(EditorContext);
- const click = () => {
- dispatch?.({
- preview: preview === 'edit' ? 'preview' : 'edit',
- });
- };
- if (preview === 'edit') {
- return (
- // TODO: styles don't work for some reason
- <button
- className="mr-2 w-10 px-2 font-bold"
- style={{
- width: '4rem !important',
- padding: '0.25rem 1rem !important',
- fontWeight: 'bold !important',
- }}
- type="button"
- onClick={click}
- >
- Preview
- </button>
- );
- }
- return (
- // TODO: styles don't work for some reason
- <button
- className="mr-2 w-10 px-2 font-bold"
- style={{
- width: '4rem !important',
- padding: '0.25rem 1rem !important',
- fontWeight: 'bold !important',
- }}
- type="button"
- onClick={click}
- >
- Edit
- </button>
- );
-};
-
-const codePreview: ICommand = {
- name: 'preview',
- keyCommand: 'preview',
- value: 'preview',
- icon: <PreviewToggle />,
-};
+import { RichMarkdownEditor } from '~/components/ui/rich-markdown-editor';
const getDefaultMarkdown = (solution: string) => `
## Thoughts
@@ -171,53 +121,10 @@ export function SolutionEditor({ setOpen, challenge }: Props) {
name="content"
render={({ field }) => (
// @ts-ignore
- <MDEditor
- height="100%"
+ <RichMarkdownEditor
value={field.value}
// non-split-screen by default
- preview="edit"
- // TODO: might be a better way to select certain buttons insetad of re-rendering to <></>
- // commands={[commands.codeEdit, commands.codePreview]}
- // removes resize handle on bottom right
- visibleDragbar={false}
- extraCommands={[codePreview, commands.fullscreen]}
- // @ts-ignore
onChange={field.onChange}
- components={{
- toolbar: (command) => {
- // toolbar: (command, disabled, executeCommand) => {
- // re-render these to nothing
- if (
- command.keyCommand === 'hr' ||
- command.keyCommand === 'link' ||
- command.keyCommand === 'quote' ||
- command.keyCommand === 'image' ||
- command.keyCommand === 'comment' ||
- command.keyCommand === 'list' ||
- // TODO: these aren't under command.keyCommand?
- command.keyCommand === 'unordered-list' ||
- command.keyCommand === 'ordered-list' ||
- command.keyCommand === 'checked-list'
- ) {
- return <></>;
- }
- // custom code button option
- if (command.keyCommand === 'code') {
- // return (
- // <button
- // aria-label="Insert code"
- // disabled={disabled}
- // onClick={(evn) => {
- // evn.stopPropagation();
- // executeCommand(command, command.groupName);
- // }}
- // >
- // a
- // </button>
- // );
- }
- },
- }}
/>
)}
/>
diff --git a/src/components/ui/rich-markdown-editor.tsx b/src/components/ui/rich-markdown-editor.tsx
new file mode 100644
index 00000000..9e2c6ee6
--- /dev/null
+++ b/src/components/ui/rich-markdown-editor.tsx
@@ -0,0 +1,100 @@
+import { useTheme } from 'next-themes';
+import { useContext } from 'react';
+
+import MDEditor, { commands, type ICommand, EditorContext } from '@uiw/react-md-editor';
+
+const PreviewToggle = () => {
+ console.warn(commands);
+ const { preview, dispatch } = useContext(EditorContext);
+ const click = () => {
+ dispatch?.({
+ preview: preview === 'edit' ? 'preview' : 'edit',
+ });
+ };
+ if (preview === 'edit') {
+ return (
+ // TODO: styles don't work for some reason
+ <button
+ className="mr-2 w-10 px-2 font-bold"
+ style={{
+ width: '4rem !important',
+ padding: '0.25rem 1rem !important',
+ fontWeight: 'bold !important',
+ }}
+ type="button"
+ onClick={click}
+ >
+ Preview
+ </button>
+ );
+ }
+ return (
+ // TODO: styles don't work for some reason
+ <button
+ className="mr-2 w-10 px-2 font-bold"
+ style={{
+ width: '4rem !important',
+ padding: '0.25rem 1rem !important',
+ fontWeight: 'bold !important',
+ }}
+ type="button"
+ onClick={click}
+ >
+ Edit
+ </button>
+ );
+};
+
+const codePreview: ICommand = {
+ name: 'preview',
+ keyCommand: 'preview',
+ value: 'preview',
+ icon: <PreviewToggle />,
+};
+
+interface Props {
+ value: string;
+ onChange: (v: string) => void;
+}
+
+export function RichMarkdownEditor({ value, onChange }: Props) {
+ const { theme } = useTheme();
+ theme == 'dark'
+ ? document.documentElement.setAttribute('data-color-mode', 'dark')
+ : document.documentElement.setAttribute('data-color-mode', 'light');
+
+ return (
+ <div className="h-full flex-1">
+ <MDEditor
+ height="100%"
+ value={value}
+ // non-split-screen by default
+ preview="edit"
+ visibleDragbar={false}
+ extraCommands={[codePreview, commands.fullscreen]}
+ // @ts-ignore
+ onChange={onChange}
+ components={{
+ toolbar: (command) => {
+ // toolbar: (command, disabled, executeCommand) => {
+ // re-render these to nothing
+ if (
+ command.keyCommand === 'hr' ||
+ command.keyCommand === 'link' ||
+ command.keyCommand === 'quote' ||
+ command.keyCommand === 'image' ||
+ command.keyCommand === 'comment' ||
+ command.keyCommand === 'list' ||
+ // TODO: these aren't under command.keyCommand?
+ command.keyCommand === 'unordered-list' ||
+ command.keyCommand === 'ordered-list' ||
+ command.keyCommand === 'checked-list'
+ ) {
+ return <></>;
+ }
+ },
+ }}
+ />
+ </div>
+ );
+}