demeter

Autonomous Hydroponic Intelligence

LandingPage.jsx (18658B)


  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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
import React, { useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
import {
  Leaf,
  ArrowRight,
  Cpu,
  Database,
  Eye,
  Zap,
  Activity,
  Sparkles,
} from "lucide-react";
import { useFarmData } from "../hooks/useFarmData";
import { extractSensors, deriveCropStatus } from "../utils/dataUtils";
import { useT } from "../hooks/useTranslation";

function computeFleetStats(dashData) {
  if (!dashData?.length) return null;
  const sensors = dashData.map((d) => extractSensors(d.payload));
  const avg = (arr) =>
    arr.reduce((s, v) => s + parseFloat(v || 0), 0) / arr.length;

  const ph = avg(sensors.map((s) => s.ph));
  const ec = avg(sensors.map((s) => s.ec));
  const temp = avg(sensors.map((s) => s.temp));
  const humidity = avg(sensors.map((s) => s.humidity));

  const totalSeqs = dashData.reduce(
    (s, d) => s + (d.payload?.sequence_number || 0),
    0,
  );
  const cropTypes = [
    ...new Set(dashData.map((d) => d.payload?.crop).filter(Boolean)),
  ];

  const alerts = dashData.filter((d) => {
    const status = deriveCropStatus(d.payload);
    return status === "Critical" || status === "Attention";
  }).length;

  return {
    ph: ph.toFixed(2),
    ec: ec.toFixed(2),
    temp: temp.toFixed(1),
    humidity: humidity.toFixed(1),
    cropCount: dashData.length,
    totalSeqs,
    cropTypes,
    alerts,
  };
}

function buildActivityLog(historyData) {
  if (!historyData?.length) return [];
  return historyData
    .slice(-5)
    .reverse()
    .map((d, i) => {
      const p = d.payload || {};
      const agent = (p.strategic_intent || "SUPERVISOR")
        .replace(/_/g, " ")
        .split(" ")[0];
      const msg = p.strategic_intent
        ? `Strategy: ${p.strategic_intent.replace(/_/g, " ")}`
        : `Monitoring ${p.crop || "crop"} - seq #${p.sequence_number || 1}`;
      const ts = p.timestamp
        ? new Date(p.timestamp).toLocaleTimeString([], {
            hour: "2-digit",
            minute: "2-digit",
          })
        : `0${i}:0${i}`;
      return { agent, msg, time: ts };
    });
}

export default function LandingPage() {
  const navigate = useNavigate();
  const { t, td } = useT();
  const [mounted, setMounted] = useState(false);
  const { dashboard, history, loading } = useFarmData();

  const [stats, setStats] = useState(null);
  const [log, setLog] = useState([]);

  useEffect(() => {
    setMounted(true);
  }, []);

  useEffect(() => {
    if (!loading) {
      setStats(computeFleetStats(dashboard));
      setLog(buildActivityLog(history));
    }
  }, [dashboard, history, loading]);

  const FEATURES = [
    {
      icon: Cpu,
      label: t("landing_feature_rl"),
      desc: t("landing_feature_rl_desc"),
    },
    {
      icon: Eye,
      label: t("landing_feature_cv"),
      desc: t("landing_feature_cv_desc"),
    },
    {
      icon: Database,
      label: t("landing_feature_vector"),
      desc: t("landing_feature_vector_desc"),
    },
    {
      icon: Zap,
      label: t("landing_feature_sim"),
      desc: t("landing_feature_sim_desc"),
    },
  ];

  const headlineStats = stats
    ? [
        { val: stats.cropCount.toString(), label: t("landing_active_crops") },
        {
          val: stats.cropTypes.length.toString(),
          label: t("landing_crop_types"),
        },
        { val: stats.totalSeqs.toString(), label: t("landing_total_cycles") },
        {
          val: stats.alerts > 0 ? stats.alerts.toString() : "0",
          label: t("landing_active_alerts"),
        },
      ]
    : [
        { val: "-", label: t("landing_active_crops") },
        { val: "-", label: t("landing_crop_types") },
        { val: "-", label: t("landing_total_cycles") },
        { val: "-", label: t("landing_active_alerts") },
      ];

  // Live sensor readings
  const readings = stats
    ? [
        {
          label: t("analytics_avg_ph"),
          value: stats.ph,
          ok: parseFloat(stats.ph) >= 5.5 && parseFloat(stats.ph) <= 6.5,
        },
        {
          label: t("analytics_avg_ec"),
          value: `${stats.ec}`,
          ok: parseFloat(stats.ec) <= 2.5,
        },
        {
          label: t("sensor_temp").toUpperCase(),
          value: `${stats.temp}°C`,
          ok: parseFloat(stats.temp) >= 18 && parseFloat(stats.temp) <= 30,
        },
        {
          label: t("sensor_humidity").toUpperCase(),
          value: `${stats.humidity}%`,
          ok:
            parseFloat(stats.humidity) >= 40 &&
            parseFloat(stats.humidity) <= 80,
        },
      ]
    : [
        { label: t("analytics_avg_ph"), value: "-", ok: true },
        { label: t("analytics_avg_ec"), value: "-", ok: true },
        { label: t("sensor_temp").toUpperCase(), value: "-", ok: true },
        { label: t("sensor_humidity").toUpperCase(), value: "-", ok: true },
      ];

  return (
    <div
      className="min-h-screen grid-bg relative overflow-hidden"
      style={{ background: "var(--bg)" }}
    >
      <div className="scanline" />

      {/* Ambient glow */}
      <div
        className="absolute top-0 left-1/2 -translate-x-1/2 w-[800px] h-[400px] pointer-events-none"
        style={{
          background:
            "radial-gradient(ellipse at center, rgba(74,222,128,0.06) 0%, transparent 70%)",
        }}
      />

      {/* Nav */}
      <nav
        className="relative z-10 flex items-center justify-between px-8 py-5 border-b"
        style={{ borderColor: "rgba(74,222,128,0.1)" }}
      >
        <div className="flex items-center gap-3">
          <div
            className="w-9 h-9 rounded-xl flex items-center justify-center"
            style={{ background: "linear-gradient(135deg, #1a5c2d, #4ade80)" }}
          >
            <Leaf size={18} fill="white" color="white" />
          </div>
          <div>
            <div
              className="font-bold text-base tracking-tight"
              style={{ color: "var(--text)" }}
            >
              DEMETER
            </div>
            <div
              className="text-[9px] font-mono tracking-[0.2em]"
              style={{ color: "var(--text-3)" }}
            >
              {td("AUTONOMOUS FARM INTELLIGENCE")}
            </div>
          </div>
        </div>

        <div className="flex items-center gap-4">
          <div
            className="flex items-center gap-2 text-[11px] font-mono px-3 py-1.5 rounded-full"
            style={{
              border: "1px solid rgba(74,222,128,0.3)",
              color: "var(--green)",
              background: "rgba(74,222,128,0.05)",
            }}
          >
            <span
              className="status-dot w-1.5 h-1.5 rounded-full"
              style={{ background: "var(--green)" }}
            />
            {loading
              ? t("sidebar_connecting")
              : stats
                ? t("sidebar_farm_online")
                : t("sidebar_no_data")}
          </div>
          <button
            onClick={() => navigate("/dashboard")}
            className="px-4 py-2 rounded-lg text-sm font-semibold transition-all"
            style={{
              background: "var(--surface)",
              color: "var(--text-2)",
              border: "1px solid var(--border)",
            }}
          >
            {t("landing_enter_dash")}
          </button>
        </div>
      </nav>

      {/* Hero */}
      <div className="relative z-10 max-w-7xl mx-auto px-8 pt-24 pb-16">
        <div className="grid grid-cols-1 lg:grid-cols-2 gap-16 items-center">
          {/* Left */}
          <div
            className={`space-y-8 ${mounted ? "animate-fade-up" : "opacity-0"}`}
          >
            <div
              className="inline-flex items-center gap-2 text-[11px] font-mono px-3 py-1.5 rounded-full"
              style={{
                border: "1px solid rgba(245,158,11,0.4)",
                color: "var(--amber)",
                background: "rgba(245,158,11,0.06)",
              }}
            >
              <Zap size={10} fill="currentColor" />
              {td("MULTI-AGENT SYSTEM · LANGGRAPH · AZURE")}
            </div>

            <h1
              className="text-6xl lg:text-7xl font-bold leading-[1.0] tracking-tight"
              style={{ color: "var(--text)" }}
            >
              {t("landing_hero_1")}
              <br />
              <span
                className="font-serif italic"
                style={{ color: "var(--green)" }}
              >
                {t("landing_hero_2")}
              </span>
              <br />
              {t("landing_hero_3")}
            </h1>

            <p
              className="text-lg leading-relaxed max-w-md"
              style={{ color: "var(--text-2)", fontWeight: 300 }}
            >
              {t("landing_hero_sub")}
            </p>

            <div className="flex gap-4">
              <button
                onClick={() => navigate("/dashboard")}
                className="group flex items-center gap-3 px-7 py-3.5 rounded-xl font-semibold text-sm transition-all glow-green"
                style={{ background: "var(--green)", color: "#0c1a0e" }}
              >
                {t("landing_enter_dash")}{" "}
                <ArrowRight
                  size={16}
                  className="group-hover:translate-x-1 transition-transform"
                />
              </button>
              <button
                onClick={() => navigate("/intelligence")}
                className="flex items-center gap-3 px-7 py-3.5 rounded-xl font-semibold text-sm transition-all"
                style={{
                  border: "1px solid var(--border)",
                  color: "var(--text-2)",
                  background: "var(--surface)",
                }}
              >
                <Sparkles size={16} /> {t("landing_intelligence")}
              </button>
            </div>

            {/* Live headline stats from DB */}
            <div
              className="grid grid-cols-4 gap-4 pt-4 border-t"
              style={{ borderColor: "var(--border)" }}
            >
              {headlineStats.map(({ val, label }) => (
                <div key={label}>
                  <div
                    className="text-2xl font-bold font-mono"
                    style={{ color: "var(--green)" }}
                  >
                    {loading ? (
                      <span className="inline-block w-8 h-6 rounded shimmer" />
                    ) : (
                      val
                    )}
                  </div>
                  <div
                    className="text-[11px] mt-0.5"
                    style={{ color: "var(--text-3)" }}
                  >
                    {label}
                  </div>
                </div>
              ))}
            </div>
          </div>

          {/* Right - live HUD */}
          <div
            className={`relative ${mounted ? "animate-fade-in" : "opacity-0"}`}
          >
            <div
              className="rounded-2xl overflow-hidden"
              style={{
                border: "1px solid var(--border)",
                background: "var(--bg-2)",
              }}
            >
              {/* Terminal header */}
              <div
                className="flex items-center gap-2 px-4 py-3 border-b"
                style={{
                  borderColor: "var(--border)",
                  background: "var(--bg-3)",
                }}
              >
                <div
                  className="w-3 h-3 rounded-full"
                  style={{ background: "#ff5f57" }}
                />
                <div
                  className="w-3 h-3 rounded-full"
                  style={{ background: "#ffbd2e" }}
                />
                <div
                  className="w-3 h-3 rounded-full"
                  style={{ background: "#28ca41" }}
                />
                <span
                  className="ml-2 text-[11px] font-mono"
                  style={{ color: "var(--text-3)" }}
                >
                  {t("landing_live_feed")}
                </span>
                <Activity
                  size={11}
                  className="ml-auto"
                  style={{ color: "var(--green)" }}
                />
              </div>

              <div className="p-6 space-y-5">
                {/* Live sensor grid */}
                <div className="grid grid-cols-2 gap-3">
                  {readings.map(({ label, value, ok }) => (
                    <div
                      key={label}
                      className="rounded-xl p-4"
                      style={{
                        background: "var(--surface)",
                        border: "1px solid var(--border)",
                      }}
                    >
                      <div
                        className="text-[10px] font-mono mb-2"
                        style={{ color: "var(--text-3)" }}
                      >
                        {label}
                      </div>
                      {loading ? (
                        <div className="h-7 w-16 rounded shimmer" />
                      ) : (
                        <div
                          className="text-2xl font-mono font-bold"
                          style={{ color: ok ? "var(--green)" : "var(--red)" }}
                        >
                          {value}
                        </div>
                      )}
                      <div
                        className="text-[9px] font-mono mt-1"
                        style={{
                          color: ok
                            ? "rgba(74,222,128,0.6)"
                            : "rgba(248,113,113,0.6)",
                        }}
                      >
                        {ok ? t("landing_optimal") : t("landing_alert")}
                      </div>
                    </div>
                  ))}
                </div>

                {/* Agent activity feed */}
                <div
                  className="rounded-xl p-4"
                  style={{
                    background: "var(--surface)",
                    border: "1px solid var(--border)",
                  }}
                >
                  <div
                    className="text-[10px] font-mono mb-3"
                    style={{ color: "var(--text-3)" }}
                  >
                    {t("landing_recent_activity")}
                  </div>
                  <div className="space-y-2">
                    {loading ? (
                      [1, 2, 3].map((i) => (
                        <div key={i} className="h-4 rounded shimmer" />
                      ))
                    ) : log.length > 0 ? (
                      log.slice(0, 3).map(({ agent, msg, time }, i) => (
                        <div
                          key={i}
                          className="flex items-start gap-2 text-[11px] font-mono"
                        >
                          <span style={{ color: "var(--green)", opacity: 0.6 }}>
                            {time}
                          </span>
                          <span
                            className="px-1.5 py-0.5 rounded text-[9px]"
                            style={{
                              background: "rgba(74,222,128,0.12)",
                              color: "var(--green)",
                              whiteSpace: "nowrap",
                            }}
                          >
                            {td(agent.substring(0, 12))}
                          </span>
                          <span style={{ color: "var(--text-2)" }}>
                            {td(msg.substring(0, 40))}
                            {msg.length > 40 ? "…" : ""}
                          </span>
                        </div>
                      ))
                    ) : (
                      <div
                        className="text-[11px] font-mono"
                        style={{ color: "var(--text-3)" }}
                      >
                        {t("landing_no_cycles")}
                      </div>
                    )}
                    <div
                      className="flex items-center gap-1 text-[11px] font-mono cursor-blink"
                      style={{ color: "var(--green)" }}
                    >
                      <span style={{ opacity: 0.4 }}> </span>
                    </div>
                  </div>
                </div>
              </div>
            </div>

            {/* Live crop count chip */}
            {stats && (
              <div
                className="absolute -top-4 -right-4 px-3 py-2 rounded-lg text-[11px] font-mono"
                style={{
                  background: "var(--surface-2)",
                  border: "1px solid var(--border)",
                  color: "var(--amber)",
                }}
              >
                 {stats.cropCount} {t("common_crop").toLowerCase()}
                {stats.cropCount !== 1 ? "s" : ""}
              </div>
            )}
          </div>
        </div>

        {/* Feature grid */}
        <div
          className="mt-24 pt-12 border-t"
          style={{ borderColor: "var(--border)" }}
        >
          <div
            className="text-[11px] font-mono mb-8"
            style={{ color: "var(--text-3)" }}
          >
            {t("landing_capabilities")}
          </div>
          <div className="grid grid-cols-2 lg:grid-cols-4 gap-4">
            {FEATURES.map(({ icon: Icon, label, desc }) => (
              <div
                key={label}
                className="p-5 rounded-xl card-hover"
                style={{
                  background: "var(--surface)",
                  border: "1px solid var(--border)",
                }}
              >
                <div
                  className="w-9 h-9 rounded-lg flex items-center justify-center mb-4"
                  style={{
                    background: "rgba(74,222,128,0.1)",
                    border: "1px solid rgba(74,222,128,0.2)",
                  }}
                >
                  <Icon size={18} style={{ color: "var(--green)" }} />
                </div>
                <div
                  className="font-semibold text-sm mb-1"
                  style={{ color: "var(--text)" }}
                >
                  {label}
                </div>
                <div
                  className="text-xs leading-relaxed"
                  style={{ color: "var(--text-3)" }}
                >
                  {desc}
                </div>
              </div>
            ))}
          </div>
        </div>
      </div>
    </div>
  );
}