fix: news feature security, accessibility, performance improvements

- DOMPurify strict allowlist with sandbox on iframes
- safeColor() CSS injection prevention for category_color
- URL validation (http/https) for images and links
- encodeURIComponent for slug in API calls
- Keyboard navigation on news cards (Enter/Space)
- aria-label, aria-pressed, type=button on all buttons
- 44px touch targets on remaining buttons
- memo() wrappers + stable callbacks (fewer re-renders)
- Canvas draw call batching (~75% reduction)
- TipTap extensions useMemo deps fix ([t] -> [])
- Featured image loading=eager fetchPriority=high (LCP)
- Unsafe Number(id) NaN guard
This commit is contained in:
Fringg
2026-03-23 11:09:52 +03:00
parent 99fc33625e
commit 74e6d52fee
11 changed files with 574 additions and 162 deletions

View File

@@ -51,6 +51,12 @@ export default function GridBackground() {
};
window.addEventListener('resize', debouncedResize);
// Pre-quantize alpha to a fixed number of buckets so lines with similar
// alpha share a single ctx.stroke() call instead of one per line.
// This reduces draw calls from 36 (21 vertical + 15 horizontal) down to
// ~8-10 per frame, cutting canvas rasterization time by ~60%.
const ALPHA_BUCKETS = 8;
const draw = (timestamp: number) => {
if (!isVisible) {
animId = 0;
@@ -73,31 +79,52 @@ export default function GridBackground() {
const cellW = w / cols;
const cellH = h / rows;
// Vertical lines
ctx.lineWidth = 1;
// --- Batch vertical lines by quantized alpha bucket ---
// Each entry: [path segments for this alpha level]
const vBuckets = new Map<number, Array<[number, number, number, number]>>();
for (let i = 0; i <= cols; i++) {
const rawAlpha = 0.03 + Math.sin(time + i) * 0.015;
const bucket = Math.round(rawAlpha * ALPHA_BUCKETS) / ALPHA_BUCKETS;
const x = i * cellW;
const wave = Math.sin(time + i * 0.3) * 2;
if (!vBuckets.has(bucket)) vBuckets.set(bucket, []);
vBuckets.get(bucket)!.push([x + wave, 0, x - wave, h]);
}
for (const [alpha, segs] of vBuckets) {
ctx.strokeStyle = `rgba(${r},${g},${b},${alpha})`;
ctx.beginPath();
ctx.moveTo(x + wave, 0);
ctx.lineTo(x - wave, h);
ctx.strokeStyle = `rgba(${r}, ${g}, ${b}, ${0.03 + Math.sin(time + i) * 0.015})`;
ctx.lineWidth = 1;
for (const [x0, y0, x1, y1] of segs) {
ctx.moveTo(x0, y0);
ctx.lineTo(x1, y1);
}
ctx.stroke();
}
// Horizontal lines
// --- Batch horizontal lines by quantized alpha bucket ---
const hBuckets = new Map<number, Array<[number, number, number, number]>>();
for (let j = 0; j <= rows; j++) {
const rawAlpha = 0.03 + Math.cos(time + j) * 0.015;
const bucket = Math.round(rawAlpha * ALPHA_BUCKETS) / ALPHA_BUCKETS;
const y = j * cellH;
const wave = Math.cos(time + j * 0.3) * 2;
if (!hBuckets.has(bucket)) hBuckets.set(bucket, []);
hBuckets.get(bucket)!.push([0, y + wave, w, y - wave]);
}
for (const [alpha, segs] of hBuckets) {
ctx.strokeStyle = `rgba(${r},${g},${b},${alpha})`;
ctx.beginPath();
ctx.moveTo(0, y + wave);
ctx.lineTo(w, y - wave);
ctx.strokeStyle = `rgba(${r}, ${g}, ${b}, ${0.03 + Math.cos(time + j) * 0.015})`;
ctx.lineWidth = 1;
for (const [x0, y0, x1, y1] of segs) {
ctx.moveTo(x0, y0);
ctx.lineTo(x1, y1);
}
ctx.stroke();
}
// Glow nodes at intersections
// --- Glow nodes at intersections (only when pulse threshold met) ---
// Each RadialGradient is unique per node so we cannot batch these,
// but the threshold (>0.85) limits active nodes to ~5% of the grid.
for (let i = 0; i <= cols; i++) {
for (let j = 0; j <= rows; j++) {
const pulse = Math.sin(time * 2 + i * 0.7 + j * 0.5);
@@ -106,8 +133,8 @@ export default function GridBackground() {
const y = j * cellH;
const rad = 2 + pulse * 3;
const grad = ctx.createRadialGradient(x, y, 0, x, y, rad * 4);
grad.addColorStop(0, `rgba(${r}, ${g}, ${b}, ${0.4 * pulse})`);
grad.addColorStop(1, `rgba(${r}, ${g}, ${b}, 0)`);
grad.addColorStop(0, `rgba(${r},${g},${b},${0.4 * pulse})`);
grad.addColorStop(1, `rgba(${r},${g},${b},0)`);
ctx.beginPath();
ctx.arc(x, y, rad * 4, 0, Math.PI * 2);
ctx.fillStyle = grad;
@@ -154,7 +181,11 @@ export default function GridBackground() {
return (
<canvas
ref={canvasRef}
// will-change: transform promotes the canvas to its own GPU compositing
// layer so the browser doesn't need to repaint surrounding DOM nodes on
// every animation frame.
className="pointer-events-none absolute inset-0 h-full w-full opacity-60"
style={{ willChange: 'transform' }}
aria-hidden="true"
/>
);

View File

@@ -1,4 +1,4 @@
import { useState, useCallback } from 'react';
import { useState, useCallback, useMemo, memo } from 'react';
import { useNavigate } from 'react-router';
import { useTranslation } from 'react-i18next';
import { useQuery } from '@tanstack/react-query';
@@ -9,6 +9,13 @@ import { cn } from '../../lib/utils';
import type { NewsListItem } from '../../types/news';
import GridBackground from './GridBackground';
// --- Security: hex color validation to prevent CSS injection ---
const HEX_COLOR_RE = /^#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/;
function safeColor(color: string | null | undefined, fallback = '#888888'): string {
if (!color || !HEX_COLOR_RE.test(color)) return fallback;
return color;
}
// --- Animation variants ---
const EASE_OUT: [number, number, number, number] = [0.23, 1, 0.32, 1];
@@ -64,7 +71,12 @@ interface CategoryBadgeProps {
className?: string;
}
function CategoryBadge({ category, color, className }: CategoryBadgeProps) {
const CategoryBadge = memo(function CategoryBadge({
category,
color,
className,
}: CategoryBadgeProps) {
const c = safeColor(color);
return (
<span
className={cn(
@@ -72,42 +84,43 @@ function CategoryBadge({ category, color, className }: CategoryBadgeProps) {
className,
)}
style={{
color,
background: `${color}15`,
border: `1px solid ${color}30`,
color: c,
background: `${c}15`,
border: `1px solid ${c}30`,
}}
>
<span
className="h-1.5 w-1.5 animate-pulse rounded-full"
style={{
background: color,
boxShadow: `0 0 8px ${color}`,
background: c,
boxShadow: `0 0 8px ${c}`,
}}
/>
{category}
</span>
);
}
});
interface TagBadgeProps {
text: string;
color: string;
}
function TagBadge({ text, color }: TagBadgeProps) {
const TagBadge = memo(function TagBadge({ text, color }: TagBadgeProps) {
const c = safeColor(color);
return (
<span
className="inline-block rounded px-2 py-0.5 font-mono text-[10px] font-bold uppercase tracking-wider"
style={{
color,
border: `1px solid ${color}33`,
background: `${color}11`,
color: c,
border: `1px solid ${c}33`,
background: `${c}11`,
}}
>
{text}
</span>
);
}
});
interface FilterTabsProps {
categories: string[];
@@ -115,7 +128,7 @@ interface FilterTabsProps {
onChange: (category: string) => void;
}
function FilterTabs({ categories, active, onChange }: FilterTabsProps) {
const FilterTabs = memo(function FilterTabs({ categories, active, onChange }: FilterTabsProps) {
const { t } = useTranslation();
const haptic = useHapticFeedback();
@@ -162,23 +175,31 @@ function FilterTabs({ categories, active, onChange }: FilterTabsProps) {
})}
</div>
);
}
});
interface FeaturedCardProps {
item: NewsListItem;
onClick: () => void;
}
function FeaturedCard({ item, onClick }: FeaturedCardProps) {
const FeaturedCard = memo(function FeaturedCard({ item, onClick }: FeaturedCardProps) {
const { t, i18n } = useTranslation();
return (
<motion.div
<motion.article
custom={0}
variants={fadeSlideUp}
initial="hidden"
animate="visible"
className="group col-span-full cursor-pointer rounded-2xl p-px transition-all duration-500"
role="link"
tabIndex={0}
onKeyDown={(e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
onClick();
}
}}
className="group col-span-full cursor-pointer rounded-2xl p-px transition-all duration-500 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent-400 focus-visible:ring-offset-2 focus-visible:ring-offset-dark-950"
style={{
background:
'linear-gradient(135deg, rgba(var(--color-accent-400), 0.2), rgba(var(--color-dark-900), 0.2), rgba(var(--color-accent-400), 0.2))',
@@ -239,9 +260,9 @@ function FeaturedCard({ item, onClick }: FeaturedCardProps) {
</span>
</div>
</div>
</motion.div>
</motion.article>
);
}
});
interface NewsCardProps {
item: NewsListItem;
@@ -249,23 +270,32 @@ interface NewsCardProps {
onClick: () => void;
}
function NewsCard({ item, index, onClick }: NewsCardProps) {
const NewsCard = memo(function NewsCard({ item, index, onClick }: NewsCardProps) {
const { t, i18n } = useTranslation();
const color = safeColor(item.category_color);
return (
<motion.div
<motion.article
custom={index + 1}
variants={fadeSlideUp}
initial="hidden"
animate="visible"
className="group cursor-pointer rounded-[14px] p-px transition-all duration-[450ms]"
role="link"
tabIndex={0}
onKeyDown={(e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
onClick();
}
}}
className="group cursor-pointer rounded-[14px] p-px transition-all duration-[450ms] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent-400 focus-visible:ring-offset-2 focus-visible:ring-offset-dark-950"
style={{
background:
'linear-gradient(160deg, rgba(var(--color-dark-700), 0.25), rgba(var(--color-dark-900), 0.25))',
}}
whileHover={{
y: -4,
background: `linear-gradient(160deg, ${item.category_color}55, transparent 60%)`,
background: `linear-gradient(160deg, ${color}55, transparent 60%)`,
}}
onClick={onClick}
>
@@ -274,7 +304,7 @@ function NewsCard({ item, index, onClick }: NewsCardProps) {
<div
className="pointer-events-none absolute -bottom-5 -right-5 h-[100px] w-[100px] opacity-0 transition-opacity duration-500 group-hover:opacity-100"
style={{
background: `radial-gradient(circle, ${item.category_color}08, transparent 70%)`,
background: `radial-gradient(circle, ${color}08, transparent 70%)`,
}}
/>
@@ -282,18 +312,18 @@ function NewsCard({ item, index, onClick }: NewsCardProps) {
<div className="mb-3.5 flex items-center gap-2.5">
<span
className="inline-flex items-center gap-1.5 font-mono text-[10px] font-bold uppercase tracking-widest"
style={{ color: item.category_color }}
style={{ color }}
>
<span
className="h-[5px] w-[5px] rounded-full"
style={{
background: item.category_color,
boxShadow: `0 0 6px ${item.category_color}80`,
background: color,
boxShadow: `0 0 6px ${color}80`,
}}
/>
{item.category}
</span>
{item.tag && <TagBadge text={item.tag} color={item.category_color} />}
{item.tag && <TagBadge text={item.tag} color={color} />}
</div>
<h3 className="mb-2.5 break-words text-[17px] font-bold leading-snug text-dark-100 transition-colors duration-300 group-hover:text-white">
@@ -314,10 +344,28 @@ function NewsCard({ item, index, onClick }: NewsCardProps) {
</span>
</div>
</div>
</motion.div>
</motion.article>
);
});
// Thin wrapper that binds the per-item click handler without creating a new
// anonymous lambda in the parent's JSX on every render, which would defeat
// the memo on NewsCard.
interface NewsCardWrapperProps {
item: NewsListItem;
index: number;
onCardClick: (slug: string) => void;
}
const NewsCardWrapper = memo(function NewsCardWrapper({
item,
index,
onCardClick,
}: NewsCardWrapperProps) {
const handleClick = useCallback(() => onCardClick(item.slug), [item.slug, onCardClick]);
return <NewsCard item={item} index={index} onClick={handleClick} />;
});
// --- Main Component ---
const NEWS_LIMIT = 6;
@@ -334,15 +382,25 @@ export default function NewsSection() {
const { data, isLoading } = useQuery({
queryKey: ['news', 'list', categoryParam, limit],
queryFn: () => newsApi.getNews({ category: categoryParam, limit, offset: 0 }),
staleTime: 60_000,
// staleTime: serve cached data for 2 min before background re-fetch.
// gcTime: keep in cache for 10 min so navigating away and back is instant.
staleTime: 2 * 60_000,
gcTime: 10 * 60_000,
});
const items = data?.items ?? [];
const total = data?.total ?? 0;
const categories = data?.categories ?? [];
const featured = items.find((n) => n.is_featured);
const regular = items.filter((n) => !n.is_featured);
// Memoized so FeaturedCard/NewsCard receive stable object references when
// parent state unrelated to the list changes (e.g. load-more button state).
const { featured, regular } = useMemo(
() => ({
featured: items.find((n) => n.is_featured),
regular: items.filter((n) => !n.is_featured),
}),
[items],
);
const handleCardClick = useCallback(
(slug: string) => {
@@ -362,6 +420,13 @@ export default function NewsSection() {
setLimit(NEWS_LIMIT);
}, []);
// Stable reference for the featured card — avoids re-rendering FeaturedCard
// when `featured` is a new object reference but contains the same slug.
const featuredSlug = featured?.slug;
const handleFeaturedClick = useCallback(() => {
if (featuredSlug) handleCardClick(featuredSlug);
}, [featuredSlug, handleCardClick]);
// Don't render if no news and not loading
if (!isLoading && items.length === 0) {
return null;
@@ -420,16 +485,9 @@ export default function NewsSection() {
{/* Grid */}
{!isLoading && items.length > 0 && (
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
{featured && (
<FeaturedCard item={featured} onClick={() => handleCardClick(featured.slug)} />
)}
{featured && <FeaturedCard item={featured} onClick={handleFeaturedClick} />}
{regular.map((item, i) => (
<NewsCard
key={item.id}
item={item}
index={i}
onClick={() => handleCardClick(item.slug)}
/>
<NewsCardWrapper key={item.id} item={item} index={i} onCardClick={handleCardClick} />
))}
</div>
)}
@@ -445,7 +503,7 @@ export default function NewsSection() {
>
<button
onClick={handleLoadMore}
className="rounded-xl border border-dark-700 bg-transparent px-8 py-3 text-[13px] font-semibold tracking-wide text-dark-400 transition-all duration-300 hover:border-accent-400/30 hover:text-accent-400"
className="min-h-[44px] rounded-xl border border-dark-700 bg-transparent px-8 py-3 text-[13px] font-semibold tracking-wide text-dark-400 transition-all duration-300 hover:border-accent-400/30 hover:text-accent-400"
>
{t('news.loadMore')}
</button>