mirror of
https://github.com/h3poteto/whalebird-desktop
synced 2025-01-31 01:27:26 +01:00
refs #850 Replace Local with typescript
This commit is contained in:
parent
3127ebe050
commit
6c3fe850f0
@ -2,7 +2,7 @@ import SideBar, { SideBarModuleState } from './Contents/SideBar'
|
||||
import Home, { HomeState } from './Contents/Home'
|
||||
import Notifications from './Contents/Notifications'
|
||||
import Favourites from './Contents/Favourites'
|
||||
import Local from './Contents/Local'
|
||||
import Local, { LocalState } from './Contents/Local'
|
||||
import Public from './Contents/Public'
|
||||
import Search from './Contents/Search'
|
||||
import Lists from './Contents/Lists'
|
||||
@ -16,7 +16,8 @@ export interface ContentsState {}
|
||||
|
||||
export interface ContentsModuleState extends ContentsState {
|
||||
SideBar: SideBarModuleState,
|
||||
Home: HomeState
|
||||
Home: HomeState,
|
||||
Local: LocalState
|
||||
}
|
||||
|
||||
const state = (): ContentsState => ({})
|
||||
|
@ -1,110 +0,0 @@
|
||||
import Mastodon from 'megalodon'
|
||||
|
||||
const Local = {
|
||||
namespaced: true,
|
||||
state: {
|
||||
timeline: [],
|
||||
unreadTimeline: [],
|
||||
lazyLoading: false,
|
||||
heading: true,
|
||||
filter: ''
|
||||
},
|
||||
mutations: {
|
||||
changeHeading (state, value) {
|
||||
state.heading = value
|
||||
},
|
||||
appendTimeline (state, update) {
|
||||
if (state.heading) {
|
||||
state.timeline = [update].concat(state.timeline)
|
||||
} else {
|
||||
state.unreadTimeline = [update].concat(state.unreadTimeline)
|
||||
}
|
||||
},
|
||||
updateTimeline (state, messages) {
|
||||
state.timeline = messages
|
||||
},
|
||||
mergeTimeline (state) {
|
||||
state.timeline = state.unreadTimeline.slice(0, 80).concat(state.timeline)
|
||||
state.unreadTimeline = []
|
||||
},
|
||||
insertTimeline (state, messages) {
|
||||
state.timeline = state.timeline.concat(messages)
|
||||
},
|
||||
archiveTimeline (state) {
|
||||
state.timeline = state.timeline.slice(0, 40)
|
||||
},
|
||||
clearTimeline (state) {
|
||||
state.timeline = []
|
||||
state.unreadTimeline = []
|
||||
},
|
||||
updateToot (state, message) {
|
||||
state.timeline = state.timeline.map((toot) => {
|
||||
if (toot.id === message.id) {
|
||||
return message
|
||||
} else if (toot.reblog !== null && toot.reblog.id === message.id) {
|
||||
// When user reblog/favourite a reblogged toot, target message is a original toot.
|
||||
// So, a message which is received now is original toot.
|
||||
const reblog = {
|
||||
reblog: message
|
||||
}
|
||||
return Object.assign(toot, reblog)
|
||||
} else {
|
||||
return toot
|
||||
}
|
||||
})
|
||||
},
|
||||
deleteToot (state, message) {
|
||||
state.timeline = state.timeline.filter((toot) => {
|
||||
if (toot.reblog !== null && toot.reblog.id === message.id) {
|
||||
return false
|
||||
} else {
|
||||
return toot.id !== message.id
|
||||
}
|
||||
})
|
||||
},
|
||||
changeLazyLoading (state, value) {
|
||||
state.lazyLoading = value
|
||||
},
|
||||
changeFilter (state, filter) {
|
||||
state.filter = filter
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
fetchLocalTimeline ({ commit, rootState }) {
|
||||
const client = new Mastodon(
|
||||
rootState.TimelineSpace.account.accessToken,
|
||||
rootState.TimelineSpace.account.baseURL + '/api/v1'
|
||||
)
|
||||
return client.get('/timelines/public', { limit: 40, local: true })
|
||||
.then(res => {
|
||||
commit('updateTimeline', res.data)
|
||||
return res.data
|
||||
})
|
||||
},
|
||||
lazyFetchTimeline ({ state, commit, rootState }, last) {
|
||||
if (last === undefined || last === null) {
|
||||
return Promise.resolve(null)
|
||||
}
|
||||
if (state.lazyLoading) {
|
||||
return Promise.resolve(null)
|
||||
}
|
||||
commit('changeLazyLoading', true)
|
||||
const client = new Mastodon(
|
||||
rootState.TimelineSpace.account.accessToken,
|
||||
rootState.TimelineSpace.account.baseURL + '/api/v1'
|
||||
)
|
||||
return client.get('/timelines/public', { max_id: last.id, limit: 40, local: true })
|
||||
.then(res => {
|
||||
commit('changeLazyLoading', false)
|
||||
commit('insertTimeline', res.data)
|
||||
return res.data
|
||||
})
|
||||
.catch(err => {
|
||||
commit('changeLazyLoading', false)
|
||||
throw err
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default Local
|
133
src/renderer/store/TimelineSpace/Contents/Local.ts
Normal file
133
src/renderer/store/TimelineSpace/Contents/Local.ts
Normal file
@ -0,0 +1,133 @@
|
||||
import Mastodon, { Status, Response } from 'megalodon'
|
||||
import { Module, MutationTree, ActionTree } from 'vuex'
|
||||
import { RootState } from '@/store'
|
||||
|
||||
export interface LocalState {
|
||||
timeline: Array<Status>,
|
||||
unreadTimeline: Array<Status>,
|
||||
lazyLoading: boolean,
|
||||
heading: boolean,
|
||||
filter: string
|
||||
}
|
||||
|
||||
const state = (): LocalState => ({
|
||||
timeline: [],
|
||||
unreadTimeline: [],
|
||||
lazyLoading: false,
|
||||
heading: true,
|
||||
filter: ''
|
||||
})
|
||||
|
||||
export const MUTATION_TYPES = {
|
||||
CHANGE_HEADING: 'changeHeading',
|
||||
APPEND_TIMELINE: 'appendTimeline',
|
||||
UPDATE_TIMELINE: 'updateTimeline',
|
||||
MERGE_TIMELINE:' mergeTimeline',
|
||||
INSERT_TIMELINE: 'insertTimeline',
|
||||
ARCHIVE_TIMELINE: 'archiveTimeline',
|
||||
CLEAR_TIMELINE: 'clearTimeline',
|
||||
UPDATE_TOOT: 'updateToot',
|
||||
DELETE_TOOT: 'deleteToot',
|
||||
CHANGE_LAZY_LOADING: 'changeLazyLoading',
|
||||
CHANGE_FILTER: 'changeFilter'
|
||||
}
|
||||
|
||||
const mutations: MutationTree<LocalState> = {
|
||||
[MUTATION_TYPES.CHANGE_HEADING]: (state, value: boolean) => {
|
||||
state.heading = value
|
||||
},
|
||||
[MUTATION_TYPES.APPEND_TIMELINE]: (state, update: Status) => {
|
||||
if (state.heading) {
|
||||
state.timeline = [update].concat(state.timeline)
|
||||
} else {
|
||||
state.unreadTimeline = [update].concat(state.unreadTimeline)
|
||||
}
|
||||
},
|
||||
[MUTATION_TYPES.UPDATE_TIMELINE]: (state, messages: Array<Status>) => {
|
||||
state.timeline = messages
|
||||
},
|
||||
[MUTATION_TYPES.MERGE_TIMELINE]: (state) => {
|
||||
state.timeline = state.unreadTimeline.slice(0, 80).concat(state.timeline)
|
||||
state.unreadTimeline = []
|
||||
},
|
||||
[MUTATION_TYPES.INSERT_TIMELINE]: (state, messages: Array<Status>) => {
|
||||
state.timeline = state.timeline.concat(messages)
|
||||
},
|
||||
[MUTATION_TYPES.ARCHIVE_TIMELINE]: (state) => {
|
||||
state.timeline = state.timeline.slice(0, 40)
|
||||
},
|
||||
[MUTATION_TYPES.CLEAR_TIMELINE]: (state) => {
|
||||
state.timeline = []
|
||||
state.unreadTimeline = []
|
||||
},
|
||||
[MUTATION_TYPES.UPDATE_TOOT]: (state, message: Status) => {
|
||||
state.timeline = state.timeline.map((toot) => {
|
||||
if (toot.id === message.id) {
|
||||
return message
|
||||
} else if (toot.reblog !== null && toot.reblog.id === message.id) {
|
||||
// When user reblog/favourite a reblogged toot, target message is a original toot.
|
||||
// So, a message which is received now is original toot.
|
||||
const reblog = {
|
||||
reblog: message
|
||||
}
|
||||
return Object.assign(toot, reblog)
|
||||
} else {
|
||||
return toot
|
||||
}
|
||||
})
|
||||
},
|
||||
[MUTATION_TYPES.DELETE_TOOT]: (state, message: Status) => {
|
||||
state.timeline = state.timeline.filter((toot) => {
|
||||
if (toot.reblog !== null && toot.reblog.id === message.id) {
|
||||
return false
|
||||
} else {
|
||||
return toot.id !== message.id
|
||||
}
|
||||
})
|
||||
},
|
||||
[MUTATION_TYPES.CHANGE_LAZY_LOADING]: (state, value: boolean) => {
|
||||
state.lazyLoading = value
|
||||
},
|
||||
[MUTATION_TYPES.CHANGE_FILTER]: (state, filter: string) => {
|
||||
state.filter = filter
|
||||
}
|
||||
}
|
||||
|
||||
const actions: ActionTree<LocalState, RootState> = {
|
||||
fetchLocalTimeline: async ({ commit, rootState }) => {
|
||||
const client = new Mastodon(
|
||||
rootState.TimelineSpace.account.accessToken!,
|
||||
rootState.TimelineSpace.account.baseURL + '/api/v1'
|
||||
)
|
||||
const res: Response<Array<Status>> = await client.get<Array<Status>>('/timelines/public', { limit: 40, local: true })
|
||||
commit(MUTATION_TYPES.UPDATE_TIMELINE, res.data)
|
||||
return res.data
|
||||
},
|
||||
lazyFetchTimeline: async ({ state, commit, rootState }, lastStatus: Status): Promise<Array<Status> | null> => {
|
||||
if (state.lazyLoading) {
|
||||
return Promise.resolve(null)
|
||||
}
|
||||
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, true)
|
||||
const client = new Mastodon(
|
||||
rootState.TimelineSpace.account.accessToken!,
|
||||
rootState.TimelineSpace.account.baseURL + '/api/v1'
|
||||
)
|
||||
return client.get<Array<Status>>('/timelines/public', { max_id: lastStatus.id, limit: 40, local: true })
|
||||
.then(res => {
|
||||
commit(MUTATION_TYPES.INSERT_TIMELINE, res.data)
|
||||
return res.data
|
||||
})
|
||||
.finally(() => {
|
||||
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, false)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const Local: Module<LocalState, RootState> = {
|
||||
namespaced: true,
|
||||
state: state,
|
||||
mutations: mutations,
|
||||
actions: actions
|
||||
}
|
||||
|
||||
export default Local
|
Loading…
x
Reference in New Issue
Block a user