|
| 1 | +import { CarbonInfinitySymbol } from "@follow/components/icons/infinify.jsx" |
| 2 | +import { Button } from "@follow/components/ui/button/index.js" |
| 3 | +import { Label } from "@follow/components/ui/label/index.jsx" |
| 4 | +import { Slider } from "@follow/components/ui/slider/index.js" |
| 5 | +import { env } from "@follow/shared/env" |
| 6 | +import { useQuery } from "@tanstack/react-query" |
| 7 | +import { useEffect } from "react" |
| 8 | +import { useTranslation } from "react-i18next" |
| 9 | + |
| 10 | +import { setGeneralSetting, useGeneralSettingValue } from "~/atoms/settings/general" |
| 11 | +import { useModalStack } from "~/components/ui/modal/stacked/hooks" |
| 12 | +import { exportDB } from "~/database" |
| 13 | +import { initAnalytics } from "~/initialize/analytics" |
| 14 | +import { tipcClient } from "~/lib/client" |
| 15 | +import { queryClient } from "~/lib/query-client" |
| 16 | +import { clearLocalPersistStoreData } from "~/store/utils/clear" |
| 17 | + |
| 18 | +import { SettingDescription } from "../control" |
| 19 | +import { createSetting } from "../helper/builder" |
| 20 | +import { SettingItemGroup } from "../section" |
| 21 | + |
| 22 | +const { defineSettingItem, SettingBuilder } = createSetting( |
| 23 | + useGeneralSettingValue, |
| 24 | + setGeneralSetting, |
| 25 | +) |
| 26 | +export const SettingDataControl = () => { |
| 27 | + const { t } = useTranslation("settings") |
| 28 | + useEffect(() => { |
| 29 | + tipcClient?.getLoginItemSettings().then((settings) => { |
| 30 | + setGeneralSetting("appLaunchOnStartup", settings.openAtLogin) |
| 31 | + }) |
| 32 | + }, []) |
| 33 | + |
| 34 | + const { present } = useModalStack() |
| 35 | + |
| 36 | + return ( |
| 37 | + <div className="mt-4"> |
| 38 | + <SettingBuilder |
| 39 | + settings={[ |
| 40 | + { |
| 41 | + type: "title", |
| 42 | + value: t("general.privacy"), |
| 43 | + }, |
| 44 | + defineSettingItem("sendAnonymousData", { |
| 45 | + label: t("general.send_anonymous_data.label"), |
| 46 | + description: t("general.send_anonymous_data.description"), |
| 47 | + onChange(value) { |
| 48 | + setGeneralSetting("sendAnonymousData", value) |
| 49 | + if (value) { |
| 50 | + initAnalytics() |
| 51 | + } else { |
| 52 | + window.analytics?.reset() |
| 53 | + delete window.analytics |
| 54 | + } |
| 55 | + }, |
| 56 | + }), |
| 57 | + |
| 58 | + { |
| 59 | + type: "title", |
| 60 | + value: t("general.data"), |
| 61 | + }, |
| 62 | + defineSettingItem("dataPersist", { |
| 63 | + label: t("general.data_persist.label"), |
| 64 | + description: t("general.data_persist.description"), |
| 65 | + }), |
| 66 | + |
| 67 | + { |
| 68 | + label: t("general.rebuild_database.label"), |
| 69 | + action: () => { |
| 70 | + present({ |
| 71 | + title: t("general.rebuild_database.title"), |
| 72 | + clickOutsideToDismiss: true, |
| 73 | + content: () => ( |
| 74 | + <div className="text-sm"> |
| 75 | + <p>{t("general.rebuild_database.warning.line1")}</p> |
| 76 | + <p>{t("general.rebuild_database.warning.line2")}</p> |
| 77 | + <div className="mt-4 flex justify-end"> |
| 78 | + <Button |
| 79 | + className="bg-red-500 px-3 text-white" |
| 80 | + onClick={async () => { |
| 81 | + await clearLocalPersistStoreData() |
| 82 | + window.location.reload() |
| 83 | + }} |
| 84 | + > |
| 85 | + {t("ok", { ns: "common" })} |
| 86 | + </Button> |
| 87 | + </div> |
| 88 | + </div> |
| 89 | + ), |
| 90 | + }) |
| 91 | + }, |
| 92 | + description: t("general.rebuild_database.description"), |
| 93 | + buttonText: t("general.rebuild_database.button"), |
| 94 | + }, |
| 95 | + { |
| 96 | + label: t("general.export_database.label"), |
| 97 | + description: t("general.export_database.description"), |
| 98 | + buttonText: t("general.export_database.button"), |
| 99 | + action: () => { |
| 100 | + exportDB() |
| 101 | + }, |
| 102 | + }, |
| 103 | + { |
| 104 | + label: t("general.export.label"), |
| 105 | + description: t("general.export.description"), |
| 106 | + buttonText: t("general.export.button"), |
| 107 | + action: () => { |
| 108 | + const link = document.createElement("a") |
| 109 | + link.href = `${env.VITE_API_URL}/subscriptions/export` |
| 110 | + link.download = "follow.opml" |
| 111 | + link.click() |
| 112 | + }, |
| 113 | + }, |
| 114 | + |
| 115 | + { |
| 116 | + type: "title", |
| 117 | + value: t("general.cache"), |
| 118 | + }, |
| 119 | + AppCacheLimit, |
| 120 | + { |
| 121 | + label: t("data_control.clean_cache.button"), |
| 122 | + description: t("data_control.clean_cache.description"), |
| 123 | + buttonText: t("data_control.clean_cache.button"), |
| 124 | + action: async () => { |
| 125 | + await tipcClient?.clearCache() |
| 126 | + queryClient.invalidateQueries({ queryKey: ["app", "cache", "size"] }) |
| 127 | + }, |
| 128 | + }, |
| 129 | + ]} |
| 130 | + /> |
| 131 | + </div> |
| 132 | + ) |
| 133 | +} |
| 134 | +const AppCacheLimit = () => { |
| 135 | + const { t } = useTranslation("settings") |
| 136 | + const { data: cacheSize, isLoading: isLoadingCacheSize } = useQuery({ |
| 137 | + queryKey: ["app", "cache", "size"], |
| 138 | + queryFn: async () => { |
| 139 | + const byteSize = (await tipcClient?.getCacheSize()) ?? 0 |
| 140 | + return Math.round(byteSize / 1024 / 1024) |
| 141 | + }, |
| 142 | + }) |
| 143 | + const { |
| 144 | + data: cacheLimit, |
| 145 | + isLoading: isLoadingCacheLimit, |
| 146 | + refetch: refetchCacheLimit, |
| 147 | + } = useQuery({ |
| 148 | + queryKey: ["app", "cache", "limit"], |
| 149 | + queryFn: async () => { |
| 150 | + const size = (await tipcClient?.getCacheLimit()) ?? 0 |
| 151 | + return size |
| 152 | + }, |
| 153 | + }) |
| 154 | + |
| 155 | + const onChange = (value: number[]) => { |
| 156 | + tipcClient?.limitCacheSize(value[0]) |
| 157 | + refetchCacheLimit() |
| 158 | + } |
| 159 | + |
| 160 | + if (isLoadingCacheSize || isLoadingCacheLimit) return null |
| 161 | + |
| 162 | + const InfinitySymbol = <CarbonInfinitySymbol /> |
| 163 | + return ( |
| 164 | + <SettingItemGroup> |
| 165 | + <div className={"mb-3 flex items-center justify-between gap-4"}> |
| 166 | + <Label className="center flex"> |
| 167 | + {t("data_control.app_cache_limit.label")} |
| 168 | + |
| 169 | + <span className="center ml-2 flex shrink-0 gap-1 text-xs text-gray-500"> |
| 170 | + <span>({cacheSize}M</span> /{" "} |
| 171 | + <span className="center flex shrink-0"> |
| 172 | + {cacheLimit ? `${cacheLimit}M` : InfinitySymbol}) |
| 173 | + </span> |
| 174 | + </span> |
| 175 | + </Label> |
| 176 | + |
| 177 | + <div className="relative flex w-1/5 flex-col gap-1"> |
| 178 | + <Slider |
| 179 | + min={0} |
| 180 | + max={500} |
| 181 | + step={100} |
| 182 | + defaultValue={[cacheLimit ?? 0]} |
| 183 | + onValueCommit={onChange} |
| 184 | + /> |
| 185 | + <div className="absolute bottom-[-1.5em] text-base opacity-50">{InfinitySymbol}</div> |
| 186 | + <div className="absolute bottom-[-1.5em] right-0 text-xs opacity-50">500M</div> |
| 187 | + </div> |
| 188 | + </div> |
| 189 | + <SettingDescription>{t("data_control.app_cache_limit.description")}</SettingDescription> |
| 190 | + </SettingItemGroup> |
| 191 | + ) |
| 192 | +} |
0 commit comments