Skip to content

Commit

Permalink
fix(web): the new example & use provide/inject the user info
Browse files Browse the repository at this point in the history
  • Loading branch information
Chilfish committed Mar 30, 2024
1 parent 7af724a commit 4327673
Show file tree
Hide file tree
Showing 17 changed files with 66 additions and 56 deletions.
5 changes: 2 additions & 3 deletions apps/monkey/src/stores/postStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const usePostStore = defineStore('post', () => {
fetchedCount: 0,
})

setDB()
await setDB()
await idb.value.clearDB()
}

Expand All @@ -68,8 +68,7 @@ export const usePostStore = defineStore('post', () => {

async function getAll() {
await waitIDB()
const posts = await idb.value.getAllDBPosts()
return posts
return await idb.value.getAllDBPosts()
}

async function setCount() {
Expand Down
6 changes: 5 additions & 1 deletion apps/web/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ useHead({
],
})
const loaded = ref(false)
const route = useRoute()
const loaded = ref(route.path === '/example')
onNuxtReady(async () => {
const publicStore = usePublicStore()
const users = localStorage.getItem('users')
Expand All @@ -47,6 +49,8 @@ onNuxtReady(async () => {
publicStore.curUid = curUid || ''
watchImmediate(() => publicStore.curUid, async () => {
if (route.path === '/example')
return
loaded.value = false
await publicStore.migrateUser()
loaded.value = true
Expand Down
10 changes: 6 additions & 4 deletions apps/web/src/pages/example.vue

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions packages/core/src/constants/vueProvide.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { InjectionKey } from 'vue'
import type { User } from '@types'

export const KeyUser = Symbol('user') as InjectionKey<User>
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './composables'
export * from './utils'
export * from './constants'
export * from './constants/vueProvide'
export * from './stores'
export * from './services'
26 changes: 13 additions & 13 deletions packages/core/src/stores/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const usePostStore = defineStore('post', () => {
}),
})

const seachFn = ref<(text: string) => number[]>()
const searchFn = ref<(text: string) => number[]>()

/* 该结果的总帖子数 */
const total = ref(0)
Expand All @@ -70,7 +70,9 @@ export const usePostStore = defineStore('post', () => {
/**
* 设置帖子数据,可选择是否替换或是追加合并
* @param data
* @param replace
* @param user
* @param _followings
* @param isReplace
*/
async function set(
data: Post[],
Expand All @@ -89,7 +91,7 @@ export const usePostStore = defineStore('post', () => {
const { count, search } = await idb.value.addDBPosts(data, isReplace)
totalDB.value = count
total.value = count
seachFn.value = search
searchFn.value = search

await idb.value.setUserInfo(user)
}
Expand All @@ -99,16 +101,15 @@ export const usePostStore = defineStore('post', () => {
) {
await waitIDB()

if (!seachFn.value) {
if (!searchFn.value) {
const posts = await idb.value.getAllDBPosts()
const { search } = idb.value.buildSearch(posts)
seachFn.value = search
searchFn.value = search
}

const result = seachFn.value(query)
const result = searchFn.value(query)

const count = result.length
total.value = count
total.value = result.length
return result
}

Expand All @@ -131,7 +132,7 @@ export const usePostStore = defineStore('post', () => {
const path = route.path
const query = route.query.q as string

let result: Post[] = []
let result: Post[]

await waitIDB()

Expand Down Expand Up @@ -181,8 +182,7 @@ export const usePostStore = defineStore('post', () => {
const endIdx = startIdx + pageSize.value
const sliced = result.slice(startIdx, endIdx)

const posts = await idb.value.getDBPostByTime(sliced)
return posts
return await idb.value.getDBPostByTime(sliced)
}

async function updateTotal() {
Expand All @@ -196,7 +196,7 @@ export const usePostStore = defineStore('post', () => {
const p = _page || curPage.value
const query = route.query.q as string

let posts: Post[] = []
let posts: Post[]
if (route.path === '/post')
posts = await _getByTime(start, end, _page)

Expand Down Expand Up @@ -226,7 +226,7 @@ export const usePostStore = defineStore('post', () => {

clearDB: async () => {
await waitIDB()
idb.value.clearDB()
await idb.value.clearDB()
},
getAll: async () => {
await waitIDB()
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { ProtocolMap } from '@types'
export const protocolMap: ProtocolMap = {
img: {
text: '查看图片',
reg: /\[img:\/\/(.+?)\]/gm,
reg: /\[img:\/\/(.+?)]/gm,
/**
* 解析包含查看图片协议的文本
* 将返回包含 <ViewImgBtn /> 组件和原文本的 VNode
Expand Down
18 changes: 9 additions & 9 deletions packages/core/src/utils/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { Post, UID, UserBio, UserInfo } from '@types'

const POST_STORE = 'posts'
const USER_STORE = 'user'
const FOLLOWERINGS_STORE = 'followings'
const FLOWERINGS_STORE = 'followings'
export const DB_VERSION = 4

type AppDB = DBSchema & {
Expand All @@ -22,7 +22,7 @@ type AppDB = DBSchema & {
key: string
value: UserInfo
}
[FOLLOWERINGS_STORE]: {
[FLOWERINGS_STORE]: {
key: string
value: UserBio
}
Expand Down Expand Up @@ -67,8 +67,8 @@ export class IDB {
if (!db.objectStoreNames.contains(USER_STORE))
db.createObjectStore(USER_STORE, { keyPath: 'uid' })

if (!db.objectStoreNames.contains(FOLLOWERINGS_STORE))
db.createObjectStore(FOLLOWERINGS_STORE, { keyPath: 'uid' })
if (!db.objectStoreNames.contains(FLOWERINGS_STORE))
db.createObjectStore(FLOWERINGS_STORE, { keyPath: 'uid' })
},
})
}
Expand Down Expand Up @@ -211,13 +211,13 @@ export class IDB {
await Promise.all([
db.clear(POST_STORE),
db.clear(USER_STORE),
db.clear(FOLLOWERINGS_STORE),
db.clear(FLOWERINGS_STORE),
])
}

async clearFollowings() {
const db = await this.idb
await db.clear(FOLLOWERINGS_STORE)
await db.clear(FLOWERINGS_STORE)
}

/**
Expand Down Expand Up @@ -345,7 +345,7 @@ export class IDB {

async addFollowings(followings: UserBio[]) {
const db = await this.idb
const ts = db.transaction(FOLLOWERINGS_STORE, 'readwrite')
const ts = db.transaction(FLOWERINGS_STORE, 'readwrite')
const store = ts.store

followings.forEach((following) => {
Expand All @@ -357,11 +357,11 @@ export class IDB {

async getFollowings() {
const db = await this.idb
return await db.getAll(FOLLOWERINGS_STORE)
return await db.getAll(FLOWERINGS_STORE)
}
}

export type SaerchResult = FuseResult<{
export type SeachResult = FuseResult<{
time: number
text: string
}>[]
Expand Down
4 changes: 2 additions & 2 deletions packages/ui/src/AppMain.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import {
} from 'naive-ui'
// 如果在油猴脚本中,或者不是暗黑模式,不使用暗黑主题
const theme = computed(() => isInMonkey || !isDark.value ? undefined : darkTheme)
const theme = computed(() => isInMonkey || !isDark.value ? null : darkTheme)
</script>

<template>
<NConfigProvider
:locale="zhCN"
:date-locale="dateZhCN"
:theme="theme"
:theme
>
<NMessageProvider>
<slot />
Expand Down
3 changes: 1 addition & 2 deletions packages/ui/src/MainImage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ async function setImgSrc() {
return
const config = window.config.data
const src = `file://${config.publicPath}${props.src.slice(1)}`
realSrc.value = src
realSrc.value = `file://${config.publicPath}${props.src.slice(1)}`
}
watch(() => props.src, setImgSrc)
Expand Down
8 changes: 6 additions & 2 deletions packages/ui/src/Preview.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<script setup lang="ts">
import type { Post } from '@types'
import type { Post, User } from '@types'
import { KeyUser } from '@core/constants/vueProvide'
import { deleteOld } from '@core/utils/storage'
import { storeToRefs } from 'pinia'
import { useRoute } from 'vue-router'
const postStore = usePostStore()
const { curUid, users } = storeToRefs(usePublicStore())
const { curUid, users, curUser } = storeToRefs(usePublicStore())
const { totalDB } = storeToRefs(postStore)
const route = useRoute()
Expand All @@ -14,6 +16,8 @@ const posts = ref([] as Post[])
const loaded = ref(false)
const postsLoaded = ref(false)
provide(KeyUser, curUser as unknown as User)
onMounted(async () => {
// 删除旧版数据
await deleteOld()
Expand Down
6 changes: 2 additions & 4 deletions packages/ui/src/post/Item.vue
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
<script setup lang="ts">
import type { Post } from '@types'
import { storeToRefs } from 'pinia'
defineProps<{
post: Post
}>()
const openComment = ref(false)
const articleRef = ref<HTMLElement | null>(null)
const { curUser } = storeToRefs(usePublicStore())
watchEffect(() => {
if (!articleRef.value)
return
const comments = articleRef.value.querySelector<HTMLDivElement>('.n-collapse-item__content-wrapper')
const comments = articleRef.value?.querySelector<HTMLDivElement>('.n-collapse-item__content-wrapper')
comments?.style.setProperty('display', openComment.value ? 'block' : 'none')
})
Expand All @@ -26,7 +24,7 @@ watchEffect(() => {
class="post"
>
<div class="flex justify-between">
<post-profile :user="curUser" />
<post-profile />
<post-meta
:is-body="true"
:meta="post"
Expand Down
9 changes: 2 additions & 7 deletions packages/ui/src/post/Meta.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import type { Comment, Post, Retweet } from '@types'
import { useRoute } from 'vue-router'
import { useMessage } from 'naive-ui'
const props = defineProps<{
meta: Post | Comment | Retweet
Expand All @@ -9,12 +9,7 @@ const props = defineProps<{
const date = useDateFormat(props.meta.created_at, 'YY-MM-DD HH:mm')
const route = useRoute()
const { origin } = document.location
const url = computed(() =>
`${origin}${route.fullPath.replace(/#.+/, '')}#${(props.meta as any).mblogid}`,
)
const url = computed(() => `#${(props.meta as any).mblogid}`)
const message = useMessage()
const { copy } = useClipboard({
Expand Down
13 changes: 8 additions & 5 deletions packages/ui/src/post/Profile.vue
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
<script setup lang="ts">
import type { UserInfo } from '@types'
import type { User } from '@types'
import { KeyUser } from '@core/constants/vueProvide'
const props = defineProps<{
user: UserInfo
user?: User
}>()
const publicStore = usePublicStore()
const { user } = toRefs(props)
const user = toRef(props.user || inject(KeyUser)!)
const avatar = computed(() => {
const url = props.user.avatar
const url = user.value.avatar
if (!url)
return '/placeholder.webp'
if (publicStore.curUid === props.user.uid)
if (publicStore.curUid === user.value.uid)
return replaceImg(url)
// 对于其他人的头像,就使用 CDN
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/settings/base.vue
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ async function exportDatas() {
async function clearData() {
try {
postStore.clearDB()
await postStore.clearDB()
publicStore.rmUser()
message.success('清空成功')
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/shared.css
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
display: flex;
flex-direction: column;
min-height: 100dvh;
--uno: bg-light-700 dark:bg-dark-700
--uno: bg-light-700 dark:bg-dark-700;
}

.n-message-container.n-message-container--top {
Expand Down
3 changes: 2 additions & 1 deletion types/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ export type Retweet = Omit<Post, 'retweeted_status' | 'card' | 'user'> & {
user?: User // 转发的微博可能被删除或是被夹
}

export type Comment = Pick<Post, 'user' | 'text' | 'like_count' | 'comments_count' > & {
export type Comment = Pick<Post, 'text' | 'like_count' | 'comments_count' > & {
img: string
user: User
} & Meta

export interface PostMeta {
Expand Down

0 comments on commit 4327673

Please sign in to comment.