feat: rebuild database action

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2024-07-18 17:21:12 +08:00
parent b83388962b
commit e6d5d0c614
4 changed files with 82 additions and 25 deletions
@@ -1,3 +1,4 @@
import { StyledButton } from "@renderer/components/ui/button"
import { Checkbox } from "@renderer/components/ui/checkbox"
import { Label } from "@renderer/components/ui/label"
import { Switch } from "@renderer/components/ui/switch"
@@ -53,3 +54,18 @@ export const SettingDescription: Component = ({ children, className }) => (
{children}
</small>
)
export const SettingActionItem = ({
label,
action,
buttonText,
}: {
label: string
action: () => void
buttonText: string
}) => (
<div className={cn("mb-3 mt-4 flex items-center justify-between gap-4")}>
<div className="text-sm font-medium">{label}</div>
<StyledButton className="text-xs" onClick={action}>{buttonText}</StyledButton>
</div>
)
+11 -2
View File
@@ -1,3 +1,4 @@
/* eslint-disable @eslint-react/no-clone-element */
/* eslint-disable @eslint-react/no-children-to-array */
/* eslint-disable @eslint-react/no-children-map */
@@ -5,7 +6,11 @@ import type { FC, PropsWithChildren, ReactNode } from "react"
import { cloneElement } from "react"
import * as React from "react"
import { SettingDescription, SettingSwitch } from "./control"
import {
SettingActionItem,
SettingDescription,
SettingSwitch,
} from "./control"
export const SettingSectionTitle: FC<{
title: string
@@ -30,11 +35,15 @@ export const SettingItemGroup: FC<PropsWithChildren> = ({ children }) => {
switch (prevType) {
case SettingSwitch: {
// eslint-disable-next-line @eslint-react/no-clone-element
return cloneElement(child as React.ReactElement, {
className: "!-mt-2",
})
}
case SettingActionItem: {
return cloneElement(child as React.ReactElement, {
className: "!-mt-3",
})
}
default: {
return child
}
@@ -3,7 +3,11 @@ import type { FC, ReactNode } from "react"
import { isValidElement } from "react"
import * as React from "react"
import { SettingDescription, SettingSwitch } from "./control"
import {
SettingActionItem,
SettingDescription,
SettingSwitch,
} from "./control"
import { SettingItemGroup, SettingSectionTitle } from "./section"
type SharedSettingItem = {
@@ -17,11 +21,17 @@ type SettingItem<T, K extends keyof T = keyof T> = {
onChange: (value: T[K]) => void
} & SharedSettingItem
type SpecificSettingItem = {
type SectionSettingItem = {
type: "title"
value?: string
} & SharedSettingItem
type ActionSettingItem = {
label: string
action: () => void
description?: string
buttonText: string
} & SharedSettingItem
type CustomSettingItem = ReactNode | FC
export const createSettingBuilder =
@@ -29,8 +39,9 @@ export const createSettingBuilder =
<K extends keyof T>(props: {
settings: (
| SettingItem<T, K>
| SpecificSettingItem
| SectionSettingItem
| CustomSettingItem
| ActionSettingItem
| boolean
)[]
}) => {
@@ -41,8 +52,13 @@ export const createSettingBuilder =
.filter((i) => typeof i !== "boolean")
.map((setting, index) => {
if (isValidElement(setting)) return setting
if (typeof setting === "function") { return React.createElement(setting, { key: index }) }
const assertSetting = setting as SettingItem<T> | SpecificSettingItem
if (typeof setting === "function") {
return React.createElement(setting, { key: index })
}
const assertSetting = setting as
| SettingItem<T>
| SectionSettingItem
| ActionSettingItem
if (assertSetting.disabled) return null
@@ -60,25 +76,30 @@ export const createSettingBuilder =
}
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
if ("key" in assertSetting) {
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
}
}
} else if ("action" in assertSetting) {
ControlElement = <SettingActionItem {...assertSetting} key={index} />
}
return (
<SettingItemGroup key={index}>
@@ -4,6 +4,7 @@ import {
} from "@renderer/atoms/settings/general"
import { initPostHog } from "@renderer/initialize/posthog"
import { tipcClient } from "@renderer/lib/client"
import { clearLocalPersistStoreData } from "@renderer/store/utils/clear"
import { useCallback, useEffect } from "react"
import { createSettingBuilder } from "../setting-builder"
@@ -100,6 +101,16 @@ export const SettingGeneral = () => {
}
},
},
{
label: "Rebuild Database",
action: () => {
clearLocalPersistStoreData()
window.location.reload()
},
description:
"If you are experiencing data rendering anomalies, try rebuilding the database, this action will reload the page.",
buttonText: "Rebuild",
},
]}
/>