All files / demo/src App.tsx

98.57% Statements 138/140
92.85% Branches 26/28
100% Functions 2/2
98.57% Lines 138/140

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1411x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 11x 11x 8x 7x 11x 11x 7x 7x 11x 11x 1x 1x 11x 22x 22x 22x 22x 11x 11x 7x 6x 11x 11x 6x 6x 11x 11x 2x 2x 11x 22x 22x 22x 22x 2x 2x 2x 2x 2x 2x     2x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 2x 2x 2x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 8x 8x 8x 8x 22x 22x 22x 22x 22x 1x 1x  
import { useState, useEffect } from 'react';
 
interface FeatureFlag {
  enabled: boolean;
  description: string;
  rollout_percentage: number;
}
 
interface FeatureFlags {
  flags: Record<string, FeatureFlag>;
  version: string;
  last_updated: string;
}
 
function App() {
  const [count, setCount] = useState<number | null>(null);
  const [countLoading, setCountLoading] = useState(true);
  const [countError, setCountError] = useState<string | null>(null);
  const [flags, setFlags] = useState<FeatureFlags | null>(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);
 
  // Fetch initial count on mount
  useEffect(() => {
    fetch('/api/count')
      .then((res) => {
        if (!res.ok) throw new Error(`HTTP ${res.status}`);
        return res.json();
      })
      .then((data) => {
        setCount(data.count);
        setCountLoading(false);
      })
      .catch((err) => {
        setCountError(err.message);
        setCountLoading(false);
      });
  }, []);
 
  // Fetch feature flags on mount
  useEffect(() => {
    fetch('/flags/features.json')
      .then((res) => {
        if (!res.ok) throw new Error(`HTTP ${res.status}`);
        return res.json();
      })
      .then((data) => {
        setFlags(data);
        setLoading(false);
      })
      .catch((err) => {
        setError(err.message);
        setLoading(false);
      });
  }, []);
 
  // Handle increment via API
  const handleIncrement = async () => {
    try {
      const res = await fetch('/api/count', { method: 'POST' });
      if (!res.ok) throw new Error(`HTTP ${res.status}`);
      const data = await res.json();
      setCount(data.count);
    } catch (err) {
      setCountError(err instanceof Error ? err.message : 'Update failed');
    }
  };
 
  return (
    <div className="container">
      <h1>Demo App</h1>
      <p>A simple React + TypeScript + Vite demo.</p>
      <div className="card">
        <button onClick={handleIncrement} disabled={countLoading}>
          {countLoading ? 'Loading...' : `Count is ${count ?? 0}`}
        </button>
        {countError && (
          <p style={{ color: 'red', fontSize: '0.8rem', marginTop: '0.5rem' }}>
            Counter error: {countError}
          </p>
        )}
      </div>
 
      <div className="card" style={{ marginTop: '2rem', textAlign: 'left' }}>
        <h2>Feature Flags</h2>
        <p style={{ fontSize: '0.9rem', opacity: 0.7 }}>
          Fetched via proxy rule: <code>/flags/*</code> → <code>demo-feature-flags</code>
        </p>
 
        {loading && <p>Loading flags...</p>}
        {error && <p style={{ color: 'red' }}>Error: {error}</p>}
        {flags && (
          <div>
            <p style={{ fontSize: '0.8rem', opacity: 0.6 }}>
              Version: {flags.version} | Updated: {flags.last_updated}
            </p>
            <table style={{ width: '100%', borderCollapse: 'collapse' }}>
              <thead>
                <tr>
                  <th style={{ textAlign: 'left', padding: '0.5rem' }}>Flag</th>
                  <th style={{ textAlign: 'center', padding: '0.5rem' }}>Status</th>
                  <th style={{ textAlign: 'right', padding: '0.5rem' }}>Rollout</th>
                </tr>
              </thead>
              <tbody>
                {Object.entries(flags.flags).map(([key, flag]) => (
                  <tr key={key} style={{ borderTop: '1px solid #333' }}>
                    <td style={{ padding: '0.5rem' }}>
                      <strong>{key}</strong>
                      <br />
                      <span style={{ fontSize: '0.8rem', opacity: 0.7 }}>{flag.description}</span>
                    </td>
                    <td style={{ textAlign: 'center', padding: '0.5rem' }}>
                      <span
                        style={{
                          padding: '0.25rem 0.5rem',
                          borderRadius: '4px',
                          fontSize: '0.8rem',
                          background: flag.enabled ? '#22c55e' : '#ef4444',
                          color: 'white',
                        }}
                      >
                        {flag.enabled ? 'ON' : 'OFF'}
                      </span>
                    </td>
                    <td style={{ textAlign: 'right', padding: '0.5rem' }}>
                      {flag.rollout_percentage}%
                    </td>
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        )}
      </div>
    </div>
  );
}
 
export default App;