feat(app): support cache limit and clean cache

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2024-11-05 23:01:42 +08:00
parent 1d0eca1ca1
commit dee294dda5
48 changed files with 624 additions and 128 deletions
@@ -5,7 +5,7 @@ function getSettings() {
const settings = [] as {
name: I18nKeysForSettings
iconName: string
icon: string | React.ReactNode
path: string
Component: () => JSX.Element
priority: number
@@ -0,0 +1,192 @@
import { CarbonInfinitySymbol } from "@follow/components/icons/infinify.jsx"
import { Button } from "@follow/components/ui/button/index.js"
import { Label } from "@follow/components/ui/label/index.jsx"
import { Slider } from "@follow/components/ui/slider/index.js"
import { env } from "@follow/shared/env"
import { useQuery } from "@tanstack/react-query"
import { useEffect } from "react"
import { useTranslation } from "react-i18next"
import { setGeneralSetting, useGeneralSettingValue } from "~/atoms/settings/general"
import { useModalStack } from "~/components/ui/modal/stacked/hooks"
import { exportDB } from "~/database"
import { initAnalytics } from "~/initialize/analytics"
import { tipcClient } from "~/lib/client"
import { queryClient } from "~/lib/query-client"
import { clearLocalPersistStoreData } from "~/store/utils/clear"
import { SettingDescription } from "../control"
import { createSetting } from "../helper/builder"
import { SettingItemGroup } from "../section"
const { defineSettingItem, SettingBuilder } = createSetting(
useGeneralSettingValue,
setGeneralSetting,
)
export const SettingDataControl = () => {
const { t } = useTranslation("settings")
useEffect(() => {
tipcClient?.getLoginItemSettings().then((settings) => {
setGeneralSetting("appLaunchOnStartup", settings.openAtLogin)
})
}, [])
const { present } = useModalStack()
return (
<div className="mt-4">
<SettingBuilder
settings={[
{
type: "title",
value: t("general.privacy"),
},
defineSettingItem("sendAnonymousData", {
label: t("general.send_anonymous_data.label"),
description: t("general.send_anonymous_data.description"),
onChange(value) {
setGeneralSetting("sendAnonymousData", value)
if (value) {
initAnalytics()
} else {
window.analytics?.reset()
delete window.analytics
}
},
}),
{
type: "title",
value: t("general.data"),
},
defineSettingItem("dataPersist", {
label: t("general.data_persist.label"),
description: t("general.data_persist.description"),
}),
{
label: t("general.rebuild_database.label"),
action: () => {
present({
title: t("general.rebuild_database.title"),
clickOutsideToDismiss: true,
content: () => (
<div className="text-sm">
<p>{t("general.rebuild_database.warning.line1")}</p>
<p>{t("general.rebuild_database.warning.line2")}</p>
<div className="mt-4 flex justify-end">
<Button
className="bg-red-500 px-3 text-white"
onClick={async () => {
await clearLocalPersistStoreData()
window.location.reload()
}}
>
{t("ok", { ns: "common" })}
</Button>
</div>
</div>
),
})
},
description: t("general.rebuild_database.description"),
buttonText: t("general.rebuild_database.button"),
},
{
label: t("general.export_database.label"),
description: t("general.export_database.description"),
buttonText: t("general.export_database.button"),
action: () => {
exportDB()
},
},
{
label: t("general.export.label"),
description: t("general.export.description"),
buttonText: t("general.export.button"),
action: () => {
const link = document.createElement("a")
link.href = `${env.VITE_API_URL}/subscriptions/export`
link.download = "follow.opml"
link.click()
},
},
{
type: "title",
value: t("general.cache"),
},
AppCacheLimit,
{
label: t("data_control.clean_cache.button"),
description: t("data_control.clean_cache.description"),
buttonText: t("data_control.clean_cache.button"),
action: async () => {
await tipcClient?.clearCache()
queryClient.invalidateQueries({ queryKey: ["app", "cache", "size"] })
},
},
]}
/>
</div>
)
}
const AppCacheLimit = () => {
const { t } = useTranslation("settings")
const { data: cacheSize, isLoading: isLoadingCacheSize } = useQuery({
queryKey: ["app", "cache", "size"],
queryFn: async () => {
const byteSize = (await tipcClient?.getCacheSize()) ?? 0
return Math.round(byteSize / 1024 / 1024)
},
})
const {
data: cacheLimit,
isLoading: isLoadingCacheLimit,
refetch: refetchCacheLimit,
} = useQuery({
queryKey: ["app", "cache", "limit"],
queryFn: async () => {
const size = (await tipcClient?.getCacheLimit()) ?? 0
return size
},
})
const onChange = (value: number[]) => {
tipcClient?.limitCacheSize(value[0])
refetchCacheLimit()
}
if (isLoadingCacheSize || isLoadingCacheLimit) return null
const InfinitySymbol = <CarbonInfinitySymbol />
return (
<SettingItemGroup>
<div className={"mb-3 flex items-center justify-between gap-4"}>
<Label className="center flex">
{t("data_control.app_cache_limit.label")}
<span className="center ml-2 flex shrink-0 gap-1 text-xs text-gray-500">
<span>({cacheSize}M</span> /{" "}
<span className="center flex shrink-0">
{cacheLimit ? `${cacheLimit}M` : InfinitySymbol})
</span>
</span>
</Label>
<div className="relative flex w-1/5 flex-col gap-1">
<Slider
min={0}
max={500}
step={100}
defaultValue={[cacheLimit ?? 0]}
onValueCommit={onChange}
/>
<div className="absolute bottom-[-1.5em] text-base opacity-50">{InfinitySymbol}</div>
<div className="absolute bottom-[-1.5em] right-0 text-xs opacity-50">500M</div>
</div>
</div>
<SettingDescription>{t("data_control.app_cache_limit.description")}</SettingDescription>
</SettingItemGroup>
)
}
@@ -1,4 +1,3 @@
import { Button } from "@follow/components/ui/button/index.js"
import { LoadingCircle } from "@follow/components/ui/loading/index.jsx"
import {
Select,
@@ -8,7 +7,6 @@ import {
SelectValue,
} from "@follow/components/ui/select/index.jsx"
import { IN_ELECTRON } from "@follow/shared/constants"
import { env } from "@follow/shared/env"
import { cn } from "@follow/utils/utils"
import { useQuery } from "@tanstack/react-query"
import { useAtom } from "jotai"
@@ -24,13 +22,9 @@ import {
useGeneralSettingSelector,
useGeneralSettingValue,
} from "~/atoms/settings/general"
import { useModalStack } from "~/components/ui/modal/stacked/hooks"
import { exportDB } from "~/database"
import { useProxyValue, useSetProxy } from "~/hooks/biz/useProxySetting"
import { fallbackLanguage } from "~/i18n"
import { initAnalytics } from "~/initialize/analytics"
import { tipcClient } from "~/lib/client"
import { clearLocalPersistStoreData } from "~/store/utils/clear"
import { SettingDescription, SettingInput } from "../control"
import { createSetting } from "../helper/builder"
@@ -54,8 +48,6 @@ export const SettingGeneral = () => {
setGeneralSetting("appLaunchOnStartup", checked)
}, [])
const { present } = useModalStack()
return (
<div className="mt-4">
<SettingBuilder
@@ -105,82 +97,6 @@ export const SettingGeneral = () => {
IN_ELECTRON && VoiceSelector,
// { type: "title", value: "Secure" },
// defineSettingItem("jumpOutLinkWarn", {
// label: "Warn when opening external links",
// description: "When you open an untrusted external link, you need to make sure that you open the link.",
// }),
{
type: "title",
value: t("general.privacy_data"),
},
defineSettingItem("dataPersist", {
label: t("general.data_persist.label"),
description: t("general.data_persist.description"),
}),
defineSettingItem("sendAnonymousData", {
label: t("general.send_anonymous_data.label"),
description: t("general.send_anonymous_data.description"),
onChange(value) {
setGeneralSetting("sendAnonymousData", value)
if (value) {
initAnalytics()
} else {
window.analytics?.reset()
delete window.analytics
}
},
}),
{
label: t("general.rebuild_database.label"),
action: () => {
present({
title: t("general.rebuild_database.title"),
clickOutsideToDismiss: true,
content: () => (
<div className="text-sm">
<p>{t("general.rebuild_database.warning.line1")}</p>
<p>{t("general.rebuild_database.warning.line2")}</p>
<div className="mt-4 flex justify-end">
<Button
className="bg-red-500 px-3 text-white"
onClick={async () => {
await clearLocalPersistStoreData()
window.location.reload()
}}
>
{t("ok", { ns: "common" })}
</Button>
</div>
</div>
),
})
},
description: t("general.rebuild_database.description"),
buttonText: t("general.rebuild_database.button"),
},
{
label: t("general.export_database.label"),
description: t("general.export_database.description"),
buttonText: t("general.export_database.button"),
action: () => {
exportDB()
},
},
{
label: t("general.export.label"),
description: t("general.export.description"),
buttonText: t("general.export.button"),
action: () => {
const link = document.createElement("a")
link.href = `${env.VITE_API_URL}/subscriptions/export`
link.download = "follow.opml"
link.click()
},
},
{ type: "title", value: t("general.network"), disabled: !IN_ELECTRON },
IN_ELECTRON && NettingSetting,
]}
@@ -189,7 +105,7 @@ export const SettingGeneral = () => {
)
}
export const VoiceSelector = () => {
const VoiceSelector = () => {
const { t } = useTranslation("settings")
const { data } = useQuery({
+9 -3
View File
@@ -1,5 +1,6 @@
import { EllipsisHorizontalTextWithTooltip } from "@follow/components/ui/typography/index.js"
import { cn } from "@follow/utils/utils"
import { Slot } from "@radix-ui/react-slot"
import { useContext } from "react"
import { useTranslation } from "react-i18next"
import { useLoaderData } from "react-router-dom"
@@ -18,7 +19,11 @@ export const SettingsSidebarTitle = ({ path, className }: { path: string; classN
return (
<div className={cn("flex min-w-0 items-center gap-2 text-[0.94rem] font-medium", className)}>
<i className={`${tab.iconName} shrink-0 text-[19px]`} />
{typeof tab.icon === "string" ? (
<i className={`${tab.icon} shrink-0 text-[19px]`} />
) : (
<Slot className="shrink-0 text-[19px]">{tab.icon}</Slot>
)}
<EllipsisHorizontalTextWithTooltip>{t(tab.name as any)}</EllipsisHorizontalTextWithTooltip>
</div>
)
@@ -33,11 +38,12 @@ export const SettingsTitle = ({
}) => {
const { t } = useTranslation("settings")
const {
iconName,
icon: iconName,
name: title,
headerIcon,
} = (useLoaderData() || loader?.() || {}) as SettingPageConfig
const usedIcon = headerIcon || iconName
const isInSettingIndependentWindow = useContext(IsInSettingIndependentWindowContext)
if (!title) {
return null
@@ -51,7 +57,7 @@ export const SettingsTitle = ({
className,
)}
>
<i className={headerIcon || iconName} />
{typeof usedIcon === "string" ? <i className={usedIcon} /> : usedIcon}
<span>{t(title as any)}</span>
</div>
)
+2 -2
View File
@@ -10,10 +10,10 @@ export enum DisableWhy {
}
export interface SettingPageConfig {
iconName: string
icon: string | React.ReactNode
name: I18nKeysForSettings
priority: number
headerIcon?: string
headerIcon?: string | React.ReactNode
hideIf?: (ctx: SettingPageContext) => boolean
disableIf?: (ctx: SettingPageContext) => [boolean, DisableWhy]
}