feat: add path navigation and shared settings transitions
This commit is contained in:
1 parent
caad17d195
commit
84c9e97451
25 files changed
+689
-151
No files matched your search
+274
-79
@@ -13,7 +13,7 @@ const POST_TRANSITION_PARTS = [
|
||||
['.post-title', 'post-heading'],
|
||||
['.post-time', 'post-time'],
|
||||
['.post-content', 'post-body'],
|
||||
['.post-images', 'post-feed-media'],
|
||||
['.post-images', 'post-media'],
|
||||
['.post-pin-state', 'post-pin-state'],
|
||||
['.post-edited-label', 'post-edited-label'],
|
||||
['.post-actions', 'post-feed-actions'],
|
||||
@@ -35,6 +35,143 @@ function clearPostTransitionParts(elements) {
|
||||
});
|
||||
}
|
||||
|
||||
const SETTINGS_AVATAR_SELECTOR = [
|
||||
'.post-avatar',
|
||||
'.reply-avatar',
|
||||
'.user-profile-avatar',
|
||||
'.admin-user-avatar',
|
||||
].join(', ');
|
||||
|
||||
const SETTINGS_NAME_SELECTOR = [
|
||||
'.post-username',
|
||||
'.reply-username',
|
||||
'.user-profile-name',
|
||||
'.admin-user-name',
|
||||
].join(', ');
|
||||
|
||||
function visibleAreaRatio(element) {
|
||||
if (!element?.isConnected) return 0;
|
||||
const style = window.getComputedStyle(element);
|
||||
if (style.display === 'none' || style.visibility === 'hidden' || Number(style.opacity) === 0) return 0;
|
||||
|
||||
const bounds = element.getBoundingClientRect();
|
||||
const totalArea = bounds.width * bounds.height;
|
||||
if (totalArea <= 0) return 0;
|
||||
|
||||
let left = Math.max(0, bounds.left);
|
||||
let top = Math.max(0, bounds.top);
|
||||
let right = Math.min(window.innerWidth, bounds.right);
|
||||
let bottom = Math.min(window.innerHeight, bounds.bottom);
|
||||
|
||||
// Account for scroll/clip containers, but deliberately do not treat the
|
||||
// translucent navbar as an occluder: content underneath it is still visible.
|
||||
for (let parent = element.parentElement; parent && parent !== document.body; parent = parent.parentElement) {
|
||||
const parentStyle = window.getComputedStyle(parent);
|
||||
const clipsX = ['auto', 'scroll', 'hidden', 'clip'].includes(parentStyle.overflowX);
|
||||
const clipsY = ['auto', 'scroll', 'hidden', 'clip'].includes(parentStyle.overflowY);
|
||||
if (!clipsX && !clipsY) continue;
|
||||
const parentBounds = parent.getBoundingClientRect();
|
||||
if (clipsX) {
|
||||
left = Math.max(left, parentBounds.left);
|
||||
right = Math.min(right, parentBounds.right);
|
||||
}
|
||||
if (clipsY) {
|
||||
top = Math.max(top, parentBounds.top);
|
||||
bottom = Math.min(bottom, parentBounds.bottom);
|
||||
}
|
||||
}
|
||||
|
||||
return Math.max(0, right - left) * Math.max(0, bottom - top) / totalArea;
|
||||
}
|
||||
|
||||
function randomElement(elements) {
|
||||
return elements[Math.floor(Math.random() * elements.length)] || null;
|
||||
}
|
||||
|
||||
function findSettingsTransitionSource(username) {
|
||||
const groups = Array.from(document.querySelectorAll('[data-username]')).filter(
|
||||
(element) => !element.closest('#navbar') && element.dataset.username === String(username || '')
|
||||
);
|
||||
const candidates = groups.map((group) => {
|
||||
const avatarElement = group.querySelector(SETTINGS_AVATAR_SELECTOR);
|
||||
const nameElement = group.querySelector(SETTINGS_NAME_SELECTOR);
|
||||
return {
|
||||
avatarElement: visibleAreaRatio(avatarElement) >= 0.5 ? avatarElement : null,
|
||||
nameElement: visibleAreaRatio(nameElement) >= 0.5 ? nameElement : null,
|
||||
};
|
||||
});
|
||||
const completeCandidates = candidates.filter(({ avatarElement, nameElement }) => avatarElement && nameElement);
|
||||
if (completeCandidates.length > 0) return randomElement(completeCandidates);
|
||||
|
||||
return randomElement(candidates.filter(({ avatarElement, nameElement }) => avatarElement || nameElement)) || {
|
||||
avatarElement: null,
|
||||
nameElement: null,
|
||||
};
|
||||
}
|
||||
|
||||
const BUILT_IN_PAGES = new Set(['messages', 'settings', 'admin', 'new']);
|
||||
|
||||
function cleanPathSegment(value) {
|
||||
return encodeURIComponent(String(value || '').trim());
|
||||
}
|
||||
|
||||
function pathForPage(page) {
|
||||
return page === 'feed' ? '/' : `/${cleanPathSegment(page)}`;
|
||||
}
|
||||
|
||||
function pathForUser(username) {
|
||||
return `/user/${cleanPathSegment(username)}`;
|
||||
}
|
||||
|
||||
function pathForPost(postRef) {
|
||||
return `/post/${cleanPathSegment(postRef)}`;
|
||||
}
|
||||
|
||||
function pathForCustomPage(pageName) {
|
||||
return `/${cleanPathSegment(pageName)}`;
|
||||
}
|
||||
|
||||
function routeFromLocation(location) {
|
||||
const parts = location.pathname.split('/').filter(Boolean).map((part) => {
|
||||
try { return decodeURIComponent(part); } catch { return part; }
|
||||
});
|
||||
const legacy = new URLSearchParams(location.search);
|
||||
const legacyPost = legacy.get('post');
|
||||
const legacyUser = legacy.get('user');
|
||||
const legacyPage = legacy.get('page');
|
||||
|
||||
if (parts[0] === 'post' && parts.length === 2) return { page: 'post', postId: parts[1] };
|
||||
if (parts[0] === 'user' && parts.length === 2) return { page: 'user', username: parts[1] };
|
||||
if (parts.length === 1 && BUILT_IN_PAGES.has(parts[0])) return { page: parts[0] };
|
||||
if (parts.length === 1) return { page: 'custom', pageName: parts[0] };
|
||||
|
||||
// Compatibility for links created before path-based navigation.
|
||||
if (legacyPost) return { page: 'post', postId: legacyPost, legacy: true };
|
||||
if (legacyUser) return { page: 'user', username: legacyUser, legacy: true };
|
||||
if (legacyPage && BUILT_IN_PAGES.has(legacyPage)) return { page: legacyPage, legacy: true };
|
||||
if (legacyPage) return { page: 'custom', pageName: legacyPage, legacy: true };
|
||||
return { page: 'feed' };
|
||||
}
|
||||
|
||||
function pathForRoute(route) {
|
||||
if (route.page === 'post') return pathForPost(route.postId);
|
||||
if (route.page === 'user') return pathForUser(route.username);
|
||||
if (route.page === 'custom') return pathForCustomPage(route.pageName);
|
||||
return pathForPage(route.page);
|
||||
}
|
||||
|
||||
function clearLegacyPageParams(url) {
|
||||
url.searchParams.delete('page');
|
||||
url.searchParams.delete('post');
|
||||
url.searchParams.delete('user');
|
||||
return url;
|
||||
}
|
||||
|
||||
function postCardMatches(element, postRef) {
|
||||
const ref = String(postRef || '');
|
||||
return element?.dataset.postRef === ref || element?.dataset.postId === ref;
|
||||
}
|
||||
|
||||
function waitForPostImages(container) {
|
||||
const images = Array.from(container?.querySelectorAll('img') || []);
|
||||
if (images.length === 0) return Promise.resolve();
|
||||
@@ -320,39 +457,32 @@ export default function AppProvider({ children, cachedName = '' }) {
|
||||
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);
|
||||
}
|
||||
const applyRoute = useCallback((route, state = {}) => {
|
||||
setView(route.page);
|
||||
setCurrentPostId(route.page === 'post' ? route.postId : null);
|
||||
setCurrentPostPreview(null);
|
||||
setCurrentPostOrigin(route.page === 'post' ? state.origin || null : null);
|
||||
setCurrentUsername(route.page === 'user' ? route.username : null);
|
||||
setCurrentUserPreview(null);
|
||||
setCurrentUserPostsPreview(null);
|
||||
setCurrentPageName(route.page === 'custom' ? route.pageName : null);
|
||||
}, []);
|
||||
|
||||
// Initialize from path routes and normalize legacy query-based links.
|
||||
useEffect(() => {
|
||||
const route = routeFromLocation(window.location);
|
||||
applyRoute(route, window.history.state || {});
|
||||
if (route.legacy) {
|
||||
const normalized = clearLegacyPageParams(new URL(window.location));
|
||||
normalized.pathname = pathForRoute(route);
|
||||
window.history.replaceState({ ...window.history.state, ...route }, '', normalized);
|
||||
}
|
||||
}, [applyRoute]);
|
||||
|
||||
// 视图切换:同步 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 url = clearLegacyPageParams(new URL(window.location));
|
||||
url.pathname = pathForPage(page);
|
||||
const commitNavigation = (refreshFeed = true) => {
|
||||
window.history.pushState({ page }, '', url);
|
||||
setView(page);
|
||||
@@ -366,6 +496,100 @@ export default function AppProvider({ children, cachedName = '' }) {
|
||||
};
|
||||
|
||||
const reduceMotion = window.matchMedia?.('(prefers-reduced-motion: reduce)').matches;
|
||||
if (page === 'settings' && view !== 'settings' && document.startViewTransition && !reduceMotion) {
|
||||
const root = document.documentElement;
|
||||
const visibleSource = findSettingsTransitionSource(currentUser?.username);
|
||||
const navbarAvatar = document.querySelector('#navbar #userDropdown .avatar');
|
||||
const sourceAvatar = visibleSource.avatarElement || navbarAvatar;
|
||||
const sourceName = visibleSource.nameElement;
|
||||
const avatarFromNavbar = sourceAvatar === navbarAvatar;
|
||||
|
||||
if (!sourceAvatar) {
|
||||
commitNavigation();
|
||||
return;
|
||||
}
|
||||
|
||||
root.classList.add('settings-view-transition');
|
||||
if (avatarFromNavbar) root.classList.add('settings-avatar-from-navbar');
|
||||
if (!sourceName) root.classList.add('settings-name-fade-only');
|
||||
sourceAvatar.style.viewTransitionName = 'settings-avatar';
|
||||
if (sourceName) sourceName.style.viewTransitionName = 'settings-user-name';
|
||||
|
||||
const transition = document.startViewTransition(() => {
|
||||
sourceAvatar.style.viewTransitionName = '';
|
||||
if (sourceName) sourceName.style.viewTransitionName = '';
|
||||
if (avatarFromNavbar) root.classList.add('settings-navbar-avatar-hidden');
|
||||
flushSync(() => commitNavigation(false));
|
||||
});
|
||||
transition.finished.finally(() => {
|
||||
root.classList.remove(
|
||||
'settings-view-transition',
|
||||
'settings-avatar-from-navbar',
|
||||
'settings-name-fade-only',
|
||||
'settings-navbar-avatar-hidden'
|
||||
);
|
||||
if (avatarFromNavbar) {
|
||||
root.classList.add('settings-navbar-avatar-reveal');
|
||||
window.setTimeout(() => root.classList.remove('settings-navbar-avatar-reveal'), 360);
|
||||
}
|
||||
refresh();
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (page === 'feed' && view === 'settings' && document.startViewTransition && !reduceMotion) {
|
||||
const root = document.documentElement;
|
||||
const sourceAvatar = document.querySelector('#pageSettings .settings-profile-avatar');
|
||||
const sourceName = document.querySelector('#pageSettings .settings-profile-name');
|
||||
|
||||
if (!sourceAvatar) {
|
||||
commitNavigation();
|
||||
return;
|
||||
}
|
||||
|
||||
let targetAvatar = null;
|
||||
let targetName = null;
|
||||
root.classList.add(
|
||||
'settings-view-transition',
|
||||
'returning-settings-home',
|
||||
'settings-navbar-avatar-hidden'
|
||||
);
|
||||
|
||||
const transition = document.startViewTransition(() => {
|
||||
flushSync(() => commitNavigation(false));
|
||||
const visibleTarget = findSettingsTransitionSource(currentUser?.username);
|
||||
const navbarAvatar = document.querySelector('#navbar #userDropdown .avatar');
|
||||
targetAvatar = visibleTarget.avatarElement || navbarAvatar;
|
||||
targetName = visibleTarget.nameElement;
|
||||
|
||||
// Hide the navbar avatar only in the old snapshot. The new snapshot
|
||||
// must expose it as the shared target when no visible feed avatar exists.
|
||||
root.classList.remove('settings-navbar-avatar-hidden');
|
||||
|
||||
if (targetAvatar) targetAvatar.style.viewTransitionName = 'settings-avatar';
|
||||
if (targetName) {
|
||||
targetName.style.viewTransitionName = 'settings-user-name';
|
||||
} else {
|
||||
root.classList.add('settings-name-return-fade-only');
|
||||
}
|
||||
});
|
||||
const clearTargetNames = () => {
|
||||
if (targetAvatar) targetAvatar.style.viewTransitionName = '';
|
||||
if (targetName) targetName.style.viewTransitionName = '';
|
||||
};
|
||||
transition.ready.then(clearTargetNames, clearTargetNames);
|
||||
transition.finished.finally(() => {
|
||||
root.classList.remove(
|
||||
'settings-view-transition',
|
||||
'returning-settings-home',
|
||||
'settings-name-return-fade-only',
|
||||
'settings-navbar-avatar-hidden'
|
||||
);
|
||||
refresh();
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (page !== 'feed' || view === 'feed' || !document.startViewTransition || reduceMotion) {
|
||||
commitNavigation();
|
||||
return;
|
||||
@@ -377,8 +601,8 @@ export default function AppProvider({ children, cachedName = '' }) {
|
||||
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)
|
||||
const exact = Array.from(document.querySelectorAll('[data-post-ref], [data-post-id]')).find(
|
||||
(element) => postCardMatches(element, currentUserPreview.sourcePostId)
|
||||
);
|
||||
if (exact) return exact;
|
||||
}
|
||||
@@ -414,7 +638,7 @@ export default function AppProvider({ children, cachedName = '' }) {
|
||||
}
|
||||
|
||||
let targetCard = currentPostId
|
||||
? Array.from(document.querySelectorAll('[data-post-id]')).find((element) => element.dataset.postId === String(currentPostId))
|
||||
? Array.from(document.querySelectorAll('[data-post-ref], [data-post-id]')).find((element) => postCardMatches(element, currentPostId))
|
||||
: null;
|
||||
let targetParts = [];
|
||||
document.documentElement.classList.add('home-view-transition');
|
||||
@@ -423,7 +647,7 @@ export default function AppProvider({ children, cachedName = '' }) {
|
||||
const transition = document.startViewTransition(() => {
|
||||
flushSync(() => commitNavigation(false));
|
||||
targetCard = currentPostId
|
||||
? Array.from(document.querySelectorAll('[data-post-id]')).find((element) => element.dataset.postId === String(currentPostId))
|
||||
? Array.from(document.querySelectorAll('[data-post-ref], [data-post-id]')).find((element) => postCardMatches(element, currentPostId))
|
||||
: null;
|
||||
if (targetCard) targetCard.style.viewTransitionName = 'post-expand';
|
||||
targetParts = namePostTransitionParts(targetCard);
|
||||
@@ -453,21 +677,20 @@ export default function AppProvider({ children, cachedName = '' }) {
|
||||
document.documentElement.classList.remove('home-view-transition', 'returning-home');
|
||||
refresh();
|
||||
});
|
||||
}, [currentPostId, currentUsername, currentUserPreview, refresh, view]);
|
||||
}, [currentPostId, currentUser, 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 postRef = preview?.post_number || postId;
|
||||
const url = clearLegacyPageParams(new URL(window.location));
|
||||
url.pathname = pathForPost(postRef);
|
||||
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);
|
||||
window.history.pushState({ page: 'post', postId: postRef, origin: historyOrigin }, '', url);
|
||||
setView('post');
|
||||
setCurrentPostId(postId);
|
||||
setCurrentPostId(postRef);
|
||||
setCurrentPostPreview(preview);
|
||||
setCurrentPostOrigin(resolvedOrigin);
|
||||
setCurrentUserPreview(null);
|
||||
@@ -520,10 +743,8 @@ export default function AppProvider({ children, cachedName = '' }) {
|
||||
return;
|
||||
}
|
||||
|
||||
const url = new URL(window.location);
|
||||
url.searchParams.set('user', currentPostOrigin.username);
|
||||
url.searchParams.delete('page');
|
||||
url.searchParams.delete('post');
|
||||
const url = clearLegacyPageParams(new URL(window.location));
|
||||
url.pathname = pathForUser(currentPostOrigin.username);
|
||||
const commitNavigation = () => {
|
||||
window.history.pushState({ page: 'user', username: currentPostOrigin.username }, '', url);
|
||||
setView('user');
|
||||
@@ -549,8 +770,8 @@ export default function AppProvider({ children, cachedName = '' }) {
|
||||
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)
|
||||
targetCard = Array.from(document.querySelectorAll('#userProfileContent [data-post-ref], #userProfileContent [data-post-id]')).find(
|
||||
(element) => postCardMatches(element, currentPostId)
|
||||
) || null;
|
||||
if (targetCard) targetCard.style.viewTransitionName = 'post-expand';
|
||||
targetParts = namePostTransitionParts(targetCard);
|
||||
@@ -583,10 +804,8 @@ export default function AppProvider({ children, cachedName = '' }) {
|
||||
}, [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 url = clearLegacyPageParams(new URL(window.location));
|
||||
url.pathname = pathForUser(username);
|
||||
const commitNavigation = () => {
|
||||
window.history.pushState({ page: 'user', username }, '', url);
|
||||
setView('user');
|
||||
@@ -628,10 +847,8 @@ export default function AppProvider({ children, cachedName = '' }) {
|
||||
}, []);
|
||||
|
||||
const openCustomPage = useCallback((pageName) => {
|
||||
const url = new URL(window.location);
|
||||
url.searchParams.set('page', pageName);
|
||||
url.searchParams.delete('post');
|
||||
url.searchParams.delete('user');
|
||||
const url = clearLegacyPageParams(new URL(window.location));
|
||||
url.pathname = pathForCustomPage(pageName);
|
||||
window.history.pushState({ page: 'custom', pageName }, '', url);
|
||||
setView('custom');
|
||||
setCurrentPageName(pageName);
|
||||
@@ -642,33 +859,11 @@ export default function AppProvider({ children, cachedName = '' }) {
|
||||
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);
|
||||
}
|
||||
applyRoute(routeFromLocation(window.location), state);
|
||||
};
|
||||
window.addEventListener('popstate', onPop);
|
||||
return () => window.removeEventListener('popstate', onPop);
|
||||
}, []);
|
||||
}, [applyRoute]);
|
||||
|
||||
const login = useCallback(async (email, password) => {
|
||||
const result = await API.login(email, password);
|
||||
|
||||
+2
-2
@@ -163,8 +163,8 @@ export default function Feed({ onOpenModal, onReport }) {
|
||||
posts.map((p) => {
|
||||
const time = p.created_at ? new Date(p.created_at).toLocaleString('zh-CN') : '';
|
||||
return (
|
||||
<div key={p.id} className="post-card" data-post-id={p.id} data-username={p.username || ''} style={{ cursor: 'pointer' }}
|
||||
onClick={(e) => openPost(p.id, e.currentTarget, p)}>
|
||||
<div key={p.id} className="post-card" data-post-id={p.id} data-post-ref={p.post_number || p.id} data-username={p.username || ''} style={{ cursor: 'pointer' }}
|
||||
onClick={(e) => openPost(p.post_number || p.id, e.currentTarget, p)}>
|
||||
{p.is_pinned && (
|
||||
<div className="post-pin-state" style={{ width: 'fit-content', display: 'flex', alignItems: 'center', gap: 3, fontSize: 12, color: 'var(--primary)', fontWeight: 600, marginBottom: 4 }}><Icon name="pin" size={12} /> {t('feed.pinned')}</div>
|
||||
)}
|
||||
|
||||
@@ -63,7 +63,7 @@ export default function Navbar({ onOpenModal }) {
|
||||
{customPages.map((p) => (
|
||||
<a
|
||||
key={p.id}
|
||||
href="#"
|
||||
href={'/' + encodeURIComponent(p.name)}
|
||||
className="custom-page-nav-link"
|
||||
data-custom={p.name}
|
||||
onClick={(e) => { e.preventDefault(); openCustomPage(p.name); }}
|
||||
|
||||
@@ -52,9 +52,23 @@ export default function PostDetail({ postId, initialPost = null }) {
|
||||
|
||||
const initialRefreshKey = useRef(refreshKey);
|
||||
useEffect(() => {
|
||||
if (initialPost?.id === postId && refreshKey === initialRefreshKey.current) return;
|
||||
const initialReference = initialPost?.post_number || initialPost?.id;
|
||||
if (String(initialReference || '') === String(postId) && refreshKey === initialRefreshKey.current) return;
|
||||
load();
|
||||
}, [initialPost?.id, load, postId, refreshKey]);
|
||||
}, [initialPost?.id, initialPost?.post_number, load, postId, refreshKey]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!post?.post_number || !window.location.pathname.startsWith('/post/')) return;
|
||||
const canonicalPath = `/post/${post.post_number}`;
|
||||
if (window.location.pathname === canonicalPath) return;
|
||||
const url = new URL(window.location);
|
||||
url.pathname = canonicalPath;
|
||||
window.history.replaceState(
|
||||
{ ...window.history.state, page: 'post', postId: String(post.post_number) },
|
||||
'',
|
||||
url
|
||||
);
|
||||
}, [post?.post_number]);
|
||||
const renderedContent = useMemo(() => renderMarkdown(post?.content), [post?.content]);
|
||||
const renderedSignature = useMemo(() => renderMarkdown(post?.signature), [post?.signature]);
|
||||
|
||||
@@ -161,7 +175,7 @@ export default function PostDetail({ postId, initialPost = null }) {
|
||||
<div className="post-page-layout">
|
||||
<Sidebar id="postSidebar" />
|
||||
<div id="postDetailContent" className="post-page-main">
|
||||
<div className="post-detail-card">
|
||||
<div className="post-detail-card" data-username={post.username || ''}>
|
||||
<div className="post-header">
|
||||
<img
|
||||
src={post.avatar_url || avatar(post.username)}
|
||||
|
||||
@@ -46,7 +46,7 @@ export default function SettingsPage() {
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="page-slide active" style={{ overflow: 'hidden', height: '100vh', display: 'flex', flexDirection: 'column' }}>
|
||||
<div id="pageSettings" className="page-slide active" style={{ overflow: 'hidden', height: '100vh', display: 'flex', flexDirection: 'column' }}>
|
||||
<div className="page-header" style={{ width: '100%', flexShrink: 0 }}>
|
||||
<h2><Icon name="settings" size={20} /> {t('settings.title')}</h2>
|
||||
</div>
|
||||
|
||||
@@ -56,7 +56,7 @@ export default function SettingsProfile() {
|
||||
{/* 头像 */}
|
||||
<div style={{ textAlign: 'center', marginBottom: 24 }}>
|
||||
<div style={{ position: 'relative', display: 'inline-block' }}>
|
||||
<img src={avatarSrc} style={{ width: 100, height: 100, borderRadius: '50%', objectFit: 'cover', border: '3px solid var(--primary)' }} alt="" />
|
||||
<img className="settings-profile-avatar" src={avatarSrc} style={{ width: 100, height: 100, borderRadius: '50%', objectFit: 'cover', border: '3px solid var(--primary)' }} alt="" />
|
||||
<button
|
||||
style={{ position: 'absolute', bottom: 0, right: 0, background: 'var(--primary)', color: '#fff', border: 'none', borderRadius: '50%', width: 32, height: 32, cursor: 'pointer', fontSize: 16, display: 'flex', alignItems: 'center', justifyContent: 'center', boxShadow: '0 2px 8px rgba(99,102,241,0.4)' }}
|
||||
onClick={() => avatarInputRef.current?.click()}
|
||||
@@ -67,7 +67,7 @@ export default function SettingsProfile() {
|
||||
</button>
|
||||
</div>
|
||||
<input type="file" ref={avatarInputRef} accept="image/*" style={{ display: 'none' }} onChange={(e) => { if (e.target.files?.[0]) handleAvatar(e.target.files[0]); }} />
|
||||
<h2 style={{ margin: '12px 0 4px' }}>{currentUser?.username}</h2>
|
||||
<h2 className="settings-profile-name" style={{ margin: '12px 0 4px', fontSize: 14, fontWeight: 600, color: 'var(--primary)' }}>{currentUser?.username}</h2>
|
||||
<p style={{ color: 'var(--text-secondary)', fontSize: 14 }}>{bio || t('user.bioEmpty')}</p>
|
||||
{avatarStatus && (
|
||||
<div style={{ fontSize: 13, marginTop: 8, color: avatarStatus.ok ? '#22c55e' : '#ef4444', display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 5 }}><Icon name={avatarStatus.ok ? 'success' : 'error'} size={14} /> {avatarStatus.msg}</div>
|
||||
|
||||
@@ -69,7 +69,7 @@ export default function UserProfile({ username, initialUser = null, initialPosts
|
||||
<h2><Icon name="users" size={20} /> 用户主页</h2>
|
||||
</div>
|
||||
<div id="userProfileContent" style={{ maxWidth: 700, margin: '0 auto', width: '100%' }}>
|
||||
<div className="user-profile-card" style={{ background: 'var(--surface)', border: '1px solid var(--border)', borderRadius: 8, padding: 32, textAlign: 'center' }}>
|
||||
<div className="user-profile-card" data-username={user.username || ''} style={{ background: 'var(--surface)', border: '1px solid var(--border)', borderRadius: 8, padding: 32, textAlign: 'center' }}>
|
||||
<img className="user-profile-avatar" src={user.avatar_url || avatar(user.username)} style={{ width: 96, height: 96, borderRadius: '50%', objectFit: 'cover', border: '3px solid var(--primary)' }} alt={user.username || ''} />
|
||||
<h2 className="user-profile-name" style={{ margin: '16px 0 4px', fontSize: 24, color: 'var(--text)' }}>{user.username}</h2>
|
||||
{user.id && (
|
||||
@@ -105,9 +105,10 @@ export default function UserProfile({ username, initialUser = null, initialPosts
|
||||
key={p.id}
|
||||
className="post-card user-post-card"
|
||||
data-post-id={p.id}
|
||||
data-post-ref={p.post_number || p.id}
|
||||
data-username={user.username || ''}
|
||||
style={{ cursor: 'pointer' }}
|
||||
onClick={(event) => openPost(p.id, event.currentTarget, p, {
|
||||
onClick={(event) => openPost(p.post_number || p.id, event.currentTarget, p, {
|
||||
view: 'user',
|
||||
username: user.username,
|
||||
user,
|
||||
|
||||
@@ -56,15 +56,16 @@ export default function AdminUsers() {
|
||||
const isAdmin = u.role === 'admin';
|
||||
const isCurrent = currentUser && currentUser.id === u.id;
|
||||
return (
|
||||
<tr key={u.id} style={{ borderBottom: '1px solid #f1f5f9' }}>
|
||||
<tr key={u.id} data-username={u.username || ''} style={{ borderBottom: '1px solid #f1f5f9' }}>
|
||||
<td style={{ padding: '10px 12px' }}>
|
||||
<div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
|
||||
<img
|
||||
src={u.avatar_url || 'https://ui-avatars.com/api/?name=' + encodeURIComponent(u.username) + '&background=6366f1&color=fff&size=64'}
|
||||
className="admin-user-avatar"
|
||||
style={{ width: 28, height: 28, borderRadius: '50%', objectFit: 'cover' }}
|
||||
alt=""
|
||||
/>
|
||||
<span style={{ fontWeight: 500 }}>{u.username}</span>
|
||||
<span className="admin-user-name" style={{ fontWeight: 500 }}>{u.username}</span>
|
||||
{isCurrent && <span style={{ fontSize: 11, color: '#94a3b8', background: '#eef2ff', padding: '1px 8px', borderRadius: 4 }}>{t('admin.user.you')}</span>}
|
||||
</div>
|
||||
</td>
|
||||
|
||||
Reference in new issue
Block a user