Files
DocsGPT/frontend/src/hooks/useDefaultDocument.ts
2025-08-16 15:19:19 +05:30

35 lines
937 B
TypeScript

import React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { Doc } from '../models/misc';
import { getDocs } from '../preferences/preferenceApi';
import {
selectSelectedDocs,
selectToken,
setSelectedDocs,
setSourceDocs,
} from '../preferences/preferenceSlice';
export default function useDefaultDocument() {
const dispatch = useDispatch();
const token = useSelector(selectToken);
const selectedDoc = useSelector(selectSelectedDocs);
const fetchDocs = () => {
getDocs(token).then((data) => {
dispatch(setSourceDocs(data));
if (!selectedDoc || (Array.isArray(selectedDoc) && selectedDoc.length === 0))
Array.isArray(data) &&
data?.forEach((doc: Doc) => {
if (doc.model && doc.name === 'default') {
dispatch(setSelectedDocs([doc]));
}
});
});
};
React.useEffect(() => {
fetchDocs();
}, []);
}