Skip to content

Commit

Permalink
Merge pull request #1408 from ManishMadan2882/main
Browse files Browse the repository at this point in the history
Adding sorting in documents, minor UI changes
  • Loading branch information
dartpain authored Nov 5, 2024
2 parents dbfc1bb + 0425190 commit 89529f4
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 11 deletions.
4 changes: 3 additions & 1 deletion application/api/user/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ class CombinedJson(Resource):
@api.doc(description="Provide JSON file with combined available indexes")
def get(self):
user = "local"
sort_field = request.args.get('sort', 'date') # Default to 'date'
sort_order = request.args.get('order', "desc") # Default to 'desc'
data = [
{
"name": "default",
Expand All @@ -445,7 +447,7 @@ def get(self):
]

try:
for index in sources_collection.find({"user": user}).sort("date", -1):
for index in sources_collection.find({"user": user}).sort(sort_field, 1 if sort_order=="asc" else -1):
data.append(
{
"id": str(index["_id"]),
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export default function Navigation({ navOpen, setNavOpen }: NavigationProps) {
ref={navRef}
className={`${
!navOpen && '-ml-96 md:-ml-[18rem]'
} duration-20 fixed top-0 z-40 flex h-full w-72 flex-col border-r-[1px] border-b-0 bg-white transition-all dark:border-r-purple-taupe dark:bg-chinese-black dark:text-white`}
} duration-20 fixed top-0 z-20 flex h-full w-72 flex-col border-r-[1px] border-b-0 bg-white transition-all dark:border-r-purple-taupe dark:bg-chinese-black dark:text-white`}
>
<div
className={'visible mt-2 flex h-[6vh] w-full justify-between md:h-12'}
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/api/services/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import apiClient from '../client';
import endpoints from '../endpoints';

const userService = {
getDocs: (): Promise<any> => apiClient.get(endpoints.USER.DOCS),
getDocs: (sort = 'date', order = 'desc'): Promise<any> =>
apiClient.get(`${endpoints.USER.DOCS}?sort=${sort}&order=${order}`),
checkDocs: (data: any): Promise<any> =>
apiClient.post(endpoints.USER.DOCS_CHECK, data),
getAPIKeys: (): Promise<any> => apiClient.get(endpoints.USER.API_KEYS),
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/conversation/ConversationBubble.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ function AllSources(sources: AllSourcesProps) {
></img>
) : null}
</span>
<p className="mt-3 max-h-24 overflow-y-auto break-words rounded-md text-left text-xs text-black dark:text-chinese-silver">
<p className="mt-3 max-h-16 overflow-y-auto break-words rounded-md text-left text-xs text-black dark:text-chinese-silver">
{source.text}
</p>
</div>
Expand Down
7 changes: 5 additions & 2 deletions frontend/src/preferences/preferenceApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import userService from '../api/services/userService';
import { Doc } from '../models/misc';

//Fetches all JSON objects from the source. We only use the objects with the "model" property in SelectDocsModal.tsx. Hopefully can clean up the source file later.
export async function getDocs(): Promise<Doc[] | null> {
export async function getDocs(
sort = 'date',
order = 'desc',
): Promise<Doc[] | null> {
try {
const response = await userService.getDocs();
const response = await userService.getDocs(sort, order);
const data = await response.json();

const docs: Doc[] = [];
Expand Down
35 changes: 30 additions & 5 deletions frontend/src/settings/Documents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,30 @@ const Documents: React.FC<DocumentsProps> = ({
const [modalState, setModalState] = useState<ActiveState>('INACTIVE'); // Initialize with inactive state
const [isOnboarding, setIsOnboarding] = useState(false); // State for onboarding flag
const [loading, setLoading] = useState(false);

const [sortField, setSortField] = useState<'date' | 'tokens'>('date');
const [sortOrder, setSortOrder] = useState<'asc' | 'desc'>('desc');
const syncOptions = [
{ label: 'Never', value: 'never' },
{ label: 'Daily', value: 'daily' },
{ label: 'Weekly', value: 'weekly' },
{ label: 'Monthly', value: 'monthly' },
];

const refreshDocs = (field: 'date' | 'tokens') => {
if (field === sortField) {
setSortOrder(sortOrder === 'asc' ? 'desc' : 'asc');
} else {
setSortOrder('desc');
setSortField(field);
}
getDocs(sortField, sortOrder)
.then((data) => {
dispatch(setSourceDocs(data));
})
.catch((error) => console.error(error))
.finally(() => {
setLoading(false);
});
};
const handleManageSync = (doc: Doc, sync_frequency: string) => {
setLoading(true);
userService
Expand Down Expand Up @@ -110,19 +126,28 @@ const Documents: React.FC<DocumentsProps> = ({
<th>
<div className="flex justify-center items-center">
{t('settings.documents.date')}
<img src={caretSort} alt="" />
<img
className="cursor-pointer"
onClick={() => refreshDocs('date')}
src={caretSort}
alt="sort"
/>
</div>
</th>
<th>
<div className="flex justify-center items-center">
{t('settings.documents.tokenUsage')}
<img src={caretSort} alt="" />
<img
className="cursor-pointer"
onClick={() => refreshDocs('tokens')}
src={caretSort}
alt="sort"
/>
</div>
</th>
<th>
<div className="flex justify-center items-center">
{t('settings.documents.type')}
<img src={caretSort} alt="" />
</div>
</th>
<th></th>
Expand Down

0 comments on commit 89529f4

Please sign in to comment.