refactor: define settings

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2024-07-10 21:07:32 +08:00
parent 3b0fc2bdf3
commit e54ac42480
10 changed files with 285 additions and 133 deletions
+36 -26
View File
@@ -172,7 +172,11 @@ export const router = {
),
setMacOSBadge: t.procedure.input<number>().action(async ({ input }) => {
if (app.dock) {
app.dock.setBadge(input.toString())
if (input === 0) {
app.dock.setBadge("")
} else {
app.dock.setBadge(input.toString())
}
} else {
app.setBadgeCount(input)
}
@@ -185,41 +189,47 @@ export const router = {
if (result.canceled) return
// return result.filePath;
await downloadFile(input, result.filePath)
.catch((err) => {
const mainWindow = getMainWindow()
if (!mainWindow) return
callGlobalContextMethod(mainWindow, "toast.error", ["Download failed!"])
throw err
})
await downloadFile(input, result.filePath).catch((err) => {
const mainWindow = getMainWindow()
if (!mainWindow) return
callGlobalContextMethod(mainWindow, "toast.error", ["Download failed!"])
throw err
})
const mainWindow = getMainWindow()
if (!mainWindow) return
callGlobalContextMethod(mainWindow, "toast.success", ["Download success!"])
}),
windowAction: t.procedure.input<{ action: "close" | "minimize" | "maximum" }>().action(async ({ input, context }) => {
if (context.sender.getType() === "window") {
const window: BrowserWindow | null = (context.sender as Sender).getOwnerBrowserWindow()
windowAction: t.procedure
.input<{ action: "close" | "minimize" | "maximum" }>()
.action(async ({ input, context }) => {
if (context.sender.getType() === "window") {
const window: BrowserWindow | null = (
context.sender as Sender
).getOwnerBrowserWindow()
if (!window) return
switch (input.action) {
case "close": { window.close(); break
}
case "minimize": { window.minimize(); break
}
case "maximum": {
if (window.isMaximized()) {
window.unmaximize()
} else {
window.maximize()
if (!window) return
switch (input.action) {
case "close": {
window.close()
break
}
case "minimize": {
window.minimize()
break
}
case "maximum": {
if (window.isMaximized()) {
window.unmaximize()
} else {
window.maximize()
}
break
}
break
}
}
}
}),
}),
quitAndInstall: t.procedure.action(async () => {
autoUpdater.quitAndInstall()
}),
@@ -3,6 +3,8 @@ import { jotaiStore } from "@renderer/lib/jotai"
import { createSettingAtom } from "./helper"
const createDefaultSettings = () => ({
// App
appLaunchOnStartup: false,
// Data control
dataPersist: true,
sendAnonymousData: true,
@@ -10,7 +10,7 @@ import { SettingDescription, SettingSwitch } from "./control"
export const SettingSectionTitle: FC<{
title: string
}> = ({ title }) => (
<div className="mb-4 mt-6 text-sm font-medium capitalize text-gray-400 dark:text-neutral-500">
<div className="mb-4 mt-8 text-sm font-medium capitalize text-gray-400 first:mt-0 dark:text-neutral-500">
{title}
</div>
)
@@ -0,0 +1,81 @@
import type { FC, ReactNode } from "react"
import { isValidElement } from "react"
import * as React from "react"
import { SettingDescription, SettingSwitch } from "./control"
import { SettingItemGroup, SettingSectionTitle } from "./section"
type SharedSettingItem = {
disabled?: boolean
}
type SettingItem<T, K extends keyof T = keyof T> = {
key: K
label: string
description?: string
onChange: (value: T[K]) => void
} & SharedSettingItem
type SpecificSettingItem = {
type: "title"
value?: string
} & SharedSettingItem
type CustomSettingItem = ReactNode | FC
export const createSettingBuilder =
<T extends object>(useSetting: () => T) =>
<K extends keyof T>(props: {
settings: (SettingItem<T, K> | SpecificSettingItem | CustomSettingItem)[]
}) => {
const { settings } = props
const settingObject = useSetting()
return settings.map((setting, index) => {
if (isValidElement(setting)) return setting
if (typeof setting === "function") return React.createElement(setting)
const assertSetting = setting as SettingItem<T> | SpecificSettingItem
if (
"type" in assertSetting &&
assertSetting.type === "title" &&
assertSetting.value
) {
return <SettingSectionTitle key={index} title={assertSetting.value} />
}
if ("type" in assertSetting) {
return null
}
if (assertSetting.disabled) return null
let ControlElement: React.ReactNode
switch (typeof settingObject[assertSetting.key]) {
case "boolean": {
ControlElement = (
<SettingSwitch
className="mt-4"
checked={settingObject[assertSetting.key] as boolean}
onCheckedChange={(checked) =>
assertSetting.onChange(checked as T[keyof T])}
label={assertSetting.label}
/>
)
break
}
case "string": {
return null
}
default: {
return null
}
}
return (
<SettingItemGroup key={index}>
{ControlElement}
{!!assertSetting.description && (
<SettingDescription>{assertSetting.description}</SettingDescription>
)}
</SettingItemGroup>
)
})
}
@@ -18,108 +18,93 @@ import { useQuery } from "@tanstack/react-query"
import { useCallback } from "react"
import { bundledThemes } from "shiki/themes"
import { SettingDescription, SettingSwitch } from "../control"
import { SettingItemGroup, SettingSectionTitle } from "../section"
import { SettingSwitch } from "../control"
import { createSettingBuilder } from "../setting-builder"
import { SettingsTitle } from "../title"
const SettingBuilder = createSettingBuilder(useUISettingValue)
export const SettingAppearance = () => {
const { isDark, toggleDark } = useDark()
const saveDarkSetting = useCallback(() => {
toggleDark()
}, [])
const state = useUISettingValue()
const onlyMacos = window.electron && getOS() === "macOS"
return (
<div>
<>
<SettingsTitle />
<SettingSectionTitle title="General" />
<SettingSwitch
label="Dark Mode"
checked={isDark}
onCheckedChange={saveDarkSetting}
/>
{onlyMacos && (
<SettingSwitch
label="Opaque Sidebars"
checked={state.opaqueSidebar}
onCheckedChange={(checked) => {
setUISetting("opaqueSidebar", checked)
}}
<div className="mt-4">
<SettingBuilder
settings={[
{
type: "title",
value: "General",
},
<SettingSwitch
key="darkMode"
label="Dark Mode"
checked={isDark}
onCheckedChange={saveDarkSetting}
/>,
{
label: "Opaque Sidebars",
key: "opaqueSidebar",
onChange: (value) => setUISetting("opaqueSidebar", value),
},
{
type: "title",
value: "UI",
},
TextSize,
{
type: "title",
value: "Display counts",
},
{
disabled:
window.electron && !["macOS", "Linux"].includes(getOS()),
label: "Dock Badge",
key: "showDockBadge",
onChange: (value) => setUISetting("showDockBadge", value),
},
{
label: "Show sidebar unread count",
key: "sidebarShowUnreadCount",
onChange: (value) =>
setUISetting("sidebarShowUnreadCount", value),
},
{
type: "title",
value: "Content",
},
!!window.electron && Fonts,
ShikiTheme,
{
label: "Render inline style",
key: "readerRenderInlineStyle",
onChange: (value) =>
setUISetting("readerRenderInlineStyle", value),
},
{
type: "title",
value: "Misc",
},
{
label: "Show modal overlay",
key: "modalOverlay",
onChange: (value) => setUISetting("modalOverlay", value),
},
{
label: "Reduce motion",
key: "reduceMotion",
onChange: (value) => setUISetting("reduceMotion", value),
description: `Enabling this option will reduce the motion of the element to improve performance and device life, and if it is disabled, it will adapt to the system settings.`,
},
]}
/>
)}
<SettingSectionTitle title="UI" />
<TextSize />
<SettingSectionTitle title="Display counts" />
{onlyMacos && (
<SettingSwitch
label="Dock Badge"
checked={state.showDockBadge}
onCheckedChange={(c) => {
setUISetting("showDockBadge", c)
}}
/>
)}
<SettingSwitch
label="Show sidebar unread count"
checked={state.sidebarShowUnreadCount}
onCheckedChange={(c) => {
setUISetting("sidebarShowUnreadCount", c)
}}
/>
{/* <SettingSwitch
label="Modal draggable"
checked={state.modalDraggable}
onCheckedChange={(c) => {
setUISetting("modalDraggable", c)
}}
/> */}
{/* <SettingSwitch
label="Modal opaque"
checked={state.modalOpaque}
onCheckedChange={(c) => {
setUISetting("modalOpaque", c)
}}
/> */}
<SettingSectionTitle title="Content" />
{window.electron && <Fonts />}
<ShikiTheme />
<SettingSwitch
label="Render inline style"
checked={state.readerRenderInlineStyle}
onCheckedChange={(c) => {
setUISetting("readerRenderInlineStyle", c)
}}
/>
<SettingSectionTitle title="Misc" />
<SettingSwitch
label="Show modal overlay"
checked={state.modalOverlay}
onCheckedChange={(c) => {
setUISetting("modalOverlay", c)
}}
/>
<SettingItemGroup>
<SettingSwitch
label="Reduce motion"
checked={state.reduceMotion}
onCheckedChange={(c) => {
setUISetting("reduceMotion", c)
}}
/>
<SettingDescription>
Enabling this option will reduce the motion of the element to improve
performance and device life, and if it is disabled, it will adapt to the
system settings.
</SettingDescription>
</SettingItemGroup>
</div>
</div>
</>
)
}
const ShikiTheme = () => {
@@ -4,32 +4,106 @@ import {
} from "@renderer/atoms/settings/general"
import { initPostHog } from "@renderer/initialize/posthog"
import { tipcClient } from "@renderer/lib/client"
import { useCallback, useEffect, useState } from "react"
import { useCallback, useEffect } from "react"
import { SettingDescription, SettingSwitch } from "../control"
import { SettingItemGroup, SettingSectionTitle } from "../section"
import { createSettingBuilder } from "../setting-builder"
import { SettingsTitle } from "../title"
const SettingBuilder = createSettingBuilder(useGeneralSettingValue)
export const SettingGeneral = () => {
const [loginSetting, setLoginSetting] = useState(false)
useEffect(() => {
tipcClient?.getLoginItemSettings().then((settings) => {
setLoginSetting(settings.openAtLogin)
setGeneralSetting("appLaunchOnStartup", settings.openAtLogin)
})
}, [])
const saveLoginSetting = useCallback((checked: boolean) => {
tipcClient?.setLoginItemSettings(checked)
setLoginSetting(checked)
}, [])
const settings = useGeneralSettingValue()
setGeneralSetting("appLaunchOnStartup", checked)
}, [])
return (
<>
<SettingsTitle />
<div className="mt-6">
{window.electron && (
<div className="mt-4">
<SettingBuilder
settings={[
{
type: "title",
value: "App",
disabled: !window.electron,
},
{
disabled: !window.electron,
label: "Launch Follow at Login",
key: "appLaunchOnStartup",
onChange(value) {
saveLoginSetting(value)
},
},
{
type: "title",
value: "view",
},
{
key: "unreadOnly",
label: "Show unread content initially",
description:
"Only show unread content initially when you open the app",
onChange: (value) => setGeneralSetting("unreadOnly", value),
},
{
key: "scrollMarkUnread",
label: "Mark as read when scrolling",
description:
"Automatic marking of feed entries as read when the item is scrolled up out of the viewport.",
onChange: (value) => setGeneralSetting("scrollMarkUnread", value),
},
{
key: "hoverMarkUnread",
label: "Mark as read when hovering",
description:
"Automatic marking of feed entries as read when the item is hovered.",
onChange: (value) => setGeneralSetting("hoverMarkUnread", value),
},
{
key: "renderMarkUnread",
label: "Mark as read when in the viewport",
description:
"Automatically mark feed entries with only one level of content(e.g. Social Media, Picture, Video views) as read when the item is in the viewport.",
onChange: (value) => setGeneralSetting("renderMarkUnread", value),
},
{
type: "title",
value: "Data control",
},
{
key: "dataPersist",
label: "Persist data to offline usage",
description:
"Data will be stored locally on your device for offline usage and speed up the data loading of the first screen. If you disable this, all local data will be removed.",
onChange: (value) => setGeneralSetting("dataPersist", value),
},
{
key: "sendAnonymousData",
label: "Send anonymous data",
description:
"By selecting to send telemetry data, you can help us improve the overall user experience of Follow",
onChange: (value) => {
setGeneralSetting("sendAnonymousData", value)
if (value) {
initPostHog()
} else {
window.posthog?.reset()
delete window.posthog
}
},
},
]}
/>
{/* {window.electron && (
<SettingSwitch
label="Launch Follow at Login"
checked={loginSetting}
@@ -127,7 +201,7 @@ export const SettingGeneral = () => {
{" "}
{APP_NAME}
</SettingDescription>
</SettingItemGroup>
</SettingItemGroup> */}
</div>
</>
)
@@ -3,7 +3,7 @@ import { MyWalletSection } from "./my-wallet-section"
import { TransactionsSection } from "./transaction-section"
export const SettingWallet = () => (
<div>
<div className="mt-4">
<SettingsTitle />
<MyWalletSection />
@@ -91,7 +91,7 @@ export function Component() {
return (
<>
<SettingsTitle />
<div className="space-y-4">
<div className="mt-4 space-y-4">
{actionsData.map((action, actionIdx) => (
<ActionCard
key={actionIdx}
@@ -66,7 +66,7 @@ export function Component() {
<>
<SettingsTitle />
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
<form onSubmit={form.handleSubmit(onSubmit)} className="mt-4 space-y-4">
<FormField
control={form.control}
name="handle"
@@ -16,7 +16,7 @@ export function Component() {
return (
<>
<SettingsTitle />
<div className="space-y-6">
<div className="mt-4 space-y-6">
{Object.keys(shortcuts).map((type) => (
<section key={type}>
<div className="mb-2 text-sm font-medium capitalize">{type}</div>