Skip to content

Commit

Permalink
feat: target model
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Sep 20, 2024
1 parent 7eda5f6 commit 65fa3ad
Show file tree
Hide file tree
Showing 14 changed files with 409 additions and 238 deletions.
14 changes: 7 additions & 7 deletions apps/renderer/src/components/feed-icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { forwardRef, useMemo } from "react"
import { getColorScheme, stringToHue } from "~/lib/color"
import { getImageProxyUrl } from "~/lib/img-proxy"
import { cn, getUrlIcon } from "~/lib/utils"
import type { CombinedEntryModel, FeedModel } from "~/models"
import type { CombinedEntryModel, FeedModel,TargetModel } from "~/models"

import { PlatformIcon } from "./ui/platform-icon"

Expand Down Expand Up @@ -75,7 +75,7 @@ export function FeedIcon({
siteUrl,
useMedia,
}: {
feed?: FeedModel
feed?: TargetModel
entry?: CombinedEntryModel["entries"]
fallbackUrl?: string
className?: string
Expand All @@ -98,8 +98,8 @@ export function FeedIcon({
}

const colors = useMemo(
() => getColorScheme(stringToHue(feed?.title || feed?.url || siteUrl!), true),
[feed?.title, feed?.url, siteUrl],
() => getColorScheme(stringToHue(feed?.title || (feed as FeedModel)?.url || siteUrl!), true),
[feed?.title, (feed as FeedModel)?.url, siteUrl],
)
let ImageElement: ReactNode
let finalSrc = ""
Expand Down Expand Up @@ -137,9 +137,9 @@ export function FeedIcon({
break
}
case !!fallbackUrl:
case !!feed?.siteUrl: {
case !!(feed as FeedModel)?.siteUrl: {
const [src, fallbackSrc] = getFeedIconSrc({
siteUrl: feed?.siteUrl || fallbackUrl,
siteUrl: (feed as FeedModel)?.siteUrl || fallbackUrl,
fallback,
proxy: {
width: size * 2,
Expand All @@ -150,7 +150,7 @@ export function FeedIcon({

ImageElement = (
<PlatformIcon
url={feed?.siteUrl!}
url={(feed as FeedModel)?.siteUrl!}
style={sizeStyle}
className={cn("center mr-2", className)}
>
Expand Down
4 changes: 2 additions & 2 deletions apps/renderer/src/database/schemas/feed.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { FeedModel } from "~/models"
import type { TargetModel } from "~/models"

export type DB_FeedUnread = {
id: string
count: number
}

export type DB_Feed = FeedModel & { id: string }
export type DB_Feed = TargetModel & { id: string }
9 changes: 4 additions & 5 deletions apps/renderer/src/models/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ export type TransactionModel = ExtractBizResponse<
typeof apiClient.wallets.transactions.$get
>["data"][number]

export type FeedModel = ExtractBizResponse<typeof apiClient.feeds.$get>["data"]["feed"] & {
owner?: UserModel | null
tipUsers?: UserModel[] | null
}
export type FeedModel = ExtractBizResponse<typeof apiClient.feeds.$get>["data"]["feed"]

export type ListModel = ExtractBizResponse<typeof apiClient.lists.$get>["data"]["list"]

export type ListModel = ExtractBizResponse<typeof apiClient.lists.$get>["data"]
export type TargetModel = FeedModel | ListModel

export type EntryResponse = Exclude<
Extract<ExtractBizResponse<typeof apiClient.entries.$get>, { code: 0 }>["data"],
Expand Down
4 changes: 2 additions & 2 deletions apps/renderer/src/modules/entry-column/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { FC } from "react"

import type { CombinedEntryModel, FeedModel } from "~/models"
import type { CombinedEntryModel, TargetModel } from "~/models"

export type UniversalItemProps = {
entryId: string
entryPreview?: CombinedEntryModel & {
feeds: FeedModel
feeds: TargetModel
feedId: string
}
translation?: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ export function Component() {
<Item
entryPreview={{
entries: entry,
// @ts-expect-error
feeds: feed.data.feed as FeedModel,
feeds: feed.data.feed,
read: true,
feedId: feed.data.feed.id!,
}}
Expand Down
4 changes: 2 additions & 2 deletions apps/renderer/src/services/cleaner.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-nocheck
import type { FeedModel } from "@follow/shared/hono"
import type { TargetModel } from "@follow/shared/hono"
import { beforeAll, describe, expect, test } from "vitest"

import { browserDB } from "~/database"
Expand Down Expand Up @@ -42,7 +42,7 @@ const subscriptions: SubscriptionFlatModel[] = [
},
]

const feeds: FeedModel[] = [
const feeds: TargetModel[] = [
{
id: "feed-id-1",
},
Expand Down
14 changes: 7 additions & 7 deletions apps/renderer/src/services/feed.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { browserDB } from "~/database"
import type { FeedModel } from "~/models/types"
import type { TargetModel } from "~/models/types"

import { BaseService } from "./base"
import { CleanerService } from "./cleaner"

type FeedModelWithId = FeedModel & { id: string }
class ServiceStatic extends BaseService<FeedModelWithId> {
type TargetModelWithId = TargetModel & { id: string }
class ServiceStatic extends BaseService<TargetModelWithId> {
constructor() {
super(browserDB.feeds)
}

override async upsertMany(data: FeedModel[]) {
override async upsertMany(data: TargetModel[]) {
const filterData = data.filter((d) => d.id)
CleanerService.reset(filterData.map((d) => ({ type: "feed", id: d.id! })))

return this.table.bulkPut(filterData as FeedModelWithId[])
return this.table.bulkPut(filterData as TargetModelWithId[])
}

override async upsert(data: FeedModel): Promise<string | null> {
override async upsert(data: TargetModel): Promise<string | null> {
if (!data.id) return null
CleanerService.reset([{ type: "feed", id: data.id }])
return this.table.put(data as FeedModelWithId)
return this.table.put(data as TargetModelWithId)
}

async bulkDelete(ids: string[]) {
Expand Down
4 changes: 2 additions & 2 deletions apps/renderer/src/store/entry/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { isNil, merge, omit } from "lodash-es"
import { runTransactionInScope } from "~/database"
import { apiClient } from "~/lib/api-fetch"
import { getEntriesParams, omitObjectUndefinedValue } from "~/lib/utils"
import type { CombinedEntryModel, EntryModel, FeedModel, UserModel } from "~/models"
import type { CombinedEntryModel, EntryModel, TargetModel, UserModel } from "~/models"
import { EntryService } from "~/services"

import { feedActions } from "../feed"
Expand Down Expand Up @@ -183,7 +183,7 @@ class EntryActions {
}

upsertMany(data: CombinedEntryModel[]) {
const feeds = [] as FeedModel[]
const feeds = [] as TargetModel[]
const entries = [] as EntryModel[]
const entry2Read = {} as Record<string, boolean>
const entryFeedMap = {} as Record<string, string>
Expand Down
6 changes: 3 additions & 3 deletions apps/renderer/src/store/feed/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { useShallow } from "zustand/react/shallow"

import { FEED_COLLECTION_LIST, ROUTE_FEED_IN_FOLDER, ROUTE_FEED_PENDING, views } from "~/constants"
import { useRouteParms } from "~/hooks/biz/useRouteParams"
import type { FeedModel } from "~/models"
import type { TargetModel } from "~/models"

import { getSubscriptionByFeedId } from "../subscription"
import { useFeedStore } from "./store"
import type { FeedQueryParams } from "./types"

export const useFeedById = (feedId: Nullable<string>): FeedModel | null =>
export const useFeedById = (feedId: Nullable<string>): TargetModel | null =>
useFeedStore((state) => (feedId ? state.feeds[feedId] : null))

export const useFeedByIdOrUrl = (feed: FeedQueryParams) =>
Expand All @@ -26,7 +26,7 @@ export const useFeedByIdOrUrl = (feed: FeedQueryParams) =>

export const useFeedByIdSelector = <T>(
feedId: Nullable<string>,
selector: (feed: FeedModel) => T,
selector: (feed: TargetModel) => T,
) =>
useFeedStore(
useShallow((state) => (feedId && state.feeds[feedId] ? selector(state.feeds[feedId]) : null)),
Expand Down
10 changes: 5 additions & 5 deletions apps/renderer/src/store/feed/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { nanoid } from "nanoid"
import { whoami } from "~/atoms/user"
import { runTransactionInScope } from "~/database"
import { apiClient } from "~/lib/api-fetch"
import type { FeedModel, UserModel } from "~/models"
import type { TargetModel, UserModel } from "~/models"
import { FeedService } from "~/services"

import { getSubscriptionByFeedId } from "../subscription"
Expand All @@ -23,7 +23,7 @@ class FeedActions {
set({ feeds: {} })
}

upsertMany(feeds: FeedModel[]) {
upsertMany(feeds: TargetModel[]) {
runTransactionInScope(() => {
FeedService.upsertMany(feeds)
})
Expand Down Expand Up @@ -60,7 +60,7 @@ class FeedActions {
)
}

private patch(feedId: string, patch: Partial<FeedModel>) {
private patch(feedId: string, patch: Partial<TargetModel>) {
set((state) =>
produce(state, (state) => {
const feed = state.feeds[feedId]
Expand Down Expand Up @@ -123,10 +123,10 @@ class FeedActions {
}
export const feedActions = new FeedActions()

export const getFeedById = (feedId: string): Nullable<FeedModel> =>
export const getFeedById = (feedId: string): Nullable<TargetModel> =>
useFeedStore.getState().feeds[feedId]

export const getPreferredTitle = (feed?: FeedModel | null) => {
export const getPreferredTitle = (feed?: TargetModel | null) => {
if (!feed?.id) {
return feed?.title
}
Expand Down
8 changes: 4 additions & 4 deletions apps/renderer/src/store/feed/types.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { FeedModel } from "~/models"
import type { TargetModel } from "~/models"

type FeedId = string

export interface FeedState {
feeds: Record<FeedId, FeedModel>
feeds: Record<FeedId, TargetModel>
}

export interface FeedActions {
upsertMany: (feeds: FeedModel[]) => void
upsertMany: (feeds: TargetModel[]) => void
clear: () => void
patch: (feedId: FeedId, patch: Partial<FeedModel>) => void
patch: (feedId: FeedId, patch: Partial<TargetModel>) => void
}

export type FeedQueryParams = { id?: string; url?: string }
4 changes: 2 additions & 2 deletions apps/renderer/src/store/search/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { EntryModel, FeedModel } from "~/models"
import type { EntryModel, TargetModel } from "~/models"

import type { SubscriptionFlatModel } from "../subscription"
import type { SearchType } from "./constants"
Expand All @@ -9,7 +9,7 @@ export interface SearchResult<T extends object, A extends object = object> exten
}

export interface SearchState {
feeds: SearchResult<FeedModel>[]
feeds: SearchResult<TargetModel>[]
entries: SearchResult<EntryModel, { feedId: string }>[]
subscriptions: SearchResult<SubscriptionFlatModel, { feedId: string }>[]

Expand Down
2 changes: 1 addition & 1 deletion apps/renderer/src/store/subscription/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class SubscriptionActions {

const transformedData = morphResponseData(res.data)
this.upsertMany(transformedData)
feedActions.upsertMany(res.data.map((s) => s.feeds))
feedActions.upsertMany(res.data.map((s) => ("feeds" in s ? s.feeds : s.lists)))

return res.data
}
Expand Down
Loading

0 comments on commit 65fa3ad

Please sign in to comment.