-
Notifications
You must be signed in to change notification settings - Fork 0
Add react for frontend #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| { | ||
| "name": "neuralink-client", | ||
| "version": "0.1.0", | ||
| "private": true, | ||
| "dependencies": { | ||
| "@testing-library/jest-dom": "^5.17.0", | ||
| "@testing-library/react": "^13.4.0", | ||
| "@testing-library/user-event": "^13.5.0", | ||
| "axios": "^1.6.2", | ||
| "react": "^18.2.0", | ||
| "react-dom": "^18.2.0", | ||
| "react-scripts": "5.0.1", | ||
| "tailwindcss": "^3.3.5", | ||
| "web-vitals": "^2.1.4" | ||
| }, | ||
| "scripts": { | ||
| "start": "react-scripts start", | ||
| "build": "react-scripts build", | ||
| "test": "react-scripts test", | ||
| "eject": "react-scripts eject" | ||
| }, | ||
| "eslintConfig": { | ||
| "extends": [ | ||
| "react-app", | ||
| "react-app/jest" | ||
| ] | ||
| }, | ||
| "browserslist": { | ||
| "production": [ | ||
| ">0.2%", | ||
| "not dead", | ||
| "not op_mini all" | ||
| ], | ||
| "development": [ | ||
| "last 1 chrome version", | ||
| "last 1 firefox version", | ||
| "last 1 safari version" | ||
| ] | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
|
|
||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <link rel="icon" href="%PUBLIC_URL%/favicon.ico" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
| <meta name="theme-color" content="#000000" /> | ||
| <meta name="description" content="NeuralInk - AI-powered article generator" /> | ||
| <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" /> | ||
| <link rel="manifest" href="%PUBLIC_URL%/manifest.json" /> | ||
| <title>NeuralInk</title> | ||
| </head> | ||
|
|
||
| <body> | ||
| <noscript>You need to enable JavaScript to run this app.</noscript> | ||
| <div id="root"></div> | ||
| </body> | ||
|
|
||
| </html> |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,18 @@ | ||||||
| import React, { useState } from 'react' | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: useState is imported but never used in this component
Suggested change
|
||||||
| import ArticleGenerator from './components/ArticleGenerator' | ||||||
| import './App.css' | ||||||
|
|
||||||
| function App() { | ||||||
| return ( | ||||||
| <div className="min-h-screen bg-gray-100"> | ||||||
| <div className="container mx-auto px-4 py-8"> | ||||||
| <div className="max-w-2xl mx-auto"> | ||||||
| <h1 className="text-4xl font-bold text-center mb-8 text-gray-800">NeuralInk</h1> | ||||||
| <ArticleGenerator /> | ||||||
| </div> | ||||||
| </div> | ||||||
| </div> | ||||||
| ) | ||||||
| } | ||||||
|
|
||||||
| export default App | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import React, { useState } from 'react' | ||
| import axios from 'axios' | ||
|
|
||
| const ArticleGenerator = () => { | ||
| const [topic, setTopic] = useState('') | ||
| const [article, setArticle] = useState('') | ||
| const [loading, setLoading] = useState(false) | ||
| const [error, setError] = useState('') | ||
|
|
||
| const generateArticle = async (e) => { | ||
| e.preventDefault() | ||
| if (!topic.trim()) { | ||
| setError('Please enter a topic') | ||
| return | ||
| } | ||
|
|
||
| setLoading(true) | ||
| setError('') | ||
| setArticle('') | ||
|
|
||
| try { | ||
| const response = await axios.post('http://localhost:3000/api/generate-article', { topic }) | ||
| setArticle(response.data.article) | ||
| } catch (err) { | ||
| setError(err.response?.data?.error || 'Failed to generate article') | ||
| } finally { | ||
| setLoading(false) | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <div> | ||
| <div className="bg-white rounded-lg shadow-md p-6 mb-6"> | ||
| <form onSubmit={generateArticle}> | ||
| <div className="mb-4"> | ||
| <label htmlFor="topic" className="block text-gray-700 text-sm font-bold mb-2"> | ||
| Enter a topic: | ||
| </label> | ||
| <input | ||
| type="text" | ||
| id="topic" | ||
| value={topic} | ||
| onChange={(e) => setTopic(e.target.value)} | ||
| className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" | ||
| placeholder="e.g., Artificial Intelligence, Climate Change, etc." | ||
| /> | ||
|
Comment on lines
+39
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Missing maxLength attribute to prevent excessively long topics that could overload the API |
||
| </div> | ||
| <button | ||
| type="submit" | ||
| disabled={loading} | ||
| className="w-full bg-blue-500 text-white py-2 px-4 rounded-md hover:bg-blue-600 transition duration-200 disabled:opacity-50" | ||
| > | ||
| {loading ? 'Generating...' : 'Generate Article'} | ||
| </button> | ||
| </form> | ||
| </div> | ||
|
|
||
| {loading && ( | ||
| <div className="bg-white rounded-lg shadow-md p-6 mb-6"> | ||
| <div className="flex items-center justify-center"> | ||
| <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-500"></div> | ||
| <span className="ml-2 text-gray-600">Generating article...</span> | ||
| </div> | ||
| </div> | ||
| )} | ||
|
|
||
| {error && ( | ||
| <div className="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative mb-6" role="alert"> | ||
| <span className="block sm:inline">{error}</span> | ||
| </div> | ||
| )} | ||
|
|
||
| {article && ( | ||
| <div className="bg-white rounded-lg shadow-md p-6"> | ||
| <h2 className="text-2xl font-semibold mb-4 text-gray-800">Generated Article</h2> | ||
| <div className="prose max-w-none text-gray-700 whitespace-pre-line"> | ||
| {article} | ||
| </div> | ||
| </div> | ||
| )} | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| export default ArticleGenerator | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import React from 'react' | ||
| import ReactDOM from 'react-dom/client' | ||
| import './index.css' | ||
| import App from './App' | ||
|
|
||
| const root = ReactDOM.createRoot(document.getElementById('root')) | ||
| root.render( | ||
| <React.StrictMode> | ||
| <App /> | ||
| </React.StrictMode> | ||
| ) |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ require('dotenv').config() | |
| const express = require('express') | ||
| const cors = require('cors') | ||
| const OpenAI = require('openai') | ||
| const path = require('path') | ||
|
|
||
| const app = express() | ||
| const port = process.env.PORT || 3000 | ||
|
|
@@ -14,9 +15,8 @@ const openai = new OpenAI({ | |
| // Middleware | ||
| app.use(cors()) | ||
| app.use(express.json()) | ||
| app.use(express.static('public')) | ||
|
|
||
| // Generate article endpoint | ||
| // API Routes | ||
| app.post('/api/generate-article', async (req, res) => { | ||
| try { | ||
| const { topic } = req.body | ||
|
|
@@ -49,6 +49,14 @@ app.post('/api/generate-article', async (req, res) => { | |
| } | ||
| }) | ||
|
|
||
| // Serve static files from the React app | ||
| if (process.env.NODE_ENV === 'production') { | ||
| app.use(express.static(path.join(__dirname, 'client/build'))) | ||
| app.get('*', (req, res) => { | ||
| res.sendFile(path.join(__dirname, 'client/build', 'index.html')) | ||
| }) | ||
| } | ||
|
Comment on lines
+53
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: No fallback route for development environment - client requests may 404 when not in production |
||
|
|
||
| app.listen(port, () => { | ||
| console.log(`Server running on port ${port}`) | ||
| }) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: Missing PostCSS and autoprefixer dependencies required by Tailwind CSS