refactor: cleanup some components

This commit is contained in:
Prayag Prajapati committed 2024-12-09 10:15:20 +05:30
1 parent 5d5f127525
commit 9dbe11ce23
12 files changed
+71 -40

No files matched your search

+14 -7
View File
@@ -1,23 +1,30 @@
import { getSetting, setSetting } from "@/utils/storage/settings";
import { FormControlLabel, Switch, Typography } from "@mui/material";
import { useQuery } from "@tanstack/react-query";
import React, { useState } from "react";
const SettingOption = ({
setting,
}: { setting: { key: string; name: string; description: string } }) => {
const [settingVal, setSettingVal] = useState(
async () => (await getSetting(setting.key)) ?? false,
);
const initValue = useQuery({
queryKey: ["setting", setting.key],
queryFn: async () => {
const result = await getSetting(setting.key);
return Boolean(result);
},
});
const [settingVal, setSettingVal] = useState(initValue.data ?? false);
return (
<FormControlLabel
control={
<Switch
color="primary"
checked={settingVal}
onChange={(e) => {
setSetting(setting.key, e.target.checked);
setSettingVal(e.target.checked);
checked={Boolean(settingVal)}
onChange={(_, checked) => {
setSetting(setting.key, checked);
setSettingVal(Boolean(checked));
}}
disabled={initValue.isLoading}
name={setting.name}
/>
}