@ -21,6 +21,16 @@ type CommentsPayload = {
comments : CommentNode [ ]
}
type MeConfigPayload = {
config ? : Record < string , unknown >
}
type MeProfilePayload = {
profile ? : {
email ? : string | null
}
}
const props = defineProps < {
mode : 'public-post' | 'unlisted'
publicSlug : string
@ -65,6 +75,32 @@ const { data, pending, error, refresh: refreshComments } = await useAsyncData(
{ watch : [ commentsBase ] } ,
)
const { data : viewerNotifyState } = await useAsyncData (
( ) => 'viewer-comment-notify-state' ,
async ( ) => {
if ( ! loggedIn . value ) {
return null
}
const fetcher = import . meta . server ? useRequestFetch ( ) : $fetch
const [ meConfigRes , meProfileRes ] = await Promise . all ( [
fetcher < ApiResponse < MeConfigPayload > > ( '/api/config/me' ) ,
fetcher < ApiResponse < MeProfilePayload > > ( '/api/me/profile' ) ,
] )
const cfg = unwrapApiBody ( meConfigRes ) . config ? ? { }
const profile = unwrapApiBody ( meProfileRes ) . profile ? ? { }
const commentNotifyEnabled = typeof cfg . commentNotifyEnabled === 'boolean' ? cfg . commentNotifyEnabled : true
const email = typeof profile . email === 'string' ? profile . email . trim ( ) : ''
return {
commentNotifyEnabled ,
hasEmail : email . length > 0 ,
}
} ,
{ watch : [ loggedIn ] } ,
)
const showNoEmailHint = computed ( ( ) => loggedIn . value && viewerNotifyState . value ? . hasEmail === false )
const showNotifyDisabledHint = computed ( ( ) => loggedIn . value && viewerNotifyState . value ? . commentNotifyEnabled === false )
const flatComments = computed ( ( ) => flattenTree ( data . value ? . comments ? ? [ ] ) )
function flattenTree ( nodes : CommentNode [ ] , depth = 0 ) : Array < { node : CommentNode ; depth : number } > {
@ -232,6 +268,12 @@ async function submitComment() {
< UButton size = "xs" variant = "link" color = "neutral" label = "取消" @ click = "replyToId = null" / >
< / p >
< template v-if ="loggedIn" >
< p v-if ="showNoEmailHint" class="text-sm text-warning" >
未填写邮箱将无法接收评论通知 。
< / p >
< p v-if ="showNotifyDisabledHint" class="text-sm text-warning" >
你已关闭评论邮件通知 。
< / p >
< UTextarea v -model = " draftBody " placeholder = "写下你的评论…" :rows ="4" class = "w-full" / >
< UButton :loading ="submitting" label = "发表" @click ="submitComment" / >
< / template >