Skip to content

Commit

Permalink
maintain folder sort when app is re-opened
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarbansod authored and gatzjames committed Aug 1, 2024
1 parent 1b35e5b commit 5b6c899
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
30 changes: 30 additions & 0 deletions packages/insomnia/src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,36 @@ export const sortOrderName: Record<SortOrder, string> = {
[SORT_TYPE_ASC]: 'Requests First',
};

export type CollectionSortOrder =
| 'name-asc'
| 'name-desc'
| 'created-asc'
| 'created-desc'
| 'http-method'
| 'type-desc'
| 'type-asc'
| 'type-manual';
export const COLLECTION_SORT_ORDERS = [
SORT_TYPE_MANUAL,
SORT_NAME_ASC,
SORT_NAME_DESC,
SORT_CREATED_ASC,
SORT_CREATED_DESC,
SORT_HTTP_METHOD,
SORT_TYPE_DESC,
SORT_TYPE_ASC,
] as const;
export const collectionSortOrderName: Record<CollectionSortOrder, string> = {
[SORT_TYPE_MANUAL]: 'Manual',
[SORT_NAME_ASC]: 'Name Ascending (A-Z)',
[SORT_NAME_DESC]: 'Name Descending (Z-A)',
[SORT_CREATED_ASC]: 'Oldest First',
[SORT_CREATED_DESC]: 'Newest First',
[SORT_HTTP_METHOD]: 'HTTP Method',
[SORT_TYPE_DESC]: 'Folders First',
[SORT_TYPE_ASC]: 'Requests First',
};

export type DashboardSortOrder =
| 'name-asc'
| 'name-desc'
Expand Down
17 changes: 7 additions & 10 deletions packages/insomnia/src/ui/routes/debug.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ import {
useRouteLoaderData,
useSearchParams,
} from 'react-router-dom';
import { useLocalStorage } from 'react-use';

import { DEFAULT_SIDEBAR_SIZE, getProductName, SORT_ORDERS, type SortOrder, sortOrderName } from '../../common/constants';
import { type CollectionSortOrder, collectionSortOrderName, DEFAULT_SIDEBAR_SIZE, getProductName, SORT_NAME_ASC, SORT_ORDERS } from '../../common/constants';
import { type ChangeBufferEvent, database as db } from '../../common/database';
import { generateId, isNotNullOrUndefined } from '../../common/misc';
import { LandingPage } from '../../common/sentry';
import type { PlatformKeyCombinations } from '../../common/settings';
import { sortMethodMap } from '../../common/sorting';
import type { GrpcMethodInfo } from '../../main/ipc/grpc';
import * as models from '../../models';
import type { Environment } from '../../models/environment';
Expand Down Expand Up @@ -444,7 +446,7 @@ export const Debug: FC = () => {

const [searchParams, setSearchParams] = useSearchParams();

const sortOrder = searchParams.get('sortOrder') as SortOrder || 'type-manual';
const [sortOrder, setSortOrder] = useLocalStorage(`${projectId}:collection-sort-order`, SORT_NAME_ASC);
const { hotKeyRegistry } = settings;

const createRequest = ({ requestType, parentId, req }: { requestType: CreateRequestType; parentId: string; req?: Partial<Request> }) =>
Expand Down Expand Up @@ -680,7 +682,7 @@ export const Debug: FC = () => {

const toggleExpandAllFetcher = useFetcher();

const visibleCollection = collection.filter(item => !item.hidden);
const visibleCollection = collection.filter(item => !item.hidden).sort((a, b) => sortMethodMap[sortOrder as CollectionSortOrder](a.doc, b.doc));;

const parentRef = useRef<HTMLDivElement>(null);
const virtualizer = useVirtualizer<HTMLDivElement, Element>({
Expand Down Expand Up @@ -803,12 +805,7 @@ export const Debug: FC = () => {
aria-label="Sort order"
className="h-full aspect-square"
selectedKey={sortOrder}
onSelectionChange={order =>
setSearchParams({
...Object.fromEntries(searchParams.entries()),
sortOrder: order.toString(),
})
}
onSelectionChange={order => setSortOrder(order as CollectionSortOrder)}
>
<Button
aria-label="Select sort order"
Expand All @@ -821,7 +818,7 @@ export const Debug: FC = () => {
items={SORT_ORDERS.map(order => {
return {
id: order,
name: sortOrderName[order],
name: collectionSortOrderName[order],
};
})}
className="border select-none text-sm min-w-max border-solid border-[--hl-sm] shadow-lg bg-[--color-bg] py-2 rounded-md overflow-y-auto max-h-[85vh] focus:outline-none"
Expand Down

0 comments on commit 5b6c899

Please sign in to comment.