managarten/apps/context/apps/mobile/ReadMe/DocumentEditorVerbesserungsplan.md
Till-JS bb0e0cf5cb 🚚 feat(context): integrate context app into monorepo
Restructure the context app (formerly basetext) to follow the monorepo
pattern with proper workspace configuration.

Changes:
- Move app files to apps/context/apps/mobile/
- Rename package to @context/mobile
- Update bundle ID to com.manacore.context
- Create pnpm-workspace.yaml for project workspace
- Add dev scripts to root package.json
- Update CLAUDE.md with project documentation

The app structure is prepared for future web/backend additions.

Note: Existing TypeScript errors in the original codebase are preserved.
These should be fixed in a follow-up PR.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 15:09:04 +01:00

12 KiB

Dokumenten-Editor Verbesserungsplan

Überblick

Der Dokumenten-Editor ist die Kernkomponente der BaseText-App mit 1.322 Zeilen Code und einer hohen Komplexität. Diese Analyse identifiziert kritische Problembereiche und bietet einen strukturierten Verbesserungsplan.

Kritische Problembereiche

1. Architektur-Probleme (Priorität: HOCH)

Single Responsibility Principle Verletzung

  • Problem: Ein Component handhabt zu viele Verantwortlichkeiten
  • Aktuelle Zuständigkeiten:
    • Dokumenten-CRUD-Operationen
    • Auto-Save-Logik
    • UI-State-Management
    • Tag-Management
    • Mention-System
    • Versionsverwaltung
    • Navigation
    • Local Storage Backup

Immediate Actions

  1. Component Aufspaltung (Woche 1-2):

    // Vorgeschlagene Struktur:
    DocumentEditor/ 
    ├── DocumentEditor.tsx          // Main orchestrator
    ├── DocumentContent.tsx         // Content editing/preview
    ├── DocumentToolbar.tsx         // Toolbar with actions
    ├── DocumentTags.tsx           // Tag management
    ├── DocumentVersions.tsx       // Version control
    └── hooks/
        ├── useDocumentEditor.ts    // Main document logic
        ├── useAutoSave.ts         // Auto-save functionality
        ├── useMentions.ts         // Mention system
        └── useDocumentVersions.ts // Version management
    
  2. Service Layer Extraction (Woche 2-3):

    // services/documentEditorService.ts
    class DocumentEditorService {
      autoSave: AutoSaveManager;
      mentions: MentionManager;
      versions: VersionManager;
    
      constructor() {
        this.autoSave = new AutoSaveManager();
        this.mentions = new MentionManager();
        this.versions = new VersionManager();
      }
    }
    

2. Performance-Probleme (Priorität: HOCH)

Excessive Re-renders

  • Problem: 20+ useState Hooks verursachen unnötige Re-renders
  • Lösung: useReducer für komplexen State
    // State consolidation
    type DocumentState = {
      content: string;
      title: string;
      tags: string[];
      saving: boolean;
      error: string | null;
      mode: 'edit' | 'preview';
      unsavedChanges: boolean;
    };
    
    const [state, dispatch] = useReducer(documentReducer, initialState);
    

Auto-Save Performance Issues

  • Problem: Mehrere konfligierende Timer und Debouncing-Mechanismen
  • Aktuelle Probleme:
    • 4 verschiedene Auto-Save-Timer
    • saveLockRef verursacht potentielle Deadlocks
    • Inkonsistente Logik für neue vs. bestehende Dokumente

Optimierte Auto-Save Implementierung:

// hooks/useAutoSave.ts
export const useAutoSave = (
  content: string,
  documentId: string,
  options: AutoSaveOptions
) => {
  const [saveState, setSaveState] = useState<SaveState>('idle');
  const debouncedSave = useMemo(
    () => debounce(saveDocument, options.delay),
    [options.delay]
  );

  useEffect(() => {
    if (content && saveState !== 'saving') {
      debouncedSave();
    }
  }, [content, debouncedSave, saveState]);

  return { saveState, forceSave: debouncedSave.flush };
};

3. State Management Komplexität (Priorität: HOCH)

Probleme:

  • State Fragmentation: 20+ useState Hooks
  • Ref-basierte State: saveLockRef, firstSaveCompletedRef
  • Race Conditions: Async-Operationen interferieren

Lösung - Unified State Management:

// reducers/documentReducer.ts
type DocumentAction = 
  | { type: 'SET_CONTENT'; payload: string }
  | { type: 'SET_SAVING'; payload: boolean }
  | { type: 'SET_ERROR'; payload: string | null }
  | { type: 'TOGGLE_MODE' }
  | { type: 'SET_TAGS'; payload: string[] };

const documentReducer = (state: DocumentState, action: DocumentAction): DocumentState => {
  switch (action.type) {
    case 'SET_CONTENT':
      return { ...state, content: action.payload, unsavedChanges: true };
    case 'SET_SAVING':
      return { ...state, saving: action.payload };
    // ... weitere cases
  }
};

4. Code Quality Probleme (Priorität: MITTEL)

Duplicate Code:

  • Problem: Mehrere ähnliche Auto-Save-Implementierungen
  • Lösung: Unified Auto-Save Hook

Magic Numbers:

  • Problem: Hardcoded Timeouts (2000ms, 5000ms, 10000ms)
  • Lösung: Konfiguration extrahieren
    // config/editorConfig.ts
    export const EDITOR_CONFIG = {
      AUTO_SAVE_DELAY: 3000,
      NEW_DOC_SAVE_DELAY: 2000,
      BACKUP_INTERVAL: 15000,
      MIN_CONTENT_LENGTH: 50,
    } as const;
    

Global CSS Injection:

  • Problem: CSS wird in Component injiziert (Zeilen 61-96)
  • Lösung: Extrahiere zu separater CSS-Datei oder styled-components

5. User Experience Probleme (Priorität: MITTEL)

Save Feedback:

  • Problem: Unklare Save-Status-Anzeige
  • Lösung: Konsistente Save-Indicator-Komponente
    // components/SaveIndicator.tsx
    const SaveIndicator = ({ status }: { status: SaveStatus }) => {
      const getStatusText = () => {
        switch (status) {
          case 'saving': return 'Speichert...';
          case 'saved': return 'Gespeichert';
          case 'error': return 'Fehler beim Speichern';
          default: return 'Ungespeichert';
        }
      };
    
      return (
        <View className="flex-row items-center">
          <StatusIcon status={status} />
          <Text className="text-sm text-gray-500">{getStatusText()}</Text>
        </View>
      );
    };
    

Focus Management:

  • Problem: Komplexe Focus-Logik funktioniert nicht zuverlässig
  • Lösung: Vereinfachte Focus-Verwaltung mit useRef

6. Accessibility Probleme (Priorität: MITTEL)

Missing ARIA Labels:

  • Problem: Keine Screen-Reader-Unterstützung
  • Lösung: ARIA-Labels hinzufügen
    <TextInput
      accessibilityLabel="Dokumentinhalt bearbeiten"
      accessibilityHint="Hier können Sie Ihren Dokumentinhalt eingeben und bearbeiten"
      accessibilityRole="textbox"
      // ...
    />
    

Keyboard Navigation:

  • Problem: Begrenzte Keyboard-only-Interaktion
  • Lösung: Keyboard-Shortcuts implementieren
    // hooks/useKeyboardShortcuts.ts
    const useKeyboardShortcuts = (actions: KeyboardActions) => {
      useEffect(() => {
        const handleKeyPress = (e: KeyboardEvent) => {
          if (e.ctrlKey || e.metaKey) {
            switch (e.key) {
              case 's': e.preventDefault(); actions.save(); break;
              case 'p': e.preventDefault(); actions.togglePreview(); break;
              // ... weitere shortcuts
            }
          }
        };
    
        document.addEventListener('keydown', handleKeyPress);
        return () => document.removeEventListener('keydown', handleKeyPress);
      }, [actions]);
    };
    

7. Potentielle Bugs & Edge Cases (Priorität: HOCH)

Memory Leaks:

  • Problem: Timer-Cleanup in useEffect-Dependencies
  • Lösung: Proper Cleanup-Funktionen
    useEffect(() => {
      const timer = setTimeout(() => {
        // Auto-save logic
      }, EDITOR_CONFIG.AUTO_SAVE_DELAY);
    
      return () => clearTimeout(timer); // Proper cleanup
    }, [content]);
    

Browser Compatibility:

  • Problem: beforeunload Handler funktioniert nicht auf Mobile
  • Lösung: Platform-specific Handling
    // hooks/useBeforeUnload.ts
    const useBeforeUnload = (hasUnsavedChanges: boolean) => {
      useEffect(() => {
        if (Platform.OS === 'web') {
          const handleBeforeUnload = (e: BeforeUnloadEvent) => {
            if (hasUnsavedChanges) {
              e.preventDefault();
              e.returnValue = '';
            }
          };
    
          window.addEventListener('beforeunload', handleBeforeUnload);
          return () => window.removeEventListener('beforeunload', handleBeforeUnload);
        }
      }, [hasUnsavedChanges]);
    };
    

Detaillierter Implementierungsplan

Phase 1: Kritische Architektur-Refactoring (Woche 1-3)

Woche 1: Component Decomposition

  1. DocumentEditor aufteilen:

    • DocumentContent für Edit/Preview
    • DocumentToolbar für Actions
    • DocumentTags für Tag-Management
  2. Custom Hooks erstellen:

    • useDocumentEditor - Main logic
    • useAutoSave - Auto-save functionality
    • useMentions - Mention system

Woche 2: State Management Refactoring

  1. useReducer implementieren:

    • Konsolidiere 20+ useState zu unified state
    • Implementiere DocumentState & Actions
    • Teste State-Transitions
  2. Service Layer erstellen:

    • DocumentEditorService implementieren
    • Auto-Save-Logik extrahieren
    • Mention-System isolieren

Woche 3: Performance Optimierung

  1. Re-render Optimierung:

    • React.memo für Sub-Components
    • useMemo für expensive computations
    • useCallback für Event-Handlers
  2. Auto-Save Optimierung:

    • Unified debouncing strategy
    • Eliminate race conditions
    • Proper error handling

Phase 2: User Experience Verbesserungen (Woche 4-5)

Woche 4: Save Feedback & Status

  1. Save-Indicator implementieren:

    • Konsistente Status-Anzeige
    • Visual feedback für Save-Operations
    • Error-Recovery-Mechanismen
  2. Focus Management:

    • Vereinfachte Focus-Logik
    • Keyboard-Navigation
    • Accessibility improvements

Woche 5: Keyboard Shortcuts & A11y

  1. Keyboard Shortcuts:

    • Strg+S für Save
    • Strg+P für Preview-Toggle
    • Weitere productivity shortcuts
  2. Accessibility:

    • ARIA-Labels hinzufügen
    • Screen-Reader-Unterstützung
    • Color contrast compliance

Phase 3: Code Quality & Testing (Woche 6-7)

Woche 6: Code Quality

  1. Duplicate Code eliminieren:

    • Unified Auto-Save-Implementation
    • Shared utility functions
    • Consistent error handling
  2. Configuration Management:

    • Magic numbers extrahieren
    • Environment-specific configs
    • Feature flags implementieren

Woche 7: Testing Implementation

  1. Unit Tests:

    • Auto-Save logic testing
    • State management testing
    • Edge case handling
  2. Integration Tests:

    • User interaction flows
    • Save/Load operations
    • Error scenarios

Phase 4: Advanced Features (Woche 8-10)

Woche 8: Performance Monitoring

  1. Performance Metrics:

    • Save operation timing
    • Re-render tracking
    • Memory usage monitoring
  2. Optimizations:

    • Lazy loading für heavy components
    • Virtual scrolling für long content
    • Bundle size optimization

Woche 9-10: Advanced UX

  1. Collaborative Features:

    • Real-time collaboration prep
    • Conflict resolution
    • Version history UI
  2. PWA Features:

    • Offline support
    • Background sync
    • Push notifications

Erfolgs-Metriken

Performance Metriken:

  • Component Size: Reduzierung von 1.322 auf <500 Zeilen
  • Re-render Count: Reduzierung um 70%
  • Save Operation Time: <500ms für normale Dokumente
  • Memory Usage: Reduzierung um 40%

Code Quality Metriken:

  • Cyclomatic Complexity: <10 pro Funktion
  • Code Coverage: >90% für kritische Pfade
  • TypeScript Errors: 0 Fehler
  • ESLint Warnings: <5 Warnungen

User Experience Metriken:

  • Time to Interactive: <2 Sekunden
  • Auto-Save Reliability: 99.9%
  • Accessibility Score: >90 (Lighthouse)
  • Mobile Performance: >60 FPS

Risiken & Mitigation

Risiken:

  1. Breaking Changes: Refactoring kann bestehende Features brechen
  2. Performance Regression: Neue Implementation könnte langsamer sein
  3. User Disruption: UI-Änderungen können Users verwirren

Mitigation Strategies:

  1. Feature Flags: Gradueller Rollout der neuen Implementation
  2. A/B Testing: Vergleiche alte vs. neue Version
  3. Monitoring: Umfangreiche Metriken und Alerting
  4. Rollback Strategy: Schnelle Rückkehr zur alten Version falls nötig

Fazit

Dieser Verbesserungsplan adressiert die kritischen Problembereiche des Dokumenten-Editors systematisch. Die Implementierung in Phasen ermöglicht es, kontinuierlich Verbesserungen zu liefern, während die Stabilität der Anwendung gewährleistet wird. Die vorgeschlagenen Änderungen werden die Wartbarkeit, Performance und Benutzererfahrung signifikant verbessern.

Geschätzte Entwicklungszeit: 10 Wochen Entwickler: 2-3 Senior Frontend-Entwickler Erwartete Verbesserungen: 70% weniger Komplexität, 50% bessere Performance, 90% bessere Maintainability