From fb0b82edd316e9cfca765c287e4d271aa0191ad1 Mon Sep 17 00:00:00 2001 From: Rainnny7 Date: Sun, 21 Apr 2024 23:55:38 -0400 Subject: [PATCH] Add code dialogs --- .../src/app/components/code/code-dialog.tsx | 75 ++++++++++++ .../{ => code}/code-highlighter.tsx | 19 ++- Frontend/src/app/components/mdx.tsx | 10 +- .../app/components/player/player-result.tsx | 47 ++++---- .../src/app/components/raw-json-badge.tsx | 14 +++ .../app/components/server/server-result.tsx | 113 ++++++++++-------- 6 files changed, 197 insertions(+), 81 deletions(-) create mode 100644 Frontend/src/app/components/code/code-dialog.tsx rename Frontend/src/app/components/{ => code}/code-highlighter.tsx (78%) create mode 100644 Frontend/src/app/components/raw-json-badge.tsx diff --git a/Frontend/src/app/components/code/code-dialog.tsx b/Frontend/src/app/components/code/code-dialog.tsx new file mode 100644 index 0000000..dc66f06 --- /dev/null +++ b/Frontend/src/app/components/code/code-dialog.tsx @@ -0,0 +1,75 @@ +import { + Dialog, + DialogContent, + DialogDescription, + DialogHeader, + DialogTitle, + DialogTrigger, +} from "@/components/ui/dialog"; +import CodeHighlighter from "@/components/code/code-highlighter"; +import { ReactElement } from "react"; + +/** + * Props for the code dialog. + */ +type CodeDialogProps = { + /** + * The title of this dialog. + */ + title: string; + + /** + * The description of this dialog. + */ + description: string; + + /** + * The language of the code, if any + */ + language: string | undefined; + + /** + * The trigger to open this dialog. + */ + trigger: ReactElement; + + /** + * The code in the dialog. + */ + children: string; +}; + +/** + * A dialog to display code. + * + * @param title the title of this dialog + * @param description the description of this dialog + * @param trigger the trigger to open this dialog + * @param language the language of the code + * @param children the children (code) to display + * @return the dialog jsx + */ +const CodeDialog = ({ + title, + description, + language, + trigger, + children, +}: CodeDialogProps) => ( + + {trigger} + + {/* Header */} + + {title} + {description} + + + {/* Code */} + + {children} + + + +); +export default CodeDialog; diff --git a/Frontend/src/app/components/code-highlighter.tsx b/Frontend/src/app/components/code/code-highlighter.tsx similarity index 78% rename from Frontend/src/app/components/code-highlighter.tsx rename to Frontend/src/app/components/code/code-highlighter.tsx index dd4cf6a..f02c4bd 100644 --- a/Frontend/src/app/components/code-highlighter.tsx +++ b/Frontend/src/app/components/code/code-highlighter.tsx @@ -3,11 +3,17 @@ import SyntaxHighlighter from "react-syntax-highlighter"; import { atomOneDark } from "react-syntax-highlighter/dist/esm/styles/hljs"; import Image from "next/image"; import { capitalize } from "@/lib/string-utils"; +import { cn } from "@/lib"; /** * Props for the code highlighter. */ type CodeHighlighterProps = { + /** + * The class name for the code block. + */ + className?: string | undefined; + /** * The language of the code, if any */ @@ -22,26 +28,33 @@ type CodeHighlighterProps = { /** * A code highlighter component. * + * @param className the class name for the code block * @param language the language for syntax highlighting * @param children the children (code) to highlight * @return the highlighter jsx */ const CodeHighlighter = ({ + className, language, children, }: CodeHighlighterProps): ReactElement => { // Return a basic code block if no language is provided if (!language) { return ( - + {children} ); } return ( -
+
( - + {children} ), diff --git a/Frontend/src/app/components/player/player-result.tsx b/Frontend/src/app/components/player/player-result.tsx index 7dabdbd..ce4a8ce 100644 --- a/Frontend/src/app/components/player/player-result.tsx +++ b/Frontend/src/app/components/player/player-result.tsx @@ -1,5 +1,4 @@ import CopyButton from "@/components/copy-button"; -import { Badge } from "@/components/ui/badge"; import { ContextMenu, ContextMenuContent, @@ -11,6 +10,8 @@ import Image from "next/image"; import Link from "next/link"; import { ReactElement } from "react"; import { CachedPlayer, SkinPart } from "restfulmc-lib"; +import CodeDialog from "@/components/code/code-dialog"; +import RawJsonBadge from "@/components/raw-json-badge"; /** * The props for a player result. @@ -30,41 +31,35 @@ type PlayerResultProps = { /** * The result of a player search. * + * @param query the original query to lookup this player * @param player the player to display * @returns the player result jsx */ -const PlayerResult = ({ - query, - player: { - uniqueId, - username, - skin: { parts }, - legacy, - }, -}: PlayerResultProps): ReactElement => ( +const PlayerResult = ({ query, player }: PlayerResultProps): ReactElement => (
{/* Raw Json */}
- } > - - Raw Json - - + {JSON.stringify(player, undefined, 4)} +
+ {/* Result */}
{/* Details */}
{/* Player Head */} {`${username}'s @@ -72,14 +67,14 @@ const PlayerResult = ({ {/* Name, Unique ID, and Badges */}

- {username} + {player.username}

- {uniqueId} + {player.uniqueId} {/* Legacy Badge */} - {legacy && ( + {player.legacy && (

Legacy

@@ -94,7 +89,7 @@ const PlayerResult = ({ {/* Skin Parts */}
- {Object.entries(parts) + {Object.entries(player.skin.parts) .filter( ([part]) => part === SkinPart.HEAD || @@ -114,7 +109,7 @@ const PlayerResult = ({ {`${username}'s ) @@ -125,11 +120,11 @@ const PlayerResult = ({
- + Copy Player Username - + Copy Player UUID diff --git a/Frontend/src/app/components/raw-json-badge.tsx b/Frontend/src/app/components/raw-json-badge.tsx new file mode 100644 index 0000000..08da08b --- /dev/null +++ b/Frontend/src/app/components/raw-json-badge.tsx @@ -0,0 +1,14 @@ +import { ReactElement } from "react"; +import { Badge } from "@/components/ui/badge"; + +/** + * A badge for raw json. + * + * @returns the raw json badge jsx + */ +const RawJsonBadge = (): ReactElement => ( + + Raw Json + +); +export default RawJsonBadge; diff --git a/Frontend/src/app/components/server/server-result.tsx b/Frontend/src/app/components/server/server-result.tsx index 94de3b3..327a7c6 100644 --- a/Frontend/src/app/components/server/server-result.tsx +++ b/Frontend/src/app/components/server/server-result.tsx @@ -7,6 +7,8 @@ import { } from "restfulmc-lib"; import config from "@/config"; import { minecraft } from "@/font/fonts"; +import CodeDialog from "@/components/code/code-dialog"; +import RawJsonBadge from "@/components/raw-json-badge"; /** * The props for a server result. @@ -28,58 +30,75 @@ const ServerResult = ({ server }: ServerResultProps): ReactElement => { const favicon: string | undefined = (server as CachedJavaMinecraftServer) .favicon?.url; // The favicon of the server return ( -
- {/* Favicon */} - {`${server.hostname}'s +
+ {/* Result */} +
+ {/* Favicon */} + {`${server.hostname}'s - {/* Content */} -
- {/* Name & Ping */} -
-

{server.hostname}

+ {/* Content */} +
+ {/* Name & Ping */} +
+

{server.hostname}

- {/* Players & Ping */} -
-

- {server.players.online} - / - {server.players.max} -

- Ping! + {/* Players & Ping */} +
+

+ {server.players.online} + / + {server.players.max} +

+ Ping! +
+
+ + {/* MOTD */} +
+ {server.motd.html.map( + (line: string, index: number): ReactElement => { + return ( +

+ ); + } + )}
- - {/* MOTD */} -
- {server.motd.html.map( - (line: string, index: number): ReactElement => { - return ( -

- ); - } - )} -
+ + {/* Raw Json */} + } + > + {JSON.stringify(server, undefined, 4)} +
); };