Skip to content

Commit

Permalink
feat(web): show post detail on clicked
Browse files Browse the repository at this point in the history
  • Loading branch information
Chilfish committed Aug 6, 2024
1 parent 88ec83b commit f667f3b
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 34 deletions.
1 change: 1 addition & 0 deletions apps/web/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default defineNuxtConfig({
alias: {
'@core': core,
'@shared': shared,
'@ui': ui,
},

imports: {
Expand Down
4 changes: 3 additions & 1 deletion apps/web/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ onNuxtReady(async () => {

<template>
<nuxt-layout>
<NuxtPage v-if="loaded" />
<AppMain v-if="loaded">
<NuxtPage />
</AppMain>
<n-spin
v-else
class="center pt-16"
Expand Down
48 changes: 43 additions & 5 deletions apps/web/src/pages/album.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
<script setup lang="ts">
import type { Album } from '@shared'
import { PostItem } from '@ui'
import { useModal } from 'naive-ui'
import { KeyUser } from '@core/constants/vueProvide'
import { useWindowSize } from '@vueuse/core'
const album = shallowRef<Album[]>([])
const postStore = usePostStore()
const width = '12rem'
const { width: windowWidth } = useWindowSize()
const isMobile = computed(() => windowWidth.value < 768)
const width = computed(() => isMobile.value ? '7rem' : '12rem')
const loadSize = 16
const loadedAlbum = ref<AlbumGroup[]>([]) // 用于存储已加载的相册
Expand Down Expand Up @@ -52,22 +62,49 @@ function handleLoad() {
})
}
const modal = useModal()
const message = useMessage()
async function openDetail(id: string) {
const post = await postStore.getById(id)
if (!post)
return message.warning('微博不存在本地')
modal.create({
title: '微博详情',
preset: 'card',
style: {
width: '50rem',
},
content: () =>
h(
PostItem,
{
post,
},
),
})
}
const publicStore = usePublicStore()
onMounted(async () => {
album.value = await postStore.getAllImgs()
handleLoad()
const { curUser } = publicStore
console.log(curUser)
provide(KeyUser, curUser as unknown as User)
})
</script>

<template>
<main
id="album"
class="mx-auto mt-20 rounded-2 p-4 md:w-70rem"
class="mx-auto rounded-2 p-4 md:w-70rem"
bg="light dark:dark"
>
<n-infinite-scroll
:distance="5"
:style="{
height: 'calc(100vh - 8rem)',
height: 'calc(100vh - 6rem)',
}"
@load="handleLoad"
>
Expand All @@ -78,13 +115,12 @@ onMounted(async () => {
:key="item.date"
class="mb-4 flex flex-col gap-2"
>
<h2 class="w-full text-start text-2xl font-bold">
<h2 class="w-full text-start text-5 font-bold">
{{ item.date }}
</h2>
<div
class="flex flex-wrap items-center gap-1 px-6"
>
<!-- {{ item.imgs }} -->
<MainImage
v-for="{ id, url } in item.imgs"
:id="`img-${id}`"
Expand All @@ -95,7 +131,9 @@ onMounted(async () => {
height: width,
}"
:min-height="width"
:preview="false"
fit="cover"
@click="() => openDetail(id)"
/>
</div>
</section>
Expand Down
19 changes: 11 additions & 8 deletions packages/core/src/stores/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { EmptyIDB, IDB } from '../utils/storage'
export const usePostStore = defineStore('post', () => {
const publicStore = usePublicStore()
const followings = shallowRef<UserBio[]>([])
const allImages: Album[] = []

const idb = ref(new EmptyIDB())
watchImmediate(() => publicStore.curUid, async (uid) => {
Expand Down Expand Up @@ -206,6 +207,11 @@ export const usePostStore = defineStore('post', () => {
return posts
}

async function getById(id: string) {
await waitIDB()
return await idb.value.getDBPostById(id)
}

async function getFollowings() {
await waitIDB()
return await idb.value
Expand All @@ -217,17 +223,12 @@ export const usePostStore = defineStore('post', () => {
* 获取所有图片,以月份分组
*/
async function getAllImgs() {
if (allImages.length)
return allImages

await waitIDB()
const imgs = await idb.value.getImgs()

/**
* [
* {
* date: '2021年01月',
* imgs: ['url1', 'url2', ...]
* }
* ]
*/
const result: Album[] = []

for (const { img, date, id } of imgs) {
Expand All @@ -238,6 +239,7 @@ export const usePostStore = defineStore('post', () => {
const data = { url: img, id, date: key }
result.push(data)
}
allImages.push(...result)

return result
}
Expand Down Expand Up @@ -275,5 +277,6 @@ export const usePostStore = defineStore('post', () => {
searchAndTime,
getFollowings,
getAllImgs,
getById,
}
})
10 changes: 9 additions & 1 deletion packages/core/src/utils/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ export class IDB {
return posts
}

async getDBPostById(id: string) {
const posts = await this.getAllDBPosts()
return posts.find(post => post.mblogid === id)
}

/**
* 获取帖子总数
*/
Expand Down Expand Up @@ -381,7 +386,9 @@ export class IDB {
const posts = await this.getAllDBPosts()

posts
.reverse()
.sort((a, b) => {
return dayjs(b.created_at).valueOf() - dayjs(a.created_at).valueOf()
}) // 新帖子在前
.forEach((post) => {
post.imgs.forEach((img) => {
result.push({
Expand Down Expand Up @@ -415,6 +422,7 @@ export class EmptyIDB extends IDB {
async getDBPosts(_page = 1, _limit = 10) { return [] as Post[] }
async getDBPostByTime(_times: number[]) { return [] as Post[] }
async getAllDBPosts() { return [] as Post[] }
async getDBPostById(_id: string) { return {} as Post | undefined }
async getPostCount() { return 0 }
async clearDB() { }
async getSize() { return '0' }
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "",
"license": "ISC",
"keywords": [],
"main": "index.ts",
"main": "src/index.ts",
"dependencies": {
"@weibo-archiver/core": "workspace:^",
"sass": "^1.77.8"
Expand Down
9 changes: 6 additions & 3 deletions packages/ui/src/AppMain.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import {
NConfigProvider,
NMessageProvider,
NModalProvider,
darkTheme,
dateZhCN,
zhCN,
Expand All @@ -17,8 +18,10 @@ const theme = computed(() => isInMonkey || !isDark.value ? null : darkTheme)
:date-locale="dateZhCN"
:theme
>
<NMessageProvider>
<slot />
</NMessageProvider>
<NModalProvider>
<NMessageProvider>
<slot />
</NMessageProvider>
</NModalProvider>
</NConfigProvider>
</template>
44 changes: 31 additions & 13 deletions packages/ui/src/MainHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,25 @@ const showSettings = ref(false)
:style="headerStyle"
class="fixed z-99 h-16 w-full flex items-center gap-4 bg-[#69696A30] px-4 backdrop-blur-8 transition-all sm:px-8"
>
<router-link
class="i-tabler-brand-weibo icon h-6 w-6"
:to="`/post?page=1&pageSize=${route.query.pageSize || 10}`"
title="返回首页"
/>
<n-tooltip trigger="hover">
<template #trigger>
<router-link
class="i-tabler-brand-weibo icon h-6 w-6"
to="/post"
/>
</template>
返回首页
</n-tooltip>

<n-tooltip trigger="hover">
<template #trigger>
<router-link
class="i-tabler:photo icon h-6 w-6"
to="/album"
/>
</template>
查看相册
</n-tooltip>

<form
class="relative mr-auto h-12 min-w-2/5"
Expand All @@ -66,13 +80,17 @@ const showSettings = ref(false)
>
</form>

<button
class="rounded-2 p-1.6"
hover:bg="light-200 dark:dark-200"
title="打开设置"
@click="() => showSettings = true"
>
<i class="i-tabler:settings icon h-6 w-6" />
</button>
<n-tooltip trigger="hover">
<template #trigger>
<button
class="rounded-2 p-1.6"
hover:bg="light-200 dark:dark-200"
@click="() => showSettings = true"
>
<i class="i-tabler:settings icon h-6 w-6" />
</button>
</template>
打开设置
</n-tooltip>
</header>
</template>
7 changes: 6 additions & 1 deletion packages/ui/src/MainImage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { useStorage } from '@vueuse/core'
import { ImgPlaceholder } from '@core/constants'
import type { ImageProps } from 'naive-ui'
const props = withDefaults(defineProps<{
src: string
alt?: string
Expand All @@ -10,10 +12,12 @@ const props = withDefaults(defineProps<{
minHeight?: string | number
fit?: 'fill' | 'contain' | 'cover' | 'none' | 'scale-down'
lazy?: boolean
}>(), {
preview?: boolean
} >(), {
fit: 'contain',
alt: 'image',
lazy: true,
preview: true,
})
const realSrc = ref(props.src)
Expand Down Expand Up @@ -64,6 +68,7 @@ onUnmounted(() => {
minHeight,
},
}"
:preview-disabled="!preview"
/>
</template>

Expand Down
5 changes: 5 additions & 0 deletions packages/ui/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import PostItem from './post/Item.vue'

export {
PostItem,
}
2 changes: 1 addition & 1 deletion packages/ui/src/post/Profile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const props = defineProps<{
const publicStore = usePublicStore()
const user = toRef(props.user || inject(KeyUser)!)
const user = toRef(props.user || inject(KeyUser) || publicStore.curUser!)
const avatar = computed(() => {
const url = user.value.avatar
Expand Down

0 comments on commit f667f3b

Please sign in to comment.