import { CarbonInfinitySymbol } from "@follow/components/icons/infinify.jsx"
import { Button, MotionButtonBase } 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 { useTranslation } from "react-i18next"
import { setGeneralSetting, useGeneralSettingValue } from "~/atoms/settings/general"
import { useModalStack } from "~/components/ui/modal/stacked/hooks"
import { isElectronBuild } from "~/constants"
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 { SettingActionItem, 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")
const { present } = useModalStack()
return (
{
present({
title: t("general.rebuild_database.title"),
clickOutsideToDismiss: true,
content: () => (
{t("general.rebuild_database.warning.line1")}
{t("general.rebuild_database.warning.line2")}
),
})
},
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()
},
},
isElectronBuild && {
type: "title",
value: t("general.cache"),
},
isElectronBuild && AppCacheLimit,
isElectronBuild ? CleanElectronCache : CleanCacheStorage,
isElectronBuild && {
type: "title",
value: t("general.data_file.label"),
},
isElectronBuild && {
label: t("general.log_file.label"),
description: t("general.log_file.description"),
buttonText: t("general.log_file.button"),
action: () => {
tipcClient?.revealLogFile()
},
},
]}
/>
)
}
/**
* @description clean web app service worker cache
*/
const CleanCacheStorage = () => {
const { t } = useTranslation("settings")
return (
{t("data_control.clean_cache.button")}
}
action={async () => {
const keys = await caches.keys()
return Promise.all(
keys.map((key) => {
if (key.startsWith("workbox-precache-")) return null
return caches.delete(key)
}),
)
}}
buttonText={t("data_control.clean_cache.button")}
/>
{t("data_control.clean_cache.description_web")}
)
}
const CleanElectronCache = () => {
const { t } = useTranslation("settings")
return (
{t("data_control.clean_cache.button")}
{
tipcClient?.openCacheFolder()
}}
className="center flex"
>
}
action={async () => {
await tipcClient?.clearCache()
queryClient.setQueryData(["app", "cache", "size"], 0)
}}
buttonText={t("data_control.clean_cache.button")}
/>
{t("data_control.clean_cache.description")}
)
}
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)
},
refetchOnMount: "always",
})
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 =
return (
{t("data_control.app_cache_limit.description")}
)
}