Skip to content

Commit

Permalink
fix: latest blog posts query
Browse files Browse the repository at this point in the history
  • Loading branch information
CaliCastle committed May 17, 2023
1 parent f3be95c commit 93d0055
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
24 changes: 24 additions & 0 deletions app/(main)/blog/BlogPostPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use client'

import Image from 'next/image'
import React from 'react'
import Balancer from 'react-wrap-balancer'

import {
Expand All @@ -13,9 +14,32 @@ import { PostPortableText } from '~/components/PostPortableText'
import { Prose } from '~/components/Prose'
import { Container } from '~/components/ui/Container'
import { prettifyNumber } from '~/lib/math'
import { useLivePostStore } from '~/lib/store'
import { type Post } from '~/sanity/schemas/post'

export function BlogPostPage({ post, views }: { post: Post; views?: number }) {
const {
liveblocks: { enterRoom, leaveRoom },
} = useLivePostStore()

React.useEffect(() => {
enterRoom(post._id)
return () => leaveRoom(post._id)
}, [enterRoom, leaveRoom, post._id])

const setCursor = useLivePostStore((state) => state.setCursor)
React.useEffect(() => {
const handleMouseMove = (e: MouseEvent) => {
const x = e.clientX
const y = e.clientY
setCursor({ x, y })
}

window.addEventListener('mousemove', handleMouseMove)

return () => window.removeEventListener('mousemove', handleMouseMove)
}, [setCursor])

return (
<Container className="mt-16 lg:mt-32">
<div className="xl:relative">
Expand Down
16 changes: 9 additions & 7 deletions lib/store.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import { createClient } from '@liveblocks/client'
import type { WithLiveblocks } from '@liveblocks/zustand'
import { liveblocks } from '@liveblocks/zustand'
import create from 'zustand'
import { create } from 'zustand'

import { env } from '~/env.mjs'

type Cursor = { x: number; y: number }

type State = {
// Your Zustand state type will be defined here
cursor: Cursor
setCursor: (cursor: Cursor) => void
}

const client = createClient({
publicApiKey: env.NEXT_PUBLIC_LIVEBLOCKS_API_KEY,
})

const useStore = create<WithLiveblocks<State>>()(
export const useLivePostStore = create<WithLiveblocks<State>>()(
liveblocks(
(set) => ({
// Your state and actions will go here
cursor: { x: 0, y: 0 },
setCursor: (cursor) => set({ cursor }),
}),
{ client }
{ client, presenceMapping: { cursor: true } }
)
)

export default useStore
2 changes: 1 addition & 1 deletion sanity/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { type Post } from '~/sanity/schemas/post'

export const getLatestBlogPostsQuery = (limit = 5) =>
groq`
*[_type == "post" && !(_id in path("drafts.**")) && publishedAt > "${
*[_type == "post" && !(_id in path("drafts.**")) && publishedAt <= "${
new Date().toISOString().split('T')[0]
}" && defined(slug.current)][0...${limit}] | order(publishedAt desc) {
_id,
Expand Down

0 comments on commit 93d0055

Please sign in to comment.