|
| 1 | +"use client"; |
| 2 | +import React, { useRef, useState } from "react"; |
| 3 | +import { Timer } from "lucide-react"; |
| 4 | +import { crontab, crontabAI } from "@/app/server"; |
| 5 | +import { readStreamableValue } from "ai/rsc"; |
| 6 | + |
| 7 | +function Crontab() { |
| 8 | + const [isLoading, setIsLoading] = useState(false); |
| 9 | + const inputRef = useRef<HTMLInputElement>(null); |
| 10 | + const [list, setList] = useState<string[]>([]); |
| 11 | + const [err, setErr] = useState(""); |
| 12 | + |
| 13 | + async function onClick() { |
| 14 | + if (!inputRef.current?.value) { |
| 15 | + return; |
| 16 | + } |
| 17 | + try { |
| 18 | + const ret = await crontab(inputRef.current.value); |
| 19 | + if (ret.error) { |
| 20 | + throw ret.error; |
| 21 | + } |
| 22 | + err && setErr(""); |
| 23 | + setList(ret); |
| 24 | + } catch (err: any) { |
| 25 | + list.length && setList([]); |
| 26 | + setErr(err.message ?? err); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + async function onClickAI() { |
| 31 | + if (!inputRef.current?.value) { |
| 32 | + return; |
| 33 | + } |
| 34 | + err && setErr(""); |
| 35 | + list.length && setList([]); |
| 36 | + setIsLoading(true); |
| 37 | + try { |
| 38 | + const { data, error } = await crontabAI(inputRef.current.value); |
| 39 | + if (error) { |
| 40 | + setErr(error); |
| 41 | + return; |
| 42 | + } |
| 43 | + let ret = ""; |
| 44 | + for await (const delta of readStreamableValue(data!)) { |
| 45 | + ret += delta; |
| 46 | + inputRef.current.value = ret; |
| 47 | + } |
| 48 | + await onClick(); |
| 49 | + } catch (err: any) { |
| 50 | + setErr(err.message ?? err); |
| 51 | + } finally { |
| 52 | + setIsLoading(false); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + async function onKeyDown(e: React.KeyboardEvent<HTMLInputElement>) { |
| 57 | + if (e.key !== "Enter" || e.nativeEvent.isComposing) { |
| 58 | + return; |
| 59 | + } |
| 60 | + e.preventDefault(); |
| 61 | + if (isLoading) { |
| 62 | + return; |
| 63 | + } |
| 64 | + await onClick(); |
| 65 | + } |
| 66 | + |
| 67 | + return ( |
| 68 | + <> |
| 69 | + <div className="flex w-full gap-2 sm:gap-4"> |
| 70 | + <label className="input input-bordered flex grow items-center gap-2 px-3"> |
| 71 | + <Timer className="opacity-70" /> |
| 72 | + <input |
| 73 | + ref={inputRef} |
| 74 | + className="grow" |
| 75 | + placeholder={`Crontab 或 "每两小时执行一次"`} |
| 76 | + onKeyDown={onKeyDown} |
| 77 | + disabled={isLoading} |
| 78 | + /> |
| 79 | + </label> |
| 80 | + {isLoading ? ( |
| 81 | + <span className="loading loading-infinity loading-lg mx-3" /> |
| 82 | + ) : ( |
| 83 | + <button className="btn-normal" onClick={onClickAI}> |
| 84 | + 问问 AI |
| 85 | + </button> |
| 86 | + )} |
| 87 | + </div> |
| 88 | + <button className="btn-normal" onClick={onClick} disabled={isLoading}> |
| 89 | + 查看执行时间 |
| 90 | + </button> |
| 91 | + {list.length !== 0 && ( |
| 92 | + <div className="flex flex-col gap-2 rounded-md bg-base-200 p-3 font-medium"> |
| 93 | + <span>接下来 7 次的执行时间:</span> |
| 94 | + {list.map((v, i) => ( |
| 95 | + <div className="rounded-md bg-base-300 p-2" key={i}> |
| 96 | + {i + 1} - {v} |
| 97 | + </div> |
| 98 | + ))} |
| 99 | + </div> |
| 100 | + )} |
| 101 | + {err && ( |
| 102 | + <div className="rounded-md bg-warning/50 p-3 text-warning-content"> |
| 103 | + {err} |
| 104 | + </div> |
| 105 | + )} |
| 106 | + <pre className="shrink-0 overflow-x-auto rounded-md bg-base-200 p-3"> |
| 107 | + {`* * * * * |
| 108 | +| | | | | |
| 109 | +| | | | +----- day of week (0 - 7) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat |
| 110 | +| | | +-------- month (1 - 12) OR jan,feb,mar,apr ... |
| 111 | +| | +----------- day of month (1 - 31) |
| 112 | +| +-------------- hour (0 - 23) |
| 113 | ++----------------- minute (0 - 59)`} |
| 114 | + </pre> |
| 115 | + </> |
| 116 | + ); |
| 117 | +} |
| 118 | + |
| 119 | +export default Crontab; |
0 commit comments