feat: enhance general settings with compact timeline option
- Added a new setting to show timeline items in the subscription column while reading, improving user experience for content navigation. - Updated localization files to include descriptions and labels for the new setting in English, Japanese, Chinese (Simplified and Traditional). - Refactored the settings structure to utilize a new hook for enhanced settings management, streamlining the codebase. These changes provide users with more control over their reading layout, enhancing the overall functionality of the application. Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -1,18 +1,21 @@
|
||||
import { createSettingAtom } from "@follow/atoms/helper/setting.js"
|
||||
import type { SupportedLanguages } from "@follow/models"
|
||||
import { defaultGeneralSettings } from "@follow/shared/settings/defaults"
|
||||
import { enhancedGeneralSettingKeys } from "@follow/shared/settings/enhanced"
|
||||
import type { GeneralSettings } from "@follow/shared/settings/interface"
|
||||
import { useCallback, useMemo } from "react"
|
||||
import { hookEnhancedSettings as baseHookEnhancedSettings } from "@follow/shared/settings/hook"
|
||||
import type { GeneralSettings as BaseGeneralSettings } from "@follow/shared/settings/interface"
|
||||
|
||||
import { jotaiStore } from "~/lib/jotai"
|
||||
import { getDefaultLanguage } from "~/lib/language"
|
||||
|
||||
export const DEFAULT_ACTION_LANGUAGE = "default"
|
||||
|
||||
export interface GeneralSettings extends BaseGeneralSettings {
|
||||
showCompactTimelineInSub: boolean
|
||||
}
|
||||
export const createDefaultGeneralSettings = (): GeneralSettings => ({
|
||||
...defaultGeneralSettings,
|
||||
language: getDefaultLanguage(),
|
||||
showCompactTimelineInSub: false,
|
||||
})
|
||||
|
||||
const {
|
||||
@@ -27,35 +30,7 @@ const {
|
||||
|
||||
settingAtom: __generalSettingAtom,
|
||||
} = createSettingAtom("general", createDefaultGeneralSettings)
|
||||
|
||||
const [
|
||||
useGeneralSettingKey,
|
||||
useGeneralSettingSelector,
|
||||
useGeneralSettingKeys,
|
||||
getGeneralSettings,
|
||||
useGeneralSettingValue,
|
||||
] = hookEnhancedSettings(
|
||||
useGeneralSettingKeyInternal,
|
||||
useGeneralSettingSelectorInternal,
|
||||
useGeneralSettingKeysInternal,
|
||||
getGeneralSettingsInternal,
|
||||
useGeneralSettingValueInternal,
|
||||
|
||||
enhancedGeneralSettingKeys,
|
||||
defaultGeneralSettings,
|
||||
)
|
||||
export {
|
||||
__generalSettingAtom,
|
||||
clearGeneralSettings,
|
||||
getGeneralSettings,
|
||||
initializeDefaultGeneralSettings,
|
||||
setGeneralSetting,
|
||||
useGeneralSettingKey,
|
||||
useGeneralSettingKeys,
|
||||
useGeneralSettingSelector,
|
||||
useGeneralSettingValue,
|
||||
}
|
||||
export function hookEnhancedSettings<
|
||||
export const hookEnhancedSettings = <
|
||||
T1 extends (key: any) => any,
|
||||
T2 extends (selector: (s: any) => any) => any,
|
||||
T3 extends (keys: any) => any,
|
||||
@@ -70,108 +45,21 @@ export function hookEnhancedSettings<
|
||||
|
||||
enhancedSettingKeys: Set<string>,
|
||||
defaultSettings: Record<string, any>,
|
||||
): [T1, T2, T3, T4, T5] {
|
||||
const useNextSettingKey = (key: string) => {
|
||||
const enableEnhancedSettings = useGeneralSettingKeyInternal("enhancedSettings")
|
||||
const settingValue = useSettingKey(key)
|
||||
const shouldBackToDefault = enhancedSettingKeys.has(key) && !enableEnhancedSettings
|
||||
if (!shouldBackToDefault) {
|
||||
return settingValue
|
||||
}
|
||||
): [T1, T2, T3, T4, T5] => {
|
||||
return baseHookEnhancedSettings(
|
||||
useSettingKey,
|
||||
useSettingSelector,
|
||||
useSettingKeys,
|
||||
getSettings,
|
||||
useSettingValue,
|
||||
|
||||
return defaultSettings[key] === undefined ? settingValue : defaultSettings[key]
|
||||
}
|
||||
|
||||
const useNextSettingSelector = (selector: (s: any) => any) => {
|
||||
const enableEnhancedSettings = useGeneralSettingKeyInternal("enhancedSettings")
|
||||
return useSettingSelector(
|
||||
useCallback(
|
||||
(settings) => {
|
||||
if (enableEnhancedSettings) {
|
||||
return selector(settings)
|
||||
}
|
||||
|
||||
const enhancedSettings = { ...settings }
|
||||
for (const key of enhancedSettingKeys) {
|
||||
if (defaultSettings[key] !== undefined) {
|
||||
enhancedSettings[key] = defaultSettings[key]
|
||||
}
|
||||
}
|
||||
|
||||
return selector(enhancedSettings)
|
||||
},
|
||||
[enableEnhancedSettings, selector],
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
const useNextSettingKeys = (keys: string[]) => {
|
||||
const enableEnhancedSettings = useGeneralSettingKeyInternal("enhancedSettings")
|
||||
const rawSettingValues: string[] = useSettingKeys(keys)
|
||||
|
||||
return useMemo(() => {
|
||||
if (enableEnhancedSettings) {
|
||||
return rawSettingValues
|
||||
}
|
||||
|
||||
const result: string[] = []
|
||||
|
||||
for (const [i, key] of keys.entries()) {
|
||||
if (enhancedSettingKeys.has(key) && defaultSettings[key] !== undefined) {
|
||||
result.push(defaultSettings[key])
|
||||
} else if (rawSettingValues[i] !== undefined) {
|
||||
result.push(rawSettingValues[i])
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}, [enableEnhancedSettings, keys, rawSettingValues])
|
||||
}
|
||||
|
||||
const getNextSettings = () => {
|
||||
const settings = getSettings()
|
||||
const enableEnhancedSettings = jotaiStore.get(__generalSettingAtom).enhancedSettings
|
||||
|
||||
if (enableEnhancedSettings) {
|
||||
return settings
|
||||
}
|
||||
|
||||
const enhancedSettings = { ...settings }
|
||||
for (const key of enhancedSettingKeys) {
|
||||
if (defaultSettings[key] !== undefined) {
|
||||
enhancedSettings[key] = defaultSettings[key]
|
||||
}
|
||||
}
|
||||
|
||||
return enhancedSettings
|
||||
}
|
||||
|
||||
const useNextSettingValue = () => {
|
||||
const settingValues = useSettingValue()
|
||||
const enableEnhancedSettings = useGeneralSettingKeyInternal("enhancedSettings")
|
||||
|
||||
return useMemo(() => {
|
||||
if (enableEnhancedSettings) {
|
||||
return settingValues
|
||||
}
|
||||
|
||||
const result = { ...settingValues }
|
||||
for (const key of enhancedSettingKeys) {
|
||||
if (defaultSettings[key] !== undefined) {
|
||||
result[key] = defaultSettings[key]
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}, [enableEnhancedSettings, settingValues])
|
||||
}
|
||||
return [
|
||||
useNextSettingKey as T1,
|
||||
useNextSettingSelector as T2,
|
||||
useNextSettingKeys as T3,
|
||||
getNextSettings as T4,
|
||||
useNextSettingValue as T5,
|
||||
]
|
||||
enhancedSettingKeys,
|
||||
defaultSettings,
|
||||
{
|
||||
useEnhancedEnabled: () => useGeneralSettingKeyInternal("enhancedSettings"),
|
||||
getEnhancedEnabled: () => jotaiStore.get(__generalSettingAtom).enhancedSettings,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
export function useActionLanguage() {
|
||||
@@ -201,3 +89,37 @@ export const generalServerSyncWhiteListKeys: (keyof GeneralSettings)[] = [
|
||||
"language",
|
||||
"voice",
|
||||
]
|
||||
|
||||
export const enhancedGeneralSettingKeys = new Set<keyof GeneralSettings>([
|
||||
"groupByDate",
|
||||
"autoExpandLongSocialMedia",
|
||||
"showCompactTimelineInSub",
|
||||
])
|
||||
|
||||
const [
|
||||
useGeneralSettingKey,
|
||||
useGeneralSettingSelector,
|
||||
useGeneralSettingKeys,
|
||||
getGeneralSettings,
|
||||
useGeneralSettingValue,
|
||||
] = hookEnhancedSettings(
|
||||
useGeneralSettingKeyInternal,
|
||||
useGeneralSettingSelectorInternal,
|
||||
useGeneralSettingKeysInternal,
|
||||
getGeneralSettingsInternal,
|
||||
useGeneralSettingValueInternal,
|
||||
|
||||
enhancedGeneralSettingKeys,
|
||||
defaultGeneralSettings,
|
||||
)
|
||||
export {
|
||||
__generalSettingAtom,
|
||||
clearGeneralSettings,
|
||||
getGeneralSettings,
|
||||
initializeDefaultGeneralSettings,
|
||||
setGeneralSetting,
|
||||
useGeneralSettingKey,
|
||||
useGeneralSettingKeys,
|
||||
useGeneralSettingSelector,
|
||||
useGeneralSettingValue,
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { createSettingAtom } from "@follow/atoms/helper/setting.js"
|
||||
import { defaultUISettings } from "@follow/shared/settings/defaults"
|
||||
import { enhancedUISettingKeys } from "@follow/shared/settings/enhanced"
|
||||
import type { UISettings } from "@follow/shared/settings/interface"
|
||||
|
||||
import { getDefaultLanguage } from "~/lib/language"
|
||||
@@ -29,6 +28,26 @@ const {
|
||||
settingAtom: __uiSettingAtom,
|
||||
} = createSettingAtom("ui", createDefaultUISettings)
|
||||
|
||||
export const uiServerSyncWhiteListKeys: (keyof UISettings)[] = [
|
||||
"uiFontFamily",
|
||||
"readerFontFamily",
|
||||
"opaqueSidebar",
|
||||
"accentColor",
|
||||
// "customCSS",
|
||||
]
|
||||
|
||||
export const enhancedUISettingKeys = new Set<keyof UISettings>([
|
||||
"hideExtraBadge",
|
||||
"codeHighlightThemeLight",
|
||||
"codeHighlightThemeDark",
|
||||
"dateFormat",
|
||||
"readerRenderInlineStyle",
|
||||
"modalOverlay",
|
||||
"reduceMotion",
|
||||
"usePointerCursor",
|
||||
"opaqueSidebar",
|
||||
])
|
||||
|
||||
const [useUISettingKey, useUISettingSelector, useUISettingKeys, getUISettings, useUISettingValue] =
|
||||
hookEnhancedSettings(
|
||||
useUISettingKeyInternal,
|
||||
@@ -51,11 +70,3 @@ export {
|
||||
useUISettingSelector,
|
||||
useUISettingValue,
|
||||
}
|
||||
|
||||
export const uiServerSyncWhiteListKeys: (keyof UISettings)[] = [
|
||||
"uiFontFamily",
|
||||
"readerFontFamily",
|
||||
"opaqueSidebar",
|
||||
"accentColor",
|
||||
// "customCSS",
|
||||
]
|
||||
|
||||
@@ -86,7 +86,7 @@ if (import.meta.hot) {
|
||||
console.info("reload", lang, nsName)
|
||||
await i18next.reloadResources(lang, nsName)
|
||||
|
||||
import.meta.env.DEV && EventBus.dispatch("I18N_UPDATE", "")
|
||||
EventBus.dispatch("I18N_UPDATE", "")
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
+4
-1
@@ -8,6 +8,7 @@ import { memo, startTransition, useCallback, useEffect, useMemo, useRef } from "
|
||||
import { useResizable } from "react-resizable-layout"
|
||||
|
||||
import { AIChatPanelStyle, useAIChatPanelStyle, useAIPanelVisibility } from "~/atoms/settings/ai"
|
||||
import { useGeneralSettingKey } from "~/atoms/settings/general"
|
||||
import { getUISettings, setUISetting } from "~/atoms/settings/ui"
|
||||
import { setSubscriptionColumnApronNode, useSubscriptionEntryPlaneVisible } from "~/atoms/sidebar"
|
||||
import { m } from "~/components/common/Motion"
|
||||
@@ -114,6 +115,8 @@ const AIEnhancedTimelineLayoutImpl = () => {
|
||||
// Trigger layout update
|
||||
window.dispatchEvent(new Event("resize"))
|
||||
}, [resolvePreferredWidth])
|
||||
|
||||
const showCompactTimelineColumn = useGeneralSettingKey("showCompactTimelineInSub")
|
||||
return (
|
||||
<div className="relative flex min-w-0 grow">
|
||||
<div className={cn("h-full flex-1", aiPanelStyle === AIChatPanelStyle.Fixed && "border-r")}>
|
||||
@@ -182,7 +185,7 @@ const AIEnhancedTimelineLayoutImpl = () => {
|
||||
|
||||
{/* Floating panel - renders outside layout flow */}
|
||||
{aiPanelStyle === AIChatPanelStyle.Floating && <AIChatLayout key="ai-chat-layout" />}
|
||||
<SubscriptionColumnToggler />
|
||||
{showCompactTimelineColumn && <SubscriptionColumnToggler />}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
+4
-4
@@ -1,8 +1,8 @@
|
||||
import { defaultSettings } from "@follow/shared/settings/defaults"
|
||||
import { enhancedSettingKeys } from "@follow/shared/settings/enhanced"
|
||||
import { useCallback } from "react"
|
||||
|
||||
import { useGeneralSettingKey } from "~/atoms/settings/general"
|
||||
import { enhancedGeneralSettingKeys, useGeneralSettingKey } from "~/atoms/settings/general"
|
||||
import { enhancedUISettingKeys } from "~/atoms/settings/ui"
|
||||
|
||||
export enum WrapEnhancedSettingTab {
|
||||
General,
|
||||
@@ -10,8 +10,8 @@ export enum WrapEnhancedSettingTab {
|
||||
}
|
||||
|
||||
const enhancedSettingMapper: Record<WrapEnhancedSettingTab, Set<keyof any>> = {
|
||||
[WrapEnhancedSettingTab.General]: enhancedSettingKeys.general,
|
||||
[WrapEnhancedSettingTab.Appearance]: enhancedSettingKeys.ui,
|
||||
[WrapEnhancedSettingTab.General]: enhancedGeneralSettingKeys,
|
||||
[WrapEnhancedSettingTab.Appearance]: enhancedUISettingKeys,
|
||||
}
|
||||
const defaultSettingMapper: Record<WrapEnhancedSettingTab, Record<keyof any, any>> = {
|
||||
[WrapEnhancedSettingTab.General]: defaultSettings.general,
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { useMobile } from "@follow/components/hooks/useMobile.js"
|
||||
import { ResponsiveSelect } from "@follow/components/ui/select/responsive.js"
|
||||
import { useTypeScriptHappyCallback } from "@follow/hooks"
|
||||
import { ACTION_LANGUAGE_MAP } from "@follow/shared"
|
||||
@@ -22,6 +21,7 @@ import {
|
||||
useGeneralSettingValue,
|
||||
} from "~/atoms/settings/general"
|
||||
import { useDialog } from "~/components/ui/modal/stacked/hooks"
|
||||
import { useFeature } from "~/hooks/biz/useFeature"
|
||||
import { useProxyValue, useSetProxy } from "~/hooks/biz/useProxySetting"
|
||||
import { useMinimizeToTrayValue, useSetMinimizeToTray } from "~/hooks/biz/useTraySetting"
|
||||
import { fallbackLanguage } from "~/i18n"
|
||||
@@ -65,11 +65,10 @@ export const SettingGeneral = () => {
|
||||
WrapEnhancedSettingTab.General,
|
||||
)
|
||||
|
||||
const isMobile = useMobile()
|
||||
|
||||
const { ask } = useDialog()
|
||||
const reRenderKey = useGeneralSettingKey("enhancedSettings")
|
||||
|
||||
const isAiLayout = useFeature("ai")
|
||||
return (
|
||||
<div className="mt-4">
|
||||
<SettingBuilder
|
||||
@@ -88,7 +87,7 @@ export const SettingGeneral = () => {
|
||||
},
|
||||
}),
|
||||
IN_ELECTRON && MinimizeToTraySetting,
|
||||
isMobile && StartupScreenSelector,
|
||||
StartupScreenSelector,
|
||||
LanguageSelector,
|
||||
|
||||
{
|
||||
@@ -143,10 +142,10 @@ export const SettingGeneral = () => {
|
||||
label: t("general.dim_read.label"),
|
||||
description: t("general.dim_read.description"),
|
||||
}),
|
||||
isMobile &&
|
||||
defineSettingItem("showQuickTimeline", {
|
||||
label: t("general.show_quick_timeline.label"),
|
||||
description: t("general.show_quick_timeline.description"),
|
||||
isAiLayout &&
|
||||
defineSettingItem("showCompactTimelineInSub", {
|
||||
label: t("general.show_compact_timeline_in_sub.label"),
|
||||
description: t("general.show_compact_timeline_in_sub.description"),
|
||||
}),
|
||||
|
||||
{ type: "title", value: t("general.mark_as_read.title") },
|
||||
@@ -155,11 +154,11 @@ export const SettingGeneral = () => {
|
||||
label: t("general.mark_as_read.scroll.label"),
|
||||
description: t("general.mark_as_read.scroll.description"),
|
||||
}),
|
||||
!isMobile &&
|
||||
defineSettingItem("hoverMarkUnread", {
|
||||
label: t("general.mark_as_read.hover.label"),
|
||||
description: t("general.mark_as_read.hover.description"),
|
||||
}),
|
||||
|
||||
defineSettingItem("hoverMarkUnread", {
|
||||
label: t("general.mark_as_read.hover.label"),
|
||||
description: t("general.mark_as_read.hover.description"),
|
||||
}),
|
||||
defineSettingItem("renderMarkUnread", {
|
||||
label: t("general.mark_as_read.render.label"),
|
||||
description: t("general.mark_as_read.render.description"),
|
||||
@@ -253,8 +252,6 @@ export const LanguageSelector = ({
|
||||
|
||||
const [loadingLanguageLockMap] = useAtom(langLoadingLockMapAtom)
|
||||
|
||||
const isMobile = useMobile()
|
||||
|
||||
return (
|
||||
<div className={cn("mb-3 mt-4 flex w-full items-center", containerClassName)}>
|
||||
<div className="flex grow flex-col gap-1">
|
||||
@@ -281,9 +278,6 @@ export const LanguageSelector = ({
|
||||
|
||||
const originalLanguageName = defaultResources[lang].lang.name
|
||||
|
||||
if (isMobile) {
|
||||
return `${originalLanguageName} (${percent}%)`
|
||||
}
|
||||
return (
|
||||
<span className="group" key={lang}>
|
||||
<span>
|
||||
|
||||
@@ -306,6 +306,8 @@
|
||||
"general.rebuild_database.warning.line2": "Are you sure you want to continue?",
|
||||
"general.send_anonymous_data.description": "By opting to send anonymized telemetry data, you contribute to improving the overall user experience of Folo.",
|
||||
"general.send_anonymous_data.label": "Send anonymous data",
|
||||
"general.show_compact_timeline_in_sub.description": "When viewing entry content, show timeline items in the left subscription column for quick selection.",
|
||||
"general.show_compact_timeline_in_sub.label": "Show timeline in subscription column while reading",
|
||||
"general.show_quick_timeline.description": "Show the quick timeline at the top of the feed list.",
|
||||
"general.show_quick_timeline.label": "Show feed list timeline",
|
||||
"general.show_unread_on_launch.description": "Automatically filter to unread content when the app starts",
|
||||
|
||||
@@ -306,6 +306,8 @@
|
||||
"general.rebuild_database.warning.line2": "本当に続行しますか?",
|
||||
"general.send_anonymous_data.description": "匿名化されたテレメトリーデータを送信することにより、Folo の全体的なユーザーエクスペリエンスの向上に貢献します。",
|
||||
"general.send_anonymous_data.label": "匿名データを送信",
|
||||
"general.show_compact_timeline_in_sub.description": "記事コンテンツを閲覧中、左側の購読カラムにタイムラインの項目を表示して素早く選択できるようにします。",
|
||||
"general.show_compact_timeline_in_sub.label": "読書中に購読カラムへタイムラインを表示",
|
||||
"general.show_quick_timeline.description": "フィードリストの最上部にクイックタイムラインを表示する",
|
||||
"general.show_quick_timeline.label": "フィードリストにタイムラインを表示する",
|
||||
"general.show_unread_on_launch.description": "アプリ起動時に未読コンテンツに自動的にフィルタリングします",
|
||||
|
||||
@@ -306,6 +306,8 @@
|
||||
"general.rebuild_database.warning.line2": "确定继续?",
|
||||
"general.send_anonymous_data.description": "发送匿名用户数据,帮助产品改进和功能迭代。",
|
||||
"general.send_anonymous_data.label": "发送匿名数据",
|
||||
"general.show_compact_timeline_in_sub.description": "进入文章内容页时,在左侧订阅栏显示时间线条目以便快速选择。",
|
||||
"general.show_compact_timeline_in_sub.label": "阅读时在订阅栏显示时间线",
|
||||
"general.show_quick_timeline.description": "在时间线顶部显示列表和分类的时间线快递导航。",
|
||||
"general.show_quick_timeline.label": "显示时间线快速导航",
|
||||
"general.show_unread_on_launch.description": "应用启动时仅显示未读内容。",
|
||||
|
||||
@@ -271,6 +271,8 @@
|
||||
"general.rebuild_database.warning.line2": "您確定要繼續嗎?",
|
||||
"general.send_anonymous_data.description": "選擇傳送匿名使用資料,您將幫助改善 Folo 的整體使用體驗。",
|
||||
"general.send_anonymous_data.label": "傳送匿名資料",
|
||||
"general.show_compact_timeline_in_sub.description": "進入文章內容頁時,在左側訂閱欄顯示時間軸項目以便快速選擇。",
|
||||
"general.show_compact_timeline_in_sub.label": "閱讀時在訂閱欄顯示時間軸",
|
||||
"general.show_quick_timeline.description": "在 RSS 摘要列表頂部顯示快速時間軸。",
|
||||
"general.show_quick_timeline.label": "顯示 RSS 摘要列表時間軸",
|
||||
"general.sidebar_title": "一般",
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
import type { GeneralSettings, UISettings } from "./interface"
|
||||
|
||||
export const enhancedGeneralSettingKeys = new Set<keyof GeneralSettings>([
|
||||
"groupByDate",
|
||||
"autoExpandLongSocialMedia",
|
||||
])
|
||||
export const enhancedUISettingKeys = new Set<keyof UISettings>([
|
||||
"hideExtraBadge",
|
||||
"codeHighlightThemeLight",
|
||||
"codeHighlightThemeDark",
|
||||
"dateFormat",
|
||||
"readerRenderInlineStyle",
|
||||
"modalOverlay",
|
||||
"reduceMotion",
|
||||
"usePointerCursor",
|
||||
"opaqueSidebar",
|
||||
])
|
||||
|
||||
export const enhancedSettingKeys = {
|
||||
general: enhancedGeneralSettingKeys,
|
||||
ui: enhancedUISettingKeys,
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
import { useCallback, useMemo } from "react"
|
||||
|
||||
// Generic enhanced settings hook composer shared across platforms
|
||||
export function hookEnhancedSettings<
|
||||
TUseKey extends (key: any) => any,
|
||||
TUseSelector extends (selector: (s: any) => any) => any,
|
||||
TUseKeys extends (keys: any) => any,
|
||||
TGetSettings extends () => any,
|
||||
TUseValue extends () => any,
|
||||
>(
|
||||
useSettingKey: TUseKey,
|
||||
useSettingSelector: TUseSelector,
|
||||
useSettingKeys: TUseKeys,
|
||||
getSettings: TGetSettings,
|
||||
useSettingValue: TUseValue,
|
||||
|
||||
enhancedSettingKeys: Set<string>,
|
||||
defaultSettings: Record<string, any>,
|
||||
options: {
|
||||
// Provide whether enhanced settings are enabled (reactive version)
|
||||
useEnhancedEnabled: () => boolean
|
||||
// Provide whether enhanced settings are enabled (sync read version)
|
||||
getEnhancedEnabled: () => boolean
|
||||
},
|
||||
) {
|
||||
const { useEnhancedEnabled, getEnhancedEnabled } = options
|
||||
|
||||
const useNextSettingKey = (key: string) => {
|
||||
const enableEnhancedSettings = useEnhancedEnabled()
|
||||
const settingValue = useSettingKey(key)
|
||||
const shouldBackToDefault = enhancedSettingKeys.has(key) && !enableEnhancedSettings
|
||||
if (!shouldBackToDefault) {
|
||||
return settingValue
|
||||
}
|
||||
|
||||
return defaultSettings[key] === undefined ? settingValue : defaultSettings[key]
|
||||
}
|
||||
|
||||
const useNextSettingSelector = (selector: (s: any) => any) => {
|
||||
const enableEnhancedSettings = useEnhancedEnabled()
|
||||
return useSettingSelector(
|
||||
useCallback(
|
||||
(settings) => {
|
||||
if (enableEnhancedSettings) {
|
||||
return selector(settings)
|
||||
}
|
||||
|
||||
const enhancedSettings = { ...settings }
|
||||
for (const key of enhancedSettingKeys) {
|
||||
if (defaultSettings[key] !== undefined) {
|
||||
enhancedSettings[key] = defaultSettings[key]
|
||||
}
|
||||
}
|
||||
|
||||
return selector(enhancedSettings)
|
||||
},
|
||||
[enableEnhancedSettings, selector],
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
const useNextSettingKeys = (keys: string[]) => {
|
||||
const enableEnhancedSettings = useEnhancedEnabled()
|
||||
const rawSettingValues: string[] = useSettingKeys(keys)
|
||||
|
||||
return useMemo(() => {
|
||||
if (enableEnhancedSettings) {
|
||||
return rawSettingValues
|
||||
}
|
||||
|
||||
const result: string[] = []
|
||||
|
||||
for (const [i, key] of keys.entries()) {
|
||||
if (enhancedSettingKeys.has(key) && defaultSettings[key] !== undefined) {
|
||||
result.push(defaultSettings[key])
|
||||
} else if (rawSettingValues[i] !== undefined) {
|
||||
result.push(rawSettingValues[i])
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}, [enableEnhancedSettings, keys, rawSettingValues])
|
||||
}
|
||||
|
||||
const getNextSettings = () => {
|
||||
const settings = getSettings()
|
||||
const enableEnhancedSettings = getEnhancedEnabled()
|
||||
|
||||
if (enableEnhancedSettings) {
|
||||
return settings
|
||||
}
|
||||
|
||||
const enhancedSettings = { ...settings }
|
||||
for (const key of enhancedSettingKeys) {
|
||||
if (defaultSettings[key] !== undefined) {
|
||||
enhancedSettings[key] = defaultSettings[key]
|
||||
}
|
||||
}
|
||||
|
||||
return enhancedSettings
|
||||
}
|
||||
|
||||
const useNextSettingValue = () => {
|
||||
const settingValues = useSettingValue()
|
||||
const enableEnhancedSettings = useEnhancedEnabled()
|
||||
|
||||
return useMemo(() => {
|
||||
if (enableEnhancedSettings) {
|
||||
return settingValues
|
||||
}
|
||||
|
||||
const result = { ...settingValues }
|
||||
for (const key of enhancedSettingKeys) {
|
||||
if (defaultSettings[key] !== undefined) {
|
||||
result[key] = defaultSettings[key]
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}, [enableEnhancedSettings, settingValues])
|
||||
}
|
||||
|
||||
return [
|
||||
useNextSettingKey as TUseKey,
|
||||
useNextSettingSelector as TUseSelector,
|
||||
useNextSettingKeys as TUseKeys,
|
||||
getNextSettings as TGetSettings,
|
||||
useNextSettingValue as TUseValue,
|
||||
] as [TUseKey, TUseSelector, TUseKeys, TGetSettings, TUseValue]
|
||||
}
|
||||
Reference in New Issue
Block a user