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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useEffect, useRef, useState } from 'react';
|
| 2 |
+
import { gsap } from 'gsap';
|
| 3 |
+
import { ScrollTrigger } from 'gsap/dist/ScrollTrigger';
|
| 4 |
+
import { Terminal, Copy, Check, Zap, Shield, Rocket, Database, Wifi, Lock, Cpu, Globe, Code2 } from 'lucide-react';
|
| 5 |
+
|
| 6 |
+
gsap.registerPlugin(ScrollTrigger);
|
| 7 |
+
|
| 8 |
+
const tips = [
|
| 9 |
+
{
|
| 10 |
+
id: 1,
|
| 11 |
+
category: 'Performance',
|
| 12 |
+
icon: Rocket,
|
| 13 |
+
color: 'electric',
|
| 14 |
+
title: 'Memory Leak Detection Protocol',
|
| 15 |
+
code: `// Use WeakRef for temporary caching
|
| 16 |
+
const cache = new Map();
|
| 17 |
+
|
| 18 |
+
function getData(key) {
|
| 19 |
+
let ref = cache.get(key);
|
| 20 |
+
if (ref) {
|
| 21 |
+
const data = ref.deref();
|
| 22 |
+
if (data) return data;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
const newData = expensiveOperation(key);
|
| 26 |
+
cache.set(key, new WeakRef(newData));
|
| 27 |
+
return newData;
|
| 28 |
+
}`,
|
| 29 |
+
description: 'Prevent memory leaks in long-running applications by using WeakRef for non-essential caching. The garbage collector can reclaim memory when needed.',
|
| 30 |
+
tags: ['JavaScript', 'Memory Management', 'Advanced'],
|
| 31 |
+
},
|
| 32 |
+
{
|
| 33 |
+
id: 2,
|
| 34 |
+
category: 'Security',
|
| 35 |
+
icon: Shield,
|
| 36 |
+
color: 'magma',
|
| 37 |
+
title: 'Zero-Knowledge API Authentication',
|
| 38 |
+
code: `// Implement HMAC-based request signing
|
| 39 |
+
const crypto = require('crypto');
|
| 40 |
+
|
| 41 |
+
function signRequest(method, path, body, timestamp) {
|
| 42 |
+
const payload = \`\${method}|\${path}|\${JSON.stringify(body)}|\${timestamp}\`;
|
| 43 |
+
|
| 44 |
+
return crypto
|
| 45 |
+
.createHmac('sha256', process.env.API_SECRET)
|
| 46 |
+
.update(payload)
|
| 47 |
+
.digest('hex');
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
// Verify on server
|
| 51 |
+
function verifyRequest(signature, ...args) {
|
| 52 |
+
const expected = signRequest(...args);
|
| 53 |
+
return crypto.timingSafeEqual(
|
| 54 |
+
Buffer.from(signature),
|
| 55 |
+
Buffer.from(expected)
|
| 56 |
+
);
|
| 57 |
+
}`,
|
| 58 |
+
description: 'Never send API keys over the wire. Use HMAC signatures with timestamp validation to prevent replay attacks.',
|
| 59 |
+
tags: ['Security', 'API Design', 'Node.js'],
|
| 60 |
+
},
|
| 61 |
+
{
|
| 62 |
+
id: 3,
|
| 63 |
+
category: 'Database',
|
| 64 |
+
icon: Database,
|
| 65 |
+
color: 'gold',
|
| 66 |
+
title: 'Query Optimization Matrix',
|
| 67 |
+
code: `-- Composite index strategy
|
| 68 |
+
CREATE INDEX CONCURRENTLY idx_user_optimized
|
| 69 |
+
ON users (last_active DESC, tier, region)
|
| 70 |
+
INCLUDE (email, name)
|
| 71 |
+
WHERE status = 'active';
|
| 72 |
+
|
| 73 |
+
-- Partial index for hot paths
|
| 74 |
+
CREATE INDEX idx_hot_products
|
| 75 |
+
ON products (category, price)
|
| 76 |
+
WHERE inventory > 0 AND featured = true;`,
|
| 77 |
+
description: 'Use partial and covering indexes to reduce index size by 80% while improving hot query performance by 10x.',
|
| 78 |
+
tags: ['PostgreSQL', 'Performance', 'Database'],
|
| 79 |
+
},
|
| 80 |
+
{
|
| 81 |
+
id: 4,
|
| 82 |
+
category: 'Network',
|
| 83 |
+
icon: Wifi,
|
| 84 |
+
color: 'electric',
|
| 85 |
+
title: 'Adaptive Streaming Handler',
|
| 86 |
+
code: `class AdaptiveStreamer {
|
| 87 |
+
constructor() {
|
| 88 |
+
this.quality = 'high';
|
| 89 |
+
this.latencyHistory = [];
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
async fetchWithAdaptation(url) {
|
| 93 |
+
const start = performance.now();
|
| 94 |
+
|
| 95 |
+
try {
|
| 96 |
+
const controller = new AbortController();
|
| 97 |
+
const timeout = this.calculateTimeout();
|
| 98 |
+
|
| 99 |
+
const response = await fetch(url, {
|
| 100 |
+
signal: controller.signal,
|
| 101 |
+
headers: { 'Accept-Encoding': 'br, gzip' }
|
| 102 |
+
});
|
| 103 |
+
|
| 104 |
+
const latency = performance.now() - start;
|
| 105 |
+
this.adaptQuality(latency);
|
| 106 |
+
|
| 107 |
+
return response;
|
| 108 |
+
} catch (e) {
|
| 109 |
+
this.quality = 'low';
|
| 110 |
+
throw e;
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
}`,
|
| 114 |
+
description: 'Automatically adjust request quality based on real-time network conditions. Essential for mobile-first applications.',
|
| 115 |
+
tags: ['Network', 'PWA', 'Performance'],
|
| 116 |
+
},
|
| 117 |
+
{
|
| 118 |
+
id: 5,
|
| 119 |
+
category: 'Cryptography',
|
| 120 |
+
icon: Lock,
|
| 121 |
+
color: 'magma',
|
| 122 |
+
title: 'Post-Quantum Key Exchange',
|
| 123 |
+
code: `// Using Web Crypto API with ECDH
|
| 124 |
+
async function generateEphemeralKeys() {
|
| 125 |
+
const keyPair = await crypto.subtle.generateKey(
|
| 126 |
+
{
|
| 127 |
+
name: 'ECDH',
|
| 128 |
+
namedCurve: 'P-384' // NIST P-384 for quantum resistance
|
| 129 |
+
},
|
| 130 |
+
true, // extractable
|
| 131 |
+
['deriveBits', 'deriveKey']
|
| 132 |
+
);
|
| 133 |
+
|
| 134 |
+
// Derive shared secret
|
| 135 |
+
const sharedSecret = await crypto.subtle.deriveBits(
|
| 136 |
+
{
|
| 137 |
+
name: 'ECDH',
|
| 138 |
+
public: peerPublicKey
|
| 139 |
+
},
|
| 140 |
+
keyPair.privateKey,
|
| 141 |
+
384
|
| 142 |
+
);
|
| 143 |
+
|
| 144 |
+
return hkdfExtract(sharedSecret, salt, 'SHA-384');
|
| 145 |
+
}`,
|
| 146 |
+
description: 'Prepare for the quantum future. Use ECDH P-384 for key exchange with proper key derivation functions.',
|
| 147 |
+
tags: ['Cryptography', 'Security', 'Web Crypto'],
|
| 148 |
+
},
|
| 149 |
+
{
|
| 150 |
+
id: 6,
|
| 151 |
+
category: 'System',
|
| 152 |
+
icon: Cpu,
|
| 153 |
+
color: 'gold',
|
| 154 |
+
title: 'Web Worker Task Queue',
|
| 155 |
+
code: `class WorkerPool {
|
| 156 |
+
constructor(workerScript, poolSize = 4) {
|
| 157 |
+
this.workers = Array(poolSize).fill(null)
|
| 158 |
+
.map(() => new Worker(workerScript));
|
| 159 |
+
this.queue = [];
|
| 160 |
+
this.taskId = 0;
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
execute(data, priority = 0) {
|
| 164 |
+
return new Promise((resolve, reject) => {
|
| 165 |
+
const task = {
|
| 166 |
+
id: ++this.taskId,
|
| 167 |
+
data,
|
| 168 |
+
priority,
|
| 169 |
+
resolve,
|
| 170 |
+
reject,
|
| 171 |
+
timestamp: performance.now()
|
| 172 |
+
};
|
| 173 |
+
|
| 174 |
+
this.queue.push(task);
|
| 175 |
+
this.queue.sort((a, b) => b.priority - a.priority);
|
| 176 |
+
this.processQueue();
|
| 177 |
+
});
|
| 178 |
+
}
|
| 179 |
+
}`,
|
| 180 |
+
description: 'Maintain 60fps by offloading heavy computations to a prioritized worker pool. Critical for data visualization apps.',
|
| 181 |
+
tags: ['Web Workers', 'Performance', 'Architecture'],
|
| 182 |
+
},
|
| 183 |
+
];
|
| 184 |
+
|
| 185 |
+
export default function Workbench() {
|
| 186 |
+
const sectionRef = useRef(null);
|
| 187 |
+
const cardsRef = useRef([]);
|
| 188 |
+
const [copiedId, setCopiedId] = useState(null);
|
| 189 |
+
|
| 190 |
+
useEffect(() => {
|
| 191 |
+
const cards = cardsRef.current;
|
| 192 |
+
const triggers = [];
|
| 193 |
+
|
| 194 |
+
cards.forEach((card, index) => {
|
| 195 |
+
const trigger = ScrollTrigger.create({
|
| 196 |
+
trigger: card,
|
| 197 |
+
start: 'top 85%',
|
| 198 |
+
onEnter: () => {
|
| 199 |
+
gsap.fromTo(card,
|
| 200 |
+
{ opacity: 0, y: 60, rotateX: 15 },
|
| 201 |
+
{
|
| 202 |
+
opacity: 1,
|
| 203 |
+
y: 0,
|
| 204 |
+
rotateX: 0,
|
| 205 |
+
duration: 0.8,
|
| 206 |
+
delay: index * 0.1,
|
| 207 |
+
ease: 'power3.out'
|
| 208 |
+
}
|
| 209 |
+
);
|
| 210 |
+
},
|
| 211 |
+
once: true
|
| 212 |
+
});
|
| 213 |
+
triggers.push(trigger);
|
| 214 |
+
});
|
| 215 |
+
|
| 216 |
+
return () => {
|
| 217 |
+
triggers.forEach(t => t.kill());
|
| 218 |
+
};
|
| 219 |
+
}, []);
|
| 220 |
+
|
| 221 |
+
const copyToClipboard = async (code, id) => {
|
| 222 |
+
await navigator.clipboard.writeText(code);
|
| 223 |
+
setCopiedId(id);
|
| 224 |
+
setTimeout(() => setCopiedId(null), 2000);
|
| 225 |
+
};
|
| 226 |
+
|
| 227 |
+
const getColorClasses = (color) => {
|
| 228 |
+
const colors = {
|
| 229 |
+
electric: 'border-electric-500/30 hover:border-electric-500/60 text-electric-400 bg-electric-500/5',
|
| 230 |
+
magma: 'border-magma-500/30 hover:border-magma-500/60 text-magma-400 bg-magma-500/5',
|
| 231 |
+
gold: 'border-gold-500/30 hover:border-gold-500/60 text-gold-400 bg-gold-500/5',
|
| 232 |
+
};
|
| 233 |
+
return colors[color];
|
| 234 |
+
};
|
| 235 |
+
|
| 236 |
+
return (
|
| 237 |
+
<section id="workbench" ref={sectionRef} className="py-24 relative">
|
| 238 |
+
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
|
| 239 |
+
{/* Section Header */}
|
| 240 |
+
<div className="text-center mb-16">
|
| 241 |
+
<div className="inline-flex items-center gap-2 px-4 py-2 rounded-full bg-iron-800/80 border border-iron-600/50 mb-6">
|
| 242 |
+
<Terminal className="w-4 h-4 text-electric-500" />
|
| 243 |
+
<span className="font-mono text-sm text-iron-300">WORKBENCH // ACTIVE_SESSIONS: 1,247</span>
|
| 244 |
+
</div>
|
| 245 |
+
<h2 className="text-4xl sm:text-5xl font-bold mb-4">
|
| 246 |
+
<span className="text-iron-100">Production-Ready </span>
|
| 247 |
+
<span className="text-gradient">Protocols</span>
|
| 248 |
+
</h2>
|
| 249 |
+
<p className="max-w-2xl mx-auto text-iron-400 font-mono">
|
| 250 |
+
Battle-tested code snippets from the frontlines of high-performance systems.
|
| 251 |
+
Copy, adapt, deploy.
|
| 252 |
+
</p>
|
| 253 |
+
</div>
|
| 254 |
+
|
| 255 |
+
{/* Tips Grid */}
|
| 256 |
+
<div className="grid lg:grid-cols-2 gap-6">
|
| 257 |
+
{tips.map((tip, index) => {
|
| 258 |
+
const Icon = tip.icon;
|
| 259 |
+
const colorClasses = getColorClasses(tip.color);
|
| 260 |
+
|
| 261 |
+
return (
|
| 262 |
+
<div
|
| 263 |
+
key={tip.id}
|
| 264 |
+
ref={el => cardsRef.current[index] = el}
|
| 265 |
+
className={`group relative bg-iron-800/50 backdrop-blur-sm border rounded-xl overflow-hidden transition-all duration-500 hover:shadow-2xl ${colorClasses}`}
|
| 266 |
+
style={{ perspective: '1000px' }}
|
| 267 |
+
>
|
| 268 |
+
{/* Glow Effect */}
|
| 269 |
+
<div className={`absolute inset-0 opacity-0 group-hover:opacity-100 transition-opacity duration-500 bg-gradient-to-br ${tip.color === 'electric' ? 'from-electric-500/10' : tip.color === 'magma' ? 'from-magma-500/10' : 'from-gold-500/10'} to-transparent`} />
|
| 270 |
+
|
| 271 |
+
<div className="relative p-6">
|
| 272 |
+
{/* Header */}
|
| 273 |
+
<div className="flex items-start justify-between mb-4">
|
| 274 |
+
<div className="flex items-center gap-3">
|
| 275 |
+
<div className={`p-2 rounded-lg ${colorClasses}`}>
|
| 276 |
+
<Icon className="w-5 h-5" />
|
| 277 |
+
</div>
|
| 278 |
+
<div>
|
| 279 |
+
<span className={`text-xs font-mono uppercase tracking-wider ${tip.color === 'electric' ? 'text-electric-400' : tip.color === 'magma' ? 'text-magma-400' : 'text-gold-400'}`}>
|
| 280 |
+
{tip.category}
|
| 281 |
+
</span>
|
| 282 |
+
<h3 className="text-lg font-semibold text-iron-100">{tip.title}</h3>
|
| 283 |
+
</div>
|
| 284 |
+
</div>
|
| 285 |
+
<button
|
| 286 |
+
onClick={() => copyToClipboard(tip.code, tip.id)}
|
| 287 |
+
className="p-2 rounded-lg bg-iron-700/50 hover:bg-iron-600/50 transition-colors"
|
| 288 |
+
>
|
| 289 |
+
{copiedId === tip.id ? (
|
| 290 |
+
<Check className="w-4 h-4 text-green-400" />
|
| 291 |
+
) : (
|
| 292 |
+
<Copy className="w-4 h-4 text-iron-400" />
|
| 293 |
+
)}
|
| 294 |
+
</button>
|
| 295 |
+
</div>
|
| 296 |
+
|
| 297 |
+
{/* Code Block */}
|
| 298 |
+
<div className="relative mb-4 rounded-lg bg-iron-900/80 border border-iron-700/50 overflow-hidden">
|
| 299 |
+
<div className="flex items-center gap-1.5 px-3 py-2 border-b border-iron-700/50">
|
| 300 |
+
<div className="w-3 h-3 rounded-full bg-magma-500/80" />
|
| 301 |
+
<div className="w-3 h-3 rounded-full bg-gold-500/80" />
|
| 302 |
+
<div className="w-3 h-3 rounded-full bg-green-500/80" />
|
| 303 |
+
<span className="ml-auto text-xs text-iron-500 font-mono">{tip.tags[0]}</span>
|
| 304 |
+
</div>
|
| 305 |
+
<pre className="p-4 text-sm font-mono overflow-x-auto">
|
| 306 |
+
<code className="text-iron-300">{tip.code}</code>
|
| 307 |
+
</pre>
|
| 308 |
+
</div>
|
| 309 |
+
|
| 310 |
+
{/* Description */}
|
| 311 |
+
<p className="text-iron-400 text-sm leading-relaxed mb-4">
|
| 312 |
+
{tip.description}
|
| 313 |
+
</p>
|
| 314 |
+
|
| 315 |
+
{/* Tags */}
|
| 316 |
+
<div className="flex flex-wrap gap-2">
|
| 317 |
+
{tip.tags.map(tag => (
|
| 318 |
+
<span key={tag} className="px-2 py-1 text-xs font-mono rounded bg-iron-700/50 text-iron-300">
|
| 319 |
+
#{tag}
|
| 320 |
+
</span>
|
| 321 |
+
))}
|
| 322 |
+
</div>
|
| 323 |
+
</div>
|
| 324 |
+
</div>
|
| 325 |
+
);
|
| 326 |
+
})}
|
| 327 |
+
</div>
|
| 328 |
+
</div>
|
| 329 |
+
</section>
|
| 330 |
+
);
|
| 331 |
+
}
|