refs #3301 Rewrite TimelineSpace/Contents/SideBar/AccountProfile/Timeline/PostsAndReplies with composition API
This commit is contained in:
parent
7fc79716f6
commit
666d7a0db9
|
@ -24,118 +24,137 @@
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script lang="ts">
|
||||||
import { mapState, mapGetters } from 'vuex'
|
import { defineComponent, PropType, ref, computed, onMounted, onBeforeUnmount, onUnmounted, watch, toRefs } from 'vue'
|
||||||
import Toot from '~/src/renderer/components/organisms/Toot'
|
import { ElMessage } from 'element-plus'
|
||||||
import { EventEmitter } from '~/src/renderer/components/event'
|
import { useI18next } from 'vue3-i18next'
|
||||||
|
import { Entity } from 'megalodon'
|
||||||
|
import { useStore } from '@/store'
|
||||||
|
import Toot from '@/components/organisms/Toot.vue'
|
||||||
|
import { EventEmitter } from '@/components/event'
|
||||||
|
import { ACTION_TYPES, MUTATION_TYPES } from '@/store/TimelineSpace/Contents/SideBar/AccountProfile/Timeline/PostsAndReplies'
|
||||||
|
|
||||||
export default {
|
export default defineComponent({
|
||||||
name: 'posts-and-replies',
|
name: 'posts-and-replies',
|
||||||
props: ['account', 'buffer', 'filters'],
|
|
||||||
components: { Toot },
|
components: { Toot },
|
||||||
data() {
|
props: {
|
||||||
return {
|
account: {
|
||||||
focusedId: null
|
type: Object as PropType<Entity.Account>,
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
buffer: {
|
||||||
|
type: Number,
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
filters: {
|
||||||
|
type: Object as PropType<Array<Entity.Filter>>,
|
||||||
|
required: true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
computed: {
|
setup(props) {
|
||||||
...mapState('TimelineSpace/Contents/SideBar/AccountProfile/Timeline/PostsAndReplies', {
|
const space = 'TimelineSpace/Contents/SideBar/AccountProfile/Timeline/PostsAndReplies'
|
||||||
timeline: state => state.timeline,
|
const store = useStore()
|
||||||
lazyLoading: state => state.lazyLoading
|
const i18n = useI18next()
|
||||||
}),
|
const { account } = toRefs(props)
|
||||||
...mapState('App', {
|
|
||||||
backgroundColor: state => state.theme.background_color
|
const focusedId = ref<string | null>(null)
|
||||||
}),
|
|
||||||
...mapGetters('TimelineSpace/Modals', ['modalOpened'])
|
const timeline = computed(() => store.state.TimelineSpace.Contents.SideBar.AccountProfile.Timeline.PostsAndReplies.timeline)
|
||||||
},
|
const lazyLoading = computed(() => store.state.TimelineSpace.Contents.SideBar.AccountProfile.Timeline.PostsAndReplies.lazyLoading)
|
||||||
created() {
|
const backgroundColor = computed(() => store.state.App.theme.background_color)
|
||||||
this.load()
|
const modalOpened = computed(() => store.getters[`TimelineSpace/Modals/modalOpened`])
|
||||||
},
|
const currentFocusedIndex = computed(() => timeline.value.findIndex(toot => focusedId.value === toot.uri + toot.id))
|
||||||
mounted() {
|
|
||||||
this.$store.dispatch('TimelineSpace/Contents/SideBar/AccountProfile/Timeline/PostsAndReplies/clearTimeline')
|
onMounted(() => {
|
||||||
document.getElementById('sidebar_scrollable').addEventListener('scroll', this.onScroll)
|
load()
|
||||||
EventEmitter.on('focus-sidebar', () => {
|
store.dispatch(`${space}/${ACTION_TYPES.CLEAR_TIMELINE}`)
|
||||||
this.focusedId = 0
|
document.getElementById('sidebar_scrollable')?.addEventListener('scroll', onScroll)
|
||||||
this.$nextTick(function () {
|
|
||||||
this.focusedId = this.timeline[0].uri + this.timeline[0].id
|
|
||||||
})
|
})
|
||||||
})
|
onBeforeUnmount(() => {
|
||||||
},
|
|
||||||
beforeUnmount() {
|
|
||||||
EventEmitter.emit('focus-timeline')
|
EventEmitter.emit('focus-timeline')
|
||||||
EventEmitter.off('focus-sidebar')
|
EventEmitter.off('focus-sidebar')
|
||||||
},
|
})
|
||||||
unmounted() {
|
onUnmounted(() => {
|
||||||
if (document.getElementById('sidebar_scrollable') !== undefined && document.getElementById('sidebar_scrollable') !== null) {
|
const el = document.getElementById('sidebar_scrollable')
|
||||||
document.getElementById('sidebar_scrollable').removeEventListener('scroll', this.onScroll)
|
if (el !== undefined && el !== null) {
|
||||||
|
el.removeEventListener('scroll', onScroll)
|
||||||
}
|
}
|
||||||
},
|
})
|
||||||
watch: {
|
watch(account, () => {
|
||||||
account: function (_newAccount, _oldAccount) {
|
store.dispatch(`${space}/${ACTION_TYPES.CLEAR_TIMELINE}`)
|
||||||
this.$store.dispatch('TimelineSpace/Contents/SideBar/AccountProfile/Timeline/PostsAndReplies/clearTimeline')
|
load()
|
||||||
this.load()
|
})
|
||||||
}
|
|
||||||
},
|
const load = () => {
|
||||||
methods: {
|
store.dispatch(`${space}/${ACTION_TYPES.FETCH_TIMELINE}`, account.value).catch(() => {
|
||||||
load() {
|
ElMessage({
|
||||||
this.$store
|
message: i18n.t('message.timeline_fetch_error'),
|
||||||
.dispatch('TimelineSpace/Contents/SideBar/AccountProfile/Timeline/PostsAndReplies/fetchTimeline', this.account)
|
|
||||||
.catch(() => {
|
|
||||||
this.$message({
|
|
||||||
message: this.$t('message.timeline_fetch_error'),
|
|
||||||
type: 'error'
|
type: 'error'
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
},
|
}
|
||||||
updateToot(message) {
|
const onScroll = (event: Event) => {
|
||||||
this.$store.commit('TimelineSpace/Contents/SideBar/AccountProfile/Timeline/PostsAndReplies/updateToot', message)
|
|
||||||
},
|
|
||||||
deleteToot(message) {
|
|
||||||
this.$store.commit('TimelineSpace/Contents/SideBar/AccountProfile/Timeline/PostsAndReplies/deleteToot', message)
|
|
||||||
},
|
|
||||||
onScroll(event) {
|
|
||||||
// for lazyLoading
|
// for lazyLoading
|
||||||
if (
|
if (
|
||||||
event.target.clientHeight + event.target.scrollTop >= document.getElementById('account_profile').clientHeight - 10 &&
|
(event.target as HTMLElement)!.clientHeight + (event.target as HTMLElement)!.scrollTop >=
|
||||||
!this.lazyloading
|
document.getElementById('account_profile')!.clientHeight - 10 &&
|
||||||
|
!lazyLoading
|
||||||
) {
|
) {
|
||||||
this.$store
|
store
|
||||||
.dispatch('TimelineSpace/Contents/SideBar/AccountProfile/Timeline/PostsAndReplies/lazyFetchTimeline', {
|
.dispatch(`${space}/${ACTION_TYPES.LAZY_FETCH_TIMELINE}`, {
|
||||||
account: this.account,
|
account: account.value,
|
||||||
status: this.timeline[this.timeline.length - 1]
|
status: timeline[timeline.value.length - 1]
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
this.$message({
|
ElMessage({
|
||||||
message: this.$t('message.timeline_fetch_error'),
|
message: i18n.t('message.timeline_fetch_error'),
|
||||||
type: 'error'
|
type: 'error'
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
},
|
|
||||||
focusNext() {
|
|
||||||
const currentIndex = this.timeline.findIndex(toot => this.focusedId === toot.uri + toot.id)
|
|
||||||
if (currentIndex === -1) {
|
|
||||||
this.focusedId = this.timeline[0].uri + this.timeline[0].id
|
|
||||||
} else if (currentIndex < this.timeline.length - 1) {
|
|
||||||
this.focusedId = this.timeline[currentIndex + 1].uri + this.timeline[currentIndex + 1].id
|
|
||||||
}
|
}
|
||||||
},
|
const updateToot = (message: Entity.Status) => {
|
||||||
focusPrev() {
|
store.commit(`${space}/${MUTATION_TYPES.UPDATE_TOOT}`, message)
|
||||||
const currentIndex = this.timeline.findIndex(toot => this.focusedId === toot.uri + toot.id)
|
|
||||||
if (currentIndex > 0) {
|
|
||||||
this.focusedId = this.timeline[currentIndex - 1].uri + this.timeline[currentIndex - 1].id
|
|
||||||
}
|
}
|
||||||
},
|
const deleteToot = (message: Entity.Status) => {
|
||||||
focusToot(message) {
|
store.commit(`${space}/${MUTATION_TYPES.DELETE_TOOT}`, message)
|
||||||
this.focusedId = message.uri + message.id
|
}
|
||||||
},
|
const focusNext = () => {
|
||||||
focusTimeline() {
|
if (currentFocusedIndex.value === -1) {
|
||||||
this.focusedId = 0
|
focusedId.value = timeline.value[0].uri + timeline.value[0].id
|
||||||
|
} else if (currentFocusedIndex.value < timeline.value.length - 1) {
|
||||||
|
focusedId.value = timeline.value[currentFocusedIndex.value + 1].uri + timeline.value[currentFocusedIndex.value + 1].id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const focusPrev = () => {
|
||||||
|
if (currentFocusedIndex.value > 0) {
|
||||||
|
focusedId.value = timeline.value[currentFocusedIndex.value - 1].uri + timeline.value[currentFocusedIndex.value - 1].id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const focusToot = (message: Entity.Status) => {
|
||||||
|
focusedId.value = message.uri + message.id
|
||||||
|
}
|
||||||
|
const focusTimeline = () => {
|
||||||
|
focusedId.value = null
|
||||||
EventEmitter.emit('focus-timeline')
|
EventEmitter.emit('focus-timeline')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
timeline,
|
||||||
|
focusedId,
|
||||||
|
modalOpened,
|
||||||
|
updateToot,
|
||||||
|
deleteToot,
|
||||||
|
focusNext,
|
||||||
|
focusPrev,
|
||||||
|
focusTimeline,
|
||||||
|
focusToot,
|
||||||
|
lazyLoading,
|
||||||
|
backgroundColor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
<style lang="scss" scoped>
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import generator, { Entity } from 'megalodon'
|
import generator, { Entity } from 'megalodon'
|
||||||
import Timeline, { TimelineState } from './AccountProfile/Timeline'
|
import Timeline, { TimelineModuleState } from './AccountProfile/Timeline'
|
||||||
import Follows, { FollowsState } from './AccountProfile/Follows'
|
import Follows, { FollowsState } from './AccountProfile/Follows'
|
||||||
import Followers, { FollowersState } from './AccountProfile/Followers'
|
import Followers, { FollowersState } from './AccountProfile/Followers'
|
||||||
import { Module, MutationTree, ActionTree, GetterTree } from 'vuex'
|
import { Module, MutationTree, ActionTree, GetterTree } from 'vuex'
|
||||||
|
@ -26,7 +26,7 @@ export type AccountProfileState = {
|
||||||
type AccountProfileModule = {
|
type AccountProfileModule = {
|
||||||
Followers: FollowersState
|
Followers: FollowersState
|
||||||
Follows: FollowsState
|
Follows: FollowsState
|
||||||
Timeline: TimelineState
|
Timeline: TimelineModuleState
|
||||||
}
|
}
|
||||||
|
|
||||||
export type AccountProfileModuleState = AccountProfileModule & AccountProfileState
|
export type AccountProfileModuleState = AccountProfileModule & AccountProfileState
|
||||||
|
|
|
@ -6,7 +6,7 @@ import PostsAndReplies, { PostsAndRepliesState } from './Timeline/PostsAndReplie
|
||||||
import Media, { MediaState } from './Timeline/Media'
|
import Media, { MediaState } from './Timeline/Media'
|
||||||
import { FilterContext } from 'megalodon'
|
import { FilterContext } from 'megalodon'
|
||||||
|
|
||||||
export type TimelineState = {}
|
type TimelineState = {}
|
||||||
|
|
||||||
type TimelineModule = {
|
type TimelineModule = {
|
||||||
Posts: PostsState
|
Posts: PostsState
|
||||||
|
|
|
@ -59,8 +59,14 @@ const mutations: MutationTree<PostsAndRepliesState> = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const ACTION_TYPES = {
|
||||||
|
FETCH_TIMELINE: 'fetchTimeline',
|
||||||
|
LAZY_FETCH_TIMELINE: 'lazyFetchTimeline',
|
||||||
|
CLEAR_TIMELINE: 'clearTimeline'
|
||||||
|
}
|
||||||
|
|
||||||
const actions: ActionTree<PostsAndRepliesState, RootState> = {
|
const actions: ActionTree<PostsAndRepliesState, RootState> = {
|
||||||
fetchTimeline: async ({ commit, rootState }, account: Entity.Account) => {
|
[ACTION_TYPES.FETCH_TIMELINE]: async ({ commit, rootState }, account: Entity.Account) => {
|
||||||
commit('TimelineSpace/Contents/SideBar/AccountProfile/changeLoading', true, { root: true })
|
commit('TimelineSpace/Contents/SideBar/AccountProfile/changeLoading', true, { root: true })
|
||||||
const client = generator(
|
const client = generator(
|
||||||
rootState.TimelineSpace.sns,
|
rootState.TimelineSpace.sns,
|
||||||
|
@ -73,7 +79,7 @@ const actions: ActionTree<PostsAndRepliesState, RootState> = {
|
||||||
commit(MUTATION_TYPES.UPDATE_TIMELINE, res.data)
|
commit(MUTATION_TYPES.UPDATE_TIMELINE, res.data)
|
||||||
return res.data
|
return res.data
|
||||||
},
|
},
|
||||||
lazyFetchTimeline: async ({ state, commit, rootState }, loadPosition: LoadPositionWithAccount): Promise<null> => {
|
[ACTION_TYPES.LAZY_FETCH_TIMELINE]: async ({ state, commit, rootState }, loadPosition: LoadPositionWithAccount): Promise<null> => {
|
||||||
if (state.lazyLoading) {
|
if (state.lazyLoading) {
|
||||||
return Promise.resolve(null)
|
return Promise.resolve(null)
|
||||||
}
|
}
|
||||||
|
@ -96,7 +102,7 @@ const actions: ActionTree<PostsAndRepliesState, RootState> = {
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
},
|
},
|
||||||
clearTimeline: ({ commit }) => {
|
[ACTION_TYPES.CLEAR_TIMELINE]: ({ commit }) => {
|
||||||
commit(MUTATION_TYPES.UPDATE_TIMELINE, [])
|
commit(MUTATION_TYPES.UPDATE_TIMELINE, [])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue