mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 00:40:14 +01:00
fix: validate the import preset
This commit is contained in:
parent
8c2d577e60
commit
6abe34ee3b
3 changed files with 32 additions and 24 deletions
|
|
@ -3,23 +3,24 @@ import { useSetRecoilState } from 'recoil';
|
||||||
import { FileUp } from 'lucide-react';
|
import { FileUp } from 'lucide-react';
|
||||||
import store from '~/store';
|
import store from '~/store';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
import cleanupPreset from '~/utils/cleanupPreset.js';
|
||||||
|
|
||||||
async function fetchPresets(callback) {
|
// async function fetchPresets(callback) {
|
||||||
try {
|
// try {
|
||||||
const response = await axios.get('/api/presets', {
|
// const response = await axios.get('/api/presets', {
|
||||||
timeout: 1000,
|
// timeout: 1000,
|
||||||
withCredentials: true
|
// withCredentials: true
|
||||||
});
|
// });
|
||||||
|
|
||||||
callback(response.data);
|
// callback(response.data);
|
||||||
} catch (error) {
|
// } catch (error) {
|
||||||
console.error(error);
|
// console.error(error);
|
||||||
console.log('[FileUpload] Error fetching presets');
|
// console.log('[FileUpload] Error fetching presets');
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
const FileUpload = ({ onFileSelected }) => {
|
const FileUpload = ({ onFileSelected }) => {
|
||||||
const setPresets = useSetRecoilState(store.presets);
|
// const setPresets = useSetRecoilState(store.presets);
|
||||||
|
|
||||||
const handleFileChange = event => {
|
const handleFileChange = event => {
|
||||||
const file = event.target.files[0];
|
const file = event.target.files[0];
|
||||||
|
|
@ -28,7 +29,7 @@ const FileUpload = ({ onFileSelected }) => {
|
||||||
const reader = new FileReader();
|
const reader = new FileReader();
|
||||||
reader.onload = e => {
|
reader.onload = e => {
|
||||||
const jsonData = JSON.parse(e.target.result);
|
const jsonData = JSON.parse(e.target.result);
|
||||||
onFileSelected(jsonData, () => fetchPresets(setPresets));
|
onFileSelected({ ...cleanupPreset(jsonData), presetId: null });
|
||||||
};
|
};
|
||||||
reader.readAsText(file);
|
reader.readAsText(file);
|
||||||
};
|
};
|
||||||
|
|
@ -42,6 +43,7 @@ const FileUpload = ({ onFileSelected }) => {
|
||||||
<span className="ml-1 flex text-xs ">Import</span>
|
<span className="ml-1 flex text-xs ">Import</span>
|
||||||
<input
|
<input
|
||||||
id="file-upload"
|
id="file-upload"
|
||||||
|
value=""
|
||||||
type="file"
|
type="file"
|
||||||
className="hidden "
|
className="hidden "
|
||||||
accept=".json"
|
accept=".json"
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,10 @@ export default function NewConversationMenu() {
|
||||||
setPresets(data);
|
setPresets(data);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const importPreset = jsonData => {
|
||||||
|
handleFileSelected(jsonData).then(setPresets);
|
||||||
|
};
|
||||||
|
|
||||||
// update the default model when availableModels changes
|
// update the default model when availableModels changes
|
||||||
// typically, availableModels changes => modelsFilter or customGPTModels changes
|
// typically, availableModels changes => modelsFilter or customGPTModels changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
@ -136,7 +140,7 @@ export default function NewConversationMenu() {
|
||||||
<DropdownMenuLabel className="flex items-center dark:text-gray-300">
|
<DropdownMenuLabel className="flex items-center dark:text-gray-300">
|
||||||
<span>Select a Preset</span>
|
<span>Select a Preset</span>
|
||||||
<div className="flex-1" />
|
<div className="flex-1" />
|
||||||
<FileUpload onFileSelected={handleFileSelected} />
|
<FileUpload onFileSelected={importPreset} />
|
||||||
<Dialog>
|
<Dialog>
|
||||||
<DialogTrigger asChild>
|
<DialogTrigger asChild>
|
||||||
<Button
|
<Button
|
||||||
|
|
|
||||||
|
|
@ -73,21 +73,23 @@ export function useManualSWR({ path, params, type, onSuccess }) {
|
||||||
return useSWRMutation(path, fetchFunction, options);
|
return useSWRMutation(path, fetchFunction, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const handleFileSelected = async (jsonData, callback) => {
|
export const handleFileSelected = async jsonData => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch('/api/presets', {
|
const response = await axios({
|
||||||
|
url: '/api/presets',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify(jsonData),
|
withCredentials: true,
|
||||||
|
data: JSON.stringify(jsonData)
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
// if (!response.ok) {
|
||||||
throw new Error(`Error: ${response.statusText}`);
|
// throw new Error(`Error: ${response.statusText}`);
|
||||||
}
|
// }
|
||||||
|
console.log(response);
|
||||||
callback();
|
return response.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error uploading the preset:', error);
|
console.error('Error uploading the preset:', error);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue