Skip to content

Commit

Permalink
wip: prompt and config dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
tabarra committed Dec 1, 2023
1 parent 5fbd758 commit 566a23f
Show file tree
Hide file tree
Showing 10 changed files with 566 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/dev_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Processo:
- [ ] generic toasts
- [ ] markdown toasts
- [ ] error toasts with discord link
- [ ][2h] prompts API
- [x][2h] prompts API
- [ ][2h] server controls
- [ ][1h] server scheduled restarts (legacy style)
- [ ][3d] playerlist
Expand Down
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions panel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"dependencies": {
"@fontsource-variable/inter": "^5.0.15",
"@fontsource-variable/jetbrains-mono": "^5.0.18",
"@radix-ui/react-alert-dialog": "^1.0.5",
"@radix-ui/react-avatar": "^1.0.4",
"@radix-ui/react-checkbox": "^1.0.4",
"@radix-ui/react-dialog": "^1.0.5",
Expand Down
79 changes: 79 additions & 0 deletions panel/src/components/ConfirmDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import { useCloseConfirmDialog, useConfirmDialogState } from "@/hooks/dialogs";
import { cn } from "@/lib/utils";
import { useRef } from "react";
import { buttonVariants } from "./ui/button";
import { useOnClickOutside } from "usehooks-ts";


export default function ConfirmDialog() {
const modalRef = useRef(null);
const dialogState = useConfirmDialogState();
const closeDialog = useCloseConfirmDialog();

const handleCancel = () => {
if (!dialogState.isOpen) return;
closeDialog();
if (dialogState.onCancel) {
dialogState.onCancel();
}
}

const handleConfirm = () => {
if (!dialogState.isOpen) return;
closeDialog();
dialogState.onConfirm();
}

const handleConfirmKeyDown = (e: React.KeyboardEvent<HTMLButtonElement>) => {
e.preventDefault();
if (e.key === 'Enter' || e.key === 'NumpadEnter') {
handleConfirm();
} else if(e.key === 'Backspace' || e.key === 'Escape'){
handleCancel();
}
}

const handleOpenClose = (newOpenState: boolean) => {
if (!newOpenState) {
handleCancel();
}
}

useOnClickOutside(modalRef, handleCancel);

return (
<AlertDialog open={dialogState.isOpen} onOpenChange={handleOpenClose}>
<AlertDialogContent ref={modalRef}>
<AlertDialogHeader>
<AlertDialogTitle>{dialogState.title}</AlertDialogTitle>
<AlertDialogDescription>
{dialogState.message}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>
{dialogState.cancelLabel ?? 'Cancel'}
</AlertDialogCancel>
<AlertDialogAction
autoFocus
onKeyDown={handleConfirmKeyDown}
onClick={handleConfirm}
className={cn(buttonVariants({ variant: dialogState.confirmBtnVariant ?? 'destructive' }))}
>
{dialogState.actionLabel ?? 'Continue'}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
);
}
89 changes: 89 additions & 0 deletions panel/src/components/PromptDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { useClosePromptDialog, usePromptDialogState } from "@/hooks/dialogs";
import { useRef } from "react";


export default function PromptDialog() {
const inputRef = useRef<HTMLInputElement>(null);
const dialogState = usePromptDialogState();
const closeDialog = useClosePromptDialog();

const handleSubmit = () => {
if (!dialogState.isOpen) return;
closeDialog();
dialogState.onSubmit(inputRef.current?.value ?? '');

}

const handleForm = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
handleSubmit();
}

const handleOpenClose = (newOpenState: boolean) => {
if (!dialogState.isOpen) return;
if (!newOpenState) {
closeDialog();
if (dialogState.onCancel) {
dialogState.onCancel();
}
}
}

return (
<Dialog open={dialogState.isOpen} onOpenChange={handleOpenClose}>
<DialogContent>
<form onSubmit={handleForm} className="grid gap-4">
<DialogHeader>
<DialogTitle>{dialogState.title}</DialogTitle>
<DialogDescription>
{dialogState.message}
</DialogDescription>
</DialogHeader>

<Input
autoFocus
id="userInput"
ref={inputRef}
placeholder={dialogState.placeholder}
className="placeholder:opacity-50"
required={dialogState.required}
/>
<DialogFooter className="gap-2 flex-col">
<div className="flex flex-col-reversex flex-col sm:flex-row sm:justify-start gap-2 w-full flex-wrap">
{dialogState.suggestions && dialogState.suggestions.map((suggestion, index) => (
<Button
key={index}
type="button"
onClick={() => {
inputRef.current!.value = suggestion;
handleSubmit();
}}
variant="outline"
>
<span className="sm:hidden mr-auto text-muted-foreground">Suggestion:</span>
{suggestion}
</Button>
))}
</div>
<Button
type="submit"
variant={dialogState.submitBtnVariant ?? 'default'}
>
{dialogState.submitLabel ?? 'Submit'}
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
);
}
139 changes: 139 additions & 0 deletions panel/src/components/ui/alert-dialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import * as React from "react"
import * as AlertDialogPrimitive from "@radix-ui/react-alert-dialog"

import { cn } from "@/lib/utils"
import { buttonVariants } from "@/components/ui/button"

const AlertDialog = AlertDialogPrimitive.Root

const AlertDialogTrigger = AlertDialogPrimitive.Trigger

const AlertDialogPortal = AlertDialogPrimitive.Portal

const AlertDialogOverlay = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Overlay>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Overlay>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Overlay
className={cn(
"fixed inset-0 z-50 bg-background/80 backdrop-blur-sm data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
className
)}
{...props}
ref={ref}
/>
))
AlertDialogOverlay.displayName = AlertDialogPrimitive.Overlay.displayName

const AlertDialogContent = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Content>
>(({ className, ...props }, ref) => (
<AlertDialogPortal>
<AlertDialogOverlay />
<AlertDialogPrimitive.Content
ref={ref}
className={cn(
"fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg",
className
)}
{...props}
/>
</AlertDialogPortal>
))
AlertDialogContent.displayName = AlertDialogPrimitive.Content.displayName

const AlertDialogHeader = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col space-y-2 text-center sm:text-left",
className
)}
{...props}
/>
)
AlertDialogHeader.displayName = "AlertDialogHeader"

const AlertDialogFooter = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",
className
)}
{...props}
/>
)
AlertDialogFooter.displayName = "AlertDialogFooter"

const AlertDialogTitle = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Title>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Title>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Title
ref={ref}
className={cn("text-lg font-semibold", className)}
{...props}
/>
))
AlertDialogTitle.displayName = AlertDialogPrimitive.Title.displayName

const AlertDialogDescription = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Description>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Description>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Description
ref={ref}
className={cn("text-sm text-muted-foreground", className)}
{...props}
/>
))
AlertDialogDescription.displayName =
AlertDialogPrimitive.Description.displayName

const AlertDialogAction = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Action>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Action>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Action
ref={ref}
className={cn(buttonVariants(), className)}
{...props}
/>
))
AlertDialogAction.displayName = AlertDialogPrimitive.Action.displayName

const AlertDialogCancel = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Cancel>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Cancel>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Cancel
ref={ref}
className={cn(
buttonVariants({ variant: "outline" }),
"mt-2 sm:mt-0",
className
)}
{...props}
/>
))
AlertDialogCancel.displayName = AlertDialogPrimitive.Cancel.displayName

export {
AlertDialog,
AlertDialogPortal,
AlertDialogOverlay,
AlertDialogTrigger,
AlertDialogContent,
AlertDialogHeader,
AlertDialogFooter,
AlertDialogTitle,
AlertDialogDescription,
AlertDialogAction,
AlertDialogCancel,
}

0 comments on commit 566a23f

Please sign in to comment.