Files
forumlify/components/AppProvider.js
T

718 lines
26 KiB
JavaScript
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.
'use client';
// 全局状态 Provider:用户、主题、论坛名、当前视图(feed/post/new/admin/...)
import { createContext, useContext, useState, useEffect, useCallback, useRef } from 'react';
import { flushSync } from 'react-dom';
import { API, getTheme, setToken, getToken } from '@/lib/api';
const AppContext = createContext(null);
const POST_TRANSITION_PARTS = [
['.post-avatar', 'post-avatar'],
['.post-username', 'post-author'],
['.post-title', 'post-heading'],
['.post-time', 'post-time'],
['.post-content', 'post-body'],
['.post-images', 'post-feed-media'],
['.post-pin-state', 'post-pin-state'],
['.post-edited-label', 'post-edited-label'],
['.post-actions', 'post-feed-actions'],
];
function namePostTransitionParts(card) {
if (!card) return [];
return POST_TRANSITION_PARTS.flatMap(([selector, name]) => {
const element = card.querySelector(selector);
if (!element) return [];
element.style.viewTransitionName = name;
return [element];
});
}
function clearPostTransitionParts(elements) {
elements.forEach((element) => {
element.style.viewTransitionName = '';
});
}
function waitForPostImages(container) {
const images = Array.from(container?.querySelectorAll('img') || []);
if (images.length === 0) return Promise.resolve();
return new Promise((resolve) => {
let settled = false;
const finish = () => {
if (settled) return;
settled = true;
clearTimeout(timeout);
resolve();
};
const timeout = setTimeout(finish, 1200);
Promise.all(images.map((image) => image.decode?.().catch(() => {}) || Promise.resolve())).then(finish);
});
}
function stageSharedGeometry(transition, {
name,
duration,
moveOffset,
moveEasing = 'cubic-bezier(0.16, 1, 0.3, 1)',
resizeEasing = 'cubic-bezier(0.4, 0, 0.2, 1)',
reverse = false,
}) {
transition.ready.then(() => {
const groupAnimation = document.getAnimations().find(
(animation) => animation.effect?.pseudoElement === `::view-transition-group(${name})`
);
const frames = groupAnimation?.effect?.getKeyframes();
if (!groupAnimation || !frames || frames.length < 2) return;
const start = frames[0];
const end = frames[frames.length - 1];
groupAnimation.effect.setKeyframes(reverse ? [
{
offset: 0,
width: start.width,
height: start.height,
transform: start.transform,
easing: resizeEasing,
},
{
offset: 1 - moveOffset,
width: start.width,
height: end.height,
transform: start.transform,
easing: moveEasing,
},
{
offset: 1,
width: end.width,
height: end.height,
transform: end.transform,
},
] : [
{
offset: 0,
width: start.width,
height: start.height,
transform: start.transform,
easing: moveEasing,
},
{
offset: moveOffset,
width: end.width,
height: start.height,
transform: end.transform,
easing: resizeEasing,
},
{
offset: 1,
width: end.width,
height: end.height,
transform: end.transform,
},
]);
groupAnimation.effect.updateTiming({ duration, easing: 'linear', fill: 'both' });
}).catch(() => {});
}
function stagePostBodyGeometry(transition, {
duration,
moveOffset,
moveEasing = 'cubic-bezier(0.42, 0, 0.58, 1)',
resizeEasing = 'cubic-bezier(0.42, 0, 0.58, 1)',
}) {
transition.ready.then(() => {
const animations = document.getAnimations();
const cardAnimation = animations.find(
(animation) => animation.effect?.pseudoElement === '::view-transition-group(post-expand)'
);
const bodyAnimation = animations.find(
(animation) => animation.effect?.pseudoElement === '::view-transition-group(post-body)'
);
const cardFrames = cardAnimation?.effect?.getKeyframes();
const bodyFrames = bodyAnimation?.effect?.getKeyframes();
if (!bodyAnimation || !cardFrames || cardFrames.length < 2 || !bodyFrames || bodyFrames.length < 2) return;
const cardStart = cardFrames[0];
const cardEnd = cardFrames[cardFrames.length - 1];
const bodyStart = bodyFrames[0];
const bodyEnd = bodyFrames[bodyFrames.length - 1];
const cardStartMatrix = new DOMMatrixReadOnly(cardStart.transform);
const cardEndMatrix = new DOMMatrixReadOnly(cardEnd.transform);
const bodyStartMatrix = new DOMMatrixReadOnly(bodyStart.transform);
const bodyEndMatrix = new DOMMatrixReadOnly(bodyEnd.transform);
const startCardWidth = Number.parseFloat(cardStart.width);
const endCardWidth = Number.parseFloat(cardEnd.width);
const startBodyWidth = Number.parseFloat(bodyStart.width);
if (![startCardWidth, endCardWidth, startBodyWidth].every(Number.isFinite)) return;
// 第一阶段让正文锁定在卡片内的原始位置;卡片停稳后,正文才调整到详情布局。
const leftInset = bodyStartMatrix.e - cardStartMatrix.e;
const topInset = bodyStartMatrix.f - cardStartMatrix.f;
const rightInset = startCardWidth - leftInset - startBodyWidth;
const midpointWidth = Math.max(0, endCardWidth - leftInset - rightInset);
const midpointTransform = new DOMMatrix([
bodyEndMatrix.a,
bodyEndMatrix.b,
bodyEndMatrix.c,
bodyEndMatrix.d,
cardEndMatrix.e + leftInset,
cardEndMatrix.f + topInset,
]).toString();
bodyAnimation.effect.setKeyframes([
{
offset: 0,
width: bodyStart.width,
height: bodyStart.height,
transform: bodyStart.transform,
easing: moveEasing,
},
{
offset: moveOffset,
width: `${midpointWidth}px`,
height: bodyStart.height,
transform: midpointTransform,
easing: resizeEasing,
},
{
offset: 1,
width: bodyEnd.width,
height: bodyEnd.height,
transform: bodyEnd.transform,
},
]);
bodyAnimation.effect.updateTiming({ duration, easing: 'linear', fill: 'both' });
}).catch(() => {});
}
export function useApp() {
return useContext(AppContext);
}
export default function AppProvider({ children, cachedName = '' }) {
const [currentUser, setCurrentUser] = useState(null);
// 论坛名初始值来自 cookie(SSR 首帧即输出,SSR/客户端一致 → 无 hydration mismatch)
const [forumName, setForumName] = useState(cachedName || 'Forumlify');
const [theme, setThemeState] = useState('light');
const [view, setView] = useState('feed'); // feed | post | new | admin | settings | messages | user | custom
const [currentPostId, setCurrentPostId] = useState(null);
const [currentPostPreview, setCurrentPostPreview] = useState(null);
const [currentPostOrigin, setCurrentPostOrigin] = useState(null);
const [currentUsername, setCurrentUsername] = useState(null);
const [currentUserPreview, setCurrentUserPreview] = useState(null);
const [currentUserPostsPreview, setCurrentUserPostsPreview] = useState(null);
const [currentPageName, setCurrentPageName] = useState(null);
const [sort, setSort] = useState('latest');
const [refreshKey, setRefreshKey] = useState(0);
const [ready, setReady] = useState(false); // 初始加载完成标记(控制加载页/淡入)
// 有 cookie 缓存名即视为已加载(SSR/客户端一致)
const [forumNameLoaded, setForumNameLoaded] = useState(!!cachedName);
const loadedRef = useRef(false);
const refresh = useCallback(() => setRefreshKey((k) => k + 1), []);
// 论坛名写 cookie(SSR 可读 → 首帧输出,无需内联脚本注入 DOM)
const syncForumNameCookie = useCallback((name) => {
try {
document.cookie = 'forumlify-name=' + encodeURIComponent(name) + '; path=/; max-age=31536000';
} catch (e) {}
}, []);
// 主题
useEffect(() => {
const t = getTheme();
setThemeState(t);
document.documentElement.setAttribute('data-theme', t);
// 系统主题变化时跟随(仅当用户未手动设置过)
const mq = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)');
if (mq && mq.addEventListener) {
const onSysChange = () => {
if (!localStorage.getItem('forumlify-theme')) {
const next = mq.matches ? 'dark' : 'light';
setThemeState(next);
document.documentElement.setAttribute('data-theme', next);
}
};
mq.addEventListener('change', onSysChange);
return () => mq.removeEventListener('change', onSysChange);
}
}, []);
// 论坛名变化时同步浏览器标题(覆盖初始化/数据库加载/改名三条路径)
useEffect(() => {
document.title = forumName;
const titleEl = document.querySelector('title');
if (titleEl) titleEl.textContent = forumName;
}, [forumName]);
const toggleTheme = useCallback(() => {
setThemeState((prev) => {
const next = prev === 'light' ? 'dark' : 'light';
document.documentElement.setAttribute('data-theme', next);
localStorage.setItem('forumlify-theme', next);
return next;
});
}, []);
// 初始化:读取缓存论坛名(立即显示),恢复登录、加载服务器论坛名
useEffect(() => {
// 缓存论坛名(hydrate 后立即生效;SSR 首帧已由 cookie 输出)
try {
const cached = localStorage.getItem('forumlify-forum-name');
if (cached) {
setForumName(cached);
setForumNameLoaded(true);
syncForumNameCookie(cached);
}
} catch (e) {}
if (loadedRef.current) return;
loadedRef.current = true;
async function init() {
const token = getToken();
if (token) {
try {
const user = await API.getMe();
setCurrentUser(user);
} catch {
setToken(null);
}
}
try {
const s = await API.getSettings();
if (s.forum_name) {
setForumName(s.forum_name);
localStorage.setItem('forumlify-forum-name', s.forum_name);
syncForumNameCookie(s.forum_name);
}
// 先标记论坛名已收到(加载页原地淡入名字),稍后切换主页
setForumNameLoaded(true);
setTimeout(() => setReady(true), 650);
} catch {
setForumNameLoaded(true);
setReady(true);
}
}
init();
}, []);
// 页面标题:初始化中轮换 Loading. → Loading.. → Loading...,收到论坛名/完成后显示论坛名
useEffect(() => {
// 尚未收到论坛名且未完成初始化:轮换 Loading.
if (!ready && !forumNameLoaded) {
const seq = ['Loading.', 'Loading..', 'Loading...'];
let i = 0;
document.title = seq[0];
const t = setInterval(() => {
i = (i + 1) % seq.length;
document.title = seq[i];
}, 1000);
return () => {
clearInterval(t);
};
}
// 已收到论坛名或初始化完成:标题显示论坛名(Next 重置 metadata 时也能恢复)
document.title = forumName;
const titleEl = document.querySelector('title');
if (titleEl) titleEl.textContent = forumName;
}, [ready, forumNameLoaded, forumName]);
// 按 URL 参数初始化视图
useEffect(() => {
const params = new URLSearchParams(window.location.search);
const postParam = params.get('post');
const pageParam = params.get('page');
const userParam = params.get('user');
if (postParam) {
setView('post');
setCurrentPostId(postParam);
} else if (userParam) {
setView('user');
setCurrentUsername(userParam);
} else if (pageParam && ['messages', 'settings', 'admin', 'new'].includes(pageParam)) {
setView(pageParam);
} else if (pageParam) {
// 未知 page 参数视为自定义页面
setView('custom');
setCurrentPageName(pageParam);
}
}, []);
// 视图切换:同步 URL (pushState,模拟原 SPA 行为)
const navigate = useCallback((page) => {
const url = new URL(window.location);
if (page === 'feed') {
url.searchParams.delete('page');
url.searchParams.delete('post');
url.searchParams.delete('user');
} else {
url.searchParams.set('page', page);
url.searchParams.delete('post');
url.searchParams.delete('user');
}
const commitNavigation = (refreshFeed = true) => {
window.history.pushState({ page }, '', url);
setView(page);
setCurrentPostId(null);
setCurrentPostPreview(null);
setCurrentPostOrigin(null);
setCurrentUsername(null);
setCurrentUserPreview(null);
setCurrentUserPostsPreview(null);
if (refreshFeed) refresh();
};
const reduceMotion = window.matchMedia?.('(prefers-reduced-motion: reduce)').matches;
if (page !== 'feed' || view === 'feed' || !document.startViewTransition || reduceMotion) {
commitNavigation();
return;
}
if (view === 'user') {
const root = document.documentElement;
const sourceAvatar = document.querySelector('#pageUser .user-profile-avatar');
const sourceName = document.querySelector('#pageUser .user-profile-name');
const findTargetCard = () => {
if (currentUserPreview?.sourcePostId) {
const exact = Array.from(document.querySelectorAll('[data-post-id]')).find(
(element) => element.dataset.postId === String(currentUserPreview.sourcePostId)
);
if (exact) return exact;
}
return Array.from(document.querySelectorAll('[data-username]')).find(
(element) => element.dataset.username === String(currentUsername || '')
) || null;
};
let targetCard = findTargetCard();
if (sourceAvatar && sourceName && targetCard) {
let targetAvatar = null;
let targetName = null;
root.classList.add('user-view-transition', 'returning-user-home');
const transition = document.startViewTransition(() => {
flushSync(() => commitNavigation(false));
targetCard = findTargetCard();
targetAvatar = targetCard?.querySelector('.post-avatar') || null;
targetName = targetCard?.querySelector('.post-username') || null;
if (targetAvatar) targetAvatar.style.viewTransitionName = 'user-avatar';
if (targetName) targetName.style.viewTransitionName = 'user-name';
});
const clearTargetNames = () => {
if (targetAvatar) targetAvatar.style.viewTransitionName = '';
if (targetName) targetName.style.viewTransitionName = '';
};
transition.ready.then(clearTargetNames, clearTargetNames);
transition.finished.finally(() => {
root.classList.remove('user-view-transition', 'returning-user-home');
refresh();
});
return;
}
}
let targetCard = currentPostId
? Array.from(document.querySelectorAll('[data-post-id]')).find((element) => element.dataset.postId === String(currentPostId))
: null;
let targetParts = [];
document.documentElement.classList.add('home-view-transition');
if (targetCard) document.documentElement.classList.add('returning-home');
const transition = document.startViewTransition(() => {
flushSync(() => commitNavigation(false));
targetCard = currentPostId
? Array.from(document.querySelectorAll('[data-post-id]')).find((element) => element.dataset.postId === String(currentPostId))
: null;
if (targetCard) targetCard.style.viewTransitionName = 'post-expand';
targetParts = namePostTransitionParts(targetCard);
});
stageSharedGeometry(transition, {
name: 'post-expand',
duration: 1100,
moveOffset: 0.73,
moveEasing: 'cubic-bezier(0.42, 0, 0.58, 1)',
resizeEasing: 'cubic-bezier(0.16, 1, 0.3, 1)',
reverse: true,
});
stageSharedGeometry(transition, {
name: 'post-body',
duration: 1100,
moveOffset: 0.73,
moveEasing: 'cubic-bezier(0.42, 0, 0.58, 1)',
resizeEasing: 'cubic-bezier(0.16, 1, 0.3, 1)',
reverse: true,
});
const clearTargetNames = () => {
if (targetCard) targetCard.style.viewTransitionName = '';
clearPostTransitionParts(targetParts);
};
transition.ready.then(clearTargetNames, clearTargetNames);
transition.finished.finally(() => {
document.documentElement.classList.remove('home-view-transition', 'returning-home');
refresh();
});
}, [currentPostId, currentUsername, currentUserPreview, refresh, view]);
const openPost = useCallback((postId, sourceElement = null, preview = null, origin = null) => {
const url = new URL(window.location);
url.searchParams.set('post', postId);
url.searchParams.delete('page');
url.searchParams.delete('user');
const resolvedOrigin = origin || { view };
const historyOrigin = resolvedOrigin?.view === 'user'
? { view: 'user', username: resolvedOrigin.username }
: { view: resolvedOrigin?.view || 'feed' };
const commitNavigation = () => {
window.history.pushState({ page: 'post', postId, origin: historyOrigin }, '', url);
setView('post');
setCurrentPostId(postId);
setCurrentPostPreview(preview);
setCurrentPostOrigin(resolvedOrigin);
setCurrentUserPreview(null);
setCurrentUserPostsPreview(null);
};
const reduceMotion = window.matchMedia?.('(prefers-reduced-motion: reduce)').matches;
if (!sourceElement || !document.startViewTransition || reduceMotion) {
commitNavigation();
return;
}
document.documentElement.classList.remove('post-transition-settled');
sourceElement.style.viewTransitionName = 'post-expand';
const sourceParts = namePostTransitionParts(sourceElement);
document.documentElement.classList.add('post-view-transition');
const transition = document.startViewTransition(async () => {
flushSync(commitNavigation);
await waitForPostImages(document.querySelector('#pagePost .post-detail-card'));
});
stageSharedGeometry(transition, {
name: 'post-expand',
duration: 960,
moveOffset: 0.62,
moveEasing: 'cubic-bezier(0.42, 0, 0.58, 1)',
resizeEasing: 'cubic-bezier(0.42, 0, 0.58, 1)',
});
stagePostBodyGeometry(transition, {
duration: 960,
moveOffset: 0.62,
moveEasing: 'cubic-bezier(0.42, 0, 0.58, 1)',
resizeEasing: 'cubic-bezier(0.42, 0, 0.58, 1)',
});
const clearSourceNames = () => {
sourceElement.style.viewTransitionName = '';
clearPostTransitionParts(sourceParts);
};
transition.ready.then(clearSourceNames, clearSourceNames);
transition.finished.finally(() => {
const root = document.documentElement;
root.classList.remove('post-view-transition');
root.classList.add('post-transition-settled');
window.setTimeout(() => root.classList.remove('post-transition-settled'), 220);
});
}, [view]);
const returnFromPost = useCallback(() => {
if (currentPostOrigin?.view !== 'user' || !currentPostOrigin.username) {
navigate('feed');
return;
}
const url = new URL(window.location);
url.searchParams.set('user', currentPostOrigin.username);
url.searchParams.delete('page');
url.searchParams.delete('post');
const commitNavigation = () => {
window.history.pushState({ page: 'user', username: currentPostOrigin.username }, '', url);
setView('user');
setCurrentUsername(currentPostOrigin.username);
setCurrentUserPreview(currentPostOrigin.user || { username: currentPostOrigin.username });
setCurrentUserPostsPreview(currentPostOrigin.posts || []);
setCurrentPostId(null);
setCurrentPostPreview(null);
};
const reduceMotion = window.matchMedia?.('(prefers-reduced-motion: reduce)').matches;
const sourceCard = document.querySelector('#pagePost .post-detail-card');
if (!sourceCard || !document.startViewTransition || reduceMotion) {
commitNavigation();
return;
}
const root = document.documentElement;
let targetCard = null;
let targetParts = [];
root.classList.add('home-view-transition', 'returning-home', 'returning-user-post');
const transition = document.startViewTransition(() => {
flushSync(commitNavigation);
const userPage = document.getElementById('pageUser');
if (userPage) userPage.scrollTop = currentPostOrigin.scrollTop || 0;
targetCard = Array.from(document.querySelectorAll('#userProfileContent [data-post-id]')).find(
(element) => element.dataset.postId === String(currentPostId)
) || null;
if (targetCard) targetCard.style.viewTransitionName = 'post-expand';
targetParts = namePostTransitionParts(targetCard);
});
stageSharedGeometry(transition, {
name: 'post-expand',
duration: 1100,
moveOffset: 0.73,
moveEasing: 'cubic-bezier(0.42, 0, 0.58, 1)',
resizeEasing: 'cubic-bezier(0.16, 1, 0.3, 1)',
reverse: true,
});
stageSharedGeometry(transition, {
name: 'post-body',
duration: 1100,
moveOffset: 0.73,
moveEasing: 'cubic-bezier(0.42, 0, 0.58, 1)',
resizeEasing: 'cubic-bezier(0.16, 1, 0.3, 1)',
reverse: true,
});
const clearTargetNames = () => {
if (targetCard) targetCard.style.viewTransitionName = '';
clearPostTransitionParts(targetParts);
};
transition.ready.then(clearTargetNames, clearTargetNames);
transition.finished.finally(() => {
root.classList.remove('home-view-transition', 'returning-home', 'returning-user-post');
setCurrentPostOrigin(null);
});
}, [currentPostId, currentPostOrigin, navigate]);
const openUser = useCallback((username, source = null, preview = null) => {
const url = new URL(window.location);
url.searchParams.set('user', username);
url.searchParams.delete('page');
url.searchParams.delete('post');
const commitNavigation = () => {
window.history.pushState({ page: 'user', username }, '', url);
setView('user');
setCurrentUsername(username);
setCurrentUserPreview(preview || { username });
setCurrentPostId(null);
setCurrentPostPreview(null);
setCurrentPostOrigin(null);
setCurrentUserPostsPreview(null);
};
const avatarElement = source?.avatarElement;
const nameElement = source?.nameElement;
const reduceMotion = window.matchMedia?.('(prefers-reduced-motion: reduce)').matches;
if (!avatarElement || !nameElement || !document.startViewTransition || reduceMotion) {
commitNavigation();
return;
}
const root = document.documentElement;
root.classList.remove('user-transition-settled');
root.classList.add('user-view-transition');
avatarElement.style.viewTransitionName = 'user-avatar';
nameElement.style.viewTransitionName = 'user-name';
const transition = document.startViewTransition(() => {
flushSync(commitNavigation);
});
const clearSourceNames = () => {
avatarElement.style.viewTransitionName = '';
nameElement.style.viewTransitionName = '';
};
transition.ready.then(clearSourceNames, clearSourceNames);
transition.finished.finally(() => {
root.classList.remove('user-view-transition');
root.classList.add('user-transition-settled');
window.setTimeout(() => root.classList.remove('user-transition-settled'), 320);
});
}, []);
const openCustomPage = useCallback((pageName) => {
const url = new URL(window.location);
url.searchParams.set('page', pageName);
url.searchParams.delete('post');
url.searchParams.delete('user');
window.history.pushState({ page: 'custom', pageName }, '', url);
setView('custom');
setCurrentPageName(pageName);
setCurrentUserPreview(null);
}, []);
// 浏览器前进后退
useEffect(() => {
const onPop = (e) => {
const state = e.state || {};
if (state.postId) {
setView('post');
setCurrentPostId(state.postId);
setCurrentPostPreview(null);
setCurrentPostOrigin(state.origin || null);
} else if (state.username) {
setView('user');
setCurrentUsername(state.username);
setCurrentUserPreview(null);
setCurrentUserPostsPreview(null);
} else if (state.pageName) {
setView('custom');
setCurrentPageName(state.pageName);
} else {
setView(state.page || 'feed');
setCurrentPostId(null);
setCurrentPostPreview(null);
setCurrentPostOrigin(null);
setCurrentUsername(null);
setCurrentUserPreview(null);
setCurrentUserPostsPreview(null);
setCurrentPageName(null);
}
};
window.addEventListener('popstate', onPop);
return () => window.removeEventListener('popstate', onPop);
}, []);
const login = useCallback(async (email, password) => {
const result = await API.login(email, password);
if (result.user) {
setCurrentUser(result.user);
}
return result;
}, []);
const register = useCallback(async (email, password, username, captcha) => {
const result = await API.register(email, password, username, captcha);
const loginResult = await API.login(email, password);
if (loginResult.user) {
setCurrentUser(loginResult.user);
}
return result;
}, []);
const logout = useCallback(() => {
API.logout();
setCurrentUser(null);
navigate('feed');
}, [navigate]);
const updateForumName = useCallback((name) => {
setForumName(name);
localStorage.setItem('forumlify-forum-name', name);
syncForumNameCookie(name);
}, [syncForumNameCookie]);
const value = {
currentUser, setCurrentUser,
forumName, forumNameLoaded, updateForumName,
theme, toggleTheme,
view, navigate,
currentPostId, currentPostPreview, currentPostOrigin, openPost, returnFromPost,
currentUsername, currentUserPreview, currentUserPostsPreview, openUser,
currentPageName, openCustomPage,
sort, setSort,
refreshKey, refresh,
ready,
login, register, logout,
};
return <AppContext.Provider value={value}>{children}</AppContext.Provider>;
}