File size: 7,268 Bytes
ba95018
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import { useState, useRef, useEffect } from 'react';
import { Toaster } from 'react-hot-toast';
import { ThemeProvider } from './context/ThemeContext';
import { AuthProvider, useAuth } from './context/AuthContext';
import { FormProvider, useForm } from './context/FormContext';
import { fetchTemplateData, hydrateTemplateData } from './data/templateData';
import LoginPage from './components/auth/LoginPage';
import Navbar from './components/layout/Navbar';
import Sidebar from './components/layout/Sidebar';
import PatientHeader from './components/sections/PatientHeader';
import SectionRenderer from './components/sections/SectionRenderer';
import AbbreviationPanel from './components/reference/AbbreviationPanel';
import SmartPhrasesPanel from './components/reference/SmartPhrasesPanel';
import AIGeneratorPanel from './components/ai/AIGeneratorPanel';
import PreviewPanel from './components/preview/PreviewPanel';
import AddSectionModal from './components/modals/AddSectionModal';
import './index.css';

function AppContent({ templateSections }) {
  const [activeSection, setActiveSection] = useState('_header');
  const [showPreview, setShowPreview] = useState(false);
  const [showGenerator, setShowGenerator] = useState(false);
  const [showAddSection, setShowAddSection] = useState(false);
  const [sidebarOpen, setSidebarOpen] = useState(() => window.innerWidth > 1024);
  const mainRef = useRef(null);
  const { addCustomSection, formState } = useForm();

  const isMobile = () => window.innerWidth <= 1024;

  const handleSectionClick = (sectionId) => {
    setActiveSection(sectionId);
    const el = document.getElementById(`section-${sectionId}`);
    if (el) {
      el.scrollIntoView({ behavior: 'smooth', block: 'start' });
    }
    // Auto-close sidebar on mobile after clicking a section
    if (isMobile()) {
      setSidebarOpen(false);
    }
  };

  const handleAddSection = (title) => {
    addCustomSection(title);
  };

  return (
    <>
      <Navbar
        onPreview={() => setShowPreview(true)}
        onGenerate={() => setShowGenerator(true)}
        onToggleSidebar={() => setSidebarOpen(!sidebarOpen)}
      />

      <div className="app-layout">
        {/* Backdrop overlay for mobile sidebar */}
        {sidebarOpen && isMobile() && (
          <div
            className="sidebar-backdrop"
            onClick={() => setSidebarOpen(false)}
          />
        )}

        <Sidebar
          activeSection={activeSection}
          onSectionClick={handleSectionClick}
          onAddSection={() => setShowAddSection(true)}
          isOpen={sidebarOpen}
        />

        <main className="main-content" ref={mainRef}>
          <div id="section-_header">
            <PatientHeader />
          </div>

          <div id="section-_abbreviations">
            <AbbreviationPanel />
          </div>

          <div id="section-_smart_phrases">
            <SmartPhrasesPanel />
          </div>

          {templateSections.map(section => (
            <SectionRenderer key={section.id} section={section} />
          ))}

          {formState.customSections.map(cs => (
            <div key={cs.id} id={`section-${cs.id}`} className="section">
              <div className="section__header">
                <div className="section__header-icon" style={{ background: 'linear-gradient(135deg, var(--accent-500), var(--accent-700))' }}>
                  <span style={{ color: 'white', fontWeight: 700, fontSize: 'var(--font-sm)' }}>C</span>
                </div>
                <h2 className="section__title">{cs.title}</h2>
              </div>
              <div className="subsection">
                <div className="subsection__title">
                  <span className="subsection__title-dot" />
                  Custom Content
                </div>
                <p style={{ color: 'var(--text-secondary)', fontSize: 'var(--font-sm)' }}>
                  This custom section will be included in AI generation. Add relevant notes below.
                </p>
                <div style={{ marginTop: 'var(--space-3)' }}>
                  <textarea
                    className="text-input__field"
                    placeholder="Enter custom notes, observations, or selections for this section..."
                    rows={4}
                    style={{ resize: 'vertical', width: '100%' }}
                  />
                </div>
              </div>
            </div>
          ))}

          <div style={{ height: 80 }} />
        </main>
      </div>

      <PreviewPanel isOpen={showPreview} onClose={() => setShowPreview(false)} />
      <AIGeneratorPanel isOpen={showGenerator} onClose={() => setShowGenerator(false)} />
      <AddSectionModal
        isOpen={showAddSection}
        onClose={() => setShowAddSection(false)}
        onAdd={handleAddSection}
      />

      <Toaster
        position="bottom-right"
        toastOptions={{
          style: {
            background: 'var(--surface-card)',
            color: 'var(--text-primary)',
            border: '1px solid var(--border-primary)',
            fontFamily: 'var(--font-family)',
            fontSize: 'var(--font-sm)',
          },
        }}
      />
    </>
  );
}

function AuthGate() {
  const { isAuthenticated, loading, token } = useAuth();
  const [templateLoaded, setTemplateLoaded] = useState(false);
  const [templateSections, setTemplateSections] = useState([]);
  const [templateError, setTemplateError] = useState('');

  // Fetch template data once authenticated
  useEffect(() => {
    if (isAuthenticated && token && !templateLoaded) {
      fetchTemplateData(token)
        .then(data => {
          const hydrated = hydrateTemplateData(data);
          setTemplateSections(hydrated.TEMPLATE_SECTIONS);
          setTemplateLoaded(true);
        })
        .catch(err => {
          setTemplateError(err.message);
        });
    }
  }, [isAuthenticated, token, templateLoaded]);

  // Checking stored token
  if (loading) {
    return (
      <div className="login-page">
        <div className="loading-container">
          <div className="loading-spinner" />
          <p className="loading-text">Verifying session...</p>
        </div>
      </div>
    );
  }

  // Not authenticated
  if (!isAuthenticated) {
    return <LoginPage />;
  }

  // Authenticated but template not yet loaded
  if (!templateLoaded) {
    return (
      <div className="login-page">
        <div className="loading-container">
          <div className="loading-spinner" />
          <p className="loading-text">
            {templateError || 'Loading clinical template...'}
          </p>
          {templateError && (
            <button
              className="navbar__btn navbar__btn--primary"
              onClick={() => { setTemplateError(''); setTemplateLoaded(false); }}
              style={{ marginTop: 'var(--space-4)' }}
            >
              Retry
            </button>
          )}
        </div>
      </div>
    );
  }

  // Fully ready
  return (
    <FormProvider>
      <AppContent templateSections={templateSections} />
    </FormProvider>
  );
}

export default function App() {
  return (
    <ThemeProvider>
      <AuthProvider>
        <AuthGate />
      </AuthProvider>
    </ThemeProvider>
  );
}