Skip to content

Commit

Permalink
Merge pull request #103 from replicate/autofocus-input
Browse files Browse the repository at this point in the history
autofocus input
  • Loading branch information
zeke committed May 13, 2024
2 parents 69ba9db + 67a394a commit 43856bc
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions app/page.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
'use client';

import { useState } from "react";
import { useState, useEffect, useRef } from "react";
import Image from "next/image";

const sleep = (ms) => new Promise((r) => setTimeout(r, ms));

export default function Home() {
const [prediction, setPrediction] = useState(null);
const [error, setError] = useState(null);
const promptInputRef = useRef(null);

useEffect(() => {
promptInputRef.current.focus();
}, []);

const handleSubmit = async (e) => {
e.preventDefault();
Expand All @@ -32,7 +37,7 @@ export default function Home() {
prediction.status !== "failed"
) {
await sleep(1000);
const response = await fetch("/api/predictions/" + prediction.id);
const response = await fetch(`/api/predictions/${prediction.id}`);
prediction = await response.json();
if (response.status !== 200) {
setError(prediction.detail);
Expand All @@ -58,6 +63,7 @@ export default function Home() {
className="flex-grow"
name="prompt"
placeholder="Enter a prompt to display an image"
ref={promptInputRef}
/>
<button className="button" type="submit">
Go!
Expand Down

0 comments on commit 43856bc

Please sign in to comment.