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

View File

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