Skip to content

Commit

Permalink
fix: sort the posts
Browse files Browse the repository at this point in the history
  • Loading branch information
Chilfish committed Mar 3, 2024
1 parent e3cc14c commit 195c236
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions packages/core/src/stores/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,10 @@ export const usePostStore = defineStore('post', () => {
})
}

const _ids = after.map(post => `post-${post.mblogid}`)
const _ids = after.map(post => `post-${post.id}`)

await setDB('ids', _ids)
await setMany(
after
.sort((a, b) => b.id - a.id)
.map(post => [`post-${post.mblogid}`, post]),
)
await setMany(after.map(post => [`post-${post.id}`, post]))
ids.value = _ids
total.value = after.length
posts.value = after
Expand All @@ -75,12 +71,18 @@ export const usePostStore = defineStore('post', () => {
const path = route.path
const query = route.query.q as string

if (posts.value.length === 0) {
posts.value = (
await getMany<Post>(ids.value)
.then(res =>
res.filter(Boolean).sort((a, b) => b.id - a.id),
)
)
}

if (path === '/post') {
total.value = ids.value.length
if (posts.value.length === 0)
return await getMany<Post>(ids.value.slice(...sliceDis))
else
return posts.value.slice(...sliceDis)
return posts.value.slice(...sliceDis)
}
else {
const data = await searchText(query)
Expand All @@ -91,9 +93,6 @@ export const usePostStore = defineStore('post', () => {

// TODO: 优化
async function searchText(p: string): Promise<Post[]> {
if (posts.value.length === 0)
posts.value = (await getMany<Post>(ids.value)) || []

const res = posts.value.filter((post) => {
const word = p.toLowerCase().trim().replace(/ /g, '')
const regex = new RegExp(word, 'igm')
Expand Down

0 comments on commit 195c236

Please sign in to comment.