File size: 4,137 Bytes
6c8fc4b
 
 
 
 
5193b92
6c8fc4b
 
 
 
5193b92
 
 
6c8fc4b
 
 
 
 
5193b92
 
 
 
 
6c8fc4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5193b92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78b3b2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Cache utilities for storing and retrieving analysis data
 */

import type { AnalysisResult } from '../types/analysis.types.ts';
import type { MCPTool } from '../types/mcp.types.ts';

const CACHE_KEY = 'analysis_data_cache';
const CACHE_TIMESTAMP_KEY = 'analysis_data_cache_timestamp';

const MCP_TOOLS_CACHE_KEY = 'mcp_tools_cache';
const MCP_TOOLS_CACHE_TIMESTAMP_KEY = 'mcp_tools_cache_timestamp';

type CachedAnalysisData = {
  results: AnalysisResult[];
  timestamp: number;
};

type CachedMCPToolsData = {
  tools: MCPTool[];
  timestamp: number;
};

/**
 * Cache analysis results to localStorage
 */
export function cacheAnalysisData(results: AnalysisResult[]): void {
  try {
    const cacheData: CachedAnalysisData = {
      results,
      timestamp: Date.now(),
    };
    localStorage.setItem(CACHE_KEY, JSON.stringify(cacheData));
    localStorage.setItem(CACHE_TIMESTAMP_KEY, String(cacheData.timestamp));
  } catch (error) {
    // Silently fail if localStorage is not available or quota exceeded
    console.warn('Failed to cache analysis data:', error);
  }
}

/**
 * Get cached analysis results from localStorage
 */
export function getCachedAnalysisData(): AnalysisResult[] | null {
  try {
    const cached = localStorage.getItem(CACHE_KEY);
    if (!cached) return null;

    const cacheData: CachedAnalysisData = JSON.parse(cached);
    return cacheData.results;
  } catch (error) {
    // Silently fail if localStorage is not available or data is corrupted
    console.warn('Failed to retrieve cached analysis data:', error);
    return null;
  }
}

/**
 * Get cache timestamp
 */
export function getCacheTimestamp(): number | null {
  try {
    const timestamp = localStorage.getItem(CACHE_TIMESTAMP_KEY);
    return timestamp ? Number(timestamp) : null;
  } catch (error) {
    return null;
  }
}

/**
 * Clear cached analysis data
 */
export function clearAnalysisCache(): void {
  try {
    localStorage.removeItem(CACHE_KEY);
    localStorage.removeItem(CACHE_TIMESTAMP_KEY);
  } catch (error) {
    console.warn('Failed to clear analysis cache:', error);
  }
}

export function cacheMcpTools(tools: MCPTool[]): void {
  try {
    const cacheData: CachedMCPToolsData = {
      tools,
      timestamp: Date.now(),
    };
    localStorage.setItem(MCP_TOOLS_CACHE_KEY, JSON.stringify(cacheData));
    localStorage.setItem(
      MCP_TOOLS_CACHE_TIMESTAMP_KEY,
      String(cacheData.timestamp)
    );
  } catch (error) {
    console.warn('Failed to cache MCP tools:', error);
  }
}

export function getCachedMcpTools(): MCPTool[] | null {
  try {
    const cached = localStorage.getItem(MCP_TOOLS_CACHE_KEY);
    if (!cached) return null;

    const cacheData: CachedMCPToolsData = JSON.parse(cached);
    return cacheData.tools;
  } catch (error) {
    console.warn('Failed to retrieve cached MCP tools:', error);
    return null;
  }
}

export function clearMcpToolsCache(): void {
  try {
    localStorage.removeItem(MCP_TOOLS_CACHE_KEY);
    localStorage.removeItem(MCP_TOOLS_CACHE_TIMESTAMP_KEY);
  } catch (error) {
    console.warn('Failed to clear MCP tools cache:', error);
  }
}

/**
 * Cache recent analysis results to localStorage
 */
export function cacheRecentAnalysisData(results: AnalysisResult[]): void {
  try {
    const cacheData: CachedAnalysisData = {
      results,
      timestamp: Date.now(),
    };
    localStorage.setItem('recent_analysis_data_cache', JSON.stringify(cacheData));
  } catch (error) {
    // Silently fail if localStorage is not available or quota exceeded
    console.warn('Failed to cache recent analysis data:', error);
  }
}

/**
 * Get cached recent analysis results from localStorage
 */
export function getCachedRecentAnalysisData(): AnalysisResult[] | null {
  try {
    const cached = localStorage.getItem('recent_analysis_data_cache');
    if (!cached) return null;

    const cacheData: CachedAnalysisData = JSON.parse(cached);
    return cacheData.results;
  } catch (error) {
    // Silently fail if localStorage is not available or data is corrupted
    console.warn('Failed to retrieve cached recent analysis data:', error);
    return null;
  }
}