| 1 | type GenerationNotificationTranslate = ( |
| 2 | key: |
| 3 | | 'generationNotifications.completed' |
| 4 | | 'generationNotifications.partial' |
| 5 | | 'generationNotifications.failed', |
| 6 | params?: Record<string, string | number> |
| 7 | ) => string |
| 8 | |
| 9 | type GenerationNotificationAction = { |
| 10 | label: string |
| 11 | onClick: () => void |
| 12 | } |
| 13 | |
| 14 | type GenerationNotificationEvent = |
| 15 | | { |
| 16 | type: 'run_completed' |
| 17 | payload: { |
| 18 | failedPageCount?: number |
| 19 | } |
| 20 | } |
| 21 | | { |
| 22 | type: 'run_error' |
| 23 | payload: { |
| 24 | message?: string |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | export type GenerationNotificationToast = { |
| 29 | type: 'success' | 'warning' | 'error' |
| 30 | message: string |
| 31 | options: { |
| 32 | action: GenerationNotificationAction |
| 33 | duration: number |
| 34 | description?: string |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | export function createGenerationNotificationToast({ |
| 39 | event, |
| 40 | title, |
| 41 | action, |
| 42 | t |
| 43 | }: { |
| 44 | event: GenerationNotificationEvent |
| 45 | title: string |
| 46 | action: GenerationNotificationAction |
| 47 | t: GenerationNotificationTranslate |
| 48 | }): GenerationNotificationToast { |
| 49 | if (event.type === 'run_completed') { |
| 50 | const failedPageCount = Math.max(0, Number(event.payload.failedPageCount) || 0) |
| 51 | if (failedPageCount > 0) { |
| 52 | return { |
| 53 | type: 'warning', |
| 54 | message: t('generationNotifications.partial', { title, count: failedPageCount }), |
| 55 | options: { |
| 56 | action, |
| 57 | duration: 8000 |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return { |
| 63 | type: 'success', |
| 64 | message: t('generationNotifications.completed', { title }), |
| 65 | options: { |
| 66 | action, |
| 67 | duration: 6000 |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return { |
| 73 | type: 'error', |
| 74 | message: t('generationNotifications.failed', { title }), |
| 75 | options: { |
| 76 | action, |
| 77 | duration: 8000 |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 |