export-atoms.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * Jotai atoms for data export state management.
  3. * Manages export jobs, progress, and download state.
  4. *
  5. * Requirements: 8.1, 8.2, 8.3, 8.4, 8.6
  6. */
  7. import { atom } from 'jotai';
  8. /**
  9. * Export format type
  10. */
  11. export type ExportFormat = 'json' | 'csv' | 'coco' | 'yolo' | 'pascal_voc' | 'sharegpt' | 'alpaca';
  12. /**
  13. * Export status type
  14. */
  15. export type ExportStatus = 'pending' | 'processing' | 'completed' | 'failed';
  16. /**
  17. * Status filter type for export
  18. */
  19. export type StatusFilter = 'all' | 'completed' | 'pending' | 'in_progress';
  20. /**
  21. * Export request interface
  22. */
  23. export interface ExportRequest {
  24. format: ExportFormat;
  25. status_filter: StatusFilter;
  26. include_metadata: boolean;
  27. }
  28. /**
  29. * Export job interface
  30. */
  31. export interface ExportJob {
  32. id: string;
  33. project_id: string;
  34. format: ExportFormat;
  35. status: ExportStatus;
  36. status_filter: StatusFilter;
  37. include_metadata: boolean;
  38. file_path: string | null;
  39. download_url: string | null;
  40. error_message: string | null;
  41. created_at: string;
  42. completed_at: string | null;
  43. total_tasks: number;
  44. exported_tasks: number;
  45. }
  46. /**
  47. * Export progress interface
  48. */
  49. export interface ExportProgress {
  50. id: string;
  51. status: ExportStatus;
  52. progress: number;
  53. total_tasks: number;
  54. exported_tasks: number;
  55. error_message: string | null;
  56. }
  57. /**
  58. * Atom storing the current export job
  59. */
  60. export const exportJobAtom = atom<ExportJob | null>(null);
  61. /**
  62. * Atom storing export progress
  63. */
  64. export const exportProgressAtom = atom<number>(0);
  65. /**
  66. * Atom tracking export loading state
  67. */
  68. export const exportLoadingAtom = atom<boolean>(false);
  69. /**
  70. * Atom storing export-related errors
  71. */
  72. export const exportErrorAtom = atom<string | null>(null);
  73. /**
  74. * Atom storing export dialog open state
  75. */
  76. export const exportDialogOpenAtom = atom<boolean>(false);
  77. /**
  78. * Atom storing export request configuration
  79. */
  80. export const exportRequestAtom = atom<ExportRequest>({
  81. format: 'json',
  82. status_filter: 'all',
  83. include_metadata: true,
  84. });
  85. /**
  86. * Atom storing export history (list of past exports)
  87. */
  88. export const exportHistoryAtom = atom<ExportJob[]>([]);
  89. /**
  90. * Derived atom to check if export is in progress
  91. */
  92. export const isExportingAtom = atom((get) => {
  93. const job = get(exportJobAtom);
  94. return job?.status === 'pending' || job?.status === 'processing';
  95. });
  96. /**
  97. * Derived atom to check if export is completed
  98. */
  99. export const isExportCompletedAtom = atom((get) => {
  100. const job = get(exportJobAtom);
  101. return job?.status === 'completed';
  102. });
  103. /**
  104. * Derived atom to check if export failed
  105. */
  106. export const isExportFailedAtom = atom((get) => {
  107. const job = get(exportJobAtom);
  108. return job?.status === 'failed';
  109. });
  110. /**
  111. * Derived atom to get download URL
  112. */
  113. export const exportDownloadUrlAtom = atom((get) => {
  114. const job = get(exportJobAtom);
  115. return job?.download_url ?? null;
  116. });
  117. /**
  118. * Write-only atom to reset export state
  119. */
  120. export const resetExportAtom = atom(null, (get, set) => {
  121. set(exportJobAtom, null);
  122. set(exportProgressAtom, 0);
  123. set(exportErrorAtom, null);
  124. set(exportRequestAtom, {
  125. format: 'json',
  126. status_filter: 'all',
  127. include_metadata: true,
  128. });
  129. });
  130. /**
  131. * Write-only atom to open export dialog
  132. */
  133. export const openExportDialogAtom = atom(null, (get, set) => {
  134. set(exportDialogOpenAtom, true);
  135. set(resetExportAtom);
  136. });
  137. /**
  138. * Write-only atom to close export dialog
  139. */
  140. export const closeExportDialogAtom = atom(null, (get, set) => {
  141. set(exportDialogOpenAtom, false);
  142. });
  143. /**
  144. * Export format options for UI
  145. */
  146. export const exportFormatOptions: Array<{
  147. value: ExportFormat;
  148. label: string;
  149. description: string;
  150. }> = [
  151. {
  152. value: 'json',
  153. label: 'JSON',
  154. description: '原始 JSON 格式,包含完整数据',
  155. },
  156. {
  157. value: 'csv',
  158. label: 'CSV',
  159. description: '表格格式,适合数据分析',
  160. },
  161. {
  162. value: 'coco',
  163. label: 'COCO',
  164. description: '目标检测标准格式',
  165. },
  166. {
  167. value: 'yolo',
  168. label: 'YOLO',
  169. description: 'YOLO 训练格式',
  170. },
  171. {
  172. value: 'pascal_voc',
  173. label: 'VOC',
  174. description: 'PascalVOC XML 格式',
  175. },
  176. {
  177. value: 'sharegpt',
  178. label: 'ShareGPT',
  179. description: '对话模型训练格式',
  180. },
  181. {
  182. value: 'alpaca',
  183. label: 'Alpaca',
  184. description: 'LLM 指令微调格式',
  185. },
  186. ];
  187. /**
  188. * Status filter options for UI
  189. */
  190. export const statusFilterOptions: Array<{
  191. value: StatusFilter;
  192. label: string;
  193. }> = [
  194. { value: 'all', label: '全部' },
  195. { value: 'completed', label: '已完成' },
  196. { value: 'in_progress', label: '进行中' },
  197. { value: 'pending', label: '待处理' },
  198. ];