Skip to content

Commit 121fcf3

Browse files
committed
import from local gallery and frontend notif changes
1 parent fed9871 commit 121fcf3

File tree

3 files changed

+108
-28
lines changed

3 files changed

+108
-28
lines changed

src/renderer/components/TaskLibrary/NewTaskModal.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import {
1818
Alert,
1919
} from '@mui/joy';
2020
import * as chatAPI from 'renderer/lib/transformerlab-api-sdk';
21-
import useSWR from 'swr';
21+
import { useNotification } from 'renderer/components/Shared/NotificationSystem';
22+
import { useAPI } from 'renderer/lib/api-client/hooks';
2223

2324
interface NewTaskModalProps {
2425
open: boolean;
@@ -32,12 +33,10 @@ export default function NewTaskModal({ open, onClose, onSuccess }: NewTaskModalP
3233
const [selectedTaskId, setSelectedTaskId] = useState('');
3334
const [isSubmitting, setIsSubmitting] = useState(false);
3435
const [error, setError] = useState('');
36+
const { addNotification } = useNotification();
3537

3638
// Fetch existing REMOTE tasks
37-
const { data: remoteTasksResp } = useSWR(
38-
chatAPI.getAPIFullPath('tasks', ['getAll'], {}),
39-
chatAPI.fetcher
40-
);
39+
const { data: remoteTasksResp } = useAPI('tasks', ['getAll'], {});
4140

4241
const remoteTasks = Array.isArray(remoteTasksResp)
4342
? remoteTasksResp.filter((task: any) => task.remote_task === true)
@@ -71,6 +70,10 @@ export default function NewTaskModal({ open, onClose, onSuccess }: NewTaskModalP
7170
const result = await response.json();
7271

7372
if (result.status === 'success') {
73+
addNotification({
74+
type: 'success',
75+
message: `Task "${taskName}" successfully exported to local gallery!`,
76+
});
7477
onSuccess?.();
7578
onClose();
7679
setTaskName('');
@@ -80,6 +83,10 @@ export default function NewTaskModal({ open, onClose, onSuccess }: NewTaskModalP
8083
setError(result.message || 'Failed to create task');
8184
}
8285
} catch (err) {
86+
addNotification({
87+
type: 'danger',
88+
message: `Failed to export task: ${err}`,
89+
});
8390
setError('Network error occurred');
8491
} finally {
8592
setIsSubmitting(false);

src/renderer/components/TaskLibrary/TaskLibrary.tsx

Lines changed: 92 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,19 @@ import {
1818
} from 'lucide-react';
1919

2020
import { useExperimentInfo } from 'renderer/lib/ExperimentInfoContext';
21+
import { useNotification } from 'renderer/components/Shared/NotificationSystem';
2122
import TaskModal from './TaskModal';
2223
import NewTaskModal from './NewTaskModal';
2324
import * as chatAPI from 'renderer/lib/transformerlab-api-sdk';
24-
import useSWR from 'swr';
25+
import { useAPI } from 'renderer/lib/api-client/hooks';
2526
import 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
))}

src/renderer/lib/api-client/allEndpoints.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
"importToLocalGallery": {
5252
"method": "POST",
5353
"path": "tasks/export_to_local_gallery"
54+
},
55+
"importFromLocalGallery": {
56+
"method": "POST",
57+
"path": "tasks/import_from_local_gallery"
5458
}
5559
},
5660
"server": {

0 commit comments

Comments
 (0)