Skip to content
TheChatArchiveThe Chat Archive
o4-mini-2025-04-16· OpenAIHugging Face LMArena Human Preference 140K

Generate a react page composed of 3 different sub-pages with components which can be…

  1. User

    Generate a react page composed of 3 different sub-pages with components which can be navigated to with a tab bar.

    The page will request the API on page load which will contain basic water quality information.

    The 3 sub-pages are as follows:

    1. A table view to show the information in a table format.
    2. A mapbox map view to show the information on a map using latitude and longitude.
    3. A chart view to show the data using a Plotly JS line chart.
  2. Assistant

    Below is a minimal “Create-React-App”–style example that fetches an array of water‐quality readings on load and lets you switch between three sub‐pages (Table, Map, Chart) via a simple TabBar. You can drop these into your src/ folder, install the dependencies, and run.


    Installation

    npx create-react-app water-quality-dashboard
    cd water-quality-dashboard
    
    # install Mapbox + React-Map-GL + Plotly
    npm install react-map-gl mapbox-gl react-plotly.js plotly.js-basic-dist
    

    Create a file at project root called .env:

    REACT_APP_MAPBOX_TOKEN=pk.your_mapbox_token_here
    

    Restart your dev server after touching .env.


    src/index.js

    import React from 'react';
    import ReactDOM from 'react-dom/client';
    import App from './App';
    import './index.css';
    
    const root = ReactDOM.createRoot(document.getElementById('root'));
    root.render(<App />);
    

    src/App.js

    import React, { useState, useEffect } from 'react';
    import TabBar from './components/TabBar';
    import TableView from './pages/TableView';
    import MapView from './pages/MapView';
    import ChartView from './pages/ChartView';
    import './App.css';
    
    function App() {
      const [tab, setTab] = useState('table');
      const [data, setData] = useState([]);
      const [loading, setLoading] = useState(true);
    
      // Fetch water-quality data on mount
      useEffect(() => {
        async function fetchData() {
          try {
            const res = await fetch('https://api.example.com/water-quality');
            const json = await res.json();
            // Expecting array of:
            // { timestamp: "...", latitude: 12.34, longitude: 56.78, pH: 7.2, turbidity: 3.1 }
            setData(json);
          } catch (err) {
            console.error(err);
          } finally {
            setLoading(false);
          }
        }
        fetchData();
      }, []);
    
      return (
        <div className="App">
          <h1>Water Quality Dashboard</h1>
          <TabBar current={tab} onChange={setTab} />
          {loading && <p>Loading data…</p>}
          {!loading && tab === 'table' && <TableView data={data} />}
          {!loading && tab === 'map' && <MapView data={data} />}
          {!loading && tab === 'chart' && <ChartView data={data} />}
        </div>
      );
    }
    
    export default App;
    

    src/components/TabBar.js

    import React from 'react';
    import './TabBar.css';
    
    const tabs = [
      { key: 'table', label: 'Table' },
      { key: 'map',   label: 'Map'   },
      { key: 'chart', label: 'Chart' },
    ];
    
    export default function TabBar({ current, onChange }) {
      return (
        <div className="tab-bar">
          {tabs.map(t => (
            <button
              key={t.key}
              className={t.key === current ? 'tab active' : 'tab'}
              onClick={() => onChange(t.key)}
            >
              {t.label}
            </button>
          ))}
        </div>
      );
    }
    

    src/components/TabBar.css

    .tab-bar {
      display: flex;
      border-bottom: 2px solid #ddd;
      margin-bottom: 1rem;
    }
    
    .tab {
      padding: 0.5rem 1rem;
      background: none;
      border: none;
      cursor: pointer;
      font-size: 1rem;
      border-bottom: 2px solid transparent;
    }
    
    .tab:hover {
      background-color: #f9f9f9;
    }
    
    .tab.active {
      border-bottom-color: #0074d9;
      font-weight: bold;
    }
    

    src/pages/TableView.js

    import React from 'react';
    
    export default function TableView({ data }) {
      return (
        <table>
          <thead>
            <tr>
              <th>Time</th>
              <th>Latitude</th>
              <th>Longitude</th>
              <th>pH</th>
              <th>Turbidity (NTU)</th>
            </tr>
          </thead>
          <tbody>
            {data.map((r, i) => (
              <tr key={i}>
                <td>{new Date(r.timestamp).toLocaleString()}</td>
                <td>{r.latitude.toFixed(4)}</td>
                <td>{r.longitude.toFixed(4)}</td>
                <td>{r.pH}</td>
                <td>{r.turbidity}</td>
              </tr>
            ))}
          </tbody>
        </table>
      );
    }
    

    src/pages/MapView.js

    import React, { useState } from 'react';
    import ReactMapGL, { Marker, Popup } from 'react-map-gl';
    import 'mapbox-gl/dist/mapbox-gl.css';
    
    export default function MapView({ data }) {
      const [viewport, setViewport] = useState({
        latitude: data[0]?.latitude || 0,
        longitude: data[0]?.longitude || 0,
        zoom: 5,
        width: '100%',
        height: 500,
      });
      const [selected, setSelected] = useState(null);
    
      return (
        <ReactMapGL
          {...viewport}
          mapboxAccessToken={process.env.REACT_APP_MAPBOX_TOKEN}
          onViewportChange={vp => setViewport(vp)}
        >
          {data.map((pt, i) => (
            <Marker
              key={i}
              latitude={pt.latitude}
              longitude={pt.longitude}
              offsetLeft={-10}
              offsetTop={-20}
            >
              <div
                onClick={() => setSelected(pt)}
                style={{ cursor: 'pointer', color: '#d00' }}
              >
                📍
              </div>
            </Marker>
          ))}
    
          {selected && (
            <Popup
              latitude={selected.latitude}
              longitude={selected.longitude}
              onClose={() => setSelected(null)}
              closeOnClick={false}
              anchor="top"
            >
              <div>
                <strong>{new Date(selected.timestamp).toLocaleString()}</strong>
                <p>pH: {selected.pH}</p>
                <p>Turbidity: {selected.turbidity} NTU</p>
              </div>
            </Popup>
          )}
        </ReactMapGL>
      );
    }
    

    src/pages/ChartView.js

    import React from 'react';
    import Plot from 'react-plotly.js';
    
    export default function ChartView({ data }) {
      // Sort by time
      const sorted = [...data].sort(
        (a, b) => new Date(a.timestamp) - new Date(b.timestamp)
      );
    
      return (
        <Plot
          data={[
            {
              x: sorted.map(r => r.timestamp),
              y: sorted.map(r => r.pH),
              type: 'scatter',
              mode: 'lines+markers',
              name: 'pH',
              line: { color: '#1f77b4' },
            },
            {
              x: sorted.map(r => r.timestamp),
              y: sorted.map(r => r.turbidity),
              type: 'scatter',
              mode: 'lines+markers',
              name: 'Turbidity',
              line: { color: '#ff7f0e' },
              yaxis: 'y2',
            },
          ]}
          layout={{
            title: 'Water Quality Over Time',
            xaxis: { title: 'Timestamp' },
            yaxis: { title: 'pH' },
            yaxis2: {
              title: 'Turbidity (NTU)',
              overlaying: 'y',
              side: 'right',
            },
            margin: { t: 50, r: 50, l: 50, b: 50 },
          }}
          style={{ width: '100%', height: '500px' }}
          config={{ responsive: true }}
        />
      );
    }
    

    src/App.css

    .App {
      max-width: 900px;
      margin: 0 auto;
      padding: 1rem;
      font-family: sans-serif;
    }
    
    table {
      width: 100%;
      border-collapse: collapse;
    }
    
    th, td {
      padding: 0.5rem;
      border: 1px solid #ddd;
      text-align: left;
    }
    
    tr:nth-child(even) {
      background-color: #f9f9f9;
    }
    

    Now:

    1. npm start
    2. On load, the app fetches your water‐quality JSON (make sure CORS is enabled).
    3. Click the Table, Map or Chart tabs to switch views.

    Feel free to tweak styles, endpoints, or data‐shape to match your real API! 🚀