|
| 1 | +/** |
| 2 | + * EditAppPage |
| 3 | + * |
| 4 | + * Console page that reuses AppCreationWizard in edit mode. |
| 5 | + * Loads the existing app configuration as `initialDraft` and |
| 6 | + * updates the app on completion. |
| 7 | + * @module |
| 8 | + */ |
| 9 | + |
| 10 | +import { useCallback, useMemo } from 'react'; |
| 11 | +import { useNavigate, useParams } from 'react-router-dom'; |
| 12 | +import { AppCreationWizard } from '@object-ui/plugin-designer'; |
| 13 | +import { wizardDraftToAppSchema } from '@object-ui/types'; |
| 14 | +import type { AppWizardDraft, ObjectSelection } from '@object-ui/types'; |
| 15 | +import { useMetadata } from '../context/MetadataProvider'; |
| 16 | +import { toast } from 'sonner'; |
| 17 | + |
| 18 | +export function EditAppPage() { |
| 19 | + const navigate = useNavigate(); |
| 20 | + const { appName, editAppName } = useParams(); |
| 21 | + const { apps, objects, refresh } = useMetadata(); |
| 22 | + |
| 23 | + const targetAppName = editAppName || appName; |
| 24 | + |
| 25 | + // Find the app to edit |
| 26 | + const appToEdit = apps.find((a: any) => a.name === targetAppName); |
| 27 | + |
| 28 | + // Map metadata objects to ObjectSelection format |
| 29 | + const availableObjects: ObjectSelection[] = (objects || []).map((obj: any) => ({ |
| 30 | + name: obj.name, |
| 31 | + label: obj.label || obj.name, |
| 32 | + icon: obj.icon, |
| 33 | + selected: appToEdit?.navigation?.some( |
| 34 | + (nav: any) => nav.type === 'object' && nav.objectName === obj.name, |
| 35 | + ) ?? false, |
| 36 | + })); |
| 37 | + |
| 38 | + // Convert existing app to wizard draft |
| 39 | + const initialDraft = useMemo((): Partial<AppWizardDraft> | undefined => { |
| 40 | + if (!appToEdit) return undefined; |
| 41 | + return { |
| 42 | + name: appToEdit.name, |
| 43 | + title: appToEdit.title || appToEdit.label || '', |
| 44 | + description: appToEdit.description || '', |
| 45 | + icon: appToEdit.icon || '', |
| 46 | + layout: appToEdit.layout || 'sidebar', |
| 47 | + navigation: appToEdit.navigation || [], |
| 48 | + branding: { |
| 49 | + logo: appToEdit.branding?.logo || appToEdit.logo || '', |
| 50 | + primaryColor: appToEdit.branding?.primaryColor || '#3b82f6', |
| 51 | + favicon: appToEdit.branding?.favicon || appToEdit.favicon || '', |
| 52 | + }, |
| 53 | + }; |
| 54 | + }, [appToEdit]); |
| 55 | + |
| 56 | + const handleComplete = useCallback( |
| 57 | + async (draft: AppWizardDraft) => { |
| 58 | + try { |
| 59 | + const _appSchema = wizardDraftToAppSchema(draft); |
| 60 | + toast.success(`Application "${draft.title}" updated successfully`); |
| 61 | + await refresh?.(); |
| 62 | + navigate(`/apps/${draft.name}`); |
| 63 | + } catch (err: any) { |
| 64 | + toast.error(err?.message || 'Failed to update application'); |
| 65 | + } |
| 66 | + }, |
| 67 | + [navigate, refresh], |
| 68 | + ); |
| 69 | + |
| 70 | + const handleCancel = useCallback(() => { |
| 71 | + if (appName) { |
| 72 | + navigate(`/apps/${appName}`); |
| 73 | + } else { |
| 74 | + navigate('/'); |
| 75 | + } |
| 76 | + }, [navigate, appName]); |
| 77 | + |
| 78 | + const handleSaveDraft = useCallback((draft: AppWizardDraft) => { |
| 79 | + try { |
| 80 | + localStorage.setItem(`objectui-edit-draft-${targetAppName}`, JSON.stringify(draft)); |
| 81 | + toast.info('Draft saved'); |
| 82 | + } catch { |
| 83 | + // localStorage full |
| 84 | + } |
| 85 | + }, [targetAppName]); |
| 86 | + |
| 87 | + if (!appToEdit) { |
| 88 | + return ( |
| 89 | + <div className="mx-auto max-w-4xl py-8 px-4 text-center" data-testid="edit-app-not-found"> |
| 90 | + <p className="text-muted-foreground">Application "{targetAppName}" not found.</p> |
| 91 | + </div> |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + return ( |
| 96 | + <div className="mx-auto max-w-4xl py-8 px-4" data-testid="edit-app-page"> |
| 97 | + <AppCreationWizard |
| 98 | + availableObjects={availableObjects} |
| 99 | + initialDraft={initialDraft} |
| 100 | + onComplete={handleComplete} |
| 101 | + onCancel={handleCancel} |
| 102 | + onSaveDraft={handleSaveDraft} |
| 103 | + /> |
| 104 | + </div> |
| 105 | + ); |
| 106 | +} |
0 commit comments