Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

6.1 KB
Raw Blame
import { useEffect, useState } from 'react';
import { useSearchParams } from 'react-router';
import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer } from 'recharts';
import { RefreshCw } from 'lucide-react';
import { getAppQualityHistory } from '../../services/apiApps';
import InfoTooltip from '../ui/InfoTooltip';
import { pluralize } from '../../lib/textUtils';

export default function AppQualityHistory({ appId, categories = [] }) {
  const [params, setParams] = useSearchParams();
  const days = ['30', '90', '365'].includes(params.get('qualityDays')) ? params.get('qualityDays') : '90';
  const category = categories.some(c => c.id === params.get('qualityCategory')) ? params.get('qualityCategory') : 'overall';
  const [state, setState] = useState({ loading: true });
  const [refresh, setRefresh] = useState(0);
  useEffect(() => {
    let active = true;
    setState({ loading: true });
    getAppQualityHistory(appId, days).then(data => {
      if (active) setState({ data });
    }).catch(() => {
      if (active) setState({ error: true });
    });
    return () => { active = false; };
  }, [appId, days, refresh]);
  const change = (key, value) => setParams(previous => {
    const next = new URLSearchParams(previous);
    next.set(key, value);
    return next;
  });
  const points = (state.data?.points || []).map(point => ({ ...point,
    value: category === 'overall' ? point.score : point.categories[category]?.score ?? null,
    evidence: category === 'overall' ? `${point.ratedCategories}/${state.data.totalCategories} categories` :
      `${point.categories[category]?.coverage ?? 'unavailable'} · ${point.categories[category]?.confidence ?? 'unknown'} confidence`,
  }));
  const measured = points.filter(p => p.value !== null);
  return (
    <section aria-label="Quality over time" className="bg-port-card border border-port-border rounded-lg p-4 space-y-3 min-w-0">
      <div className="flex flex-wrap items-center justify-between gap-2">
        <h4 className="flex items-center gap-1.5 text-sm font-medium">
          Quality over time
          <InfoTooltip label="About quality history" placement="below" align="start" width={288}>
            Daily snapshots (UTC), higher is healthier. Each category value is the rolling mean of its recent qualifying runs as of that day; runs carry forward for up to 30 days, and gaps mean no fresh evidence. Coverage changes can move the overall mean. Category views include provisional assessments.
            {state.data?.federation && ` Includes ${state.data.federation.available ?? 0} available full-sync peers; peer availability can change historical coverage.`}
          </InfoTooltip>
        </h4>
        <div className="flex flex-wrap items-center gap-2 text-xs">
          <label htmlFor="quality-period" className="sr-only">Period</label>
          <select id="quality-period" className="bg-port-bg border border-port-border rounded p-1" value={days} onChange={e => change('qualityDays', e.target.value)}>
            <option value="30">30 days</option><option value="90">90 days</option><option value="365">1 year</option>
          </select>
          <label htmlFor="quality-history-category" className="sr-only">Category</label>
          <select id="quality-history-category" className="bg-port-bg border border-port-border rounded p-1 max-w-[12rem]" value={category} onChange={e => change('qualityCategory', e.target.value)}>
            <option value="overall">Overall</option>{categories.map(c => <option key={c.id} value={c.id}>{c.label}</option>)}
          </select>
          <button type="button" aria-label="Refresh history" title="Refresh history" className="min-h-[44px] min-w-[44px] inline-flex items-center justify-center text-gray-400 hover:text-port-accent" onClick={() => setRefresh(n => n + 1)}>
            <RefreshCw size={14} aria-hidden="true" />
          </button>
        </div>
      </div>
      {(state.data?.federation?.failed || state.data?.federation?.unavailable > 0) && <p className="text-xs text-port-warning">
        {state.data.federation.failed ? 'Peer quality could not be loaded; history may be incomplete.' : `${pluralize(state.data.federation.unavailable, 'peer')} unavailable or incompatible; history may be incomplete.`}
      </p>}
      {state.loading ? <p role="status">Loading quality history…</p> : state.error ? <p role="alert">Quality history could not be loaded. Use Refresh history to retry.</p> : !measured.length ? <p className="text-sm text-gray-400">No scored assessments in this period. Run an audit to start the history.</p> : <>
        <div className="h-56 w-full" role="img" aria-label="Daily quality scores from 0 to 100; values and evidence are available in the history table below">
          <ResponsiveContainer width="100%" height="100%">
            <LineChart data={points} margin={{ top: 8, right: 12, bottom: 8, left: 0 }}>
              <CartesianGrid strokeDasharray="3 3" stroke="#374151" />
              <XAxis dataKey="date" tick={{ fill: '#9ca3af', fontSize: 11 }} minTickGap={40} />
              <YAxis domain={[0, 100]} tick={{ fill: '#9ca3af', fontSize: 11 }} width={35} />
              <Tooltip contentStyle={{ backgroundColor: '#111827', borderColor: '#374151' }} formatter={(value, _name, item) => [`${value}/100 · ${item.payload.evidence}`, 'Quality']} />
              <Line type="stepAfter" dataKey="value" name="Quality" stroke="#60a5fa" strokeWidth={2} connectNulls={false} dot={measured.length === 1} isAnimationActive={false} />
            </LineChart>
          </ResponsiveContainer>
        </div>
        <details><summary className="cursor-pointer text-xs text-port-accent">History values and coverage</summary>
          <div className="max-h-64 overflow-auto"><table className="w-full text-left text-xs">
            <thead><tr><th>Date (UTC)</th><th>Score</th><th>Evidence</th></tr></thead>
            <tbody>{points.map(point => <tr key={point.date}><th scope="row" className="py-1 font-normal">{point.date}</th><td>{point.value == null ? '—' : `${point.value}/100`}</td><td>{point.evidence}</td></tr>)}</tbody>
          </table></div>
        </details>
      </>}
    </section>
  );
}