All files / lib/evolution history.pins.ts

28.57% Statements 12/42
50% Branches 2/4
50% Functions 2/4
28.57% Lines 12/42

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 621x   1x                   6x 6x 6x 6x 6x     6x             1x 6x 6x   1x                                                              
import { getEvolutionLogs, EvolutionLog } from './history.collector';
 
const STORAGE_KEY = 'os:evo:pins:v1';
 
export interface PinnedInsight {
  id: string;
  timestamp: string;
  trend: string;
  confidence: number;
  note: string;
}
 
function readPins(): PinnedInsight[] {
  if (typeof window === 'undefined') return [];
  try {
    return JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]');
  } catch {
    return [];
  }
}
 
function writePins(pins: PinnedInsight[]) {
  if (typeof window === 'undefined') return;
  localStorage.setItem(STORAGE_KEY, JSON.stringify(pins));
}
 
export function getPinnedInsights(): PinnedInsight[] {
  return readPins();
}
 
export function pinInsight(insight: {
  timestamp: string;
  trend: string;
  confidence: number;
  note?: string;
}) {
  const pins = readPins();
  const newPin: PinnedInsight = {
    id: crypto.randomUUID(),
    timestamp: insight.timestamp,
    trend: insight.trend,
    confidence: insight.confidence,
    note: insight.note || 'Pinned insight',
  };
  pins.push(newPin);
  writePins(pins);
 
  // تحديث سجلّات التطور كملاحظة جديدة
  const logs = getEvolutionLogs();
  logs.push({
    id: newPin.id,
    timestamp: newPin.timestamp,
    trend: insight.trend as any,
    confidence: insight.confidence,
    changeRate: 0,
    notes: `📌 ${newPin.note}`,
  } as EvolutionLog);
  localStorage.setItem('os:evo:logs:v1', JSON.stringify(logs));
 
  return newPin;
}