Add keyboard shortcut for form submission and remove unused useEffect
This commit is contained in:
@@ -22,7 +22,7 @@ export function EditorToolbar({
|
|||||||
variant="ghost"
|
variant="ghost"
|
||||||
size="icon"
|
size="icon"
|
||||||
onClick={() => router.push("/stories")}
|
onClick={() => router.push("/stories")}
|
||||||
className="hover:bg-secondary text-muted-foreground hover:text-white flex-shrink-0"
|
className="hover:bg-secondary text-muted-foreground hover:text-white shrink-0"
|
||||||
>
|
>
|
||||||
<ArrowLeft className="w-4 h-4 sm:w-5 sm:h-5" />
|
<ArrowLeft className="w-4 h-4 sm:w-5 sm:h-5" />
|
||||||
</Button>
|
</Button>
|
||||||
@@ -32,7 +32,7 @@ export function EditorToolbar({
|
|||||||
</h1>
|
</h1>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex items-center gap-1 sm:gap-2 flex-shrink-0">
|
<div className="flex items-center gap-1 sm:gap-2 shrink-0">
|
||||||
<Button
|
<Button
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
className="hover:bg-secondary text-muted-foreground hover:text-white gap-1.5 sm:gap-2 text-xs h-8 sm:h-9 px-2 sm:px-3 hidden md:flex"
|
className="hover:bg-secondary text-muted-foreground hover:text-white gap-1.5 sm:gap-2 text-xs h-8 sm:h-9 px-2 sm:px-3 hidden md:flex"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useState, useRef, useEffect, useMemo } from "react";
|
import { useState, useRef, useEffect } from "react";
|
||||||
import { Upload, X, Loader2 } from "lucide-react";
|
import { Upload, X, Loader2 } from "lucide-react";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import {
|
import {
|
||||||
@@ -10,6 +10,7 @@ import {
|
|||||||
DialogTitle,
|
DialogTitle,
|
||||||
} from "@/components/ui/dialog";
|
} from "@/components/ui/dialog";
|
||||||
import { useToast } from "@/hooks/use-toast";
|
import { useToast } from "@/hooks/use-toast";
|
||||||
|
import { useKeyboardShortcut } from "@/hooks/use-keyboard-shortcut";
|
||||||
import { validateFileForUpload, generateFilePreview } from "@/lib/file-utils";
|
import { validateFileForUpload, generateFilePreview } from "@/lib/file-utils";
|
||||||
import { COMIC_STYLES } from "@/lib/constants";
|
import { COMIC_STYLES } from "@/lib/constants";
|
||||||
|
|
||||||
@@ -49,6 +50,13 @@ export function GeneratePageModal({
|
|||||||
}
|
}
|
||||||
}, [isOpen]);
|
}, [isOpen]);
|
||||||
|
|
||||||
|
// Keyboard shortcut for form submission
|
||||||
|
useKeyboardShortcut(() => {
|
||||||
|
if (isOpen && !isGenerating && prompt.trim()) {
|
||||||
|
handleGenerate();
|
||||||
|
}
|
||||||
|
}, { disabled: !isOpen || isGenerating });
|
||||||
|
|
||||||
const handleFiles = async (newFiles: FileList | null) => {
|
const handleFiles = async (newFiles: FileList | null) => {
|
||||||
if (!newFiles) return;
|
if (!newFiles) return;
|
||||||
|
|
||||||
@@ -242,14 +250,14 @@ export function GeneratePageModal({
|
|||||||
{/* Character Preview Modal */}
|
{/* Character Preview Modal */}
|
||||||
{showPreview !== null && previews[showPreview] && (
|
{showPreview !== null && previews[showPreview] && (
|
||||||
<div
|
<div
|
||||||
className="fixed inset-0 bg-black/80 backdrop-blur-sm z-[100] flex items-center justify-center p-4"
|
className="fixed inset-0 bg-black/80 backdrop-blur-sm z-100 flex items-center justify-center p-4"
|
||||||
onClick={() => setShowPreview(null)}
|
onClick={() => setShowPreview(null)}
|
||||||
>
|
>
|
||||||
<div className="relative max-w-sm max-h-[80vh] glass-panel p-4 rounded-xl z-[101]">
|
<div className="relative max-w-sm max-h-[80vh] glass-panel p-4 rounded-xl z-101">
|
||||||
<Button
|
<Button
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
size="icon"
|
size="icon"
|
||||||
className="absolute top-2 right-2 h-8 w-8 hover:bg-white/10 z-[102]"
|
className="absolute top-2 right-2 h-8 w-8 hover:bg-white/10 z-102"
|
||||||
onClick={() => setShowPreview(null)}
|
onClick={() => setShowPreview(null)}
|
||||||
>
|
>
|
||||||
<X className="w-4 h-4" />
|
<X className="w-4 h-4" />
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import { useToast } from "@/hooks/use-toast";
|
|||||||
import { useS3Upload } from "next-s3-upload";
|
import { useS3Upload } from "next-s3-upload";
|
||||||
import { useAuth, SignInButton } from "@clerk/nextjs";
|
import { useAuth, SignInButton } from "@clerk/nextjs";
|
||||||
import { COMIC_STYLES } from "@/lib/constants";
|
import { COMIC_STYLES } from "@/lib/constants";
|
||||||
|
import { useKeyboardShortcut } from "@/hooks/use-keyboard-shortcut";
|
||||||
|
|
||||||
interface ComicCreationFormProps {
|
interface ComicCreationFormProps {
|
||||||
prompt: string;
|
prompt: string;
|
||||||
@@ -68,27 +69,12 @@ export function ComicCreationForm({
|
|||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
// Keyboard shortcut for form submission
|
||||||
if (!isLoading) return;
|
useKeyboardShortcut(() => {
|
||||||
|
if (!isLoading && prompt.trim()) {
|
||||||
const steps = [
|
handleCreate();
|
||||||
"Enhancing prompt...",
|
}
|
||||||
"Generating scenes...",
|
}, { disabled: isLoading });
|
||||||
"Creating your comic...",
|
|
||||||
];
|
|
||||||
let currentStep = 0;
|
|
||||||
|
|
||||||
const interval = setInterval(() => {
|
|
||||||
currentStep += 1;
|
|
||||||
if (currentStep < steps.length) {
|
|
||||||
setLoadingStep(currentStep);
|
|
||||||
} else {
|
|
||||||
clearInterval(interval);
|
|
||||||
}
|
|
||||||
}, 2500);
|
|
||||||
|
|
||||||
return () => clearInterval(interval);
|
|
||||||
}, [isLoading]);
|
|
||||||
|
|
||||||
const handleFiles = (newFiles: FileList | null) => {
|
const handleFiles = (newFiles: FileList | null) => {
|
||||||
if (!newFiles) return;
|
if (!newFiles) return;
|
||||||
@@ -228,7 +214,6 @@ export function ComicCreationForm({
|
|||||||
ref={textareaRef}
|
ref={textareaRef}
|
||||||
value={prompt}
|
value={prompt}
|
||||||
onChange={(e) => setPrompt(e.target.value)}
|
onChange={(e) => setPrompt(e.target.value)}
|
||||||
onKeyDown={handleKeyDown}
|
|
||||||
placeholder="A cyberpunk detective standing in neon rain, holding a glowing datapad, moody lighting, noir style..."
|
placeholder="A cyberpunk detective standing in neon rain, holding a glowing datapad, moody lighting, noir style..."
|
||||||
disabled={isLoading}
|
disabled={isLoading}
|
||||||
className="w-full bg-transparent border-none text-sm text-white placeholder-muted-foreground/50 focus:ring-0 focus:outline-none resize-none h-16 leading-relaxed disabled:opacity-50 disabled:cursor-not-allowed"
|
className="w-full bg-transparent border-none text-sm text-white placeholder-muted-foreground/50 focus:ring-0 focus:outline-none resize-none h-16 leading-relaxed disabled:opacity-50 disabled:cursor-not-allowed"
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
import { useEffect } from 'react';
|
||||||
|
|
||||||
|
export function useKeyboardShortcut(
|
||||||
|
callback: () => void,
|
||||||
|
options: {
|
||||||
|
ctrlOrCmd?: boolean;
|
||||||
|
shift?: boolean;
|
||||||
|
key?: string;
|
||||||
|
disabled?: boolean;
|
||||||
|
} = {}
|
||||||
|
) {
|
||||||
|
const {
|
||||||
|
ctrlOrCmd = true,
|
||||||
|
shift = false,
|
||||||
|
key = 'Enter',
|
||||||
|
disabled = false,
|
||||||
|
} = options;
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (disabled) return;
|
||||||
|
|
||||||
|
const handleKeyDown = (e: KeyboardEvent) => {
|
||||||
|
const isKeyPressed = e.key === key;
|
||||||
|
const isModifierPressed = ctrlOrCmd
|
||||||
|
? (e.ctrlKey || e.metaKey) // Ctrl on Windows/Linux, Cmd on Mac
|
||||||
|
: shift
|
||||||
|
? e.shiftKey
|
||||||
|
: false;
|
||||||
|
|
||||||
|
if (isKeyPressed && isModifierPressed) {
|
||||||
|
e.preventDefault();
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
document.addEventListener('keydown', handleKeyDown);
|
||||||
|
return () => document.removeEventListener('keydown', handleKeyDown);
|
||||||
|
}, [callback, ctrlOrCmd, shift, key, disabled]);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user