File size: 7,742 Bytes
57da3ff
 
 
 
0d8a28a
9d647a4
57da3ff
 
 
9d647a4
 
 
 
 
 
 
 
 
 
 
0d8a28a
57da3ff
 
 
 
 
 
 
 
 
 
 
 
0d8a28a
 
57da3ff
0d8a28a
57da3ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d8a28a
57da3ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d647a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57da3ff
9d647a4
 
 
57da3ff
9d647a4
57da3ff
 
9d647a4
 
57da3ff
9d647a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57da3ff
9d647a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57da3ff
 
9d647a4
 
 
 
 
 
57da3ff
 
 
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
import { Routes, Route, Navigate, useNavigate } from 'react-router-dom';
import { useStore } from './store';
import { Auth } from './components/Auth';
import { ProtectedRoute } from './components/ProtectedRoute';
import { KeyRound, Heart, X } from 'lucide-react';
import { useEffect, useState } from 'react';
import { socket } from './socket';
import { useLocation } from 'react-router-dom';

// Static imports for maximum build stability on Hugging Face
import { BuyerDashboard } from './components/BuyerDashboard';
import { SellerDashboard } from './components/SellerDashboard';
import { AddListing } from './components/AddListing';
import { ListingDetails } from './components/ListingDetails';
import { CallOverlay } from './components/CallOverlay';
import { HelpOverlay } from './components/HelpOverlay';
import { ChatOverlay } from './components/ChatOverlay';
import { InboxOverlay } from './components/InboxOverlay';
import { AdminLogin } from './components/admin/AdminLogin';
import { AdminDashboard } from './components/admin/AdminDashboard';

export default function App() {
  const { currentUser, initDevice, logout } = useStore();
  const [showQR, setShowQR] = useState(false);
  const navigate = useNavigate();
  const location = useLocation();
  const isAdminRoute = location.pathname.startsWith('/admin');

  useEffect(() => {
    if ("Notification" in window && Notification.permission !== "granted" && Notification.permission !== "denied") {
      Notification.requestPermission();
    }

    const { deviceId } = initDevice();
    if (deviceId) {
      socket.connect();
      socket.emit('join-network', deviceId);
    }
    
    socket.on('listing-updated', (data) => {
      useStore.getState().updateListingStatus(data.id, data.status);
    });

    socket.on('listing-edited', (data) => {
      useStore.getState().editListingDetails(data.id, { cropName: data.cropName, quantity: data.quantity, price: data.price });
    });

    socket.on('receive-message', (data) => {
      const state = useStore.getState();
      if (!state.activeChatUser || state.activeChatUser.id !== data.senderId) {
        state.fetchUnreadCount();
        if ("Notification" in window && Notification.permission === "granted") {
          new Notification("New Message - Meri Mandi", { 
            body: data.message,
            icon: '/upiqr.jpeg'
          });
        }
      }
    });

    return () => {
      socket.off('listing-updated');
      socket.off('listing-edited');
      socket.off('receive-message');
      socket.disconnect();
    }
  }, [initDevice]);

  useEffect(() => {
    if (currentUser) {
      useStore.getState().fetchUnreadCount();
    }
  }, [currentUser]);

  const handleSwitchProfile = () => {
    logout();
    navigate('/');
  };

  return (
    <div className={isAdminRoute ? "admin-container" : "app-container"}>
      {isAdminRoute ? (
        <Routes>
          <Route path="/admin" element={<AdminLogin />} />
          <Route path="/admin/dashboard" element={<AdminDashboard />} />
        </Routes>
      ) : (
        <>
          <header style={{ 
            padding: '15px 20px', 
            display: 'flex', 
            justifyContent: 'space-between', 
            alignItems: 'center',
            borderBottom: '1px solid var(--border-color)',
            backdropFilter: 'blur(10px)',
            position: 'sticky',
            top: 0,
            zIndex: 10
          }}>
            <div style={{ display: 'flex', alignItems: 'center', gap: '8px' }}>
              <h2 className="text-gradient" onClick={() => navigate('/')} style={{ cursor: 'pointer', margin: 0, fontSize: '20px' }}>🌾 Meri Mandi</h2>
              <button 
                onClick={() => setShowQR(true)}
                style={{ background: 'linear-gradient(135deg, #ec4899, #be185d)', color: 'white', border: 'none', borderRadius: '12px', padding: '4px 10px', fontSize: '11px', display: 'flex', alignItems: 'center', gap: '4px', cursor: 'pointer', fontWeight: 600, marginLeft: '8px' }}
              >
                <Heart size={12} fill="white" /> Support
              </button>
            </div>
            {currentUser && (
              <div style={{ display: 'flex', alignItems: 'center', gap: '15px' }}>
                <span style={{ fontSize: '12px', color: 'var(--text-muted)', textTransform: 'capitalize', display: 'none', '@media (min-width: 480px)': { display: 'inline' } }}>
                  {currentUser.name}
                </span>
                <button 
                  onClick={handleSwitchProfile}
                  className="btn-secondary" 
                  style={{ fontSize: '11px', padding: '5px 10px', display: 'flex', alignItems: 'center', gap: '6px' }}
                >
                  <KeyRound size={13} /> Switch
                </button>
              </div>
            )}
          </header>

          <main style={{ flex: 1, padding: '15px', overflowY: 'auto' }}>
            <Routes>
              <Route path="/" element={
                !currentUser ? <Auth /> : (
                  currentUser.role === 'buyer' ? <Navigate to="/buyer" /> : <Navigate to="/seller" />
                )
              } />
              
              <Route path="/buyer" element={
                <ProtectedRoute requiredRole="buyer">
                  <BuyerDashboard />
                </ProtectedRoute>
              } />
              <Route path="/buyer/listing/:id" element={
                <ProtectedRoute requiredRole="buyer">
                  <ListingDetails />
                </ProtectedRoute>
              } />
              
              <Route path="/seller" element={
                <ProtectedRoute requiredRole="seller">
                  <SellerDashboard />
                </ProtectedRoute>
              } />
              <Route path="/seller/add" element={
                <ProtectedRoute requiredRole="seller">
                  <AddListing />
                </ProtectedRoute>
              } />
            </Routes>
          </main>

          <CallOverlay />
          <HelpOverlay />
          
          {showQR && (
            <div style={{
              position: 'fixed', top: 0, left: 0, right: 0, bottom: 0, zIndex: 9999,
              background: 'rgba(0,0,0,0.8)', display: 'flex', alignItems: 'center', justifyContent: 'center', padding: '20px'
            }}>
              <div className="glass-panel" style={{ position: 'relative', width: '100%', maxWidth: '350px', padding: '30px', textAlign: 'center' }}>
                <button 
                  onClick={() => setShowQR(false)} 
                  style={{ position: 'absolute', top: '15px', right: '15px', background: 'var(--danger-glass)', border: 'none', color: 'var(--danger-color)', width: '30px', height: '30px', borderRadius: '50%', display: 'flex', alignItems: 'center', justifyContent: 'center', cursor: 'pointer' }}
                >
                  <X size={16} />
                </button>
                <Heart size={40} color="#ec4899" fill="#ec4899" style={{ marginBottom: '16px' }} />
                <h3 style={{ fontSize: '20px', marginBottom: '8px' }}>Support Meri Mandi</h3>
                <p style={{ color: 'var(--text-muted)', fontSize: '14px', marginBottom: '24px' }}>Scan to keep Meri Mandi free for farmers!</p>
                <div style={{ background: 'white', padding: '10px', borderRadius: '16px', display: 'inline-block' }}>
                  <img src="/upiqr.jpeg" alt="UPI QR Code" style={{ width: '200px', height: '200px', objectFit: 'contain', display: 'block' }} />
                </div>
              </div>
            </div>
          )}
          <InboxOverlay />
          <ChatOverlay />
        </>
      )}
    </div>
  );
}