11import { Button } from "@/components/ui/button.tsx"
22import { Input } from "@/components/ui/input.tsx"
3+ import { respData } from "@/lib/utils"
34import { CronExpressionParser } from "cron-parser"
4- import { Calculator , Timer } from "lucide-react"
5+ import { Atom , Calculator , Timer } from "lucide-react"
56import React , { useState } from "react"
67import { toast } from "sonner"
78
89function TCrontab ( ) {
910 const [ input , setInput ] = useState ( "" )
1011 const [ list , setList ] = useState < string [ ] > ( [ ] )
1112
12- function onClick ( ) {
13- const expr = input . trim ( )
13+ const [ loading , setLoading ] = useState ( false )
14+
15+ function onClick ( data ?: string ) {
16+ const expr = ( data ?? input ) . trim ( )
1417 if ( ! expr ) {
1518 return
1619 }
@@ -24,6 +27,22 @@ function TCrontab() {
2427 }
2528 }
2629
30+ function onAIClick ( ) {
31+ const desc = input . trim ( )
32+ if ( ! desc ) {
33+ return
34+ }
35+ setLoading ( true )
36+ fetch ( `/api/crontab?desc=${ desc } ` )
37+ . then ( respData )
38+ . then ( ( data ) => {
39+ setInput ( data )
40+ onClick ( data )
41+ } )
42+ . catch ( ( err ) => toast . error ( err . message ?? err ) )
43+ . finally ( ( ) => setLoading ( false ) )
44+ }
45+
2746 function onKeyDown ( e : React . KeyboardEvent < HTMLInputElement > ) {
2847 if ( e . key !== "Enter" || e . nativeEvent . isComposing ) {
2948 return
@@ -37,15 +56,19 @@ function TCrontab() {
3756 < div className = "flex flex-col gap-3 sm:flex-row" >
3857 < Input
3958 value = { input }
59+ disabled = { loading }
4060 onKeyDown = { onKeyDown }
4161 className = "font-mono"
4262 onChange = { ( e ) => setInput ( e . currentTarget . value ) }
43- placeholder = "请输入 Crontab 表达式"
44- > </ Input >
45- < Button variant = "secondary" onClick = { onClick } >
63+ placeholder = "Crontab 表达式 / 每两小时执行一次 "
64+ / >
65+ < Button onClick = { ( ) => onClick ( ) } disabled = { loading } >
4666 < Calculator />
4767 计算执行时间
4868 </ Button >
69+ < Button variant = "secondary" onClick = { onAIClick } disabled = { loading } >
70+ < Atom /> 让 AI 生成
71+ </ Button >
4972 </ div >
5073 { list . length > 0 && (
5174 < div className = "flex flex-col gap-2" >
0 commit comments