Small refactor, add ss auth
This commit is contained in:
parent
611d35bce5
commit
49e2e20086
|
@ -24,20 +24,13 @@ export function usePreferredGroupedNotifications(
|
||||||
privateUser: PrivateUser,
|
privateUser: PrivateUser,
|
||||||
cachedNotifications?: Notification[]
|
cachedNotifications?: Notification[]
|
||||||
) {
|
) {
|
||||||
const [notificationGroups, setNotificationGroups] = useState<
|
|
||||||
NotificationGroup[] | undefined
|
|
||||||
>(undefined)
|
|
||||||
const [notifications, setNotifications] = useState<Notification[]>(
|
|
||||||
cachedNotifications ?? []
|
|
||||||
)
|
|
||||||
|
|
||||||
const result = useFirestoreQueryData(
|
const result = useFirestoreQueryData(
|
||||||
['notifications-all', privateUser.id],
|
['notifications-all', privateUser.id],
|
||||||
getNotificationsQuery(privateUser.id)
|
getNotificationsQuery(privateUser.id)
|
||||||
)
|
)
|
||||||
useMemo(() => {
|
const notifications = useMemo(() => {
|
||||||
if (result.isLoading) return
|
if (result.isLoading) return cachedNotifications ?? []
|
||||||
if (!result.data) return
|
if (!result.data) return cachedNotifications ?? []
|
||||||
const notifications = result.data as Notification[]
|
const notifications = result.data as Notification[]
|
||||||
|
|
||||||
const notificationsToShow = getAppropriateNotifications(
|
const notificationsToShow = getAppropriateNotifications(
|
||||||
|
@ -46,8 +39,9 @@ export function usePreferredGroupedNotifications(
|
||||||
).filter((n) => !n.isSeenOnHref)
|
).filter((n) => !n.isSeenOnHref)
|
||||||
const cachedIds = cachedNotifications?.map((n) => n.id)
|
const cachedIds = cachedNotifications?.map((n) => n.id)
|
||||||
if (notificationsToShow.some((n) => !cachedIds?.includes(n.id))) {
|
if (notificationsToShow.some((n) => !cachedIds?.includes(n.id))) {
|
||||||
setNotifications(notificationsToShow)
|
return notificationsToShow
|
||||||
}
|
}
|
||||||
|
return cachedNotifications
|
||||||
}, [
|
}, [
|
||||||
cachedNotifications,
|
cachedNotifications,
|
||||||
privateUser.notificationPreferences,
|
privateUser.notificationPreferences,
|
||||||
|
@ -55,17 +49,9 @@ export function usePreferredGroupedNotifications(
|
||||||
result.isLoading,
|
result.isLoading,
|
||||||
])
|
])
|
||||||
|
|
||||||
useMemo(() => {
|
return useMemo(() => {
|
||||||
if (!notifications) return
|
if (notifications) return groupNotifications(notifications)
|
||||||
if (notifications.length > 0) {
|
|
||||||
const local = safeLocalStorage()
|
|
||||||
local?.setItem('notifications', JSON.stringify(notifications))
|
|
||||||
}
|
|
||||||
const groupedNotifications = groupNotifications(notifications)
|
|
||||||
setNotificationGroups(groupedNotifications)
|
|
||||||
}, [notifications])
|
}, [notifications])
|
||||||
|
|
||||||
return notificationGroups
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useUnseenPreferredNotificationGroups(privateUser: PrivateUser) {
|
export function useUnseenPreferredNotificationGroups(privateUser: PrivateUser) {
|
||||||
|
|
|
@ -6,7 +6,7 @@ const TOKEN_KINDS = ['refresh', 'id'] as const
|
||||||
type TokenKind = typeof TOKEN_KINDS[number]
|
type TokenKind = typeof TOKEN_KINDS[number]
|
||||||
|
|
||||||
const getAuthCookieName = (kind: TokenKind) => {
|
const getAuthCookieName = (kind: TokenKind) => {
|
||||||
const suffix = `${PROJECT_ID}_${kind}`.toUpperCase().replaceAll('-', '_')
|
const suffix = `${PROJECT_ID}_${kind}`.toUpperCase()
|
||||||
return `FIREBASE_TOKEN_${suffix}`
|
return `FIREBASE_TOKEN_${suffix}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,9 +14,14 @@ import {
|
||||||
MANIFOLD_USERNAME,
|
MANIFOLD_USERNAME,
|
||||||
notification_subscribe_types,
|
notification_subscribe_types,
|
||||||
PrivateUser,
|
PrivateUser,
|
||||||
|
User,
|
||||||
} from 'common/user'
|
} from 'common/user'
|
||||||
import { ChoicesToggleGroup } from 'web/components/choices-toggle-group'
|
import { ChoicesToggleGroup } from 'web/components/choices-toggle-group'
|
||||||
import { listenForPrivateUser, updatePrivateUser } from 'web/lib/firebase/users'
|
import {
|
||||||
|
getUser,
|
||||||
|
listenForPrivateUser,
|
||||||
|
updatePrivateUser,
|
||||||
|
} from 'web/lib/firebase/users'
|
||||||
import { LoadingIndicator } from 'web/components/loading-indicator'
|
import { LoadingIndicator } from 'web/components/loading-indicator'
|
||||||
import clsx from 'clsx'
|
import clsx from 'clsx'
|
||||||
import { RelativeTimestamp } from 'web/components/relative-timestamp'
|
import { RelativeTimestamp } from 'web/components/relative-timestamp'
|
||||||
|
@ -44,23 +49,40 @@ import { Pagination } from 'web/components/pagination'
|
||||||
import { useWindowSize } from 'web/hooks/use-window-size'
|
import { useWindowSize } from 'web/hooks/use-window-size'
|
||||||
import Router from 'next/router'
|
import Router from 'next/router'
|
||||||
import { safeLocalStorage } from 'web/lib/util/local'
|
import { safeLocalStorage } from 'web/lib/util/local'
|
||||||
|
import {
|
||||||
|
getServerAuthenticatedUid,
|
||||||
|
redirectIfLoggedOut,
|
||||||
|
} from 'web/lib/firebase/server-auth'
|
||||||
|
import { useQueryClient } from 'react-query'
|
||||||
|
import { getNotificationsQuery } from 'web/lib/firebase/notifications'
|
||||||
|
import { getValues } from 'web/lib/firebase/utils'
|
||||||
|
|
||||||
export const NOTIFICATIONS_PER_PAGE = 30
|
export const NOTIFICATIONS_PER_PAGE = 30
|
||||||
const MULTIPLE_USERS_KEY = 'multipleUsers'
|
const MULTIPLE_USERS_KEY = 'multipleUsers'
|
||||||
const HIGHLIGHT_CLASS = 'bg-indigo-50'
|
const HIGHLIGHT_CLASS = 'bg-indigo-50'
|
||||||
|
|
||||||
export default function Notifications() {
|
export const getServerSideProps = redirectIfLoggedOut('/', async (ctx) => {
|
||||||
const user = useUser()
|
const uid = await getServerAuthenticatedUid(ctx)
|
||||||
|
if (!uid) {
|
||||||
|
return { props: { user: null } }
|
||||||
|
}
|
||||||
|
const user = await getUser(uid)
|
||||||
|
return { props: { user } }
|
||||||
|
})
|
||||||
|
|
||||||
|
export default function Notifications(props: { user: User }) {
|
||||||
|
const { user } = props
|
||||||
const privateUser = usePrivateUser(user?.id)
|
const privateUser = usePrivateUser(user?.id)
|
||||||
const local = safeLocalStorage()
|
const local = safeLocalStorage()
|
||||||
const localSavedNotifications = local?.getItem('notifications')
|
|
||||||
let localNotifications = [] as Notification[]
|
let localNotifications = [] as Notification[]
|
||||||
if (localSavedNotifications)
|
|
||||||
localNotifications = JSON.parse(localSavedNotifications)
|
|
||||||
const localSavedNotificationGroups = local?.getItem('notification-groups')
|
const localSavedNotificationGroups = local?.getItem('notification-groups')
|
||||||
let localNotificationGroups = [] as NotificationGroup[]
|
let localNotificationGroups = [] as NotificationGroup[]
|
||||||
if (localSavedNotificationGroups)
|
if (localSavedNotificationGroups) {
|
||||||
localNotificationGroups = JSON.parse(localSavedNotificationGroups)
|
localNotificationGroups = JSON.parse(localSavedNotificationGroups)
|
||||||
|
localNotifications = localNotificationGroups
|
||||||
|
.map((g) => g.notifications)
|
||||||
|
.flat()
|
||||||
|
}
|
||||||
|
|
||||||
if (!user) return <Custom404 />
|
if (!user) return <Custom404 />
|
||||||
return (
|
return (
|
||||||
|
@ -149,7 +171,7 @@ function NotificationsList(props: {
|
||||||
const [paginatedGroupedNotifications, setPaginatedGroupedNotifications] =
|
const [paginatedGroupedNotifications, setPaginatedGroupedNotifications] =
|
||||||
useState<NotificationGroup[] | undefined>(undefined)
|
useState<NotificationGroup[] | undefined>(undefined)
|
||||||
|
|
||||||
useMemo(() => {
|
useEffect(() => {
|
||||||
if (!allGroupedNotifications) return
|
if (!allGroupedNotifications) return
|
||||||
const start = page * NOTIFICATIONS_PER_PAGE
|
const start = page * NOTIFICATIONS_PER_PAGE
|
||||||
const end = start + NOTIFICATIONS_PER_PAGE
|
const end = start + NOTIFICATIONS_PER_PAGE
|
||||||
|
|
Loading…
Reference in New Issue
Block a user