mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-01-01 16:18:51 +01:00
- Added `useUpdateSearchParams` hook to manage URL search parameters dynamically. - Updated `Landing`, `NewChat`, and various parameter components to utilize the new hook for better state management. - Enhanced styling for dark mode support in chat components. - Refactored click handlers in `NewChat` to create chat search parameters for navigation. - Improved accessibility and usability in `DynamicCombobox`, `DynamicInput`, `DynamicSlider`, `DynamicSwitch`, and `DynamicTags` components. - Introduced `createChatSearchParams` utility to streamline query parameter creation for chat sessions. remove icon borders in dark mode
102 lines
3.1 KiB
TypeScript
102 lines
3.1 KiB
TypeScript
import { useState, useMemo } from 'react';
|
|
import { OptionTypes } from 'librechat-data-provider';
|
|
import type { DynamicSettingProps } from 'librechat-data-provider';
|
|
import { Label, Switch, HoverCard, HoverCardTrigger } from '~/components/ui';
|
|
import { TranslationKeys, useLocalize, useParameterEffects, useUpdateSearchParams } from '~/hooks';
|
|
import { useChatContext } from '~/Providers';
|
|
import OptionHover from './OptionHover';
|
|
import { ESide } from '~/common';
|
|
|
|
function DynamicSwitch({
|
|
label = '',
|
|
settingKey,
|
|
defaultValue,
|
|
description = '',
|
|
columnSpan,
|
|
setOption,
|
|
optionType,
|
|
readonly = false,
|
|
showDefault = false,
|
|
labelCode = false,
|
|
descriptionCode = false,
|
|
conversation,
|
|
}: DynamicSettingProps) {
|
|
const localize = useLocalize();
|
|
const { preset } = useChatContext();
|
|
const updateSearchParams = useUpdateSearchParams();
|
|
const [inputValue, setInputValue] = useState<boolean>(!!(defaultValue as boolean | undefined));
|
|
useParameterEffects({
|
|
preset,
|
|
settingKey,
|
|
defaultValue,
|
|
conversation,
|
|
inputValue,
|
|
setInputValue,
|
|
preventDelayedUpdate: true,
|
|
});
|
|
|
|
const selectedValue = useMemo(() => {
|
|
if (optionType === OptionTypes.Custom) {
|
|
// TODO: custom logic, add to payload but not to conversation
|
|
return inputValue;
|
|
}
|
|
|
|
return conversation?.[settingKey] ?? defaultValue;
|
|
}, [conversation, defaultValue, optionType, settingKey, inputValue]);
|
|
|
|
const handleCheckedChange = (checked: boolean) => {
|
|
if (optionType === OptionTypes.Custom) {
|
|
// TODO: custom logic, add to payload but not to conversation
|
|
setInputValue(checked);
|
|
updateSearchParams({ [settingKey]: checked.toString() });
|
|
return;
|
|
}
|
|
setOption(settingKey)(checked);
|
|
};
|
|
|
|
return (
|
|
<div
|
|
className={`flex flex-col items-center justify-start gap-6 ${
|
|
columnSpan != null ? `col-span-${columnSpan}` : 'col-span-full'
|
|
}`}
|
|
>
|
|
<HoverCard openDelay={300}>
|
|
<HoverCardTrigger className="grid w-full items-center gap-2">
|
|
<div className="flex justify-between">
|
|
<Label
|
|
htmlFor={`${settingKey}-dynamic-switch`}
|
|
className="text-left text-sm font-medium"
|
|
>
|
|
{labelCode ? (localize(label as TranslationKeys) ?? label) : label || settingKey}{' '}
|
|
{showDefault && (
|
|
<small className="opacity-40">
|
|
({localize('com_endpoint_default')}:{' '}
|
|
{defaultValue != null ? 'com_ui_on' : 'com_ui_off'})
|
|
</small>
|
|
)}
|
|
</Label>
|
|
</div>
|
|
<Switch
|
|
id={`${settingKey}-dynamic-switch`}
|
|
checked={selectedValue}
|
|
onCheckedChange={handleCheckedChange}
|
|
disabled={readonly}
|
|
className="flex"
|
|
/>
|
|
</HoverCardTrigger>
|
|
{description && (
|
|
<OptionHover
|
|
description={
|
|
descriptionCode
|
|
? (localize(description as TranslationKeys) ?? description)
|
|
: description
|
|
}
|
|
side={ESide.Left}
|
|
/>
|
|
)}
|
|
</HoverCard>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export default DynamicSwitch;
|