Skip to content

Commit

Permalink
feat: note update event
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <[email protected]>
  • Loading branch information
Innei committed Jun 23, 2023
1 parent c6ce103 commit aeaedfe
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/atoms/owner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { aggregationDataAtom } from '~/providers/root/aggregation-data-provider'
import { getToken, removeToken, setToken } from '~/utils/cookie'
import { apiClient } from '~/utils/request'

import { fetchAppUrl } from './url'

const ownerAtom = atom((get) => {
return get(aggregationDataAtom)?.user
})
Expand All @@ -23,6 +25,7 @@ export const login = async (username?: string, password?: string) => {
setToken(token)
jotaiStore.set(isLoggedAtom, true)

await fetchAppUrl()
toast(`欢迎回来,${jotaiStore.get(ownerAtom)?.name}`, 'success')
}

Expand Down
2 changes: 1 addition & 1 deletion src/atoms/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const fetchAppUrl = async () => {
jotaiStore.set(adminUrlAtom, data.adminUrl)
}

export const getAppUrl = () => jotaiStore.get(adminUrlAtom)
export const getAdminUrl = () => jotaiStore.get(adminUrlAtom)
export const useAppUrl = () => {
const url = useAggregationSelector((a) => a.url)
const adminUrl = useAtomValue(adminUrlAtom)
Expand Down
4 changes: 2 additions & 2 deletions src/components/layout/header/internal/AnimatedLogo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { AnimatePresence, motion } from 'framer-motion'
import { useRouter } from 'next/navigation'

import { getAppUrl, isLogged, useViewport } from '~/atoms'
import { getAdminUrl, isLogged, useViewport } from '~/atoms'
import { useSingleAndDoubleClick } from '~/hooks/common/use-single-double-click'
import { Routes } from '~/lib/route-builder'
import { toast } from '~/lib/toast'
Expand All @@ -19,7 +19,7 @@ const TapableLogo = () => {
},
() => {
if (isLogged()) {
const adminUrl = getAppUrl()?.adminUrl
const adminUrl = getAdminUrl()
if (adminUrl) location.href = adminUrl
else {
toast('Admin url not found', 'error')
Expand Down
8 changes: 5 additions & 3 deletions src/components/layout/header/internal/HeaderMeta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,20 @@ export const HeaderMeta = () => {
>
<div className="align-center flex min-w-0 flex-shrink flex-grow flex-col">
<small className="min-w-0 truncate">
<span className="text-gray-500">{description}</span>
<span className="text-gray-600/60 dark:text-gray-300/60">
{description}
</span>
</small>
<h2 className="min-w-0 truncate text-[1.2rem] font-medium leading-[1.5]">
{title}
</h2>
</div>

<div className="hidden min-w-0 flex-shrink flex-col text-right leading-5 lg:flex">
<span className="min-w-0 truncate whitespace-pre text-gray-600/60 dark:text-gray-300/60">
<small className="min-w-0 truncate whitespace-pre text-gray-600/60 dark:text-gray-300/60">
{' '}
{slug}
</span>
</small>
<span className="font-medium text-gray-600 dark:text-gray-300">
{seoTitle}
</span>
Expand Down
2 changes: 1 addition & 1 deletion src/components/widgets/comment/Comments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const Comments: FC<{
refId: string
}> = ({ refId }) => {
return (
<div className="relative mb-[60px] mt-[120px] min-h-[10000px]">
<div className="relative mb-[60px] mt-[120px] min-h-[100px]">
Comments WIP, RefId: {refId}
</div>
)
Expand Down
1 change: 0 additions & 1 deletion src/providers/root/socket-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import { useEffect } from 'react'

// const Context = createContext<TSocketClient>(null as any)
export const SocketContainer: Component = () => {
useEffect(() => {
import('~/socket').then((module) => {
Expand Down
24 changes: 21 additions & 3 deletions src/socket/handler.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
import type { NoteModel } from '@mx-space/api-client'
import type { EventTypes } from '~/types/events'

import { setOnlineCount } from '~/atoms'
import { EventTypes } from '~/types/events'
import { toast } from '~/lib/toast'
import {
getCurrentNoteData,
setCurrentNoteData,
} from '~/providers/note/CurrentNoteDataProvider'
import { isDev } from '~/utils/env'

export const eventHandler = (type: EventTypes, data: any) => {
switch (type) {
case EventTypes.VISITOR_ONLINE:
case EventTypes.VISITOR_OFFLINE: {
case 'VISITOR_ONLINE':
case 'VISITOR_OFFLINE': {
const { online } = data
setOnlineCount(online)
break
}

case 'NOTE_UPDATE': {
const note = data as NoteModel
if (getCurrentNoteData()?.data.id === note.id) {
setCurrentNoteData((draft) => {
Object.assign(draft.data, note)
})
toast('手记已更新')
}
break
}

default: {
if (isDev) {
console.log(type, data)
Expand Down
10 changes: 9 additions & 1 deletion src/types/events.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { NoteModel } from '@mx-space/api-client'

export enum EventTypes {
GATEWAY_CONNECT = 'GATEWAY_CONNECT',
GATEWAY_DISCONNECT = 'GATEWAY_DISCONNECT',
Expand Down Expand Up @@ -26,5 +28,11 @@ export enum EventTypes {
DANMAKU_CREATE = 'DANMAKU_CREATE',

RECENTLY_CREATE = 'RECENTLY_CREATE',
RECENTLY_DElETE = 'RECENTLY_DElETE',
RECENTLY_DELETE = 'RECENTLY_DELETE',
}

export interface EventTypesPayload {
[EventTypes.VISITOR_ONLINE]: { online: number }
[EventTypes.VISITOR_OFFLINE]: { online: number }
[EventTypes.NOTE_UPDATE]: NoteModel
}

1 comment on commit aeaedfe

@vercel
Copy link

@vercel vercel bot commented on aeaedfe Jun 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

springtide – ./

springtide-git-main-innei.vercel.app
springtide-innei.vercel.app
springtide.vercel.app
innei.in

Please sign in to comment.