feat: animate user profile transitions

This commit is contained in:
lezi-fun committed 2026-08-12 10:24:20 +08:00
1 parent 8e67e82eb0
commit bbf3262aff
6 files changed
+467 -63

No files matched your search

+2 -2
View File
@@ -18,7 +18,7 @@ import Modals from '@/components/Modals';
import LoadingScreen from '@/components/LoadingScreen'; import LoadingScreen from '@/components/LoadingScreen';
function HomeInner() { function HomeInner() {
const { view, currentPostId, currentPostPreview, currentUsername, currentPageName, ready, forumName, forumNameLoaded } = useApp(); const { view, currentPostId, currentPostPreview, currentUsername, currentUserPreview, currentPageName, ready, forumName, forumNameLoaded } = useApp();
const [modal, setModal] = useState(null); // null | login | register | report const [modal, setModal] = useState(null); // null | login | register | report
const [reportPostId, setReportPostId] = useState(null); const [reportPostId, setReportPostId] = useState(null);
@@ -42,7 +42,7 @@ function HomeInner() {
{/* 独立页面 */} {/* 独立页面 */}
<div style={{ display: view !== 'feed' ? 'block' : 'none' }}> <div style={{ display: view !== 'feed' ? 'block' : 'none' }}>
{view === 'post' && currentPostId && <PostDetail postId={currentPostId} initialPost={currentPostPreview} />} {view === 'post' && currentPostId && <PostDetail postId={currentPostId} initialPost={currentPostPreview} />}
{view === 'user' && currentUsername && <UserProfile username={currentUsername} />} {view === 'user' && currentUsername && <UserProfile username={currentUsername} initialUser={currentUserPreview} />}
{view === 'custom' && currentPageName && <CustomPage pageName={currentPageName} />} {view === 'custom' && currentPageName && <CustomPage pageName={currentPageName} />}
{view === 'new' && <NewPost />} {view === 'new' && <NewPost />}
{view === 'admin' && <AdminPage />} {view === 'admin' && <AdminPage />}
+280 -10
View File
@@ -889,10 +889,217 @@ svg {
animation: slideIn 0.35s cubic-bezier(0.22, 1, 0.36, 1); animation: slideIn 0.35s cubic-bezier(0.22, 1, 0.36, 1);
} }
#pagePost.page-slide.active { #pagePost.page-slide.active,
#pageUser.page-slide.active {
animation: none; animation: none;
} }
#pageUser {
overflow-anchor: none;
}
html.user-view-transition #navbar {
view-transition-name: persistent-navbar;
}
html.user-view-transition #pageUser .user-profile-avatar {
view-transition-name: user-avatar;
}
html.user-view-transition #pageUser .user-profile-name {
view-transition-name: user-name;
}
.user-profile-name {
width: fit-content;
max-width: 100%;
margin-left: auto !important;
margin-right: auto !important;
overflow-wrap: anywhere;
}
html.user-view-transition:not(.returning-user-home) #pageUser .page-header,
html.user-view-transition:not(.returning-user-home) #pageUser .user-profile-details,
html.user-view-transition:not(.returning-user-home) #pageUser .user-profile-posts {
animation: none;
opacity: 0;
}
html.user-view-transition::view-transition {
background: linear-gradient(145deg, var(--bg-start) 0%, var(--bg-end) 100%);
pointer-events: none;
}
html.user-view-transition::view-transition-group(user-avatar) {
z-index: 20;
animation-duration: 0.84s;
animation-timing-function: cubic-bezier(0.16, 1, 0.3, 1);
}
html.user-view-transition::view-transition-image-pair(user-avatar),
html.user-view-transition::view-transition-old(user-avatar),
html.user-view-transition::view-transition-new(user-avatar) {
border-radius: 50%;
overflow: clip;
mix-blend-mode: normal;
}
html.user-view-transition::view-transition-old(user-avatar) {
animation: userAvatarSourceOut 0.42s 0.18s ease-out both;
}
html.user-view-transition::view-transition-new(user-avatar) {
animation: userAvatarTargetIn 0.48s 0.16s ease-out both;
}
html.user-view-transition::view-transition-group(user-name) {
z-index: 20;
animation-duration: 0.84s;
animation-timing-function: cubic-bezier(0.16, 1, 0.3, 1);
}
html.user-view-transition::view-transition-old(user-name),
html.user-view-transition::view-transition-new(user-name) {
height: 100%;
width: 100%;
mix-blend-mode: normal;
}
html.user-view-transition::view-transition-old(user-name) {
animation: userNameColorShift 0.84s cubic-bezier(0.16, 1, 0.3, 1) both;
}
html.user-view-transition::view-transition-new(user-name) {
animation: none;
opacity: 0;
}
html.user-view-transition.returning-user-home::view-transition-old(user-name) {
animation: none;
opacity: 0;
}
html.user-view-transition.returning-user-home::view-transition-new(user-name) {
animation: userNameColorRestore 0.84s cubic-bezier(0.16, 1, 0.3, 1) both;
opacity: 1;
}
[data-theme="light"] {
--user-name-target-filter: brightness(0) saturate(0);
}
[data-theme="dark"] {
--user-name-target-filter: brightness(2.8) saturate(0);
}
html.user-view-transition::view-transition-old(root) {
animation: userFeedFadeOut 0.34s 0.2s ease-out both;
}
html.user-view-transition::view-transition-new(root) {
animation: userPageShellReveal 0.28s 0.5s ease-out both;
}
html.user-view-transition.returning-user-home::view-transition-old(root) {
animation: userPageShellHide 0.28s 0.06s ease-in both;
}
html.user-view-transition.returning-user-home::view-transition-new(root) {
animation: userFeedReveal 0.34s 0.3s ease-out both;
}
html.user-view-transition.returning-user-home::view-transition-old(user-avatar) {
animation: userAvatarTargetOut 0.48s 0.16s ease-out both;
}
html.user-view-transition.returning-user-home::view-transition-new(user-avatar) {
animation: userAvatarSourceIn 0.42s 0.18s ease-out both;
}
html.user-view-transition::view-transition-group(persistent-navbar),
html.user-view-transition::view-transition-old(persistent-navbar),
html.user-view-transition::view-transition-new(persistent-navbar) {
animation: none;
mix-blend-mode: normal;
}
html.user-transition-settled #pageUser .page-header,
html.user-transition-settled #pageUser .user-profile-details,
html.user-transition-settled #pageUser .user-profile-posts {
animation: userPageContentReveal 0.28s ease-out both;
}
@keyframes userAvatarSourceOut {
from { opacity: 1; }
to { opacity: 0; }
}
@keyframes userAvatarTargetIn {
from { opacity: 0; }
to { opacity: 1; }
}
@keyframes userNameColorShift {
0%, 38% {
opacity: 1;
filter: brightness(1) saturate(1);
}
100% {
opacity: 1;
filter: var(--user-name-target-filter);
}
}
@keyframes userNameColorRestore {
0%, 38% {
opacity: 1;
filter: var(--user-name-target-filter);
}
100% {
opacity: 1;
filter: brightness(1) saturate(1);
}
}
@keyframes userFeedFadeOut {
from { opacity: 1; }
to { opacity: 0; }
}
@keyframes userPageShellReveal {
from { opacity: 0; }
to { opacity: 1; }
}
@keyframes userPageShellHide {
from { opacity: 1; }
to { opacity: 0; }
}
@keyframes userFeedReveal {
from { opacity: 0; }
to { opacity: 1; }
}
@keyframes userAvatarTargetOut {
from { opacity: 1; }
to { opacity: 0; }
}
@keyframes userAvatarSourceIn {
from { opacity: 0; }
to { opacity: 1; }
}
@keyframes userPageContentReveal {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
html.post-view-transition #pagePost .post-detail-card, html.post-view-transition #pagePost .post-detail-card,
html.home-view-transition.returning-home #pagePost .post-detail-card { html.home-view-transition.returning-home #pagePost .post-detail-card {
view-transition-name: post-expand; view-transition-name: post-expand;
@@ -945,9 +1152,24 @@ html.home-view-transition.returning-home #pagePost .post-detail-card .post-pin-s
view-transition-name: post-pin-state; view-transition-name: post-pin-state;
} }
html.post-view-transition #pagePost .post-detail-card .post-edited-state, html.post-view-transition #pagePost .post-detail-card .post-edited-label,
html.home-view-transition.returning-home #pagePost .post-detail-card .post-edited-state { html.home-view-transition.returning-home #pagePost .post-detail-card .post-edited-label {
view-transition-name: post-edited-state; view-transition-name: post-edited-label;
}
html.post-view-transition #pagePost .post-detail-card .post-edited-detail,
html.home-view-transition.returning-home #pagePost .post-detail-card .post-edited-detail {
view-transition-name: post-edited-detail;
}
.post-edited-state,
.post-edited-label,
.post-edited-detail {
white-space: nowrap;
}
.post-edited-detail {
display: inline-block;
} }
html.post-view-transition #pagePost .post-detail-controls, html.post-view-transition #pagePost .post-detail-controls,
@@ -1022,7 +1244,7 @@ html.post-view-transition::view-transition-group(post-heading) {
html.post-view-transition::view-transition-group(post-avatar), html.post-view-transition::view-transition-group(post-avatar),
html.post-view-transition::view-transition-group(post-author), html.post-view-transition::view-transition-group(post-author),
html.post-view-transition::view-transition-group(post-pin-state), html.post-view-transition::view-transition-group(post-pin-state),
html.post-view-transition::view-transition-group(post-edited-state) { html.post-view-transition::view-transition-group(post-edited-label) {
animation-duration: 0.68s; animation-duration: 0.68s;
animation-timing-function: cubic-bezier(0.16, 1, 0.3, 1); animation-timing-function: cubic-bezier(0.16, 1, 0.3, 1);
} }
@@ -1096,17 +1318,35 @@ html.post-view-transition::view-transition-new(post-time) {
html.post-view-transition::view-transition-old(post-avatar), html.post-view-transition::view-transition-old(post-avatar),
html.post-view-transition::view-transition-old(post-author), html.post-view-transition::view-transition-old(post-author),
html.post-view-transition::view-transition-old(post-pin-state), html.post-view-transition::view-transition-old(post-pin-state),
html.post-view-transition::view-transition-old(post-edited-state) { html.post-view-transition::view-transition-old(post-edited-label) {
animation: sharedPartMorphOut 0.68s ease-out both; animation: sharedPartMorphOut 0.68s ease-out both;
} }
html.post-view-transition::view-transition-new(post-avatar), html.post-view-transition::view-transition-new(post-avatar),
html.post-view-transition::view-transition-new(post-author), html.post-view-transition::view-transition-new(post-author),
html.post-view-transition::view-transition-new(post-pin-state), html.post-view-transition::view-transition-new(post-pin-state),
html.post-view-transition::view-transition-new(post-edited-state) { html.post-view-transition::view-transition-new(post-edited-label) {
animation: sharedPartMorphIn 0.68s ease-out both; animation: sharedPartMorphIn 0.68s ease-out both;
} }
html.post-view-transition::view-transition-group(post-edited-detail),
html.home-view-transition.returning-home::view-transition-group(post-edited-detail) {
animation: none;
z-index: 12;
}
html.post-view-transition::view-transition-image-pair(post-edited-detail),
html.home-view-transition.returning-home::view-transition-image-pair(post-edited-detail),
html.post-view-transition::view-transition-new(post-edited-detail),
html.home-view-transition.returning-home::view-transition-old(post-edited-detail) {
overflow: visible;
mix-blend-mode: normal;
}
html.post-view-transition::view-transition-new(post-edited-detail) {
animation: editedDateExpand 0.28s 0.66s cubic-bezier(0.16, 1, 0.3, 1) both;
}
html.post-view-transition::view-transition-old(post-feed-actions) { html.post-view-transition::view-transition-old(post-feed-actions) {
animation: feedOnlyPartOut 0.28s 0.24s ease-out both; animation: feedOnlyPartOut 0.28s 0.24s ease-out both;
} }
@@ -1187,7 +1427,7 @@ html.home-view-transition.returning-home::view-transition-group(post-time) {
html.home-view-transition.returning-home::view-transition-group(post-avatar), html.home-view-transition.returning-home::view-transition-group(post-avatar),
html.home-view-transition.returning-home::view-transition-group(post-author), html.home-view-transition.returning-home::view-transition-group(post-author),
html.home-view-transition.returning-home::view-transition-group(post-pin-state), html.home-view-transition.returning-home::view-transition-group(post-pin-state),
html.home-view-transition.returning-home::view-transition-group(post-edited-state) { html.home-view-transition.returning-home::view-transition-group(post-edited-label) {
animation-duration: 0.8s; animation-duration: 0.8s;
animation-delay: 0.3s; animation-delay: 0.3s;
animation-fill-mode: both; animation-fill-mode: both;
@@ -1238,14 +1478,14 @@ html.home-view-transition.returning-home::view-transition-new(post-sidebar) {
html.home-view-transition.returning-home::view-transition-old(post-avatar), html.home-view-transition.returning-home::view-transition-old(post-avatar),
html.home-view-transition.returning-home::view-transition-old(post-author), html.home-view-transition.returning-home::view-transition-old(post-author),
html.home-view-transition.returning-home::view-transition-old(post-pin-state), html.home-view-transition.returning-home::view-transition-old(post-pin-state),
html.home-view-transition.returning-home::view-transition-old(post-edited-state) { html.home-view-transition.returning-home::view-transition-old(post-edited-label) {
animation: returnSharedPartOut 0.22s 0.3s ease-out both; animation: returnSharedPartOut 0.22s 0.3s ease-out both;
} }
html.home-view-transition.returning-home::view-transition-new(post-avatar), html.home-view-transition.returning-home::view-transition-new(post-avatar),
html.home-view-transition.returning-home::view-transition-new(post-author), html.home-view-transition.returning-home::view-transition-new(post-author),
html.home-view-transition.returning-home::view-transition-new(post-pin-state), html.home-view-transition.returning-home::view-transition-new(post-pin-state),
html.home-view-transition.returning-home::view-transition-new(post-edited-state) { html.home-view-transition.returning-home::view-transition-new(post-edited-label) {
animation: returnSharedPartIn 0.22s 0.3s ease-out both; animation: returnSharedPartIn 0.22s 0.3s ease-out both;
} }
@@ -1255,6 +1495,36 @@ html.home-view-transition.returning-home::view-transition-old(post-detail-media)
animation: returnDetailOnlyPartOut 0.22s 0.12s ease-out both; animation: returnDetailOnlyPartOut 0.22s 0.12s ease-out both;
} }
html.home-view-transition.returning-home::view-transition-old(post-edited-detail) {
animation: editedDateCollapse 0.24s 0.04s cubic-bezier(0.4, 0, 1, 1) both;
}
@keyframes editedDateExpand {
from {
opacity: 0;
clip-path: inset(0 100% 0 0);
transform: translateX(-5px);
}
to {
opacity: 1;
clip-path: inset(0 0 0 0);
transform: translateX(0);
}
}
@keyframes editedDateCollapse {
from {
opacity: 1;
clip-path: inset(0 0 0 0);
transform: translateX(0);
}
to {
opacity: 0;
clip-path: inset(0 100% 0 0);
transform: translateX(-5px);
}
}
html.home-view-transition.returning-home::view-transition-new(post-feed-actions), html.home-view-transition.returning-home::view-transition-new(post-feed-actions),
html.home-view-transition.returning-home::view-transition-new(post-feed-media) { html.home-view-transition.returning-home::view-transition-new(post-feed-media) {
animation: returnFeedOnlyPartIn 0.28s 0.44s ease-out both; animation: returnFeedOnlyPartIn 0.28s 0.44s ease-out both;
+91 -7
View File
@@ -15,7 +15,7 @@ const POST_TRANSITION_PARTS = [
['.post-content', 'post-body'], ['.post-content', 'post-body'],
['.post-images', 'post-feed-media'], ['.post-images', 'post-feed-media'],
['.post-pin-state', 'post-pin-state'], ['.post-pin-state', 'post-pin-state'],
['.post-edited-state', 'post-edited-state'], ['.post-edited-label', 'post-edited-label'],
['.post-actions', 'post-feed-actions'], ['.post-actions', 'post-feed-actions'],
]; ];
@@ -129,6 +129,7 @@ export default function AppProvider({ children, cachedName = '' }) {
const [currentPostId, setCurrentPostId] = useState(null); const [currentPostId, setCurrentPostId] = useState(null);
const [currentPostPreview, setCurrentPostPreview] = useState(null); const [currentPostPreview, setCurrentPostPreview] = useState(null);
const [currentUsername, setCurrentUsername] = useState(null); const [currentUsername, setCurrentUsername] = useState(null);
const [currentUserPreview, setCurrentUserPreview] = useState(null);
const [currentPageName, setCurrentPageName] = useState(null); const [currentPageName, setCurrentPageName] = useState(null);
const [sort, setSort] = useState('latest'); const [sort, setSort] = useState('latest');
const [refreshKey, setRefreshKey] = useState(0); const [refreshKey, setRefreshKey] = useState(0);
@@ -273,15 +274,19 @@ export default function AppProvider({ children, cachedName = '' }) {
if (page === 'feed') { if (page === 'feed') {
url.searchParams.delete('page'); url.searchParams.delete('page');
url.searchParams.delete('post'); url.searchParams.delete('post');
url.searchParams.delete('user');
} else { } else {
url.searchParams.set('page', page); url.searchParams.set('page', page);
url.searchParams.delete('post'); url.searchParams.delete('post');
url.searchParams.delete('user');
} }
const commitNavigation = (refreshFeed = true) => { const commitNavigation = (refreshFeed = true) => {
window.history.pushState({ page }, '', url); window.history.pushState({ page }, '', url);
setView(page); setView(page);
setCurrentPostId(null); setCurrentPostId(null);
setCurrentPostPreview(null); setCurrentPostPreview(null);
setCurrentUsername(null);
setCurrentUserPreview(null);
if (refreshFeed) refresh(); if (refreshFeed) refresh();
}; };
@@ -291,6 +296,48 @@ export default function AppProvider({ children, cachedName = '' }) {
return; 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 let targetCard = currentPostId
? Array.from(document.querySelectorAll('[data-post-id]')).find((element) => element.dataset.postId === String(currentPostId)) ? Array.from(document.querySelectorAll('[data-post-id]')).find((element) => element.dataset.postId === String(currentPostId))
: null; : null;
@@ -331,7 +378,7 @@ export default function AppProvider({ children, cachedName = '' }) {
document.documentElement.classList.remove('home-view-transition', 'returning-home'); document.documentElement.classList.remove('home-view-transition', 'returning-home');
refresh(); refresh();
}); });
}, [currentPostId, refresh, view]); }, [currentPostId, currentUsername, currentUserPreview, refresh, view]);
const openPost = useCallback((postId, sourceElement = null, preview = null) => { const openPost = useCallback((postId, sourceElement = null, preview = null) => {
const url = new URL(window.location); const url = new URL(window.location);
@@ -343,6 +390,7 @@ export default function AppProvider({ children, cachedName = '' }) {
setView('post'); setView('post');
setCurrentPostId(postId); setCurrentPostId(postId);
setCurrentPostPreview(preview); setCurrentPostPreview(preview);
setCurrentUserPreview(null);
}; };
const reduceMotion = window.matchMedia?.('(prefers-reduced-motion: reduce)').matches; const reduceMotion = window.matchMedia?.('(prefers-reduced-motion: reduce)').matches;
@@ -380,14 +428,47 @@ export default function AppProvider({ children, cachedName = '' }) {
}); });
}, []); }, []);
const openUser = useCallback((username) => { const openUser = useCallback((username, source = null, preview = null) => {
const url = new URL(window.location); const url = new URL(window.location);
url.searchParams.set('user', username); url.searchParams.set('user', username);
url.searchParams.delete('page'); url.searchParams.delete('page');
url.searchParams.delete('post'); url.searchParams.delete('post');
window.history.pushState({ page: 'user', username }, '', url); const commitNavigation = () => {
setView('user'); window.history.pushState({ page: 'user', username }, '', url);
setCurrentUsername(username); setView('user');
setCurrentUsername(username);
setCurrentUserPreview(preview || { username });
setCurrentPostId(null);
setCurrentPostPreview(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 openCustomPage = useCallback((pageName) => {
@@ -398,6 +479,7 @@ export default function AppProvider({ children, cachedName = '' }) {
window.history.pushState({ page: 'custom', pageName }, '', url); window.history.pushState({ page: 'custom', pageName }, '', url);
setView('custom'); setView('custom');
setCurrentPageName(pageName); setCurrentPageName(pageName);
setCurrentUserPreview(null);
}, []); }, []);
// 浏览器前进后退 // 浏览器前进后退
@@ -411,6 +493,7 @@ export default function AppProvider({ children, cachedName = '' }) {
} else if (state.username) { } else if (state.username) {
setView('user'); setView('user');
setCurrentUsername(state.username); setCurrentUsername(state.username);
setCurrentUserPreview(null);
} else if (state.pageName) { } else if (state.pageName) {
setView('custom'); setView('custom');
setCurrentPageName(state.pageName); setCurrentPageName(state.pageName);
@@ -419,6 +502,7 @@ export default function AppProvider({ children, cachedName = '' }) {
setCurrentPostId(null); setCurrentPostId(null);
setCurrentPostPreview(null); setCurrentPostPreview(null);
setCurrentUsername(null); setCurrentUsername(null);
setCurrentUserPreview(null);
setCurrentPageName(null); setCurrentPageName(null);
} }
}; };
@@ -461,7 +545,7 @@ export default function AppProvider({ children, cachedName = '' }) {
theme, toggleTheme, theme, toggleTheme,
view, navigate, view, navigate,
currentPostId, currentPostPreview, openPost, currentPostId, currentPostPreview, openPost,
currentUsername, openUser, currentUsername, currentUserPreview, openUser,
currentPageName, openCustomPage, currentPageName, openCustomPage,
sort, setSort, sort, setSort,
refreshKey, refresh, refreshKey, refresh,
+37 -4
View File
@@ -107,6 +107,23 @@ export default function Feed({ onOpenModal, onReport }) {
} }
}; };
const handleOpenUser = (event, post) => {
event.stopPropagation();
const header = event.currentTarget.closest('.post-header');
openUser(
post.username,
{
avatarElement: header?.querySelector('.post-avatar'),
nameElement: header?.querySelector('.post-username'),
},
{
username: post.username,
avatar_url: post.avatar_url || avatar(post.username),
sourcePostId: post.id,
}
);
};
return ( return (
<main id="feed"> <main id="feed">
<div className="feed-header"> <div className="feed-header">
@@ -137,23 +154,39 @@ export default function Feed({ onOpenModal, onReport }) {
posts.map((p) => { posts.map((p) => {
const time = p.created_at ? new Date(p.created_at).toLocaleString('zh-CN') : ''; const time = p.created_at ? new Date(p.created_at).toLocaleString('zh-CN') : '';
return ( return (
<div key={p.id} className="post-card" data-post-id={p.id} style={{ cursor: 'pointer' }} <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)}> onClick={(e) => openPost(p.id, e.currentTarget, p)}>
{p.is_pinned && ( {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> <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>
)} )}
<div className="post-header"> <div className="post-header">
<img src={p.avatar_url || avatar(p.username)} className="post-avatar" alt="" /> <img
src={p.avatar_url || avatar(p.username)}
className="post-avatar"
alt={p.username || t('feed.anonymous')}
role="button"
tabIndex={0}
style={{ cursor: 'pointer' }}
onClick={(e) => handleOpenUser(e, p)}
onKeyDown={(e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
handleOpenUser(e, p);
}
}}
/>
<span <span
className="post-username" className="post-username"
style={{ cursor: 'pointer', color: 'var(--primary)' }} style={{ cursor: 'pointer', color: 'var(--primary)' }}
onClick={(e) => { e.stopPropagation(); openUser(p.username); }} onClick={(e) => handleOpenUser(e, p)}
> >
{p.username || t('feed.anonymous')} {p.username || t('feed.anonymous')}
</span> </span>
<span className="post-time">{time}</span> <span className="post-time">{time}</span>
{p.edited_at && ( {p.edited_at && (
<span className="post-edited-state" style={{ fontSize: 11, color: 'var(--text-light)', marginLeft: 6 }}>{t('feed.deleted')}</span> <span className="post-edited-state" style={{ fontSize: 11, color: 'var(--text-light)', marginLeft: 6 }}>
<span className="post-edited-label">{t('feed.deleted')}</span>
</span>
)} )}
</div> </div>
<div className="post-title">{p.title || t('feed.noTitle')}</div> <div className="post-title">{p.title || t('feed.noTitle')}</div>
+6 -1
View File
@@ -10,6 +10,7 @@ import { Icon } from './Icons';
import ImageViewer from './ImageViewer'; import ImageViewer from './ImageViewer';
import { renderMarkdown } from '@/lib/markdown'; import { renderMarkdown } from '@/lib/markdown';
import { useToast } from './Toast'; import { useToast } from './Toast';
import { useTranslation } from 'react-i18next';
function avatar(username) { function avatar(username) {
return 'https://ui-avatars.com/api/?name=' + encodeURIComponent(username || '匿名用户') + return 'https://ui-avatars.com/api/?name=' + encodeURIComponent(username || '匿名用户') +
@@ -18,6 +19,7 @@ function avatar(username) {
export default function PostDetail({ postId, initialPost = null }) { export default function PostDetail({ postId, initialPost = null }) {
const { currentUser, navigate, openUser, refreshKey } = useApp(); const { currentUser, navigate, openUser, refreshKey } = useApp();
const { t, i18n } = useTranslation();
const { toast, confirmAction } = useToast(); const { toast, confirmAction } = useToast();
const [post, setPost] = useState(initialPost); const [post, setPost] = useState(initialPost);
const [error, setError] = useState(null); const [error, setError] = useState(null);
@@ -156,7 +158,10 @@ export default function PostDetail({ postId, initialPost = null }) {
<span className="post-time">{time}</span> <span className="post-time">{time}</span>
{post.edited_at && ( {post.edited_at && (
<span className="post-edited-state" style={{ fontSize: 12, color: 'var(--text-light)', marginLeft: 8 }}> <span className="post-edited-state" style={{ fontSize: 12, color: 'var(--text-light)', marginLeft: 8 }}>
(已编辑 {new Date(post.edited_at).toLocaleString('zh-CN')}) <span className="post-edited-label">{t('feed.deleted')}</span>
<span className="post-edited-detail">
{' '}{new Date(post.edited_at).toLocaleString(i18n.language === 'en' ? 'en-US' : 'zh-CN')}
</span>
</span> </span>
)} )}
{post.is_pinned && ( {post.is_pinned && (
+51 -39
View File
@@ -12,10 +12,10 @@ function avatar(username, size = 128) {
'&background=6366f1&color=fff&size=' + size; '&background=6366f1&color=fff&size=' + size;
} }
export default function UserProfile({ username }) { export default function UserProfile({ username, initialUser = null }) {
const { currentUser, openPost } = useApp(); const { currentUser, openPost } = useApp();
const openPrivateChat = useOpenPrivateChat(); const openPrivateChat = useOpenPrivateChat();
const [user, setUser] = useState(null); const [user, setUser] = useState(initialUser);
const [posts, setPosts] = useState([]); const [posts, setPosts] = useState([]);
const [error, setError] = useState(null); const [error, setError] = useState(null);
@@ -35,60 +35,72 @@ export default function UserProfile({ username }) {
} }
}, [username]); }, [username]);
useEffect(() => { load(); }, [load]); useEffect(() => {
setUser(initialUser);
setPosts([]);
load();
}, [initialUser, load]);
if (error) { if (error) {
return ( return (
<div className="page-slide active"> <div id="pageUser" className="page-slide active">
<div style={{ textAlign: 'center', color: '#ef4444', padding: '40px 0' }}>加载失败:{error}</div> <div style={{ textAlign: 'center', color: '#ef4444', padding: '40px 0' }}>加载失败:{error}</div>
</div> </div>
); );
} }
if (!user) { if (!user) {
return <div style={{ textAlign: 'center', color: '#94a3b8', padding: '40px 0' }}><span className="spinner-sm" />加载中...</div>; return <div id="pageUser" className="page-slide active" style={{ textAlign: 'center', color: '#94a3b8', padding: '40px 0' }}><span className="spinner-sm" />加载中...</div>;
} }
return ( return (
<div className="page-slide active"> <div id="pageUser" className="page-slide active">
<div className="page-header" style={{ maxWidth: 700, margin: '0 auto', width: '100%' }}> <div className="page-header" style={{ maxWidth: 700, margin: '0 auto', width: '100%' }}>
<h2><Icon name="users" size={20} /> 用户主页</h2> <h2><Icon name="users" size={20} /> 用户主页</h2>
</div> </div>
<div style={{ maxWidth: 700, margin: '0 auto', width: '100%' }}> <div id="userProfileContent" style={{ maxWidth: 700, margin: '0 auto', width: '100%' }}>
<div style={{ background: 'var(--surface)', border: '1px solid var(--border)', borderRadius: 8, padding: 32, textAlign: 'center' }}> <div className="user-profile-card" style={{ background: 'var(--surface)', border: '1px solid var(--border)', borderRadius: 8, padding: 32, textAlign: 'center' }}>
<img src={user.avatar_url || avatar(user.username)} style={{ width: 96, height: 96, borderRadius: '50%', objectFit: 'cover', border: '3px solid var(--primary)' }} alt="" /> <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 style={{ margin: '16px 0 4px', fontSize: 24 }}>{user.username}</h2> <h2 className="user-profile-name" style={{ margin: '16px 0 4px', fontSize: 24, color: 'var(--text)' }}>{user.username}</h2>
<p style={{ color: 'var(--text-secondary)', fontSize: 14 }}>{user.bio || '这个人很懒,什么都没写'}</p> {user.id && (
{currentUser && currentUser.id !== user.id && ( <div className="user-profile-details">
<button <p style={{ color: 'var(--text-secondary)', fontSize: 14 }}>{user.bio || '这个人很懒,什么都没写'}</p>
className="btn-primary" {currentUser && currentUser.username !== user.username && (
style={{ marginTop: 12, padding: '8px 20px' }} <button
onClick={() => openPrivateChat(user.id, user.username)} className="btn-primary"
> style={{ marginTop: 12, padding: '8px 20px' }}
<Icon name="message" size={14} /> 发私信 onClick={() => openPrivateChat(user.id, user.username)}
</button> >
)} <Icon name="message" size={14} /> 发私信
<div style={{ display: 'flex', justifyContent: 'center', gap: 32, marginTop: 16, fontSize: 14, color: 'var(--text-secondary)', flexWrap: 'wrap' }}> </button>
<span><Icon name="calendar" size={14} /> 加入于 {user.created_at ? new Date(user.created_at).toLocaleDateString('zh-CN') : '未知'}</span> )}
<span><Icon name="file" size={14} /> 发了 {posts.length} 个帖子</span> <div style={{ display: 'flex', justifyContent: 'center', gap: 32, marginTop: 16, fontSize: 14, color: 'var(--text-secondary)', flexWrap: 'wrap' }}>
{user.role === 'admin' && <span style={{ color: 'var(--primary)', fontWeight: 600 }}><Icon name="shield" size={14} /> 管理员</span>} <span><Icon name="calendar" size={14} /> 加入于 {user.created_at ? new Date(user.created_at).toLocaleDateString('zh-CN') : '未知'}</span>
<span><Icon name="file" size={14} /> 发了 {posts.length} 个帖子</span>
{user.role === 'admin' && <span style={{ color: 'var(--primary)', fontWeight: 600 }}><Icon name="shield" size={14} /> 管理员</span>}
</div>
</div> </div>
)}
</div> </div>
<h3 style={{ margin: '24px 0 16px', fontSize: 18 }}><Icon name="file" size={18} /> 发布的帖子</h3> {user.id && (
{posts.length === 0 ? ( <div className="user-profile-posts">
<div style={{ color: '#94a3b8', padding: '20px 0', textAlign: 'center' }}>还没有发帖</div> <h3 style={{ margin: '24px 0 16px', fontSize: 18 }}><Icon name="file" size={18} /> 发布的帖子</h3>
) : ( {posts.length === 0 ? (
posts.map((p) => { <div style={{ color: '#94a3b8', padding: '20px 0', textAlign: 'center' }}>还没有发帖</div>
const time = p.created_at ? new Date(p.created_at).toLocaleString('zh-CN') : ''; ) : (
return ( posts.map((p) => {
<div key={p.id} className="post-card" style={{ cursor: 'pointer' }} onClick={(e) => openPost(p.id, e.currentTarget, p)}> const time = p.created_at ? new Date(p.created_at).toLocaleString('zh-CN') : '';
<div className="post-title" style={{ fontSize: 16, fontWeight: 600 }}>{p.title || '无标题'}</div> return (
<div className="post-content" style={{ fontSize: 14, color: 'var(--text-secondary)' }}> <div key={p.id} className="post-card" style={{ cursor: 'pointer' }} onClick={(e) => openPost(p.id, e.currentTarget, p)}>
{(p.content || '').substring(0, 100)}{(p.content || '').length > 100 ? '...' : ''} <div className="post-title" style={{ fontSize: 16, fontWeight: 600 }}>{p.title || '无标题'}</div>
<div className="post-content" style={{ fontSize: 14, color: 'var(--text-secondary)' }}>
{(p.content || '').substring(0, 100)}{(p.content || '').length > 100 ? '...' : ''}
</div>
<div style={{ fontSize: 12, color: 'var(--text-light)', marginTop: 8 }}>{time}</div>
</div> </div>
<div style={{ fontSize: 12, color: 'var(--text-light)', marginTop: 8 }}>{time}</div> );
</div> })
); )}
}) </div>
)} )}
</div> </div>
</div> </div>