Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor] Remove timeline stores #4058

Merged
merged 7 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 76 additions & 40 deletions src/renderer/components/TimelineSpace/Contents/Bookmarks.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div id="bookmarks">
<div></div>
<DynamicScroller :items="bookmarks" :min-item-size="60" id="scroller" class="scroller" ref="scroller">
<div style="width: 100%; height: 120px" v-loading="loading" :element-loading-background="backgroundColor" v-if="loading" />
<DynamicScroller :items="bookmarks" :min-item-size="60" id="scroller" class="scroller" ref="scroller" v-else>
<template #default="{ item, index, active }">
<DynamicScrollerItem :item="item" :active="active" :size-dependencies="[item.uri]" :data-index="index" :watchData="true">
<toot
Expand Down Expand Up @@ -30,10 +30,9 @@ import { useStore } from '@/store'
import { useI18next } from 'vue3-i18next'
import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Entity } from 'megalodon'
import useReloadable from '@/components/utils/reloadable'
import parse from 'parse-link-header'
import generator, { Entity, MegalodonInterface } from 'megalodon'
import Toot from '@/components/organisms/Toot.vue'
import { ACTION_TYPES, MUTATION_TYPES } from '@/store/TimelineSpace/Contents/Bookmarks'
import { MUTATION_TYPES as TIMELINE_MUTATION } from '@/store/TimelineSpace'
import { MUTATION_TYPES as HEADER_MUTATION } from '@/store/TimelineSpace/HeaderMenu'
import { LocalAccount } from '~/src/types/localAccount'
Expand All @@ -44,15 +43,14 @@ export default defineComponent({
name: 'bookmarks',
components: { Toot },
setup() {
const space = 'TimelineSpace/Contents/Bookmarks'
const store = useStore()
const route = useRoute()
const i18n = useI18next()
const { reloadable } = useReloadable(store, route, i18n)

const focusedId = ref<string | null>(null)
const heading = ref<boolean>(true)
const scroller = ref<any>()
const loading = ref(false)
const lazyLoading = ref(false)
const { j, k, Ctrl_r } = useMagicKeys()

Expand All @@ -63,31 +61,44 @@ export default defineComponent({
account: null,
server: null
})
const client = ref<MegalodonInterface | null>(null)

const bookmarks = computed(() => store.state.TimelineSpace.Contents.Bookmarks.bookmarks)
const bookmarks = ref<Array<Entity.Status>>([])
const nextMaxId = ref<string | null>(null)
const startReload = computed(() => store.state.TimelineSpace.HeaderMenu.reload)
const modalOpened = computed<boolean>(() => store.getters[`TimelineSpace/Modals/modalOpened`])
const currentFocusedIndex = computed(() => bookmarks.value.findIndex(toot => focusedId.value === toot.uri))
const shortcutEnabled = computed(() => !modalOpened.value)
const userAgent = computed(() => store.state.App.userAgent)
const backgroundColor = computed(() => store.state.App.theme.background_color)

onMounted(async () => {
const [a, s]: [LocalAccount, LocalServer] = await win.ipcRenderer.invoke('get-local-account', id.value)
account.account = a
account.server = s

document.getElementById('scroller')?.addEventListener('scroll', onScroll)
store.commit(`TimelineSpace/Contents/${TIMELINE_MUTATION.CHANGE_LOADING}`, true)
store
.dispatch(`${space}/${ACTION_TYPES.FETCH_BOOKMARKS}`, account)
.catch(() => {
ElMessage({
message: i18n.t('message.bookmark_fetch_error'),
type: 'error'
})
})
.finally(() => {
store.commit(`TimelineSpace/Contents/${TIMELINE_MUTATION.CHANGE_LOADING}`, false)

client.value = generator(s.sns, s.baseURL, a.accessToken, userAgent.value)
loading.value = true
try {
const res = await client.value.getBookmarks({ limit: 20 })
bookmarks.value = res.data
const link = parse(res.headers.link)
if (link !== null && link.next) {
nextMaxId.value = link.next.max_id
} else {
nextMaxId.value = null
}
} catch (err) {
console.error(err)
ElMessage({
message: i18n.t('message.bookmark_fetch_error'),
type: 'error'
})
} finally {
loading.value = false
}
})
watch(startReload, (newVal, oldVal) => {
if (!oldVal && newVal) {
Expand Down Expand Up @@ -115,12 +126,23 @@ export default defineComponent({
if (
(event.target as HTMLElement)!.clientHeight + (event.target as HTMLElement)!.scrollTop >=
document.getElementById('scroller')!.scrollHeight - 10 &&
!lazyLoading.value
!lazyLoading.value &&
nextMaxId.value
) {
lazyLoading.value = true
store
.dispatch(`${space}/${ACTION_TYPES.LAZY_FETCH_BOOKMARKS}`, account)
.catch(() => {
client.value
?.getBookmarks({ limit: 20, max_id: nextMaxId.value })
.then(res => {
bookmarks.value = [...bookmarks.value, ...res.data]
const link = parse(res.headers.link)
if (link !== null && link.next) {
nextMaxId.value = link.next.max_id
} else {
nextMaxId.value = null
}
})
.catch(err => {
console.error(err)
ElMessage({
message: i18n.t('message.bookmark_fetch_error'),
type: 'error'
Expand All @@ -130,7 +152,7 @@ export default defineComponent({
lazyLoading.value = false
})
}
// for upper

if ((event.target as HTMLElement)!.scrollTop > 10 && heading.value) {
heading.value = false
} else if ((event.target as HTMLElement)!.scrollTop <= 10 && !heading.value) {
Expand All @@ -139,27 +161,40 @@ export default defineComponent({
}
const reload = async () => {
store.commit(`TimelineSpace/${TIMELINE_MUTATION.CHANGE_LOADING}`, true)
if (!client.value) return
try {
await reloadable()
await store.dispatch(`${space}/${ACTION_TYPES.FETCH_BOOKMARKS}`, account).catch(() => {
ElMessage({
message: i18n.t('message.bookmark_fetch_error'),
type: 'error'
})
})
const res = await client.value.getBookmarks({ limit: 20 })
bookmarks.value = res.data
const link = parse(res.headers.link)
if (link !== null && link.next) {
nextMaxId.value = link.next.max_id
} else {
nextMaxId.value = null
}
} finally {
store.commit(`TimelineSpace/${TIMELINE_MUTATION.CHANGE_LOADING}`, false)
}
}
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)
bookmarks.value = bookmarks.value.map(status => {
if (status.id === message.id) {
return message
} else if (status.reblog && status.reblog.id === message.id) {
return Object.assign(status, {
reblog: message
})
}
return status
})
}
const upper = () => {
scroller.value.scrollToItem(0)
focusedId.value = null
const deleteToot = (id: string) => {
bookmarks.value = bookmarks.value.filter(status => {
if (status.reblog !== null && status.reblog.id === id) {
return false
} else {
return status.id !== id
}
})
}
const focusNext = () => {
if (currentFocusedIndex.value === -1) {
Expand Down Expand Up @@ -188,8 +223,9 @@ export default defineComponent({
deleteToot,
focusToot,
heading,
upper,
account
account,
loading,
backgroundColor
}
}
})
Expand Down
15 changes: 5 additions & 10 deletions src/renderer/components/TimelineSpace/Contents/DirectMessages.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
:filters="[]"
:account="account.account"
:server="account.server"
v-on:update="updateToot"
v-on:delete="deleteToot"
@selectToot="focusToot(item)"
@update="updateToot"
@delete="deleteToot"
@select-toot="focusToot(item)"
>
</toot>
</DynamicScrollerItem>
Expand Down Expand Up @@ -132,15 +132,11 @@ export default defineComponent({
store.commit(`${space}/${MUTATION_TYPES.UPDATE_TOOT}`, { status: message, accountId: account.account.id })
}
}
const deleteToot = (message: Entity.Status) => {
const deleteToot = (id: string) => {
if (account.account) {
store.commit(`${space}/${MUTATION_TYPES.DELETE_TOOT}`, { statusId: message.id, accountId: account.account.id })
store.commit(`${space}/${MUTATION_TYPES.DELETE_TOOT}`, { statusId: id, accountId: account.account.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
Expand Down Expand Up @@ -168,7 +164,6 @@ export default defineComponent({
deleteToot,
focusToot,
heading,
upper,
account
}
}
Expand Down
Loading