@@ -18,23 +18,19 @@ import {
1818} from 'lucide-react' ;
1919
2020import { useExperimentInfo } from 'renderer/lib/ExperimentInfoContext' ;
21+ import { useNotification } from 'renderer/components/Shared/NotificationSystem' ;
2122import TaskModal from './TaskModal' ;
2223import NewTaskModal from './NewTaskModal' ;
2324import * as chatAPI from 'renderer/lib/transformerlab-api-sdk' ;
24- import useSWR from 'swr ' ;
25+ import { useAPI } from 'renderer/lib/api-client/hooks ' ;
2526import Chip from '@mui/joy/Chip' ;
2627
27- export default function TaskLibrary ( { } ) {
28+ export default function TaskLibrary ( ) {
2829 const { experimentInfo } = useExperimentInfo ( ) ;
30+ const { addNotification } = useNotification ( ) ;
2931
30- const { data : localGalleryResp } = useSWR (
31- chatAPI . getAPIFullPath ( 'tasks' , [ 'localGallery' ] , { } ) ,
32- chatAPI . fetcher
33- ) ;
34- const { data : remoteGalleryResp } = useSWR (
35- chatAPI . getAPIFullPath ( 'tasks' , [ 'gallery' ] , { } ) ,
36- chatAPI . fetcher
37- ) ;
32+ const { data : localGalleryResp } = useAPI ( 'tasks' , [ 'localGallery' ] , { } ) ;
33+ const { data : remoteGalleryResp } = useAPI ( 'tasks' , [ 'gallery' ] , { } ) ;
3834
3935 // local overlay for create/edit/delete without waiting for refetch
4036 const [ overlayTasks , setOverlayTasks ] = useState < any [ ] > ( [ ] ) ;
@@ -74,19 +70,81 @@ export default function TaskLibrary({}) {
7470 } ;
7571
7672 const handleImportFromGallery = async ( subdir : string ) => {
77- const url = chatAPI . getAPIFullPath ( 'tasks' , [ 'importFromGallery' ] , { } ) ;
78- const form = new URLSearchParams ( ) ;
79- form . set ( 'subdir' , subdir ) ;
80- // experimentId optional; if available in context add it
81- if ( experimentInfo ?. id ) form . set ( 'experiment_id' , experimentInfo . id ) ;
82- await fetch ( url , {
83- method : 'POST' ,
84- headers : {
85- 'Content-Type' : 'application/x-www-form-urlencoded' ,
86- } ,
87- body : form . toString ( ) ,
88- credentials : 'include' ,
89- } ) ;
73+ try {
74+ const url = chatAPI . getAPIFullPath ( 'tasks' , [ 'importFromGallery' ] , { } ) ;
75+ const form = new URLSearchParams ( ) ;
76+ form . set ( 'subdir' , subdir ) ;
77+ // experimentId optional; if available in context add it
78+ if ( experimentInfo ?. id ) form . set ( 'experiment_id' , experimentInfo . id ) ;
79+
80+ const response = await fetch ( url , {
81+ method : 'POST' ,
82+ headers : {
83+ 'Content-Type' : 'application/x-www-form-urlencoded' ,
84+ } ,
85+ body : form . toString ( ) ,
86+ credentials : 'include' ,
87+ } ) ;
88+
89+ const result = await response . json ( ) ;
90+
91+ if ( result . status === 'success' ) {
92+ const action = result . action || 'imported' ;
93+ addNotification ( {
94+ type : 'success' ,
95+ message : `Task ${ action === 'updated' ? 'updated' : 'imported' } successfully from gallery!` ,
96+ } ) ;
97+ } else {
98+ addNotification ( {
99+ type : 'danger' ,
100+ message : `Failed to import task: ${ result . message || 'Unknown error' } ` ,
101+ } ) ;
102+ }
103+ } catch ( error ) {
104+ addNotification ( {
105+ type : 'danger' ,
106+ message : `Failed to import task: ${ error } ` ,
107+ } ) ;
108+ }
109+ } ;
110+
111+ const handleImportFromLocal = async ( subdir : string ) => {
112+ try {
113+ const url = chatAPI . getAPIFullPath ( 'tasks' , [ 'importFromLocalGallery' ] , { } ) ;
114+ const form = new URLSearchParams ( ) ;
115+ form . set ( 'subdir' , subdir ) ;
116+ // experimentId optional; if available in context add it
117+ if ( experimentInfo ?. id ) form . set ( 'experiment_id' , experimentInfo . id ) ;
118+
119+ const response = await fetch ( url , {
120+ method : 'POST' ,
121+ headers : {
122+ 'Content-Type' : 'application/x-www-form-urlencoded' ,
123+ } ,
124+ body : form . toString ( ) ,
125+ credentials : 'include' ,
126+ } ) ;
127+
128+ const result = await response . json ( ) ;
129+
130+ if ( result . status === 'success' ) {
131+ const action = result . action || 'imported' ;
132+ addNotification ( {
133+ type : 'success' ,
134+ message : `Task ${ action === 'updated' ? 'updated' : 'imported' } successfully from local gallery!` ,
135+ } ) ;
136+ } else {
137+ addNotification ( {
138+ type : 'danger' ,
139+ message : `Failed to import task: ${ result . message || 'Unknown error' } ` ,
140+ } ) ;
141+ }
142+ } catch ( error ) {
143+ addNotification ( {
144+ type : 'danger' ,
145+ message : `Failed to import task: ${ error } ` ,
146+ } ) ;
147+ }
90148 } ;
91149
92150 const handleCreate = ( ) => {
@@ -235,6 +293,17 @@ export default function TaskLibrary({}) {
235293 Import
236294 </ Button >
237295 ) }
296+
297+ { task . _isLocal && (
298+ < Button
299+ size = "sm"
300+ variant = "outlined"
301+ onClick = { ( ) => handleImportFromLocal ( task . _subdir || task . id . split ( ':' ) [ 1 ] ) }
302+ startDecorator = { < FilePlus size = { 12 } /> }
303+ >
304+ Import
305+ </ Button >
306+ ) }
238307 </ Box >
239308 </ ListItem >
240309 ) ) }
0 commit comments