@@ -31,16 +31,22 @@ export async function addChecklistItem(
3131 config : Config ,
3232 checklistId : string ,
3333 name : string ,
34+ parent ?: string | null ,
3435) : Promise < Checklist > {
3536 const client = new ClickUpClient ( config )
36- return client . createChecklistItem ( checklistId , name )
37+ return client . createChecklistItem ( checklistId , name , parent )
3738}
3839
3940export async function editChecklistItem (
4041 config : Config ,
4142 checklistId : string ,
4243 checklistItemId : string ,
43- updates : { name ?: string ; resolved ?: boolean ; assignee ?: number | null } ,
44+ updates : {
45+ name ?: string
46+ resolved ?: boolean
47+ assignee ?: number | null
48+ parent ?: string | null
49+ } ,
4450) : Promise < Checklist > {
4551 const client = new ClickUpClient ( config )
4652 return client . editChecklistItem ( checklistId , checklistItemId , updates )
@@ -56,33 +62,63 @@ export async function deleteChecklistItem(
5662 return { checklistId, checklistItemId }
5763}
5864
65+ function sortByOrder ( items : ChecklistItem [ ] ) : ChecklistItem [ ] {
66+ return [ ...items ] . sort ( ( a , b ) => ( a . orderindex ?? 0 ) - ( b . orderindex ?? 0 ) )
67+ }
68+
69+ function countItems ( items : ChecklistItem [ ] ) : { total : number ; resolved : number } {
70+ let total = 0
71+ let resolved = 0
72+ for ( const item of items ) {
73+ total ++
74+ if ( item . resolved ) resolved ++
75+ if ( item . children ?. length ) {
76+ const nested = countItems ( item . children )
77+ total += nested . total
78+ resolved += nested . resolved
79+ }
80+ }
81+ return { total, resolved }
82+ }
83+
5984export function formatChecklists ( checklists : Checklist [ ] ) : string {
6085 if ( checklists . length === 0 ) return 'No checklists'
6186 const lines : string [ ] = [ ]
87+ const renderItem = ( item : ChecklistItem , depth : number ) : void => {
88+ const indent = ' ' . repeat ( depth + 1 )
89+ const check = item . resolved ? chalk . green ( '[x]' ) : chalk . dim ( '[ ]' )
90+ const name = item . resolved ? chalk . dim ( item . name ) : item . name
91+ const assignee = item . assignee ? chalk . dim ( ` @${ item . assignee . username } ` ) : ''
92+ lines . push ( `${ indent } ${ check } ${ name } ${ assignee } ` )
93+ lines . push ( chalk . dim ( `${ indent } item-id: ${ item . id } ` ) )
94+ for ( const child of sortByOrder ( item . children ?? [ ] ) ) {
95+ renderItem ( child , depth + 1 )
96+ }
97+ }
6298 for ( const cl of checklists ) {
63- const resolved = cl . items . filter ( i => i . resolved ) . length
64- lines . push ( chalk . bold ( `${ cl . name } (${ resolved } /${ cl . items . length } )` ) )
99+ const { total , resolved } = countItems ( cl . items )
100+ lines . push ( chalk . bold ( `${ cl . name } (${ resolved } /${ total } )` ) )
65101 lines . push ( chalk . dim ( ` ID: ${ cl . id } ` ) )
66- for ( const item of cl . items ) {
67- const check = item . resolved ? chalk . green ( '[x]' ) : chalk . dim ( '[ ]' )
68- const name = item . resolved ? chalk . dim ( item . name ) : item . name
69- const assignee = item . assignee ? chalk . dim ( ` @${ item . assignee . username } ` ) : ''
70- lines . push ( ` ${ check } ${ name } ${ assignee } ` )
71- lines . push ( chalk . dim ( ` item-id: ${ item . id } ` ) )
72- }
102+ for ( const item of sortByOrder ( cl . items ) ) renderItem ( item , 0 )
73103 }
74104 return lines . join ( '\n' )
75105}
76106
77107export function formatChecklistsMarkdown ( checklists : Checklist [ ] ) : string {
78108 if ( checklists . length === 0 ) return 'No checklists'
109+ const renderItem = ( item : ChecklistItem , depth : number ) : string [ ] => {
110+ const indent = ' ' . repeat ( depth )
111+ const lines = [ `${ indent } - [${ item . resolved ? 'x' : ' ' } ] ${ item . name } ` ]
112+ for ( const child of sortByOrder ( item . children ?? [ ] ) ) {
113+ lines . push ( ...renderItem ( child , depth + 1 ) )
114+ }
115+ return lines
116+ }
79117 return checklists
80118 . map ( cl => {
81- const resolved = cl . items . filter ( ( i : ChecklistItem ) => i . resolved ) . length
82- const header = `### ${ cl . name } (${ resolved } /${ cl . items . length } )`
83- const items = cl . items . map (
84- ( item : ChecklistItem ) => `- [${ item . resolved ? 'x' : ' ' } ] ${ item . name } ` ,
85- )
119+ const { total, resolved } = countItems ( cl . items )
120+ const header = `### ${ cl . name } (${ resolved } /${ total } )`
121+ const items = sortByOrder ( cl . items ) . flatMap ( item => renderItem ( item , 0 ) )
86122 return [ header , '' , ...items ] . join ( '\n' )
87123 } )
88124 . join ( '\n\n' )
0 commit comments