refs #3301 Rewrite TimelineSpace/Contents/Home with composition API

This commit is contained in:
AkiraFukushima 2022-05-28 13:31:33 +09:00
parent a403c93208
commit ad6a16ca89
No known key found for this signature in database
GPG Key ID: B6E51BAC4DE1A957
17 changed files with 314 additions and 291 deletions

View File

@ -30,7 +30,6 @@
<script>
import { mapState, mapGetters } from 'vuex'
import Toot from '@/components/organisms/Toot'
import reloadable from '~/src/renderer/components/mixins/reloadable'
import { EventEmitter } from '~/src/renderer/components/event'
export default {
@ -42,7 +41,6 @@ export default {
},
name: 'bookmarks',
components: { Toot },
mixins: [reloadable],
computed: {
...mapState('TimelineSpace', {
account: state => state.account
@ -145,7 +143,6 @@ export default {
async reload() {
this.$store.commit('TimelineSpace/changeLoading', true)
try {
const account = await this.reloadable()
await this.$store.dispatch('TimelineSpace/Contents/Bookmarks/fetchBookmarks', account).catch(() => {
this.$message({
message: this.$t('message.bookmark_fetch_error'),

View File

@ -33,7 +33,6 @@
import { mapState, mapGetters } from 'vuex'
import moment from 'moment'
import Toot from '~/src/renderer/components/organisms/Toot'
import reloadable from '~/src/renderer/components/mixins/reloadable'
import { EventEmitter } from '~/src/renderer/components/event'
import { ScrollPosition } from '~/src/renderer/components/utils/scroll'
@ -49,7 +48,6 @@ export default {
},
name: 'directmessages',
components: { Toot },
mixins: [reloadable],
computed: {
...mapState('TimelineSpace/Contents/DirectMessages', {
timeline: state => state.timeline,
@ -220,7 +218,6 @@ export default {
async reload() {
this.$store.commit('TimelineSpace/changeLoading', true)
try {
await this.reloadable()
} finally {
this.$store.commit('TimelineSpace/changeLoading', false)
}

View File

@ -31,7 +31,6 @@
<script>
import { mapState, mapGetters } from 'vuex'
import Toot from '~/src/renderer/components/organisms/Toot'
import reloadable from '~/src/renderer/components/mixins/reloadable'
import { EventEmitter } from '~/src/renderer/components/event'
export default {
@ -43,7 +42,6 @@ export default {
},
name: 'favourites',
components: { Toot },
mixins: [reloadable],
computed: {
...mapState({
openSideBar: state => state.TimelineSpace.Contents.SideBar.openSideBar,
@ -140,7 +138,6 @@ export default {
async reload() {
this.$store.commit('TimelineSpace/changeLoading', true)
try {
const account = await this.reloadable()
await this.$store.dispatch('TimelineSpace/Contents/Favourites/fetchFavourites', account).catch(() => {
this.$message({
message: this.$t('message.favourite_fetch_error'),

View File

@ -33,7 +33,6 @@
import { mapState, mapGetters } from 'vuex'
import moment from 'moment'
import Toot from '~/src/renderer/components/organisms/Toot'
import reloadable from '~/src/renderer/components/mixins/reloadable'
import { EventEmitter } from '~/src/renderer/components/event'
import { ScrollPosition } from '~/src/renderer/components/utils/scroll'
@ -49,7 +48,6 @@ export default {
},
name: 'tag',
components: { Toot },
mixins: [reloadable],
props: ['tag'],
computed: {
...mapState({
@ -223,7 +221,6 @@ export default {
const tag = this.tag
this.$store.commit('TimelineSpace/changeLoading', true)
try {
await this.reloadable()
await this.$store.dispatch('TimelineSpace/Contents/Hashtag/Tag/stopStreaming')
await this.$store.dispatch('TimelineSpace/Contents/Hashtag/Tag/fetch', tag).catch(() => {
this.$message({

View File

@ -37,254 +37,243 @@
</div>
</template>
<script>
import { mapState, mapGetters } from 'vuex'
<script lang="ts">
import { defineComponent, ref, computed, onMounted, onBeforeUpdate, onBeforeUnmount, watch } from 'vue'
import moment from 'moment'
import Toot from '~/src/renderer/components/organisms/Toot'
import StatusLoading from '~/src/renderer/components/organisms/StatusLoading'
import reloadable from '~/src/renderer/components/mixins/reloadable'
import { EventEmitter } from '~/src/renderer/components/event'
import { ScrollPosition } from '~/src/renderer/components/utils/scroll'
import { ElMessage } from 'element-plus'
import { Entity } from 'megalodon'
import { useI18next } from 'vue3-i18next'
import { useRoute } from 'vue-router'
import { useStore } from '@/store'
import Toot from '@/components/organisms/Toot.vue'
import StatusLoading from '@/components/organisms/StatusLoading.vue'
import { EventEmitter } from '@/components/event'
import { ScrollPosition } from '@/components/utils/scroll'
import { ACTION_TYPES, MUTATION_TYPES } from '@/store/TimelineSpace/Contents/Home'
import { MUTATION_TYPES as SIDE_MENU_MUTATION } from '@/store/TimelineSpace/SideMenu'
import { MUTATION_TYPES as TIMELINE_MUTATION } from '@/store/TimelineSpace'
import { MUTATION_TYPES as HEADER_MUTATION } from '@/store/TimelineSpace/HeaderMenu'
import useReloadable from '@/components/utils/reloadable'
export default {
data() {
return {
focusedId: null,
scrollPosition: null,
observer: null,
scrollTime: null,
resizeTime: null,
loadingMore: false
}
},
export default defineComponent({
name: 'home',
components: { Toot, StatusLoading },
mixins: [reloadable],
computed: {
...mapState('TimelineSpace/Contents/Home', {
timeline: state => state.timeline,
lazyLoading: state => state.lazyLoading,
heading: state => state.heading,
showReblogs: state => state.showReblogs,
showReplies: state => state.showReplies,
scrolling: state => state.scrolling
}),
...mapState({
backgroundColor: state => state.App.theme.background_color,
openSideBar: state => state.TimelineSpace.Contents.SideBar.openSideBar,
startReload: state => state.TimelineSpace.HeaderMenu.reload
}),
...mapGetters('TimelineSpace/Modals', ['modalOpened']),
...mapGetters('TimelineSpace/Contents/Home', ['filters']),
shortcutEnabled: function () {
if (this.modalOpened) {
return false
}
if (!this.focusedId) {
return true
}
// Sometimes toots are deleted, so perhaps focused toot don't exist.
const currentIndex = this.timeline.findIndex(toot => this.focusedId === toot.uri + toot.id)
return currentIndex === -1
},
filteredTimeline() {
return this.timeline.filter(toot => {
if (toot.in_reply_to_id) {
return this.showReplies
} else if (toot.reblog) {
return this.showReblogs
setup() {
const space = 'TimelineSpace/Contents/Home'
const store = useStore()
const route = useRoute()
const i18n = useI18next()
const { reloadable } = useReloadable(store, route, i18n)
const focusedId = ref<string | null>(null)
const scrollPosition = ref<ScrollPosition | null>(null)
const observer = ref<ResizeObserver | null>(null)
const scrollTime = ref<moment.Moment | null>(null)
const resizeTime = ref<moment.Moment | null>(null)
const loadingMore = ref(false)
const scroller = ref<any>()
const timeline = computed(() => store.state.TimelineSpace.Contents.Home.timeline)
const lazyLoading = computed(() => store.state.TimelineSpace.Contents.Home.lazyLoading)
const heading = computed(() => store.state.TimelineSpace.Contents.Home.heading)
const showReblogs = computed(() => store.state.TimelineSpace.Contents.Home.showReblogs)
const showReplies = computed(() => store.state.TimelineSpace.Contents.Home.showReplies)
const scrolling = computed(() => store.state.TimelineSpace.Contents.Home.scrolling)
const openSideBar = computed(() => store.state.TimelineSpace.Contents.SideBar.openSideBar)
const startReload = computed(() => store.state.TimelineSpace.HeaderMenu.reload)
const modalOpened = computed(() => store.getters[`TimelineSpace/Modals/modalOpened`])
const filters = computed(() => store.getters[`${space}/filters}`])
const currentFocusedIndex = computed(() => timeline.value.findIndex(toot => focusedId.value === toot.uri + toot.id))
// const shortcutEnabled = computed(() => {
// if (modalOpened.value) {
// return false
// }
// if (!focusedId.value) {
// return true
// }
// // Sometimes toots are deleted, so perhaps focused toot don't exist.
// return currentFocusedIndex.value === -1
// })
const filteredTimeline = computed(() => {
return timeline.value.filter(toot => {
if ('url' in toot) {
if (toot.in_reply_to_id) {
return showReplies.value
} else if (toot.reblog) {
return showReblogs.value
} else {
return true
}
} else {
return true
}
})
}
},
mounted() {
this.$store.commit('TimelineSpace/SideMenu/changeUnreadHomeTimeline', false)
document.getElementById('scroller').addEventListener('scroll', this.onScroll)
EventEmitter.on('focus-timeline', () => {
// If focusedId does not change, we have to refresh focusedId because Toot component watch change events.
const previousFocusedId = this.focusedId
this.focusedId = 0
this.$nextTick(function () {
this.focusedId = previousFocusedId
})
})
if (this.heading && this.timeline.length > 0) {
this.$store.dispatch('TimelineSpace/Contents/Home/saveMarker')
}
const el = document.getElementById('scroller')
this.scrollPosition = new ScrollPosition(el)
this.scrollPosition.prepare()
onMounted(() => {
store.commit(`TimelineSpace/SideMenu/${SIDE_MENU_MUTATION.CHANGE_UNREAD_HOME_TIMELINE}`, false)
document.getElementById('scroller')?.addEventListener('scroll', onScroll)
this.observer = new ResizeObserver(() => {
if (this.loadingMore || (this.scrollPosition && !this.heading && !this.lazyLoading && !this.scrolling)) {
this.resizeTime = moment()
this.scrollPosition.restore()
if (heading.value && timeline.value.length > 0) {
store.dispatch(`${space}/${ACTION_TYPES.SAVE_MARKER}`)
}
const el = document.getElementById('scroller')
if (el) {
scrollPosition.value = new ScrollPosition(el)
scrollPosition.value.prepare()
observer.value = new ResizeObserver(() => {
if (loadingMore.value || (scrollPosition.value && !heading.value && !lazyLoading.value && !scrolling.value)) {
resizeTime.value = moment()
scrollPosition.value?.restore()
}
})
const scrollWrapper = el.getElementsByClassName('vue-recycle-scroller__item-wrapper')[0]
observer.value.observe(scrollWrapper)
}
})
const scrollWrapper = el.getElementsByClassName('vue-recycle-scroller__item-wrapper')[0]
this.observer.observe(scrollWrapper)
},
beforeUpdate() {
if (this.$store.state.TimelineSpace.SideMenu.unreadHomeTimeline && this.heading) {
this.$store.commit('TimelineSpace/SideMenu/changeUnreadHomeTimeline', false)
}
if (this.scrollPosition) {
this.scrollPosition.prepare()
}
},
beforeUnmount() {
EventEmitter.off('focus-timeline')
this.observer.disconnect()
},
unmounted() {
this.$store.commit('TimelineSpace/Contents/Home/changeHeading', true)
this.$store.commit('TimelineSpace/Contents/Home/archiveTimeline')
if (document.getElementById('scroller') !== undefined && document.getElementById('scroller') !== null) {
document.getElementById('scroller').removeEventListener('scroll', this.onScroll)
document.getElementById('scroller').scrollTop = 0
}
},
watch: {
startReload: function (newState, oldState) {
if (!oldState && newState) {
this.reload().finally(() => {
this.$store.commit('TimelineSpace/HeaderMenu/changeReload', false)
onBeforeUpdate(() => {
if (store.state.TimelineSpace.SideMenu.unreadHomeTimeline && heading.value) {
store.commit(`TimelineSpace/SideMenu/${SIDE_MENU_MUTATION.CHANGE_UNREAD_HOME_TIMELINE}`, false)
}
if (scrollPosition.value) {
scrollPosition.value.prepare()
}
})
onBeforeUnmount(() => {
observer.value?.disconnect()
})
watch(startReload, (newVal, oldVal) => {
if (!oldVal && newVal) {
reload().finally(() => {
store.commit(`TimelineSpace/HeaderMenu/${HEADER_MUTATION.CHANGE_RELOAD}`, false)
})
}
},
focusedId: function (newState, _oldState) {
if (newState && this.heading) {
this.$store.commit('TimelineSpace/Contents/Home/changeHeading', false)
} else if (newState === null && !this.heading) {
this.$store.commit('TimelineSpace/Contents/Home/changeHeading', true)
}
},
timeline: {
handler(newState, _oldState) {
if (this.heading && newState.length > 0) {
this.$store.dispatch('TimelineSpace/Contents/Home/saveMarker')
}
},
deep: true
}
},
methods: {
onScroll(event) {
if (moment().diff(this.resizeTime) < 500) {
})
const onScroll = (event: Event) => {
if (moment().diff(resizeTime.value) < 500) {
return
}
this.scrollTime = moment()
if (!this.scrolling) {
this.$store.commit('TimelineSpace/Contents/Home/changeScrolling', true)
scrollTime.value = moment()
if (!scrolling.value) {
store.commit(`${space}/${MUTATION_TYPES.CHANGE_SCROLLING}`, true)
}
// for lazyLoading
if (
event.target.clientHeight + event.target.scrollTop >= document.getElementById('scroller').scrollHeight - 10 &&
!this.lazyloading
(event.target as HTMLElement)!.clientHeight + (event.target as HTMLElement)!.scrollTop >=
document.getElementById('scroller')!.scrollHeight - 10 &&
!lazyLoading.value
) {
this.$store
.dispatch('TimelineSpace/Contents/Home/lazyFetchTimeline', this.timeline[this.timeline.length - 1])
store
.dispatch(`${space}/${ACTION_TYPES.LAZY_FETCH_TIMELINE}`, timeline.value[timeline.value.length - 1])
.then(statuses => {
if (statuses === null) {
return
}
if (statuses.length > 0) {
this.$store.commit('TimelineSpace/Contents/Home/changeScrolling', true)
store.commit(`${space}/${MUTATION_TYPES.CHANGE_SCROLLING}`, true)
setTimeout(() => {
this.$store.commit('TimelineSpace/Contents/Home/changeScrolling', false)
store.commit(`${space}/${MUTATION_TYPES.CHANGE_SCROLLING}`, false)
}, 500)
}
})
.catch(() => {
this.$message({
message: this.$t('message.timeline_fetch_error'),
ElMessage({
message: i18n.t('message.timeline_fetch_error'),
type: 'error'
})
})
}
if (event.target.scrollTop > 10 && this.heading) {
this.$store.commit('TimelineSpace/Contents/Home/changeHeading', false)
} else if (event.target.scrollTop <= 5 && !this.heading) {
this.$store.commit('TimelineSpace/Contents/Home/changeHeading', true)
if ((event.target as HTMLElement)!.scrollTop > 10 && heading.value) {
store.commit(`${space}/${MUTATION_TYPES.CHANGE_HEADING}`, false)
} else if ((event.target as HTMLElement)!.scrollTop <= 5 && !heading.value) {
store.commit(`${space}/${MUTATION_TYPES.CHANGE_HEADING}`, true)
}
setTimeout(() => {
const now = moment()
if (now.diff(this.scrollTime) >= 150) {
this.scrollTime = null
this.$store.commit('TimelineSpace/Contents/Home/changeScrolling', false)
if (now.diff(scrollTime.value) >= 150) {
scrollTime.value = null
store.commit(`${space}/${MUTATION_TYPES.CHANGE_SCROLLING}`, false)
}
}, 150)
},
updateToot(message) {
this.$store.commit('TimelineSpace/Contents/Home/updateToot', message)
},
deleteToot(message) {
this.$store.commit('TimelineSpace/Contents/Home/deleteToot', message.id)
},
fetchTimelineSince(since_id) {
this.loadingMore = true
this.$store.dispatch('TimelineSpace/Contents/Home/fetchTimelineSince', since_id).finally(() => {
}
const updateToot = (message: Entity.Status) => {
store.commit(`${space}/${MUTATION_TYPES.UPDATE_TOOT}`, message)
}
const deleteToot = (message: Entity.Status) => {
store.commit(`${space}/${MUTATION_TYPES.DELETE_TOOT}`, message.id)
}
const fetchTimelineSince = (since_id: string) => {
loadingMore.value = true
store.dispatch(`${space}/${ACTION_TYPES.FETCH_TIMELINE_SINCE}`, since_id).finally(() => {
setTimeout(() => {
this.loadingMore = false
loadingMore.value = false
}, 500)
})
},
async reload() {
this.$store.commit('TimelineSpace/changeLoading', true)
}
const reload = async () => {
store.commit(`TimelineSpace/${TIMELINE_MUTATION.CHANGE_LOADING}`, true)
try {
await this.reloadable()
reloadable()
} finally {
this.$store.commit('TimelineSpace/changeLoading', false)
store.commit(`TimelineSpace/${TIMELINE_MUTATION.CHANGE_LOADING}`, false)
}
},
upper() {
this.$refs.scroller.scrollToItem(0)
this.focusedId = null
},
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) {
this.focusedId = this.timeline[currentIndex + 1].uri + this.timeline[currentIndex + 1].id
}
const upper = () => {
scroller.value.scrollToItem(0)
focusedId.value = null
}
const focusNext = () => {
if (currentFocusedIndex.value === -1) {
focusedId.value = timeline.value[0].uri + timeline.value[0].id
} else if (currentFocusedIndex.value < timeline.value.length) {
focusedId.value = timeline.value[currentFocusedIndex.value + 1].uri + timeline.value[currentFocusedIndex.value + 1].id
}
},
focusPrev() {
const currentIndex = this.timeline.findIndex(toot => this.focusedId === toot.uri + toot.id)
if (currentIndex === 0) {
this.focusedId = null
} else if (currentIndex > 0) {
this.focusedId = this.timeline[currentIndex - 1].uri + this.timeline[currentIndex - 1].id
}
const focusPrev = () => {
if (currentFocusedIndex.value === 0) {
focusedId.value = null
} else if (currentFocusedIndex.value > 0) {
focusedId.value = timeline.value[currentFocusedIndex.value - 1].uri + timeline.value[currentFocusedIndex.value - 1].id
}
},
focusToot(message) {
this.focusedId = message.uri + message.id
},
focusSidebar() {
}
const focusToot = (message: Entity.Status) => {
focusedId.value = message.uri + message.id
}
const focusSidebar = () => {
EventEmitter.emit('focus-sidebar')
},
handleKey(event) {
switch (event.srcKey) {
case 'next':
this.focusedId = this.timeline[0].uri + this.timeline[0].id
break
}
},
sizeChanged() {
this.$store.commit('TimelineSpace/Contents/Home/changeScrolling', true)
}
const sizeChanged = () => {
store.commit(`${space}/${MUTATION_TYPES.CHANGE_SCROLLING}`, true)
setTimeout(() => {
this.$store.commit('TimelineSpace/Contents/Home/changeScrolling', false)
store.commit(`${space}/${MUTATION_TYPES.CHANGE_SCROLLING}`, false)
}, 500)
}
return {
filteredTimeline,
scroller,
loadingMore,
fetchTimelineSince,
focusedId,
modalOpened,
filters,
updateToot,
deleteToot,
focusNext,
focusPrev,
focusSidebar,
focusToot,
sizeChanged,
openSideBar,
heading,
upper
}
}
}
})
</script>
<style lang="scss" scoped>

View File

@ -33,7 +33,6 @@
import { mapState, mapGetters } from 'vuex'
import moment from 'moment'
import Toot from '~/src/renderer/components/organisms/Toot'
import reloadable from '~/src/renderer/components/mixins/reloadable'
import { EventEmitter } from '~/src/renderer/components/event'
import { ScrollPosition } from '~/src/renderer/components/utils/scroll'
@ -50,7 +49,6 @@ export default {
name: 'list',
props: ['list_id'],
components: { Toot },
mixins: [reloadable],
computed: {
...mapState({
openSideBar: state => state.TimelineSpace.Contents.SideBar.openSideBar,
@ -223,7 +221,6 @@ export default {
async reload() {
this.$store.commit('TimelineSpace/changeLoading', true)
try {
await this.reloadable()
await this.$store.dispatch('TimelineSpace/Contents/Lists/Show/stopStreaming')
await this.$store.dispatch('TimelineSpace/Contents/Lists/Show/fetchTimeline', this.list_id).catch(() => {
this.$message({

View File

@ -33,7 +33,6 @@
import { mapState, mapGetters } from 'vuex'
import moment from 'moment'
import Toot from '~/src/renderer/components/organisms/Toot'
import reloadable from '~/src/renderer/components/mixins/reloadable'
import { EventEmitter } from '~/src/renderer/components/event'
import { ScrollPosition } from '~/src/renderer/components/utils/scroll'
@ -49,7 +48,6 @@ export default {
},
name: 'local',
components: { Toot },
mixins: [reloadable],
computed: {
...mapState('TimelineSpace/Contents/Local', {
timeline: state => state.timeline,
@ -219,7 +217,6 @@ export default {
async reload() {
this.$store.commit('TimelineSpace/changeLoading', true)
try {
await this.reloadable()
} finally {
this.$store.commit('TimelineSpace/changeLoading', false)
}

View File

@ -40,7 +40,6 @@ import { mapState, mapGetters } from 'vuex'
import moment from 'moment'
import Notification from '~/src/renderer/components/organisms/Notification'
import StatusLoading from '~/src/renderer/components/organisms/StatusLoading'
import reloadable from '~/src/renderer/components/mixins/reloadable'
import { EventEmitter } from '~/src/renderer/components/event'
import { ScrollPosition } from '~/src/renderer/components/utils/scroll'
@ -57,7 +56,6 @@ export default {
},
name: 'mentions',
components: { Notification, StatusLoading },
mixins: [reloadable],
computed: {
...mapState('App', {
backgroundColor: state => state.theme.background_color
@ -222,7 +220,6 @@ export default {
async reload() {
this.$store.commit('TimelineSpace/changeLoading', true)
try {
await this.reloadable()
} finally {
this.$store.commit('TimelineSpace/changeLoading', false)
}

View File

@ -39,7 +39,6 @@ import { mapState, mapGetters } from 'vuex'
import moment from 'moment'
import Notification from '~/src/renderer/components/organisms/Notification'
import StatusLoading from '~/src/renderer/components/organisms/StatusLoading'
import reloadable from '~/src/renderer/components/mixins/reloadable'
import { EventEmitter } from '~/src/renderer/components/event'
import { ScrollPosition } from '~/src/renderer/components/utils/scroll'
@ -56,7 +55,6 @@ export default {
},
name: 'notifications',
components: { Notification, StatusLoading },
mixins: [reloadable],
computed: {
...mapState({
openSideBar: state => state.TimelineSpace.Contents.SideBar.openSideBar,
@ -224,7 +222,6 @@ export default {
async reload() {
this.$store.commit('TimelineSpace/changeLoading', true)
try {
await this.reloadable()
this.$store.dispatch('TimelineSpace/Contents/Notifications/resetBadge')
} finally {
this.$store.commit('TimelineSpace/changeLoading', false)

View File

@ -33,7 +33,6 @@
import { mapState, mapGetters } from 'vuex'
import moment from 'moment'
import Toot from '~/src/renderer/components/organisms/Toot'
import reloadable from '~/src/renderer/components/mixins/reloadable'
import { EventEmitter } from '~/src/renderer/components/event'
import { ScrollPosition } from '~/src/renderer/components/utils/scroll'
@ -49,7 +48,6 @@ export default {
},
name: 'public',
components: { Toot },
mixins: [reloadable],
computed: {
...mapState('TimelineSpace/Contents/Public', {
timeline: state => state.timeline,
@ -221,7 +219,6 @@ export default {
async reload() {
this.$store.commit('TimelineSpace/changeLoading', true)
try {
await this.reloadable()
} finally {
this.$store.commit('TimelineSpace/changeLoading', false)
}

View File

@ -1,24 +0,0 @@
<script>
export default {
name: 'reloadable',
methods: {
async reloadable() {
const account = await this.$store
.dispatch('TimelineSpace/localAccount', this.$route.params.id)
.catch((err) => {
this.$message({
message: this.$t('message.account_load_error'),
type: 'error',
})
throw err
})
await this.$store.dispatch('GlobalHeader/stopUserStreamings')
await this.$store.dispatch('TimelineSpace/stopStreamings')
await this.$store.dispatch('TimelineSpace/fetchContentsTimelines')
await this.$store.dispatch('TimelineSpace/startStreamings')
this.$store.dispatch('GlobalHeader/startUserStreamings')
return account
},
},
}
</script>

View File

@ -0,0 +1,29 @@
import { Store } from 'vuex'
import { RootState } from '@/store'
import { RouteLocationNormalizedLoaded } from 'vue-router'
import { i18n } from 'i18next'
import { ElMessage } from 'element-plus'
import { ACTION_TYPES } from '@/store/TimelineSpace'
import { ACTION_TYPES as GLOBAL_ACTION } from '@/store/GlobalHeader'
export default function useReloadable(store: Store<RootState>, route: RouteLocationNormalizedLoaded, i18next: i18n) {
async function reloadable() {
const account = await store.dispatch(`TimelineSpace/${ACTION_TYPES.LOCAL_ACCOUNT}`, route.params.id).catch(err => {
ElMessage({
message: i18next.t('message.account_load_error'),
type: 'error'
})
throw err
})
await store.dispatch(`GlobalHeader/${GLOBAL_ACTION.STOP_USER_STREAMINGS}`)
await store.dispatch(`TimelineSpace/${ACTION_TYPES.STOP_STREAMINGS}`)
await store.dispatch(`TimelineSpace/${ACTION_TYPES.FETCH_CONTENTS_TIMELINES}`)
await store.dispatch(`TimelineSpace/${ACTION_TYPES.START_STREAMINGS}`)
store.dispatch(`GlobalHeader/${GLOBAL_ACTION.START_USER_STREAMINGS}`)
return account
}
return {
reloadable
}
}

View File

@ -92,8 +92,46 @@ const mutations: MutationTree<TimelineSpaceState> = {
}
}
export const ACTION_TYPES = {
INIT_LOAD: 'initLoad',
PREPARE_SPACE: 'prepareSpace',
LOCAL_ACCOUNT: 'localAccount',
FETCH_ACCOUNT: 'fetchAccount',
CLEAR_ACCOUNT: 'clearAccount',
DETECT_SNS: 'detectSNS',
WATCH_SHORTCUT_EVENTS: 'watchShortcutEvents',
REMOVE_SHORTCUT_EVENTS: 'removeShortcutEvents',
CLEAR_UNREAD: 'clearUnread',
FETCH_EMOJIS: 'fetchEmojis',
FETCH_FILTERS: 'fetchFilters',
FETCH_INSTANCE: 'fetchInstance',
LOAD_TIMELINE_SETTING: 'loadTimelineSetting',
FETCH_CONTENTS_TIMELINES: 'fetchContentsTimelines',
CLEAR_CONTENTS_TIMELINES: 'clearContentsTimelines',
BIND_STREAMINGS: 'bindStreamings',
START_STREAMINGS: 'startStreamings',
STOP_STREAMINGS: 'stopStreamings',
UNBIND_STREAMINGS: 'unbindStreamings',
BIND_USER_STREAMING: 'bindUserStreaming',
BIND_LOCAL_STREAMING: 'bindLocalStreaming',
START_LOCAL_STREAMING: 'startLocalStreaming',
BIND_PUBLIC_STREAMING: 'bindPublicStreaming',
START_PUBLIC_STREAMING: 'startPublicStreaming',
BIND_DIRECT_MESSAGES_STREAMING: 'bindDirectMessagesStreaming',
START_DIRECT_MESSAGES_STREAMING: 'startDirectMessagesStreaming',
UNBIND_USER_STREAMING: 'unbindUserStreaming',
UNBIND_LOCAL_STREAMING: 'unbindLocalStreaming',
STOP_LOCAL_STREAMING: 'stopLocalStreaming',
UNBIND_PUBLIC_STREAMING: 'unbindPublicStreaming',
STOP_PUBLIC_STREAMING: 'stopPublicStreaming',
UNBIND_DIRECT_MESSAGES_STREAMING: 'unbindDirectMessagesStreaming',
STOP_DIRECT_MESSAGES_STREAMING: 'stopDirectMessagesStreaming',
UPDATE_TOOT_FOR_ALL_TIMELINES: 'updateTootForAllTimelines',
WAIT_TO_UNBIND_USER_STREAMING: 'waitToUnbindUserStreaming'
}
const actions: ActionTree<TimelineSpaceState, RootState> = {
initLoad: async ({ dispatch, commit }, accountId: string): Promise<LocalAccount> => {
[ACTION_TYPES.INIT_LOAD]: async ({ dispatch, commit }, accountId: string): Promise<LocalAccount> => {
commit(MUTATION_TYPES.CHANGE_LOADING, true)
dispatch('watchShortcutEvents')
const account: LocalAccount = await dispatch('localAccount', accountId).catch(_ => {
@ -113,7 +151,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
})
return account
},
prepareSpace: async ({ state, dispatch }) => {
[ACTION_TYPES.PREPARE_SPACE]: async ({ state, dispatch }) => {
await dispatch('bindStreamings')
dispatch('startStreamings')
await dispatch('fetchEmojis', state.account)
@ -124,7 +162,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
// -------------------------------------------------
// Accounts
// -------------------------------------------------
localAccount: async ({ dispatch, commit }, id: string): Promise<LocalAccount> => {
[ACTION_TYPES.LOCAL_ACCOUNT]: async ({ dispatch, commit }, id: string): Promise<LocalAccount> => {
const account: LocalAccount = await win.ipcRenderer.invoke('get-local-account', id)
if (account.username === undefined || account.username === null || account.username === '') {
const acct: LocalAccount = await dispatch('fetchAccount', account)
@ -135,22 +173,22 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
return account
}
},
fetchAccount: async (_, account: LocalAccount): Promise<LocalAccount> => {
[ACTION_TYPES.FETCH_ACCOUNT]: async (_, account: LocalAccount): Promise<LocalAccount> => {
const acct: LocalAccount = await win.ipcRenderer.invoke('update-account', account)
return acct
},
clearAccount: async ({ commit }) => {
[ACTION_TYPES.CLEAR_ACCOUNT]: async ({ commit }) => {
commit(MUTATION_TYPES.UPDATE_ACCOUNT, blankAccount)
return true
},
detectSNS: async ({ commit, state }) => {
[ACTION_TYPES.DETECT_SNS]: async ({ commit, state }) => {
const sns = await detector(state.account.baseURL)
commit(MUTATION_TYPES.CHANGE_SNS, sns)
},
// -----------------------------------------------
// Shortcuts
// -----------------------------------------------
watchShortcutEvents: ({ commit, dispatch }) => {
[ACTION_TYPES.WATCH_SHORTCUT_EVENTS]: ({ commit, dispatch }) => {
win.ipcRenderer.on('CmdOrCtrl+N', () => {
dispatch('TimelineSpace/Modals/NewToot/openModal', {}, { root: true })
})
@ -161,7 +199,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
commit('TimelineSpace/Modals/Shortcut/changeModal', true, { root: true })
})
},
removeShortcutEvents: async () => {
[ACTION_TYPES.REMOVE_SHORTCUT_EVENTS]: async () => {
win.ipcRenderer.removeAllListeners('CmdOrCtrl+N')
win.ipcRenderer.removeAllListeners('CmdOrCtrl+K')
return true
@ -169,13 +207,13 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
/**
* clearUnread
*/
clearUnread: async ({ dispatch }) => {
[ACTION_TYPES.CLEAR_UNREAD]: async ({ dispatch }) => {
dispatch('TimelineSpace/SideMenu/clearUnread', {}, { root: true })
},
/**
* fetchEmojis
*/
fetchEmojis: async ({ commit, state }, account: LocalAccount): Promise<Array<Entity.Emoji>> => {
[ACTION_TYPES.FETCH_EMOJIS]: async ({ commit, state }, account: LocalAccount): Promise<Array<Entity.Emoji>> => {
const client = generator(state.sns, account.baseURL, null, 'Whalebird')
const res = await client.getInstanceCustomEmojis()
commit(MUTATION_TYPES.UPDATE_EMOJIS, res.data)
@ -184,7 +222,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
/**
* fetchFilters
*/
fetchFilters: async ({ commit, state, rootState }): Promise<Array<Entity.Filter>> => {
[ACTION_TYPES.FETCH_FILTERS]: async ({ commit, state, rootState }): Promise<Array<Entity.Filter>> => {
try {
const client = generator(state.sns, state.account.baseURL, state.account.accessToken, rootState.App.userAgent)
const res = await client.getFilters()
@ -197,17 +235,17 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
/**
* fetchInstance
*/
fetchInstance: async ({ commit, state }, account: LocalAccount) => {
[ACTION_TYPES.FETCH_INSTANCE]: async ({ commit, state }, account: LocalAccount) => {
const client = generator(state.sns, account.baseURL, null, 'Whalebird')
const res = await client.getInstance()
commit(MUTATION_TYPES.UPDATE_TOOT_MAX, res.data.max_toot_chars)
return true
},
loadTimelineSetting: async ({ commit }, accountID: string) => {
[ACTION_TYPES.LOAD_TIMELINE_SETTING]: async ({ commit }, accountID: string) => {
const setting: Setting = await win.ipcRenderer.invoke('get-account-setting', accountID)
commit(MUTATION_TYPES.UPDATE_TIMELINE_SETTING, setting.timeline)
},
fetchContentsTimelines: async ({ dispatch, state }) => {
[ACTION_TYPES.FETCH_CONTENTS_TIMELINES]: async ({ dispatch, state }) => {
dispatch('TimelineSpace/Contents/changeLoading', true, { root: true })
await dispatch('TimelineSpace/Contents/Home/fetchTimeline', {}, { root: true }).finally(() => {
dispatch('TimelineSpace/Contents/changeLoading', false, { root: true })
@ -225,7 +263,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
await dispatch('TimelineSpace/Contents/Public/fetchPublicTimeline', {}, { root: true })
}
},
clearContentsTimelines: ({ commit }) => {
[ACTION_TYPES.CLEAR_CONTENTS_TIMELINES]: ({ commit }) => {
commit('TimelineSpace/Contents/Home/clearTimeline', {}, { root: true })
commit('TimelineSpace/Contents/Local/clearTimeline', {}, { root: true })
commit('TimelineSpace/Contents/DirectMessages/clearTimeline', {}, { root: true })
@ -233,7 +271,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
commit('TimelineSpace/Contents/Public/clearTimeline', {}, { root: true })
commit('TimelineSpace/Contents/Mentions/clearMentions', {}, { root: true })
},
bindStreamings: ({ dispatch, state }) => {
[ACTION_TYPES.BIND_STREAMINGS]: ({ dispatch, state }) => {
dispatch('bindUserStreaming')
if (state.timelineSetting.unreadNotification.direct) {
dispatch('bindDirectMessagesStreaming')
@ -245,7 +283,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
dispatch('bindPublicStreaming')
}
},
startStreamings: ({ dispatch, state }) => {
[ACTION_TYPES.START_STREAMINGS]: ({ dispatch, state }) => {
if (state.timelineSetting.unreadNotification.direct) {
dispatch('startDirectMessagesStreaming')
}
@ -256,12 +294,12 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
dispatch('startPublicStreaming')
}
},
stopStreamings: ({ dispatch }) => {
[ACTION_TYPES.STOP_STREAMINGS]: ({ dispatch }) => {
dispatch('stopDirectMessagesStreaming')
dispatch('stopLocalStreaming')
dispatch('stopPublicStreaming')
},
unbindStreamings: ({ dispatch }) => {
[ACTION_TYPES.UNBIND_STREAMINGS]: ({ dispatch }) => {
dispatch('unbindUserStreaming')
dispatch('unbindDirectMessagesStreaming')
dispatch('unbindLocalStreaming')
@ -270,7 +308,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
// ------------------------------------------------
// Each streaming methods
// ------------------------------------------------
bindUserStreaming: async ({ commit, state, rootState, dispatch }) => {
[ACTION_TYPES.BIND_USER_STREAMING]: async ({ commit, state, rootState, dispatch }) => {
if (!state.account._id) {
throw new Error('Account is not set')
}
@ -306,7 +344,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
commit('TimelineSpace/Contents/Mentions/deleteToot', id, { root: true })
})
},
bindLocalStreaming: ({ commit, rootState }) => {
[ACTION_TYPES.BIND_LOCAL_STREAMING]: ({ commit, rootState }) => {
win.ipcRenderer.on('update-start-local-streaming', (_, update: Entity.Status) => {
commit('TimelineSpace/Contents/Local/appendTimeline', update, { root: true })
if (rootState.TimelineSpace.Contents.Local.heading && Math.random() > 0.8) {
@ -318,7 +356,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
commit('TimelineSpace/Contents/Local/deleteToot', id, { root: true })
})
},
startLocalStreaming: ({ state }) => {
[ACTION_TYPES.START_LOCAL_STREAMING]: ({ state }) => {
// @ts-ignore
return new Promise((resolve, reject) => {
// eslint-disable-line no-unused-vars
@ -328,7 +366,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
})
})
},
bindPublicStreaming: ({ commit, rootState }) => {
[ACTION_TYPES.BIND_PUBLIC_STREAMING]: ({ commit, rootState }) => {
win.ipcRenderer.on('update-start-public-streaming', (_, update: Entity.Status) => {
commit('TimelineSpace/Contents/Public/appendTimeline', update, { root: true })
if (rootState.TimelineSpace.Contents.Public.heading && Math.random() > 0.8) {
@ -340,7 +378,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
commit('TimelineSpace/Contents/Public/deleteToot', id, { root: true })
})
},
startPublicStreaming: ({ state }) => {
[ACTION_TYPES.START_PUBLIC_STREAMING]: ({ state }) => {
// @ts-ignore
return new Promise((resolve, reject) => {
// eslint-disable-line no-unused-vars
@ -350,7 +388,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
})
})
},
bindDirectMessagesStreaming: ({ commit, rootState }) => {
[ACTION_TYPES.BIND_DIRECT_MESSAGES_STREAMING]: ({ commit, rootState }) => {
win.ipcRenderer.on('update-start-directmessages-streaming', (_, update: Entity.Status) => {
commit('TimelineSpace/Contents/DirectMessages/appendTimeline', update, { root: true })
if (rootState.TimelineSpace.Contents.DirectMessages.heading && Math.random() > 0.8) {
@ -362,7 +400,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
commit('TimelineSpace/Contents/DirectMessages/deleteToot', id, { root: true })
})
},
startDirectMessagesStreaming: ({ state }) => {
[ACTION_TYPES.START_DIRECT_MESSAGES_STREAMING]: ({ state }) => {
// @ts-ignore
return new Promise((resolve, reject) => {
// eslint-disable-line no-unused-vars
@ -372,7 +410,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
})
})
},
unbindUserStreaming: ({ state, commit }) => {
[ACTION_TYPES.UNBIND_USER_STREAMING]: ({ state, commit }) => {
// When unbind is called, sometimes account is already cleared and account does not have _id.
// So we have to get previous account to unbind streamings.
if (state.bindingAccount) {
@ -386,31 +424,31 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
console.info('binding account does not exist')
}
},
unbindLocalStreaming: () => {
[ACTION_TYPES.UNBIND_LOCAL_STREAMING]: () => {
win.ipcRenderer.removeAllListeners('error-start-local-streaming')
win.ipcRenderer.removeAllListeners('update-start-local-streaming')
win.ipcRenderer.removeAllListeners('delete-start-local-streaming')
},
stopLocalStreaming: () => {
[ACTION_TYPES.STOP_LOCAL_STREAMING]: () => {
win.ipcRenderer.send('stop-local-streaming')
},
unbindPublicStreaming: () => {
[ACTION_TYPES.UNBIND_PUBLIC_STREAMING]: () => {
win.ipcRenderer.removeAllListeners('error-start-public-streaming')
win.ipcRenderer.removeAllListeners('update-start-public-streaming')
win.ipcRenderer.removeAllListeners('delete-start-public-streaming')
},
stopPublicStreaming: () => {
[ACTION_TYPES.STOP_PUBLIC_STREAMING]: () => {
win.ipcRenderer.send('stop-public-streaming')
},
unbindDirectMessagesStreaming: () => {
[ACTION_TYPES.UNBIND_DIRECT_MESSAGES_STREAMING]: () => {
win.ipcRenderer.removeAllListeners('error-start-directmessages-streaming')
win.ipcRenderer.removeAllListeners('update-start-directmessages-streaming')
win.ipcRenderer.removeAllListeners('delete-start-directmessages-streaming')
},
stopDirectMessagesStreaming: () => {
[ACTION_TYPES.STOP_DIRECT_MESSAGES_STREAMING]: () => {
win.ipcRenderer.send('stop-directmessages-streaming')
},
updateTootForAllTimelines: ({ commit, state }, status: Entity.Status): boolean => {
[ACTION_TYPES.UPDATE_TOOT_FOR_ALL_TIMELINES]: ({ commit, state }, status: Entity.Status): boolean => {
commit('TimelineSpace/Contents/Home/updateToot', status, { root: true })
commit('TimelineSpace/Contents/Notifications/updateToot', status, { root: true })
commit('TimelineSpace/Contents/Mentions/updateToot', status, { root: true })
@ -425,7 +463,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
}
return true
},
waitToUnbindUserStreaming: async ({ state, dispatch }): Promise<boolean> => {
[ACTION_TYPES.WAIT_TO_UNBIND_USER_STREAMING]: async ({ state, dispatch }): Promise<boolean> => {
if (!state.bindingAccount) {
return true
}

View File

@ -5,7 +5,7 @@ import { LocalMarker } from '~/src/types/localMarker'
import { MyWindow } from '~/src/types/global'
import { LoadingCard } from '@/types/loading-card'
const win = (window as any) as MyWindow
const win = window as any as MyWindow
export type HomeState = {
lazyLoading: boolean
@ -122,8 +122,16 @@ const mutations: MutationTree<HomeState> = {
}
}
export const ACTION_TYPES = {
FETCH_TIMELINE: 'fetchTimeline',
LAZY_FETCH_TIMELINE: 'lazyFetchTimeline',
FETCH_TIMELINE_SINCE: 'fetchTimelineSince',
GET_MARKER: 'getMarker',
SAVE_MARKER: 'saveMarker'
}
const actions: ActionTree<HomeState, RootState> = {
fetchTimeline: async ({ dispatch, commit, rootState }) => {
[ACTION_TYPES.FETCH_TIMELINE]: async ({ dispatch, commit, rootState }) => {
const client = generator(
rootState.TimelineSpace.sns,
rootState.TimelineSpace.account.baseURL,
@ -149,7 +157,8 @@ const actions: ActionTree<HomeState, RootState> = {
// If the number of unread statuses is less than 40, max_id is necessary, but it is enough to reject duplicated statuses.
// So we do it in mutation.
max_id: null,
id: 'loading-card'
id: 'loading-card',
uri: 'loading-card'
}
const res = await client.getHomeTimeline({ limit: 40, max_id: lastReadStatus.id })
@ -168,7 +177,10 @@ const actions: ActionTree<HomeState, RootState> = {
return res.data
}
},
lazyFetchTimeline: async ({ state, commit, rootState }, lastStatus: Entity.Status): Promise<Array<Entity.Status> | null> => {
[ACTION_TYPES.LAZY_FETCH_TIMELINE]: async (
{ state, commit, rootState },
lastStatus: Entity.Status
): Promise<Array<Entity.Status> | null> => {
if (state.lazyLoading) {
return Promise.resolve(null)
}
@ -189,7 +201,7 @@ const actions: ActionTree<HomeState, RootState> = {
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, false)
})
},
fetchTimelineSince: async ({ state, rootState, commit }, since_id: string): Promise<Array<Entity.Status> | null> => {
[ACTION_TYPES.FETCH_TIMELINE_SINCE]: async ({ state, rootState, commit }, since_id: string): Promise<Array<Entity.Status> | null> => {
const client = generator(
rootState.TimelineSpace.sns,
rootState.TimelineSpace.account.baseURL,
@ -229,7 +241,8 @@ const actions: ActionTree<HomeState, RootState> = {
type: 'middle-load',
since_id: res.data[0].id,
max_id: maxID,
id: 'loading-card'
id: 'loading-card',
uri: 'loading-card'
}
let timeline: Array<Entity.Status | LoadingCard> = [card]
timeline = timeline.concat(res.data)
@ -239,7 +252,7 @@ const actions: ActionTree<HomeState, RootState> = {
}
return res.data
},
getMarker: async ({ rootState }): Promise<LocalMarker | null> => {
[ACTION_TYPES.GET_MARKER]: async ({ rootState }): Promise<LocalMarker | null> => {
if (!rootState.TimelineSpace.timelineSetting.useMarker.home) {
return null
}
@ -265,7 +278,7 @@ const actions: ActionTree<HomeState, RootState> = {
const localMarker: LocalMarker | null = await win.ipcRenderer.invoke('get-home-marker', rootState.TimelineSpace.account._id)
return localMarker
},
saveMarker: async ({ state, rootState }) => {
[ACTION_TYPES.SAVE_MARKER]: async ({ state, rootState }) => {
const timeline = state.timeline
if (timeline.length === 0 || timeline[0].id === 'loading-card') {
return

View File

@ -138,7 +138,8 @@ const actions: ActionTree<MentionsState, RootState> = {
type: 'middle-load',
since_id: localMarker.last_read_id,
max_id: null,
id: 'loading-card'
id: 'loading-card',
uri: 'loading-card'
}
let mentions: Array<Entity.Notification | LoadingCard> = [card]
const res = await client.getNotifications({ limit: 30, max_id: nextResponse.data[0].id, exclude_types: excludes })
@ -211,7 +212,8 @@ const actions: ActionTree<MentionsState, RootState> = {
type: 'middle-load',
since_id: res.data[0].id,
max_id: maxID,
id: 'loading-card'
id: 'loading-card',
uri: 'loading-card'
}
let mentions: Array<Entity.Notification | LoadingCard> = [card]
mentions = mentions.concat(res.data)

View File

@ -5,7 +5,7 @@ import { LocalMarker } from '~/src/types/localMarker'
import { MyWindow } from '~/src/types/global'
import { LoadingCard } from '@/types/loading-card'
const win = (window as any) as MyWindow
const win = window as any as MyWindow
export type NotificationsState = {
lazyLoading: boolean
@ -135,7 +135,8 @@ const actions: ActionTree<NotificationsState, RootState> = {
// If the number of unread statuses is less than 30, max_id is necessary, but it is enough to reject duplicated statuses.
// So we do it in mutation.
max_id: null,
id: 'loading-card'
id: 'loading-card',
uri: 'loading-card'
}
let notifications: Array<Entity.Notification | LoadingCard> = [card]
const res = await client.getNotifications({ limit: 30, max_id: nextResponse.data[0].id })
@ -203,7 +204,8 @@ const actions: ActionTree<NotificationsState, RootState> = {
type: 'middle-load',
since_id: res.data[0].id,
max_id: maxID,
id: 'loading-card'
id: 'loading-card',
uri: 'loading-card'
}
let notifications: Array<Entity.Notification | LoadingCard> = [card]
notifications = notifications.concat(res.data)

View File

@ -3,4 +3,5 @@ export type LoadingCard = {
max_id: string | null
since_id: string | null
id: 'loading-card'
uri: 'loading-card'
}