@@ -10,39 +10,42 @@ import { TaskItem } from "./item"
1010import type { AnyTask } from "@/contexts/task-context"
1111
1212interface TaskPanelProps {
13- tasks : AnyTask [ ]
14- onClearTasks : ( ) => void
13+ tasks : readonly AnyTask [ ]
14+ onClearFinishedTasks : ( ) => void
1515}
1616
17- export function TaskPanel ( { tasks, onClearTasks } : TaskPanelProps ) {
17+ export function TaskPanel ( { tasks, onClearFinishedTasks } : TaskPanelProps ) {
1818 const { t } = useTranslation ( )
1919 const [ tab , setTab ] = React . useState ( "pending" )
2020
2121 const pending = tasks . filter ( ( task ) => task . status === "pending" )
2222 const processing = tasks . filter ( ( task ) => task . status === "running" )
2323 const completed = tasks . filter ( ( task ) => task . status === "completed" )
2424 const failed = tasks . filter ( ( task ) => task . status === "failed" )
25+ const canceled = tasks . filter ( ( task ) => task . status === "canceled" )
2526
2627 const total = tasks . length
27- const percentage = total === 0 ? 100 : Math . floor ( ( completed . length / total ) * 100 )
28+ const finished = completed . length + failed . length + canceled . length
29+ const percentage = total === 0 ? 100 : Math . floor ( ( finished / total ) * 100 )
2830
2931 React . useEffect ( ( ) => {
3032 if ( processing . length > 0 ) setTab ( "processing" )
3133 else if ( pending . length > 0 ) setTab ( "pending" )
3234 else if ( completed . length > 0 ) setTab ( "completed" )
35+ else if ( failed . length > 0 ) setTab ( "failed" )
36+ else if ( canceled . length > 0 ) setTab ( "canceled" )
3337 else setTab ( "pending" )
34- } , [ processing . length , pending . length , completed . length ] )
35-
36- const withCount = ( key : string , count : number ) => t ( key ) . replace ( / \{ [ ^ } ] * \} / g, String ( count ) )
38+ } , [ processing . length , pending . length , completed . length , failed . length , canceled . length ] )
3739
3840 const tabs = [
39- { value : "pending" , label : withCount ( "Pending" , pending . length ) } ,
40- { value : "processing" , label : withCount ( "Processing (with count)" , processing . length ) } ,
41- { value : "completed" , label : withCount ( "Completed" , completed . length ) } ,
42- { value : "failed" , label : withCount ( "Failed" , failed . length ) } ,
41+ { value : "pending" , label : t ( "Pending" , { count : pending . length } ) } ,
42+ { value : "processing" , label : t ( "Processing (with count)" , { count : processing . length } ) } ,
43+ { value : "completed" , label : t ( "Completed" , { count : completed . length } ) } ,
44+ { value : "failed" , label : t ( "Failed" , { count : failed . length } ) } ,
45+ { value : "canceled" , label : `${ t ( "Canceled" ) } (${ canceled . length } )` } ,
4346 ]
4447
45- const renderList = ( list : AnyTask [ ] ) => (
48+ const renderList = ( list : readonly AnyTask [ ] ) => (
4649 < div className = "flex flex-col gap-3" >
4750 { list . length === 0 ? (
4851 < p className = "py-4 text-center text-sm text-muted-foreground" > { t ( "No Data" ) } </ p >
@@ -69,11 +72,13 @@ export function TaskPanel({ tasks, onClearTasks }: TaskPanelProps) {
6972 < div className = "space-y-3" >
7073 < div className = "flex items-center justify-between text-sm text-muted-foreground" >
7174 < div >
72- { total - pending . length - processing . length } /{ total }
75+ { finished } /{ total }
7376 </ div >
74- < Button variant = "ghost" size = "sm" className = "h-auto px-2" onClick = { onClearTasks } >
75- { t ( "Clear Records" ) }
76- </ Button >
77+ { finished > 0 && (
78+ < Button variant = "ghost" size = "sm" className = "h-auto px-2" onClick = { onClearFinishedTasks } >
79+ { t ( "Clear Records" ) }
80+ </ Button >
81+ ) }
7782 </ div >
7883 < Progress value = { percentage } className = "h-[3px]" />
7984 </ div >
@@ -100,6 +105,9 @@ export function TaskPanel({ tasks, onClearTasks }: TaskPanelProps) {
100105 < TabsContent value = "failed" className = "mt-0" >
101106 { renderList ( failed ) }
102107 </ TabsContent >
108+ < TabsContent value = "canceled" className = "mt-0" >
109+ { renderList ( canceled ) }
110+ </ TabsContent >
103111 </ Tabs >
104112 </ div >
105113 )
0 commit comments