From 95b23c3be75c1a36720520a40f0f217efa6d080e Mon Sep 17 00:00:00 2001 From: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Date: Mon, 16 Jun 2025 16:25:43 +0800 Subject: [PATCH] refactor: move image sync service to mobile, bring hydration back --- .../src/modules/player/PlayerTabBar.tsx | 2 +- .../src/modules/settings/UserHeaderBanner.tsx | 3 ++- apps/mobile/src/screens/PlayerScreen.tsx | 2 +- apps/mobile/src/store/image/hooks.ts | 11 ++++++++++ apps/mobile/src/store/image/store.ts | 21 +++++++++++++++++++ packages/internal/store/src/hydrate.ts | 2 ++ packages/internal/store/src/image/hooks.ts | 11 +--------- packages/internal/store/src/image/store.ts | 19 ----------------- 8 files changed, 39 insertions(+), 32 deletions(-) create mode 100644 apps/mobile/src/store/image/hooks.ts create mode 100644 apps/mobile/src/store/image/store.ts diff --git a/apps/mobile/src/modules/player/PlayerTabBar.tsx b/apps/mobile/src/modules/player/PlayerTabBar.tsx index e46786dc1..aa2ca5670 100644 --- a/apps/mobile/src/modules/player/PlayerTabBar.tsx +++ b/apps/mobile/src/modules/player/PlayerTabBar.tsx @@ -1,4 +1,3 @@ -import { usePrefetchImageColors } from "@follow/store/image/hooks" import { cn } from "@follow/utils" import { useAtomValue } from "jotai" import { use, useEffect } from "react" @@ -15,6 +14,7 @@ import { BottomTabContext } from "@/src/lib/navigation/bottom-tab/BottomTabConte import { useNavigation } from "@/src/lib/navigation/hooks" import { useActiveTrack } from "@/src/lib/player" import { PlayerScreen } from "@/src/screens/PlayerScreen" +import { usePrefetchImageColors } from "@/src/store/image/hooks" import { PlayPauseButton, SeekButton } from "./control" diff --git a/apps/mobile/src/modules/settings/UserHeaderBanner.tsx b/apps/mobile/src/modules/settings/UserHeaderBanner.tsx index bf3c1d792..f3d4d006d 100644 --- a/apps/mobile/src/modules/settings/UserHeaderBanner.tsx +++ b/apps/mobile/src/modules/settings/UserHeaderBanner.tsx @@ -1,4 +1,4 @@ -import { useImageColors, usePrefetchImageColors } from "@follow/store/image/hooks" +import { useImageColors } from "@follow/store/image/hooks" import { useUserById } from "@follow/store/user/hooks" import { cn, getLuminance } from "@follow/utils" import { LinearGradient } from "expo-linear-gradient" @@ -21,6 +21,7 @@ import { WebCuteReIcon } from "@/src/icons/web_cute_re" import { YoutubeCuteFiIcon } from "@/src/icons/youtube_cute_fi" import { useNavigation } from "@/src/lib/navigation/hooks" import { LoginScreen } from "@/src/screens/(modal)/LoginScreen" +import { usePrefetchImageColors } from "@/src/store/image/hooks" const defaultGradientColors = ["#000", "#100", "#200"] diff --git a/apps/mobile/src/screens/PlayerScreen.tsx b/apps/mobile/src/screens/PlayerScreen.tsx index 9b6fc4bfd..0ba45a1d2 100644 --- a/apps/mobile/src/screens/PlayerScreen.tsx +++ b/apps/mobile/src/screens/PlayerScreen.tsx @@ -1,4 +1,3 @@ -import { usePrefetchImageColors } from "@follow/store/image/hooks" import { cn } from "@follow/utils" import { LinearGradient } from "expo-linear-gradient" import { useEffect, useMemo } from "react" @@ -20,6 +19,7 @@ import { useActiveTrack, useIsPlaying } from "../lib/player" import { PlayerScreenContext, usePlayerScreenContext } from "../modules/player/context" import { ControlGroup, ProgressBar, VolumeBar } from "../modules/player/control" import { useCoverGradient } from "../modules/player/hooks" +import { usePrefetchImageColors } from "../store/image/hooks" function CoverArt({ cover }: { cover?: string }) { const scale = useSharedValue(1) diff --git a/apps/mobile/src/store/image/hooks.ts b/apps/mobile/src/store/image/hooks.ts new file mode 100644 index 000000000..361b47a72 --- /dev/null +++ b/apps/mobile/src/store/image/hooks.ts @@ -0,0 +1,11 @@ +import { useQuery } from "@tanstack/react-query" + +import { imageSyncService } from "./store" + +export const usePrefetchImageColors = (url?: string | null) => { + useQuery({ + queryKey: ["image", "colors", url], + queryFn: () => imageSyncService.getColors(url), + enabled: !!url, + }) +} diff --git a/apps/mobile/src/store/image/store.ts b/apps/mobile/src/store/image/store.ts new file mode 100644 index 000000000..3f66bbd97 --- /dev/null +++ b/apps/mobile/src/store/image/store.ts @@ -0,0 +1,21 @@ +import { getImageInfo } from "@follow/store/image/getters" +import { imageActions } from "@follow/store/image/store" +import ImageColors from "react-native-image-colors" + +class ImageSyncService { + async getColors(url?: string | null) { + if (!url) { + return + } + const existing = getImageInfo(url)?.colors + if (existing) { + return existing + } + + const result = await ImageColors.getColors(url, { cache: true }) + await imageActions.upsertMany([{ url, colors: result }]) + return result + } +} + +export const imageSyncService = new ImageSyncService() diff --git a/packages/internal/store/src/hydrate.ts b/packages/internal/store/src/hydrate.ts index 1bacd3bc7..8cb28a3ab 100644 --- a/packages/internal/store/src/hydrate.ts +++ b/packages/internal/store/src/hydrate.ts @@ -3,6 +3,7 @@ import { initializeDB, migrateDB } from "@follow/database/db" import { collectionActions } from "./collection/store" import { entryActions } from "./entry/store" import { feedActions } from "./feed/store" +import { imageActions } from "./image/store" import { inboxActions } from "./inbox/store" import type { Hydratable } from "./internal/base" import { listActions } from "./list/store" @@ -23,6 +24,7 @@ const hydrates: Hydratable[] = [ collectionActions, summaryActions, translationActions, + imageActions, ] export const hydrateDatabaseToStore = async (options?: { migrateDatabase?: boolean }) => { diff --git a/packages/internal/store/src/image/hooks.ts b/packages/internal/store/src/image/hooks.ts index 222bcc5f3..35e158c1d 100644 --- a/packages/internal/store/src/image/hooks.ts +++ b/packages/internal/store/src/image/hooks.ts @@ -1,15 +1,6 @@ -import { useQuery } from "@tanstack/react-query" import { useCallback } from "react" -import { imageSyncService, useImagesStore } from "./store" - -export const usePrefetchImageColors = (url?: string | null) => { - useQuery({ - queryKey: ["image", "colors", url], - queryFn: () => imageSyncService.getColors(url), - enabled: !!url, - }) -} +import { useImagesStore } from "./store" export const useImageColors = (url?: string | null) => { return useImagesStore( diff --git a/packages/internal/store/src/image/store.ts b/packages/internal/store/src/image/store.ts index b5ccabef9..0c3454b65 100644 --- a/packages/internal/store/src/image/store.ts +++ b/packages/internal/store/src/image/store.ts @@ -1,10 +1,8 @@ import type { ImageSchema } from "@follow/database/schemas/types" import { ImagesService } from "@follow/database/services/image" -import ImageColors from "react-native-image-colors" import type { Hydratable } from "../internal/base" import { createImmerSetter, createTransaction, createZustandStore } from "../internal/helper" -import { getImageInfo } from "./getters" export type ImageModel = ImageSchema type ImageStore = { @@ -17,22 +15,6 @@ export const useImagesStore = createZustandStore("images")(() => ({ const immerSet = createImmerSetter(useImagesStore) -class ImageSyncService { - async getColors(url?: string | null) { - if (!url) { - return - } - const existing = getImageInfo(url)?.colors - if (existing) { - return existing - } - - const result = await ImageColors.getColors(url, { cache: true }) - await imageActions.upsertMany([{ url, colors: result }]) - return result - } -} - class ImageActions implements Hydratable { async hydrate() { const images = await ImagesService.getImageAll() @@ -55,5 +37,4 @@ class ImageActions implements Hydratable { } } -export const imageSyncService = new ImageSyncService() export const imageActions = new ImageActions()