Skip to content

Commit

Permalink
fix: group category (#138)
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei authored Jul 16, 2024
1 parent 90b039a commit e78fa4f
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/renderer/src/hooks/biz/useSubscriptionActions.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { apiClient } from "@renderer/lib/api-fetch"
import { Queries } from "@renderer/queries"
import type { SubscriptionPlainModel } from "@renderer/store/subscription"
import type { SubscriptionFlatModel } from "@renderer/store/subscription"
import { subscriptionActions } from "@renderer/store/subscription"
import { feedUnreadActions } from "@renderer/store/unread"
import { useMutation } from "@tanstack/react-query"
Expand All @@ -15,7 +15,7 @@ export const useDeleteSubscription = ({
onSuccess?: () => void
}) =>
useMutation({
mutationFn: async (subscription: SubscriptionPlainModel) =>
mutationFn: async (subscription: SubscriptionFlatModel) =>
subscriptionActions.unfollow(subscription.feedId).then((feed) => {
Queries.subscription.byView(subscription.view).invalidate()
feedUnreadActions.updateByFeedId(subscription.feedId, 0)
Expand Down
19 changes: 9 additions & 10 deletions src/renderer/src/modules/feed-column/category.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import { ROUTE_FEED_IN_FOLDER } from "@renderer/lib/constants"
import { stopPropagation } from "@renderer/lib/dom"
import { showNativeMenu } from "@renderer/lib/native-menu"
import { cn } from "@renderer/lib/utils"
import {
useSubscriptionByFeedId,
} from "@renderer/store/subscription"
import { useSubscriptionByFeedId } from "@renderer/store/subscription"
import { useFeedUnreadStore } from "@renderer/store/unread"
import { AnimatePresence, m } from "framer-motion"
import { memo, useEffect, useState } from "react"
Expand Down Expand Up @@ -38,19 +36,19 @@ function FeedCategoryImpl({
ids.sort((a, b) => (state.data[b] || 0) - (state.data[a] || 0)),
)

const showCollapse = sortByUnreadFeedList.length > 1
const [open, setOpen] = useState(!showCollapse)
const navigate = useNavigateEntry()

const subscription = useSubscriptionByFeedId(ids[0])
const folderName = subscription?.category || subscription.defaultCategory

const showCollapse = sortByUnreadFeedList.length > 1 || subscription?.category
const [open, setOpen] = useState(!showCollapse)
useEffect(() => {
if (showCollapse) {
setOpen(expansion)
}
}, [expansion])

const navigate = useNavigateEntry()

const subscription = useSubscriptionByFeedId(ids[0])
const folderName = subscription?.category
const setCategoryActive = () => {
if (view !== undefined) {
navigate({
Expand All @@ -67,7 +65,8 @@ function FeedCategoryImpl({
)

const isActive = useRouteParamsSelector(
(routerParams) => routerParams.feedId === `${ROUTE_FEED_IN_FOLDER}${folderName}`,
(routerParams) =>
routerParams.feedId === `${ROUTE_FEED_IN_FOLDER}${folderName}`,
)
const { present } = useModalStack()

Expand Down
9 changes: 5 additions & 4 deletions src/renderer/src/modules/feed-column/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ const useGroupedData = (view: FeedViewType) => {
const groupFolder = {} as Record<string, string[]>

for (const subscription of data) {
if (subscription.category) {
if (!groupFolder[subscription.category]) {
groupFolder[subscription.category] = []
const category = subscription.category || subscription.defaultCategory
if (category) {
if (!groupFolder[category]) {
groupFolder[category] = []
}
groupFolder[subscription.category].push(subscription.feedId)
groupFolder[category].push(subscription.feedId)
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/renderer/src/services/subscription.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { subscriptionModel } from "@renderer/database"
import type { SubscriptionPlainModel } from "@renderer/store/subscription"
import type { SubscriptionFlatModel } from "@renderer/store/subscription"

import { BaseService } from "./base"

type SubscriptionModelWithId = SubscriptionPlainModel & { id: string }
type SubscriptionModelWithId = SubscriptionFlatModel & { id: string }

class SubscriptionServiceStatic extends BaseService<SubscriptionModelWithId> {
constructor() {
super(subscriptionModel.table)
}

override async upsertMany(data: SubscriptionPlainModel[]) {
override async upsertMany(data: SubscriptionFlatModel[]) {
this.table.bulkPut(
data.map((d) => ({ ...d, id: this.uniqueId(d.userId, d.feedId) })),
)
}

override upsert(data: SubscriptionPlainModel) {
override upsert(data: SubscriptionFlatModel) {
return this.table.put({
...data,
id: this.uniqueId(data.userId, data.feedId),
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/src/store/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import type { IFuseOptions } from "fuse.js"
import Fuse from "fuse.js"

import type { SubscriptionPlainModel } from "../subscription"
import type { SubscriptionFlatModel } from "../subscription"
import { createZustandStore } from "../utils/helper"
import { SearchType } from "./constants"
import { defineSearchInstance } from "./helper"
Expand Down Expand Up @@ -83,7 +83,7 @@ class SearchActions {
}

const processedSubscriptions = [] as SearchResult<
SubscriptionPlainModel,
SubscriptionFlatModel,
{ feedId: string }
>[]
for (const subscription of subscriptions) {
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/src/store/search/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { EntryModel, FeedModel } from "@renderer/models"

import type { SubscriptionPlainModel } from "../subscription"
import type { SubscriptionFlatModel } from "../subscription"
import type { SearchType } from "./constants"

// @ts-expect-error
Expand All @@ -12,7 +12,7 @@ export interface SearchResult<T extends object, A extends object = object>
export interface SearchState {
feeds: SearchResult<FeedModel>[]
entries: SearchResult<EntryModel, { feedId: string }>[]
subscriptions: SearchResult<SubscriptionPlainModel, { feedId: string }>[]
subscriptions: SearchResult<SubscriptionFlatModel, { feedId: string }>[]

keyword: string
searchType: SearchType
Expand Down
11 changes: 8 additions & 3 deletions src/renderer/src/store/subscription/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { FEED_COLLECTION_LIST, ROUTE_FEED_IN_FOLDER } from "@renderer/lib/constants"
import {
FEED_COLLECTION_LIST,
ROUTE_FEED_IN_FOLDER,
} from "@renderer/lib/constants"
import type { FeedViewType } from "@renderer/lib/enum"

import { useSubscriptionStore } from "../subscription"
Expand All @@ -17,7 +20,9 @@ export const useSubscriptionByFeedId = (feedId: FeedId) =>
export const useFolderFeedsByFeedId = (feedId?: string) =>
useSubscriptionStore((state): string[] | null => {
if (typeof feedId !== "string") return null
if (feedId === FEED_COLLECTION_LIST) { return [feedId] }
if (feedId === FEED_COLLECTION_LIST) {
return [feedId]
}

if (!feedId.startsWith(ROUTE_FEED_IN_FOLDER)) {
return null
Expand All @@ -27,7 +32,7 @@ export const useFolderFeedsByFeedId = (feedId?: string) =>
const feedIds: string[] = []
for (const feedId in state.data) {
const subscription = state.data[feedId]
if (subscription.category === folderName) {
if (subscription.category === folderName || subscription.defaultCategory === folderName) {
feedIds.push(feedId)
}
}
Expand Down
33 changes: 22 additions & 11 deletions src/renderer/src/store/subscription/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,38 @@ import { feedActions, getFeedById } from "../feed"
import { feedUnreadActions } from "../unread"
import { createZustandStore, doMutationAndTransaction } from "../utils/helper"

export type SubscriptionPlainModel = Omit<SubscriptionModel, "feeds">
export type SubscriptionFlatModel = Omit<SubscriptionModel, "feeds"> & {
defaultCategory?: string
}
interface SubscriptionState {
/**
* Key: feedId
* Value: SubscriptionPlainModel
*/
data: Record<FeedId, SubscriptionPlainModel>
data: Record<FeedId, SubscriptionFlatModel>
/**
* Key: FeedViewType
* Value: FeedId[]
*/
dataIdByView: Record<FeedViewType, FeedId[]>
}

function morphResponseData(data: SubscriptionModel[]) {
function morphResponseData(data: SubscriptionModel[]): SubscriptionFlatModel[] {
const result: SubscriptionFlatModel[] = []
for (const subscription of data) {
const cloned: SubscriptionFlatModel = { ...subscription }
if (!subscription.category && subscription.feeds) {
const { siteUrl } = subscription.feeds
if (!siteUrl) continue
const parsed = parse(siteUrl)
parsed.domain &&
(subscription.category = capitalizeFirstLetter(parsed.domain))

if (parsed.domain) {
cloned.defaultCategory = capitalizeFirstLetter(parsed.domain)
}
}
result.push(cloned)
}
return result
}

const emptyDataIdByView: Record<FeedViewType, FeedId[]> = {
Expand Down Expand Up @@ -79,14 +87,14 @@ class SubscriptionActions {
}))
}

morphResponseData(res.data)
this.upsertMany(res.data)
const transformedData = morphResponseData(res.data)
this.upsertMany(transformedData)
feedActions.upsertMany(res.data.map((s) => s.feeds))

return res.data
}

upsertMany(subscriptions: SubscriptionPlainModel[]) {
upsertMany(subscriptions: SubscriptionFlatModel[]) {
runTransactionInScope(() => {
SubscriptionService.upsertMany(subscriptions)
})
Expand Down Expand Up @@ -151,17 +159,20 @@ class SubscriptionActions {
const { siteUrl } = feed
if (!siteUrl) return
const parsed = parse(siteUrl)
subscription.category = null
// The logic for removing Category here is to use domain as the default category name.
parsed.domain &&
(subscription.category = capitalizeFirstLetter(
parsed.domain,
(subscription.defaultCategory = (
capitalizeFirstLetter(parsed.domain)
))
}
})
}),
)
const { data } = get()
return ids.map((id) => data[id] && SubscriptionService.upsert(data[id]))
return ids.map(
(id) => data[id] && SubscriptionService.upsert(data[id]),
)
},
{
doTranscationWhenMutationFail: false,
Expand Down

0 comments on commit e78fa4f

Please sign in to comment.