fix broken groups due to empty indexedDb

This commit is contained in:
Bruce Liu 2021-06-27 14:38:07 -07:00
parent 0865041019
commit 9bdec161b0
3 changed files with 19 additions and 12 deletions

View File

@ -3,7 +3,7 @@ import { INIT_SOURCES, SourceActionTypes, ADD_SOURCE, UPDATE_SOURCE, DELETE_SOUR
import { RSSItem, ItemActionTypes, FETCH_ITEMS, fetchItems } from "./item"
import { ActionStatus, AppThunk, getWindowBreakpoint, initTouchBarWithTexts } from "../utils"
import { INIT_FEEDS, FeedActionTypes, ALL, initFeeds } from "./feed"
import { SourceGroupActionTypes, UPDATE_SOURCE_GROUP, ADD_SOURCE_TO_GROUP, DELETE_SOURCE_GROUP, REMOVE_SOURCE_FROM_GROUP, REORDER_SOURCE_GROUPS, fixBrokenGroups } from "./group"
import { SourceGroupActionTypes, UPDATE_SOURCE_GROUP, ADD_SOURCE_TO_GROUP, DELETE_SOURCE_GROUP, REMOVE_SOURCE_FROM_GROUP, REORDER_SOURCE_GROUPS } from "./group"
import { PageActionTypes, SELECT_PAGE, PageType, selectAllArticles, showItemFromId } from "./page"
import { getCurrentLocale } from "../settings"
import locales from "../i18n/_locales"
@ -323,7 +323,6 @@ export function initApp(): AppThunk {
}).then(() => dispatch(initFeeds()))
.then(async () => {
dispatch(selectAllArticles())
dispatch(fixBrokenGroups())
await dispatch(fetchItems())
}).then(() => {
dispatch(updateFavicon())

View File

@ -1,5 +1,5 @@
import intl from "react-intl-universal"
import { SourceActionTypes, ADD_SOURCE, DELETE_SOURCE, addSource, RSSSource } from "./source"
import { SourceActionTypes, ADD_SOURCE, DELETE_SOURCE, addSource, RSSSource, SourceState } from "./source"
import { SourceGroup } from "../../schema-types"
import { ActionStatus, AppThunk, domParser } from "../utils"
import { saveSettings } from "./app"
@ -162,20 +162,26 @@ export function toggleGroupExpansion(groupIndex: number): AppThunk {
}
}
export function fixBrokenGroups(): AppThunk {
export function fixBrokenGroups(sources: SourceState): AppThunk {
return (dispatch, getState) => {
const { sources, groups } = getState()
const { groups } = getState()
const sids = new Set(Object.values(sources).map(s => s.sid))
for (let group of groups) {
for (let sid of group.sids) {
sids.delete(sid)
let isBroken = false
const newGroups: SourceGroup[] = groups.map(group => {
const newGroup: SourceGroup = {
...group,
sids: group.sids.filter(sid => sids.delete(sid))
}
}
if (sids.size > 0) {
if (newGroup.sids.length !== group.sids.length) {
isBroken = true
}
return newGroup
}).filter(group => group.isMultiple || group.sids.length > 0)
if (isBroken || sids.size > 0) {
for (let sid of sids) {
groups.push(new SourceGroup([sid]))
newGroups.push(new SourceGroup([sid]))
}
dispatch(reorderSourceGroups(groups))
dispatch(reorderSourceGroups(newGroups))
}
}
}

View File

@ -6,6 +6,7 @@ import { fetchFavicon, ActionStatus, AppThunk, parseRSS } from "../utils"
import { RSSItem, insertItems, ItemActionTypes, FETCH_ITEMS, MARK_READ, MARK_UNREAD, MARK_ALL_READ } from "./item"
import { saveSettings } from "./app"
import { SourceRule } from "./rule"
import { fixBrokenGroups } from "./group"
export const enum SourceOpenTarget {
Local, Webpage, External, FullContent
@ -182,6 +183,7 @@ export function initSources(): AppThunk<Promise<void>> {
state[source.sid] = source
}
await unreadCount(state)
dispatch(fixBrokenGroups(state))
dispatch(initSourcesSuccess(state))
}
}