nomadro/apps/miniprogram/h5/src/pages/social/_components/SocialExperience.tsx
Eric f20406397a Harden social UX and APIs: idempotent chat/join, config cache, weapp polish.
Improve hall chat retries, meetup-live lifecycle, home/mine remote-config UX, and add isolated social/API regression tests without deploying.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-02 14:00:11 +08:00

274 lines
8.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {
useEffect,
useRef,
useState,
type CSSProperties,
type ReactNode,
} from 'react'
import { H5El } from './H5El'
import { toggleH5Theme, useNomadroTheme } from '@/utils/h5Theme'
const isH5 = process.env.TARO_ENV === 'h5'
function Constellation({ active }: { active: boolean }) {
const canvasRef = useRef<HTMLCanvasElement | null>(null)
useEffect(() => {
if (!isH5 || !active) return
const canvas = canvasRef.current
const context = canvas?.getContext('2d')
if (!canvas || !context) return
const media = window.matchMedia('(prefers-reduced-motion: reduce)')
let frame = 0
let width = 0
let height = 0
let last = 0
let pointer = { x: -1000, y: -1000 }
let points: {
x: number
y: number
vx: number
vy: number
r: number
color: string
}[] = []
const resize = () => {
width = Math.max(
window.innerWidth || 0,
document.documentElement.clientWidth || 0,
window.visualViewport?.width || 0,
)
height = Math.max(
window.innerHeight || 0,
document.documentElement.clientHeight || 0,
window.visualViewport?.height || 0,
)
const ratio = Math.min(window.devicePixelRatio || 1, 1.5)
canvas.style.width = `${width}px`
canvas.style.height = `${height}px`
canvas.width = Math.floor(width * ratio)
canvas.height = Math.floor(height * ratio)
context.setTransform(ratio, 0, 0, ratio, 0, 0)
points = Array.from({ length: width < 700 ? 28 : 52 }, (_, i) => ({
x: Math.random() * width,
y: Math.random() * height,
vx: (Math.random() - 0.5) * 0.28,
vy: (Math.random() - 0.5) * 0.28,
r: 0.9 + Math.random() * 1.4,
color: ['78,205,196', '255,107,107', '255,230,109'][i % 3]!,
}))
}
const draw = (time: number) => {
if (document.hidden || media.matches) return
const delta = Math.min((time - last) / 16.67 || 1, 2)
last = time
context.clearRect(0, 0, width, height)
points.forEach((p, index) => {
p.x = (p.x + p.vx * delta + width) % width
p.y = (p.y + p.vy * delta + height) % height
const near = Math.hypot(pointer.x - p.x, pointer.y - p.y) < 240
context.beginPath()
context.arc(p.x, p.y, p.r, 0, Math.PI * 2)
context.fillStyle = `rgba(${p.color},${near ? 0.55 : 0.28})`
context.fill()
if (near) {
context.beginPath()
context.arc(p.x, p.y, p.r + 4, 0, Math.PI * 2)
context.fillStyle = `rgba(${p.color},0.1)`
context.fill()
}
if (width > 700) {
points.slice(index + 1).forEach((q) => {
const distance = Math.hypot(q.x - p.x, q.y - p.y)
if (distance > 140) return
context.beginPath()
context.moveTo(p.x, p.y)
context.lineTo(q.x, q.y)
context.strokeStyle = `rgba(78,205,196,${(1 - distance / 140) * 0.16})`
context.lineWidth = 1.05
context.stroke()
})
}
})
frame = requestAnimationFrame(draw)
}
const restart = () => {
cancelAnimationFrame(frame)
last = 0
context.clearRect(0, 0, width, height)
if (!document.hidden && !media.matches) {
frame = requestAnimationFrame(draw)
}
}
const move = (event: PointerEvent) => {
if (event.pointerType === 'mouse') {
pointer = { x: event.clientX, y: event.clientY }
}
}
resize()
restart()
window.addEventListener('resize', resize)
window.addEventListener('pointermove', move, { passive: true })
document.addEventListener('visibilitychange', restart)
media.addEventListener('change', restart)
return () => {
cancelAnimationFrame(frame)
window.removeEventListener('resize', resize)
window.removeEventListener('pointermove', move)
document.removeEventListener('visibilitychange', restart)
media.removeEventListener('change', restart)
context.clearRect(0, 0, width, height)
}
}, [active])
if (!isH5) return null
return (
<canvas
ref={canvasRef}
className='social-constellation'
aria-hidden
/>
)
}
function FloatingEmojiField() {
const EMOJIS = [
{ e: '☕', x: '8%', y: '22%', delay: '0s', dur: '9s', size: '1.1rem' },
{ e: '🌏', x: '88%', y: '20%', delay: '1.2s', dur: '11s', size: '1.15rem' },
{ e: '✨', x: '78%', y: '62%', delay: '2.4s', dur: '10s', size: '0.95rem' },
{ e: '💬', x: '14%', y: '68%', delay: '0.8s', dur: '12s', size: '1rem' },
{ e: '🚀', x: '52%', y: '12%', delay: '3s', dur: '10.5s', size: '0.9rem' },
]
return (
<H5El className='social-emoji-field social-emoji-field-soft' aria-hidden>
{EMOJIS.map((item) => (
<H5El
as='span'
key={item.e + item.x}
className='social-emoji-float'
style={
{
left: item.x,
top: item.y,
fontSize: item.size,
animationDelay: item.delay,
animationDuration: item.dur,
} as CSSProperties
}
>
{item.e}
</H5El>
))}
</H5El>
)
}
export function FxBelt() {
const ROW = ['☕', '🌏', '💬', '✨', '📡', '🚀']
const line = [...ROW, ...ROW, ...ROW].join(' ')
return (
<H5El className='social-fx-belt' aria-hidden>
<H5El className='social-fx-belt-track'>
<H5El as='span'>{line}</H5El>
<H5El as='span'>{line}</H5El>
</H5El>
</H5El>
)
}
export function SocialExperience({
children,
showEmojiField = false,
}: {
children: ReactNode
showEmojiField?: boolean
}) {
const theme = useNomadroTheme()
const [immersive, setImmersive] = useState(true)
const rootRef = useRef<HTMLDivElement | null>(null)
useEffect(() => {
if (!isH5 || !immersive) return
const root = rootRef.current
if (!root) return
let frame = 0
const move = (event: PointerEvent) => {
if (
event.pointerType !== 'mouse' ||
window.matchMedia('(prefers-reduced-motion: reduce)').matches
) {
return
}
cancelAnimationFrame(frame)
frame = requestAnimationFrame(() => {
root.style.setProperty('--pointer-x', `${event.clientX}px`)
root.style.setProperty('--pointer-y', `${event.clientY}px`)
const card = (event.target as Element).closest<HTMLElement>(
'.social-card, .social-list-item, .social-panel, .social-member-card, .social-person-card, .connection-way',
)
if (card) {
const rect = card.getBoundingClientRect()
card.style.setProperty('--spot-x', `${event.clientX - rect.left}px`)
card.style.setProperty('--spot-y', `${event.clientY - rect.top}px`)
}
})
}
root.addEventListener('pointermove', move, { passive: true })
return () => {
cancelAnimationFrame(frame)
root.removeEventListener('pointermove', move)
}
}, [immersive])
// 微信禁止裸 <div>/<canvas>,否则整页黑屏;H5 才挂 pointer ref
if (!isH5) {
return (
<H5El className='social-experience' data-theme={theme}>
{children}
<H5El as='button' className='social-theme-control' aria-label={theme === 'dark' ? '切换日间模式' : '切换夜间模式'}
onClick={() => toggleH5Theme(theme)}>{theme === 'dark' ? '☀' : '☾'}</H5El>
</H5El>
)
}
return (
<div
ref={rootRef}
className='social-experience'
data-immersive={immersive ? 'true' : 'false'}
data-theme={theme}
>
<H5El className='social-ambient' aria-hidden>
<H5El as='i' />
<H5El as='i' />
<H5El as='i' />
</H5El>
<Constellation active={immersive} />
<H5El className='social-pointer-glow' aria-hidden />
{showEmojiField ? <FloatingEmojiField /> : null}
{children}
<H5El as='button' className='social-theme-control' aria-label={theme === 'dark' ? '切换日间模式' : '切换夜间模式'}
onClick={() => toggleH5Theme(theme)}>{theme === 'dark' ? '☀' : '☾'}</H5El>
<H5El
as='button'
type='button'
className='social-fx-control'
onClick={() => setImmersive((v) => !v)}
aria-pressed={immersive}
title='切换视觉动效强度'
>
<H5El as='span'>{immersive ? '✨' : '⚡'}</H5El>
<H5El as='span'>动效 · {immersive ? '沉浸' : '轻量'}</H5El>
</H5El>
</div>
)
}
export { FloatingEmojiField }