Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding sorting in documents, minor UI changes #1408

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
@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'

Check warning on line 437 in application/api/user/routes.py

View check run for this annotation

Codecov / codecov/patch

application/api/user/routes.py#L436-L437

Added lines #L436 - L437 were not covered by tests
data = [
{
"name": "default",
Expand All @@ -445,7 +447,7 @@
]

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):

Check warning on line 450 in application/api/user/routes.py

View check run for this annotation

Codecov / codecov/patch

application/api/user/routes.py#L450

Added line #L450 was not covered by tests
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