Skip to content

Commit

Permalink
Refactoring sorting & grouping (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
ransome1 committed Apr 2, 2024
1 parent 8ee6152 commit ac7c6f5
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 118 deletions.
2 changes: 1 addition & 1 deletion src/main/modules/IpcMain.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { changeCompleteState } from './ProcessDataRequest/ChangeCompleteState';
import { prepareContentForWriting, removeLineFromFile } from './File/Write';
import { archiveTodos, handleRequestArchive } from './File/Archive';
import { config, filter, notifiedTodoObjectsStorage } from '../config';
import { handleConfig } from '../util';
import { handleError } from '../util';
import { addFile, setFile, removeFile } from './File/File';
import { openFile, createFile } from './File/Dialog';
import { createTodoObject } from './ProcessDataRequest/CreateTodoObjects';
Expand Down
18 changes: 11 additions & 7 deletions src/main/modules/ProcessDataRequest/ProcessDataRequest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { applyAttributes, handleHiddenTodoObjects, handleCompletedTodoObjects, h
import { updateAttributes, attributes } from '../Attributes';
import { createTodoObjects } from './CreateTodoObjects';
import { mainWindow } from '../../main';
import { sortAndGroupTodoObjects, flattenTodoObjects } from './ProcessTodoObjects';
import { sortAndGroupTodoObjects } from './SortAndGroup';

let searchString: string;
let headers: HeadersObject = {
Expand Down Expand Up @@ -59,12 +59,16 @@ function processDataRequest(search?: string): RequestedData {

headers.visibleObjects = visibleTodoObjects.length;

if(fileSorting) {
todoObjects = flattenTodoObjects(todoObjects, '');
} else {
const sortedAndGroupedTodos: TodoObject[] = sortAndGroupTodoObjects(todoObjects, sorting);
todoObjects = flattenTodoObjects(sortedAndGroupedTodos, sorting[0].value);
}
console.log(todoObjects)

todoObjects = sortAndGroupTodoObjects(todoObjects, sorting);

// if(fileSorting) {
// todoObjects = flattenTodoObjects(todoObjects, '');
// } else {
// const sortedAndGroupedTodos: TodoObject[] = sortAndGroupTodoObjects(todoObjects, sorting);
// todoObjects = flattenTodoObjects(sortedAndGroupedTodos, sorting[0].value);
// }

const requestedData: RequestedData = {
todoObjects,
Expand Down
79 changes: 0 additions & 79 deletions src/main/modules/ProcessDataRequest/ProcessTodoObjects.tsx

This file was deleted.

45 changes: 45 additions & 0 deletions src/main/modules/ProcessDataRequest/SortAndGroup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function sortAndGroupTodoObjects(todoObjects: TodoObject[], sorting: Sorting[]): TodoObject[] {
function compareValues(a: any, b: any, invert: boolean): number {
const comparison = String(a).localeCompare(String(b), undefined, { sensitivity: 'base' });
return invert ? -comparison : comparison;
}

function applySorting(a: any, b: any): number {
for (const { value, invert } of sorting) {
const compareResult = compareValues(a[value], b[value], invert);
if (compareResult !== 0) {
return compareResult;
}
}
return 0;
}

function groupObjectsByKey(todoObjects, attributeKey) {
const grouped = {};
for (const todoObject of todoObjects) {
const groupKey = todoObject[attributeKey] || '';
if (!grouped[groupKey]) {
grouped[groupKey] = {
title: groupKey,
visible: false,
todoObjects: []
};
}
if(todoObject.visible) grouped[groupKey].visible = true;
grouped[groupKey].todoObjects.push(todoObject);
}
return Object.values(grouped);
}

function sortAndGroupObjects(objects: any[]): any {
const { value } = sorting[0];
const grouped = groupObjectsByKey(objects, value);
return grouped;
}

const sortedTodoObjects = Object.values(todoObjects).sort(applySorting);

return sortAndGroupObjects(sortedTodoObjects);
}

export { sortAndGroupTodoObjects };
2 changes: 1 addition & 1 deletion src/renderer/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ const App = () => {
: null }
</>
)}
{todoObjects && todoObjects.length > 0 && (
{todoObjects && (
<>
<GridComponent
todoObjects={todoObjects}
Expand Down
63 changes: 33 additions & 30 deletions src/renderer/Grid/Grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ const GridComponent: React.FC<GridComponentProps> = memo(({
}) => {

const list = document.getElementById('grid');
const groups: string[] = [];
const visibleTodoObjects = todoObjects?.filter(todoObject => todoObject.visible)?.slice(0, visibleRowCount);
//const groups: string[] = [];
//const visibleTodoObjects = todoObjects?.filter(todoObject => todoObject.visible)?.slice(0, visibleRowCount);
let rowCount = 0;

const totalRowCount = todoObjects?.length || 0;

const handleButtonClick = (key: string, name: string, values: string[]) => {
Expand Down Expand Up @@ -88,40 +90,41 @@ const GridComponent: React.FC<GridComponentProps> = memo(({
}
};

if(visibleTodoObjects.length === 0) return null;
//if(visibleTodoObjects.length === 0) return null;

return (
<List id="grid" onScroll={handleScroll} onKeyUp={handleKeyUp}>
{visibleTodoObjects?.map((row, index) => {
let renderGroup;
const groupValue = row[settings.sorting[0].value]?.toString() || null;
if(groups.length === 0 || !groups.includes(groupValue)) {
groups.push(groupValue);
renderGroup = true;
}
return (
<React.Fragment key={index}>
{!settings.fileSorting && renderGroup && (
<Group
value={groupValue}
todotxtAttribute={settings.sorting[0].value}
filters={filters}
onClick={handleButtonClick}
/>
)}
<Row
todoObject={row}
{todoObjects.map(group => (
group.visible && (
<React.Fragment key={group.title}>
<Group
value={group.title}
todotxtAttribute={settings.sorting[0].value}
filters={filters}
setTodoObject={setTodoObject}
setDialogOpen={setDialogOpen}
setContextMenu={setContextMenu}
setPromptItem={setPromptItem}
settings={settings}
handleButtonClick={handleButtonClick}
onClick={handleButtonClick}
/>
{group.todoObjects.map(todoObject => {
if (!todoObject.visible) {
return null;
}
rowCount++;
return (
<Row
key={todoObject.id}
todoObject={todoObject}
filters={filters}
setTodoObject={setTodoObject}
setDialogOpen={setDialogOpen}
setContextMenu={setContextMenu}
setPromptItem={setPromptItem}
settings={settings}
handleButtonClick={handleButtonClick}
/>
);
})}
</React.Fragment>
);
})}
)
))}
</List>
);

Expand Down

0 comments on commit ac7c6f5

Please sign in to comment.