File size: 7,382 Bytes
7f88bdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client";

import { useEffect, useState } from "react";

type StepInfo = {
  label: string;
  key: string;
  description: string;
};

const STEPS: StepInfo[] = [
  { label: "Uploaded", key: "UPLOADED", description: "Document received and stored securely" },
  { label: "Processing", key: "PROCESSING", description: "AI is extracting key information from your tender" },
  { label: "Preview", key: "PREVIEW", description: "Generating tender summary and identifying requirements" },
  { label: "Analysis Ready", key: "ANALYSIS_READY", description: "Compliance matrix, bid score, and proposals are ready" },
];

const ENGAGING_TIPS = [
  "💡 Did you know? Tenders with complete compliance documentation have 40% higher win rates.",
  "⏱️ Tip: While you wait, gather your company registration and tax compliance certificates.",
  "🎯 Fun fact: Our AI has analyzed over 10,000 Kenyan government tenders.",
  "📊 Did you know? Understanding evaluation criteria early helps tailor winning proposals.",
  "✅ Tip: Check the 'Mandatory Documents' section first—it is often the #1 reason bids are disqualified.",
  "🏆 Kenyan SMEs that use AI-powered tender analysis report 3x more contract wins.",
];

function getStepState(stepKey: string, currentStatus: string, failed: boolean) {
  const order = STEPS.map((s) => s.key);
  const currentIdx = order.indexOf(currentStatus);
  const stepIdx = order.indexOf(stepKey);

  if (failed && stepKey === currentStatus) return "error";
  if (stepIdx < currentIdx) return "completed";
  if (stepIdx === currentIdx) return "active";
  return "pending";
}

function getCurrentStepIndex(status: string) {
  return STEPS.findIndex((s) => s.key === status);
}

function formatTimeRemaining(seconds: number) {
  if (seconds < 60) return `${seconds}s remaining`;
  const mins = Math.ceil(seconds / 60);
  return `~${mins} min${mins > 1 ? 's' : ''} remaining`;
}

export default function ProcessingStatus({
  status,
  lastError,
  onRetry,
}: {
  status: string;
  lastError?: string | null;
  onRetry?: () => void;
}) {
  const failed = status === "FAILED";
  const currentStepIdx = getCurrentStepIndex(failed ? "PROCESSING" : status);
  const currentStep = STEPS[currentStepIdx];

  // Progress animation (0-100% based on estimated time)
  const [progress, setProgress] = useState(0);
  const [tipIndex, setTipIndex] = useState(0);
  const [elapsedSeconds, setElapsedSeconds] = useState(0);

  useEffect(() => {
    if (status !== "PROCESSING" || failed) return;

    // Animate progress from current position toward next milestone
    const targetProgress = ((currentStepIdx + 1) / STEPS.length) * 100;
    const interval = setInterval(() => {
      setProgress((prev) => {
        if (prev >= targetProgress - 5) return prev;
        return prev + Math.random() * 2;
      });
      setElapsedSeconds((s) => s + 1);
    }, 1000);

    return () => clearInterval(interval);
  }, [status, failed, currentStepIdx]);

  // Rotate tips every 15 seconds
  useEffect(() => {
    if (status !== "PROCESSING" || failed) return;

    const tipInterval = setInterval(() => {
      setTipIndex((prev) => (prev + 1) % ENGAGING_TIPS.length);
    }, 15000);

    return () => clearInterval(tipInterval);
  }, [status, failed]);

  // Reset when status changes
  useEffect(() => {
    setProgress((currentStepIdx / STEPS.length) * 100);
    setElapsedSeconds(0);
  }, [status, currentStepIdx]);

  // Estimate remaining time (average 2.5 minutes total)
  const estimatedTotalSeconds = 150;
  const remainingSeconds = Math.max(0, estimatedTotalSeconds - elapsedSeconds);

  return (
    <div>
      {/* Progress Bar */}
      {status === "PROCESSING" && !failed && (
        <div style={{ marginBottom: "1.5rem" }}>
          <div
            style={{
              display: "flex",
              justifyContent: "space-between",
              alignItems: "center",
              marginBottom: "0.5rem",
            }}
          >
            <span style={{ fontWeight: 600, color: "var(--brand)" }}>
              <span className="spinner" style={{ marginRight: "0.5rem" }}></span>
              {currentStep?.label}
            </span>
            <span className="text-sm text-muted">
              {formatTimeRemaining(remainingSeconds)}
            </span>
          </div>
          <div
            style={{
              background: "var(--border-light)",
              borderRadius: "var(--radius-full)",
              height: "8px",
              overflow: "hidden",
            }}
          >
            <div
              style={{
                height: "100%",
                background: "var(--brand-gradient)",
                borderRadius: "var(--radius-full)",
                width: `${Math.min(100, progress)}%`,
                transition: "width 0.5s ease-out",
                boxShadow: "0 0 10px rgba(5,150,105,0.3)",
              }}
            />
          </div>
          <p className="text-sm text-muted" style={{ marginTop: "0.5rem" }}>
            {currentStep?.description}
          </p>
        </div>
      )}

      {/* Step Indicators */}
      <div className="processing-steps">
        {STEPS.map((step, idx) => {
          const state = getStepState(step.key, failed ? "PROCESSING" : status, failed);
          const isCurrent = idx === currentStepIdx;
          return (
            <div key={step.key} className={`processing-step ${state}`}>
              <div
                className="step-dot"
                style={{
                  animation: isCurrent && !failed ? "pulse 1.5s infinite" : undefined,
                }}
              >
                {state === "completed" ? "✓" : state === "error" ? "!" : isCurrent ? "●" : ""}
              </div>
              <div className="step-label">{step.label}</div>
            </div>
          );
        })}
      </div>

      {/* Rotating Tips */}
      {status === "PROCESSING" && !failed && (
        <div
          style={{
            marginTop: "1.5rem",
            padding: "1rem",
            background: "linear-gradient(135deg, var(--brand-subtle) 0%, #f0fdfa 100%)",
            borderRadius: "var(--radius-md)",
            border: "1px solid var(--brand-muted)",
            textAlign: "center",
          }}
        >
          <p
            className="text-sm"
            style={{
              color: "var(--brand-text)",
              margin: 0,
              animation: "fadeIn 0.5s ease-in",
            }}
          >
            {ENGAGING_TIPS[tipIndex]}
          </p>
        </div>
      )}

      {/* Error State */}
      {failed && lastError && (
        <div
          style={{
            padding: "1rem",
            background: "var(--danger-subtle)",
            borderRadius: "var(--radius-md)",
            marginTop: "1rem",
          }}
        >
          <div style={{ fontWeight: 600, color: "var(--danger-text)", marginBottom: "0.3rem" }}>
            Processing Failed
          </div>
          <div className="text-sm" style={{ color: "var(--danger-text)" }}>
            {lastError}
          </div>
          {onRetry && (
            <button
              className="btn btn-danger btn-sm"
              style={{ marginTop: "0.75rem" }}
              onClick={onRetry}
            >
              Retry Processing
            </button>
          )}
        </div>
      )}
    </div>
  );
}