Files
coco-app/src/components/Settings/Advanced/index.tsx

341 lines
10 KiB
TypeScript
Raw Normal View History

import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import {
AppWindowMac,
ArrowUpWideNarrow,
MessageSquareMore,
PanelTopClose,
Search,
ShieldCheck,
Unplug,
} from "lucide-react";
import { useMount } from "ahooks";
feat: add selection toolbar window for mac (#980) * feat: add selection window page * fix: chat input * feat: add selection page * chore: add * chore: test * feat: add * feat: add store * feat: add selection settings * chore: remove unused code * docs: add release note * docs: add release note * chore: format code * chore: format code * fix: copy error * disable hashbrown default feature * Enable unstable feature allocator_api To make coco-app compile in CI: ``` --> /home/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/hashbrown-0.15.5/src/raw/mod.rs:3856:12 | 3856 | impl<T, A: Allocator> RawIntoIter<T, A> { | ^^^^^^^^^ | = note: see issue #32838 <https://github.com/rust-lang/rust/issues/32838> for more information = help: add `#![feature(allocator_api)]` to the crate attributes to enable = note: this compiler was built on 2025-06-25; consider upgrading it if it is out of date ``` I don't know why it does not compile, feature `allocator-api2` is enabled for `hashbrown 0.15.5`, so technically [1] it should not use the allocator APIs from the std. According to [2], enabling the `nightly` feature of `allocator-api2` may cause this issue as well, but it is not enabled in our case either. Anyway, enabling `#![feature(allocator_api)]` should make it work. [1]: https://github.com/rust-lang/hashbrown/blob/b751eef8e99ccf3652046ef4a9e1ec47c1bfb78d/src/raw/alloc.rs#L26-L47 [2]: https://github.com/rust-lang/hashbrown/issues/564 * put it in main.rs * format main.rs * Enable default-features for hashbrown 0.15.5 * format main.rs * enable feature allocator-api2 * feat: add selection set config * fix: selection setting * fix: ci error * fix: ci error * fix: ci error * fix: ci error * merge: merge main * fix: rust code warn * fix: rust code error * fix: rust code error * fix: selection settings * style: selection styles * style: selection styles --------- Co-authored-by: Steve Lau <stevelauc@outlook.com>
2025-11-27 10:12:49 +08:00
import { isNil } from "lodash-es";
import Shortcuts from "./components/Shortcuts";
import SettingsItem from "../SettingsItem";
import { useStartupStore } from "@/stores/startupStore";
import { useConnectStore } from "@/stores/connectStore";
import Appearance from "./components/Appearance";
2025-07-02 07:03:09 +08:00
import SettingsInput from "@/components//Settings/SettingsInput";
import platformAdapter from "@/utils/platformAdapter";
import UpdateSettings from "./components/UpdateSettings";
import SettingsToggle from "../SettingsToggle";
2025-12-19 16:15:03 +08:00
// import SelectionSettings from "./components/Selection";
// import { isMac } from "@/utils/platform";
2025-12-18 10:26:13 +08:00
import {
Select,
SelectTrigger,
SelectContent,
SelectItem,
SelectValue,
} from "@/components/ui/select";
const Advanced = () => {
const { t } = useTranslation();
const defaultStartupWindow = useStartupStore((state) => {
return state.defaultStartupWindow;
});
const setDefaultStartupWindow = useStartupStore((state) => {
return state.setDefaultStartupWindow;
});
const defaultContentForSearchWindow = useStartupStore((state) => {
return state.defaultContentForSearchWindow;
});
const setDefaultContentForSearchWindow = useStartupStore((state) => {
return state.setDefaultContentForSearchWindow;
});
const defaultContentForChatWindow = useStartupStore((state) => {
return state.defaultContentForChatWindow;
});
const setDefaultContentForChatWindow = useStartupStore((state) => {
return state.setDefaultContentForChatWindow;
});
const connectionTimeout = useConnectStore((state) => {
return state.connectionTimeout;
});
const setConnectionTimeout = useConnectStore((state) => {
return state.setConnectionTimeout;
});
const queryTimeout = useConnectStore((state) => {
return state.querySourceTimeout;
});
const setQueryTimeout = useConnectStore((state) => {
return state.setQuerySourceTimeout;
});
const allowSelfSignature = useConnectStore((state) => {
return state.allowSelfSignature;
});
const setAllowSelfSignature = useConnectStore((state) => {
return state.setAllowSelfSignature;
});
const {
searchDelay,
setSearchDelay,
compactModeAutoCollapseDelay,
setCompactModeAutoCollapseDelay,
} = useConnectStore();
const [localSearchResultWeight, setLocalSearchResultWeight] = useState(1);
useMount(async () => {
const allowSelfSignature = await platformAdapter.invokeBackend<boolean>(
"get_allow_self_signature"
);
setAllowSelfSignature(allowSelfSignature);
const weight = await platformAdapter.invokeBackend<number>(
"get_local_query_source_weight"
);
setLocalSearchResultWeight(weight);
});
useEffect(() => {
const unsubscribeStartup = useStartupStore.subscribe((state) => {
platformAdapter.emitEvent("change-startup-store", state);
});
return () => {
unsubscribeStartup();
};
}, []);
const startupList = [
{
icon: AppWindowMac,
title: "settings.advanced.startup.defaultStartupWindow.title",
description: "settings.advanced.startup.defaultStartupWindow.description",
value: defaultStartupWindow,
items: [
{
label:
"settings.advanced.startup.defaultStartupWindow.select.searchMode",
value: "searchMode",
},
{
label:
"settings.advanced.startup.defaultStartupWindow.select.chatMode",
value: "chatMode",
},
],
onChange: setDefaultStartupWindow,
},
{
icon: Search,
title: "settings.advanced.startup.defaultContentForSearchWindow.title",
description:
"settings.advanced.startup.defaultContentForSearchWindow.description",
value: defaultContentForSearchWindow,
items: [
{
label:
"settings.advanced.startup.defaultContentForSearchWindow.select.systemDefault",
value: "systemDefault",
},
],
onChange: setDefaultContentForSearchWindow,
},
{
icon: MessageSquareMore,
title: "settings.advanced.startup.defaultContentForChatWindow.title",
description:
"settings.advanced.startup.defaultContentForChatWindow.description",
value: defaultContentForChatWindow,
items: [
{
label:
"settings.advanced.startup.defaultContentForChatWindow.select.newChat",
value: "newChat",
},
{
label:
"settings.advanced.startup.defaultContentForChatWindow.select.oldChat",
value: "oldChat",
},
],
onChange: setDefaultContentForChatWindow,
},
];
return (
<div className="space-y-8">
<h2 className="text-lg font-semibold text-gray-900 dark:text-white mb-4">
{t("settings.advanced.startup.title")}
</h2>
<div className="space-y-6">
{startupList.map((item) => {
const { icon, title, description, value, items, onChange } = item;
return (
<SettingsItem
key={title}
icon={icon}
title={t(title)}
description={t(description)}
>
2025-12-18 10:26:13 +08:00
<Select value={value as string} onValueChange={(v) => onChange(v as never)}>
<SelectTrigger className="h-8 w-44">
<SelectValue className="truncate" />
</SelectTrigger>
<SelectContent>
{items.map((item) => {
const { label, value } = item;
return (
<SelectItem key={value} value={value as string}>
{t(label)}
</SelectItem>
);
})}
</SelectContent>
</Select>
</SettingsItem>
);
})}
</div>
2025-12-19 16:15:03 +08:00
{/* {isMac && <SelectionSettings />} */}
feat: add selection toolbar window for mac (#980) * feat: add selection window page * fix: chat input * feat: add selection page * chore: add * chore: test * feat: add * feat: add store * feat: add selection settings * chore: remove unused code * docs: add release note * docs: add release note * chore: format code * chore: format code * fix: copy error * disable hashbrown default feature * Enable unstable feature allocator_api To make coco-app compile in CI: ``` --> /home/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/hashbrown-0.15.5/src/raw/mod.rs:3856:12 | 3856 | impl<T, A: Allocator> RawIntoIter<T, A> { | ^^^^^^^^^ | = note: see issue #32838 <https://github.com/rust-lang/rust/issues/32838> for more information = help: add `#![feature(allocator_api)]` to the crate attributes to enable = note: this compiler was built on 2025-06-25; consider upgrading it if it is out of date ``` I don't know why it does not compile, feature `allocator-api2` is enabled for `hashbrown 0.15.5`, so technically [1] it should not use the allocator APIs from the std. According to [2], enabling the `nightly` feature of `allocator-api2` may cause this issue as well, but it is not enabled in our case either. Anyway, enabling `#![feature(allocator_api)]` should make it work. [1]: https://github.com/rust-lang/hashbrown/blob/b751eef8e99ccf3652046ef4a9e1ec47c1bfb78d/src/raw/alloc.rs#L26-L47 [2]: https://github.com/rust-lang/hashbrown/issues/564 * put it in main.rs * format main.rs * Enable default-features for hashbrown 0.15.5 * format main.rs * enable feature allocator-api2 * feat: add selection set config * fix: selection setting * fix: ci error * fix: ci error * fix: ci error * fix: ci error * merge: merge main * fix: rust code warn * fix: rust code error * fix: rust code error * fix: selection settings * style: selection styles * style: selection styles --------- Co-authored-by: Steve Lau <stevelauc@outlook.com>
2025-11-27 10:12:49 +08:00
<Shortcuts />
<Appearance />
<UpdateSettings />
<h2 className="text-lg font-semibold text-gray-900 dark:text-white mb-4">
{t("settings.advanced.other.title")}
</h2>
<div className="space-y-6">
<SettingsItem
icon={Unplug}
title={t("settings.advanced.other.connectionTimeout.title")}
description={t(
"settings.advanced.other.connectionTimeout.description"
)}
>
<SettingsInput
type="number"
min={10}
value={connectionTimeout}
onChange={(value) => {
setConnectionTimeout(!value ? void 0 : Number(value));
}}
/>
</SettingsItem>
<SettingsItem
icon={Unplug}
title={t("settings.advanced.other.queryTimeout.title")}
description={t("settings.advanced.other.queryTimeout.description")}
>
<SettingsInput
type="number"
min={1}
value={queryTimeout}
onChange={(value) => {
setQueryTimeout(!value ? void 0 : Number(value));
}}
/>
</SettingsItem>
<SettingsItem
icon={Unplug}
title={t("settings.advanced.other.searchDelay.title")}
description={t("settings.advanced.other.searchDelay.description")}
>
<SettingsInput
type="number"
min={0}
value={searchDelay}
onChange={(value) => {
setSearchDelay(isNil(value) ? 0 : Number(value));
}}
/>
</SettingsItem>
<SettingsItem
icon={ShieldCheck}
title={t("settings.advanced.other.allowSelfSignature.title")}
description={t(
"settings.advanced.other.allowSelfSignature.description"
)}
>
<SettingsToggle
label={t("settings.advanced.other.allowSelfSignature.title")}
checked={allowSelfSignature}
onChange={(value) => {
setAllowSelfSignature(value);
platformAdapter.invokeBackend("set_allow_self_signature", {
value,
});
}}
/>
</SettingsItem>
<SettingsItem
icon={ArrowUpWideNarrow}
title={t("settings.advanced.other.localSearchResultWeight.title")}
description={t(
"settings.advanced.other.localSearchResultWeight.description"
)}
>
2025-12-18 10:26:13 +08:00
<Select
value={String(localSearchResultWeight)}
onValueChange={(v) => {
const weight = Number(v);
setLocalSearchResultWeight(weight);
platformAdapter.invokeBackend("set_local_query_source_weight", {
value: weight,
});
}}
>
2025-12-18 10:26:13 +08:00
<SelectTrigger className="h-8 w-44">
<SelectValue className="truncate" />
</SelectTrigger>
<SelectContent>
<SelectItem value="0.5">
{t("settings.advanced.other.localSearchResultWeight.options.low")}
</SelectItem>
<SelectItem value="1">
{t(
"settings.advanced.other.localSearchResultWeight.options.medium"
)}
</SelectItem>
<SelectItem value="2">
{t(
"settings.advanced.other.localSearchResultWeight.options.high"
)}
</SelectItem>
</SelectContent>
</Select>
</SettingsItem>
<SettingsItem
icon={PanelTopClose}
title={t(
"settings.advanced.other.compactModeAutoCollapseDelay.title"
)}
description={t(
"settings.advanced.other.compactModeAutoCollapseDelay.description"
)}
>
<SettingsInput
type="number"
min={0}
value={compactModeAutoCollapseDelay}
onChange={(value) => {
setCompactModeAutoCollapseDelay(!value ? 0 : Number(value));
}}
/>
</SettingsItem>
</div>
</div>
);
};
export default Advanced;