improve notifications UX: highlight reply and distinguish new notifications

This commit is contained in:
keyan 2023-08-03 14:56:59 -05:00
parent 7596b0302a
commit 0ab9119739
2 changed files with 22 additions and 16 deletions

View File

@ -257,7 +257,7 @@ function Reply ({ n }) {
) )
} }
function NotificationAlert () { export function NotificationAlert () {
const [showAlert, setShowAlert] = useState(false) const [showAlert, setShowAlert] = useState(false)
const [hasSubscription, setHasSubscription] = useState(false) const [hasSubscription, setHasSubscription] = useState(false)
const [error, setError] = useState(null) const [error, setError] = useState(null)
@ -319,35 +319,40 @@ function NotificationAlert () {
export default function Notifications ({ ssrData }) { export default function Notifications ({ ssrData }) {
const { data, fetchMore } = useQuery(NOTIFICATIONS) const { data, fetchMore } = useQuery(NOTIFICATIONS)
const client = useApolloClient() const client = useApolloClient()
const router = useRouter()
const { notifications: { notifications, earn, lastChecked, cursor } } = useMemo(() => {
return data || ssrData || { notifications: {} }
}, [data, ssrData])
const checkedAt = router?.query?.checkedAt
useEffect(() => { useEffect(() => {
client.writeQuery({ if (lastChecked && !checkedAt) {
router.push({ query: { ...router.query, checkedAt: lastChecked } },
router.asPath, { shallow: true })
client?.writeQuery({
query: HAS_NOTIFICATIONS, query: HAS_NOTIFICATIONS,
data: { data: {
hasNewNotes: false hasNewNotes: false
} }
}) })
}, [client]) }
}, [lastChecked, checkedAt])
const { notifications: { notifications, earn, lastChecked, cursor } } = useMemo(() => {
if (!data && !ssrData) return { notifications: {} }
return data || ssrData
}, [data, ssrData])
const [fresh, old] = useMemo(() => { const [fresh, old] = useMemo(() => {
if (!notifications) return [[], []] if (!notifications) return [[], []]
return notifications.reduce((result, n) => { return notifications.reduce((result, n) => {
result[new Date(n.sortTime).getTime() > lastChecked ? 0 : 1].push(n) result[new Date(n.sortTime).getTime() > new Date(checkedAt)?.getTime() ? 0 : 1].push(n)
return result return result
}, },
[[], []]) [[], []])
}, [notifications, lastChecked]) }, [notifications, checkedAt])
if (!data && !ssrData) return <CommentsFlatSkeleton /> if (!data && !ssrData) return <CommentsFlatSkeleton />
return ( return (
<> <>
<NotificationAlert />
<div className='fresh'> <div className='fresh'>
{earn && <Notification n={earn} key='earn' />} {earn && <Notification n={earn} key='earn' />}
{fresh.map((n, i) => ( {fresh.map((n, i) => (

View File

@ -1,6 +1,6 @@
import { getGetServerSideProps } from '../api/ssrApollo' import { getGetServerSideProps } from '../api/ssrApollo'
import Layout from '../components/layout' import Layout from '../components/layout'
import Notifications from '../components/notifications' import Notifications, { NotificationAlert } from '../components/notifications'
import { NOTIFICATIONS } from '../fragments/notifications' import { NOTIFICATIONS } from '../fragments/notifications'
export const getServerSideProps = getGetServerSideProps(NOTIFICATIONS) export const getServerSideProps = getGetServerSideProps(NOTIFICATIONS)
@ -8,6 +8,7 @@ export const getServerSideProps = getGetServerSideProps(NOTIFICATIONS)
export default function NotificationPage ({ ssrData }) { export default function NotificationPage ({ ssrData }) {
return ( return (
<Layout> <Layout>
<NotificationAlert />
<Notifications ssrData={ssrData} /> <Notifications ssrData={ssrData} />
</Layout> </Layout>
) )