scientific-backend / FRONTEND_SCAFFOLD.md
Dama12's picture
Initial clean backend deployment
0bd4ab4
|
Raw
History Blame
11.1 kB
# Frontend Scaffold & Recommendations
## 🎨 Recommended Tech Stack
```
Next.js 14
β”œβ”€β”€ React 18
β”œβ”€β”€ TypeScript
β”œβ”€β”€ Tailwind CSS
β”œβ”€β”€ React Flow (for reasoning graphs)
└── Axios (API client)
```
---
## πŸ“ Suggested Project Structure
```
frontend/
β”œβ”€β”€ public/
β”‚ └── assets/
β”œβ”€β”€ src/
β”‚ β”œβ”€β”€ app/
β”‚ β”‚ β”œβ”€β”€ layout.tsx # Root layout
β”‚ β”‚ β”œβ”€β”€ page.tsx # Home page
β”‚ β”‚ β”œβ”€β”€ upload/
β”‚ β”‚ β”‚ └── page.tsx # Paper upload
β”‚ β”‚ β”œβ”€β”€ analysis/
β”‚ β”‚ β”‚ β”œβ”€β”€ page.tsx # Analysis dashboard
β”‚ β”‚ β”‚ └── [id]/
β”‚ β”‚ β”‚ └── page.tsx # Analysis details
β”‚ β”‚ β”œβ”€β”€ protocols/
β”‚ β”‚ β”‚ └── [id]/
β”‚ β”‚ β”‚ └── page.tsx # Protocol view/edit
β”‚ β”‚ └── admin/
β”‚ β”‚ └── page.tsx # Admin panel
β”‚ β”œβ”€β”€ components/
β”‚ β”‚ β”œβ”€β”€ ui/ # Reusable UI
β”‚ β”‚ β”‚ β”œβ”€β”€ Button.tsx
β”‚ β”‚ β”‚ β”œβ”€β”€ Card.tsx
β”‚ β”‚ β”‚ β”œβ”€β”€ Modal.tsx
β”‚ β”‚ β”‚ └── layout/
β”‚ β”‚ β”œβ”€β”€ features/ # Feature components
β”‚ β”‚ β”‚ β”œβ”€β”€ UploadZone.tsx
β”‚ β”‚ β”‚ β”œβ”€β”€ ContradictionView.tsx
β”‚ β”‚ β”‚ β”œβ”€β”€ ProtocolEditor.tsx
β”‚ β”‚ β”‚ β”œβ”€β”€ ReasoningTracer.tsx
β”‚ β”‚ β”‚ └── GraphVisualizer.tsx
β”‚ β”‚ └── layout/
β”‚ β”‚ β”œβ”€β”€ Header.tsx
β”‚ β”‚ β”œβ”€β”€ Sidebar.tsx
β”‚ β”‚ └── Footer.tsx
β”‚ β”œβ”€β”€ lib/
β”‚ β”‚ β”œβ”€β”€ api/
β”‚ β”‚ β”‚ β”œβ”€β”€ client.ts # Axios instance
β”‚ β”‚ β”‚ β”œβ”€β”€ endpoints.ts # API URLs
β”‚ β”‚ β”‚ └── types.ts # TypeScript interfaces
β”‚ β”‚ β”œβ”€β”€ hooks/
β”‚ β”‚ β”‚ β”œβ”€β”€ useAnalysis.ts
β”‚ β”‚ β”‚ β”œβ”€β”€ usePapers.ts
β”‚ β”‚ β”‚ └── useProtocols.ts
β”‚ β”‚ └── utils/
β”‚ β”‚ β”œβ”€β”€ formatting.ts
β”‚ β”‚ └── validation.ts
β”‚ β”œβ”€β”€ store/ # Zustand or Redux
β”‚ β”‚ β”œβ”€β”€ analysisStore.ts
β”‚ β”‚ └── userStore.ts
β”‚ β”œβ”€β”€ styles/
β”‚ β”‚ └── globals.css # Tailwind imports
β”‚ └── types/
β”‚ β”œβ”€β”€ analysis.ts
β”‚ β”œβ”€β”€ protocol.ts
β”‚ └── paper.ts
β”œβ”€β”€ package.json
└── tsconfig.json
```
---
## πŸš€ Getting Started
### 1. Create New Next.js Project
```bash
# Using create-next-app
npx create-next-app@latest scoinvestigator-frontend \
--typescript \
--tailwind \
--eslint
cd scoinvestigator-frontend
```
### 2. Install Dependencies
```bash
npm install \
axios \
react-flow-renderer \
zustand \
react-icons \
react-toastify
# Optional: for advanced visualizations
npm install \
d3 \
cytoscape \
react-cytoscape
```
### 3. Environment Setup
```bash
# .env.local
NEXT_PUBLIC_API_URL=http://localhost:8000/api/v1
NEXT_PUBLIC_WS_URL=ws://localhost:8000
```
---
## πŸ”‘ Key Pages & Components
### πŸ“„ Upload Papers (`/upload`)
```tsx
// Features:
- Drag & drop zone
- Multiple file upload
- PDF preview
- Progress indicator
- Metadata extraction preview
// Key component: UploadZone.tsx
```
### πŸ“Š Analysis Dashboard (`/analysis`)
```tsx
// Features:
- List of all analyses
- Status indicators
- Timeline view
- Quick actions (view, export, delete)
- Filtering & search
// Key component: AnalysisList.tsx
```
### πŸ” Analysis Details (`/analysis/[id]`)
```tsx
// Features:
- Tabs: Summary | Contradictions | Hypotheses | Gaps | Protocols
- Reasoning trace visualization
- Metrics display
- Document references
- Export options
// Key components:
// - ContradictionView.tsx (table with severity scores)
// - ReasoningTracer.tsx (step-by-step breakdown)
// - GraphVisualizer.tsx (document relationships)
```
### πŸ§ͺ Protocol Designer (`/protocols/[id]`)
```tsx
// Features:
- Protocol editor (rich text or form)
- Variable specification
- Risk assessment form
- Cost/duration estimator
- Version history
- Export (PDF, DOCX, LaTeX)
// Key component: ProtocolEditor.tsx
```
### πŸ“ˆ Reasoning Trace Visualization
```tsx
// Using React Flow:
Nodes: Analysis steps
Edges: Dependencies
Styling: Color-coded by status (pending/active/complete)
Interaction: Click to see details
// Key component: GraphVisualizer.tsx with react-flow-renderer
```
---
## πŸ“‘ API Integration
### API Client Setup
```typescript
// lib/api/client.ts
import axios from 'axios';
const apiClient = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_URL,
timeout: 30000,
});
// Add token to requests
apiClient.interceptors.request.use((config) => {
const token = localStorage.getItem('token');
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
});
export default apiClient;
```
### Main Endpoints to Integrate
```typescript
// lib/api/endpoints.ts
export const endpoints = {
// Analysis
analysis: {
run: '/analysis/run',
status: (id: string) => `/analysis/${id}/status`,
results: (id: string) => `/analysis/${id}/results`,
},
// Papers
papers: {
upload: '/papers/upload',
list: (projectId: string) => `/papers/${projectId}`,
},
// Protocols
protocols: {
generate: '/protocols/generate',
list: '/protocols',
detail: (id: string) => `/protocols/${id}`,
export: (id: string, format: string) => `/protocols/${id}/export?format=${format}`,
},
// Health
health: '/health/ready',
};
```
### Custom Hooks
```typescript
// lib/hooks/useAnalysis.ts
import { useState, useEffect } from 'react';
import apiClient from '@/lib/api/client';
export function useAnalysis(analysisId: string) {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
const fetchAnalysis = async () => {
try {
const response = await apiClient.get(
`/analysis/${analysisId}/results`
);
setData(response.data);
} catch (err) {
setError(err);
} finally {
setLoading(false);
}
};
fetchAnalysis();
}, [analysisId]);
return { data, loading, error };
}
```
---
## 🎨 UI Components (Tailwind)
### Theme & Colors
```tsx
// Suggested color scheme
Primary: Blue-600 (reasoning)
Secondary: Emerald-600 (validation)
Danger: Red-600 (contradictions)
Warning: Amber-600 (gaps)
```
### Key Components to Build
#### ContradictionCard
```tsx
interface Contradiction {
id: string;
variable: string;
confidence: number;
statement_a: string;
statement_b: string;
severity: 'low' | 'medium' | 'high';
}
<ContradictionCard
contradiction={contradiction}
onResolve={handleResolve}
/>
```
#### HypothesisCard
```tsx
<HypothesisCard
hypothesis={hypothesis}
stressTestResults={results}
onSelect={handleSelect}
/>
```
#### ProtocolTimeline
```tsx
// Show: Hypothesis β†’ Variables β†’ Methodology β†’ Risk Analysis β†’ Export
<ProtocolTimeline steps={protocolSteps} />
```
---
## πŸ“Š Graph Visualization (React Flow)
### Reasoning Trace Graph
```typescript
// components/features/ReasoningTracer.tsx
import { useCallback } from 'react';
import ReactFlow, {
Node,
Edge,
useNodesState,
useEdgesState
} from 'reactflow';
const nodes: Node[] = [
{ id: '1', data: { label: 'Extract Documents' }, position: { x: 0, y: 0 } },
{ id: '2', data: { label: 'Detect Contradictions' }, position: { x: 250, y: 0 } },
// ... more nodes
];
const edges: Edge[] = [
{ id: 'e1-2', source: '1', target: '2' },
// ... more edges
];
```
---
## πŸ” Authentication
### Token Management
```typescript
// lib/api/auth.ts
export const auth = {
login: async (email: string, password: string) => {
const response = await apiClient.post('/auth/login', { email, password });
localStorage.setItem('token', response.data.access_token);
return response.data;
},
logout: () => {
localStorage.removeItem('token');
},
getToken: () => localStorage.getItem('token'),
};
```
---
## πŸ“¦ Deployment Options
### Vercel (Recommended)
```bash
# Connect GitHub repo to Vercel
# Auto-deploys on push
# Environment variables in Vercel dashboard
NEXT_PUBLIC_API_URL=https://api.railway.app/api/v1
```
### Docker
```dockerfile
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["npm", "start"]
```
---
## 🚨 Error Handling
```typescript
// Global error boundary
// components/layout/ErrorBoundary.tsx
import { ReactNode } from 'react';
interface Props {
children: ReactNode;
}
export default function ErrorBoundary({ children }: Props) {
try {
return <>{children}</>;
} catch (error) {
return (
<div className="bg-red-50 p-4 rounded">
<h2>Something went wrong</h2>
<p>{error.message}</p>
</div>
);
}
}
```
---
## πŸ“± Responsive Design
Use Tailwind breakpoints:
```tsx
// Mobile first
className="w-full md:w-1/2 lg:w-1/3"
// Responsive grid
className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4"
```
---
## πŸ§ͺ Testing
```bash
# Install testing dependencies
npm install --save-dev @testing-library/react jest
# Example test
// __tests__/components/ContradictionCard.test.tsx
import { render, screen } from '@testing-library/react';
import { ContradictionCard } from '@/components/features/ContradictionCard';
describe('ContradictionCard', () => {
it('renders contradiction data', () => {
const contradiction = {
variable: 'sample_size',
confidence: 0.95,
};
render(<ContradictionCard contradiction={contradiction} />);
expect(screen.getByText('sample_size')).toBeInTheDocument();
});
});
```
---
## πŸ“ Quick Development Checklist
- [ ] Setup Next.js project
- [ ] Configure API client & endpoints
- [ ] Create layout components (Header, Sidebar)
- [ ] Implement upload page
- [ ] Build analysis dashboard
- [ ] Create analysis detail pages
- [ ] Add protocol editor
- [ ] Implement reasoning trace visualization
- [ ] Setup authentication flow
- [ ] Add error handling & loading states
- [ ] Global styling with Tailwind
- [ ] Responsive design testing
- [ ] Performance optimization
- [ ] Deploy to Vercel
---
## 🎯 UX/Design Tips for Jury
**Scientific credibility:**
- Show data sources and references
- Display confidence scores
- Allow result verification
- Show reasoning steps
**Visual hierarchy:**
- Emphasize contradictions clearly
- Highlight key hypotheses
- Color-code severity/confidence
- Use data visualization effectively
**Performance:**
- Quick upload/processing feedback
- Real-time progress indicators
- Smooth transitions
- Responsive to all devices
---
## πŸ“š Resources
- [Next.js Docs](https://nextjs.org/docs)
- [React Flow](https://reactflow.dev/)
- [Tailwind CSS](https://tailwindcss.com/)
- [TypeScript](https://www.typescriptlang.org/)
- [Axios](https://axios-http.com/)
---
Happy building! πŸš€