33
44import { useState } from 'react' ;
55
6- import { $api , fetchClient } from '@geti-inspect/api' ;
7- import { useProjectIdentifier } from '@geti-inspect/hooks' ;
86import { Button , ButtonGroup , Content , Dialog , Divider , Flex , Heading , Item , Picker , Text , type Key } from '@geti/ui' ;
9- import { useMutation } from '@tanstack/react-query' ;
107import type { SchemaCompressionType , SchemaExportType } from 'src/api/openapi-spec' ;
118import { Onnx , OpenVino , PyTorch } from 'src/assets/icons' ;
129
1310import type { ModelData } from '../../../hooks/utils' ;
14- import { useExportStatus } from '../footer/status-bar/adapters/use-export-status' ;
15- import { downloadBlob , sanitizeFilename } from '../utils' ;
1611
1712import classes from './export-model-dialog.module.scss' ;
1813
@@ -30,61 +25,21 @@ const COMPRESSION_OPTIONS: { id: SchemaCompressionType | 'none'; name: string }[
3025 { id : 'int8_acq' , name : 'INT8 ACQ' } ,
3126] ;
3227
28+ export interface ExportOptions {
29+ format : SchemaExportType ;
30+ formatLabel : string ;
31+ compression : SchemaCompressionType | null ;
32+ }
33+
3334interface ExportModelDialogProps {
3435 model : ModelData ;
3536 close : ( ) => void ;
37+ onExport : ( options : ExportOptions ) => void ;
3638}
3739
38- export const ExportModelDialog = ( { model, close } : ExportModelDialogProps ) => {
39- const { projectId } = useProjectIdentifier ( ) ;
40- const { data : project } = $api . useSuspenseQuery ( 'get' , '/api/projects/{project_id}' , {
41- params : { path : { project_id : projectId } } ,
42- } ) ;
40+ export const ExportModelDialog = ( { model, close, onExport } : ExportModelDialogProps ) => {
4341 const [ selectedFormat , setSelectedFormat ] = useState < SchemaExportType > ( 'openvino' ) ;
4442 const [ selectedCompression , setSelectedCompression ] = useState < SchemaCompressionType | 'none' > ( 'none' ) ;
45- const { startExport, completeExport } = useExportStatus ( ) ;
46-
47- const exportMutation = useMutation ( {
48- mutationFn : async ( ) => {
49- const compression = selectedCompression === 'none' ? null : selectedCompression ;
50-
51- const formatLabel = EXPORT_FORMATS . find ( ( f ) => f . id === selectedFormat ) ?. name ?? selectedFormat ;
52- startExport ( model . name , formatLabel ) ;
53-
54- const response = await fetchClient . POST ( '/api/projects/{project_id}/models/{model_id}:export' , {
55- params : {
56- path : {
57- project_id : projectId ,
58- model_id : model . id ,
59- } ,
60- } ,
61- body : {
62- format : selectedFormat ,
63- compression,
64- } ,
65- parseAs : 'blob' ,
66- } ) ;
67-
68- if ( response . error ) {
69- throw new Error ( 'Export failed' ) ;
70- }
71-
72- const blob = response . data as Blob ;
73- const compressionSuffix = compression ? `_${ compression } ` : '' ;
74- const sanitizedProjectName = sanitizeFilename ( project . name ) ;
75- const sanitizedModelName = sanitizeFilename ( model . name ) ;
76- const filename = `${ sanitizedProjectName } _${ sanitizedModelName } _${ selectedFormat } ${ compressionSuffix } .zip` ;
77-
78- return { blob, filename } ;
79- } ,
80- onSuccess : ( { blob, filename } ) => {
81- completeExport ( true ) ;
82- downloadBlob ( blob , filename ) ;
83- } ,
84- onError : ( ) => {
85- completeExport ( false ) ;
86- } ,
87- } ) ;
8843
8944 const handleFormatChange = ( value : string ) => {
9045 const format = value as SchemaExportType ;
@@ -101,7 +56,10 @@ export const ExportModelDialog = ({ model, close }: ExportModelDialogProps) => {
10156 } ;
10257
10358 const handleExport = ( ) => {
104- exportMutation . mutate ( ) ;
59+ const formatLabel = EXPORT_FORMATS . find ( ( f ) => f . id === selectedFormat ) ?. name ?? selectedFormat ;
60+ const compression = selectedCompression === 'none' ? null : selectedCompression ;
61+
62+ onExport ( { format : selectedFormat , formatLabel, compression } ) ;
10563 close ( ) ;
10664 } ;
10765
0 commit comments