Skip to content

Commit

Permalink
worked on the search feature
Browse files Browse the repository at this point in the history
  • Loading branch information
stan-dot committed Oct 5, 2023
1 parent b8f05c8 commit 8a5489c
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 6 deletions.
10 changes: 10 additions & 0 deletions app/src/features/filter/Filterer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { BookmarkNode } from "../../lib/typesFacade";

export default class Filterer {
static byRegex(nodes: BookmarkNode[], regex: RegExp): BookmarkNode[] {
return [];
}
static dateAddedBefore(nodes: BookmarkNode[], cutoff: Date): BookmarkNode[] {
return [];
}
}
123 changes: 123 additions & 0 deletions app/src/features/search/SearchResultsTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import DataEditor, {
CellClickedEventArgs,
CompactSelection,
GridSelection,
Item,
} from "@glideapps/glide-data-grid";
import React from "react";
import { BookmarkNode } from "../../lib/typesFacade";
import { isAFolder } from "../../utils/ifHasChildrenFolders";
import {
ContextMenuActionTypes,
useContextMenuDispatch,
} from "../context-menu/ContextMenuContext";
import { usePathDispatch } from "../path/PathContext";
import { columns, getData } from "../table/columns";
import {
decideContextType,
getNodesFromTableSelection,
runDoubleClickSideEffects,
} from "./utils/getNodesFromTableSelection";

// todo use a reducer for the whole thing probably
interface FilterSearch {
regex?: RegExp;
page?: string;
substringInName?: string;
onlyFolder: boolean;
onlyBookmark: boolean;
// todo this with a slider interface between 3 things
}

export function SearchResultsTable(
props: {
rows: BookmarkNode[];
},
): JSX.Element {
const pathDispatch = usePathDispatch();
const contextMenuDispatch = useContextMenuDispatch();
const [selection, setSelection] = React.useState<GridSelection>({
columns: CompactSelection.empty(),
rows: CompactSelection.empty(),
});

// CLICK HANDLING
const doubleClickHandler = (cell: Item) => {
const selectedBookmarks = getNodesFromTableSelection(
props.rows,
selection,
cell,
);
if (selectedBookmarks.length === 0) return;
const b = selectedBookmarks[0]; // always double click only on one thing
const isFolder = isAFolder(b);

runDoubleClickSideEffects(
cell[0],
contextMenuDispatch,
isFolder,
b,
pathDispatch,
);
};

const contextMenuHandler = (cell: Item, event: CellClickedEventArgs) => {
event.preventDefault();
const selectedBookmarks = getNodesFromTableSelection(
props.rows,
selection,
cell,
);
if (selectedBookmarks.length === 0) return;
const type: ContextMenuActionTypes = decideContextType(selectedBookmarks);
contextMenuDispatch({
type: type,
direction: "open",
position: [event.localEventX, event.localEventY],
things: selectedBookmarks,
});
};

return (
<div
className="table-container flex flex-grow pb-4 mb-40 "
id="search-results-table-container"
onDragOver={(e) => {
e.preventDefault();
e.dataTransfer.dropEffect = "move";
}}
>
<div id="filter" className="m-2 p-2 rounded flex flex-row">
<button id="filterBlock1" className="mx-2 py-6">
isFolder filter
</button>
<button id="filterBlock1" className="mx-2 py-6">
contains word
</button>
<button id="filterBlock1" className="mx-2 py-6">
match regex
</button>
</div>
<DataEditor
// contents
rows={props.rows.length}
columns={columns}
getCellContent={getData(props.rows)}
// click interactivity
keybindings={{
search: true,
selectAll: true,
selectRow: true,
copy: true,
paste: true,
}}
onCellActivated={doubleClickHandler}
onCellContextMenu={contextMenuHandler}
rowSelect={"single"}
gridSelection={selection}
onGridSelectionChange={setSelection}
getCellsForSelection={true}
/>
</div>
);
}
67 changes: 67 additions & 0 deletions app/src/features/search/utils/getNodesFromTableSelection.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {
GridSelection,
gridSelectionHasItem,
Item,
} from "@glideapps/glide-data-grid";
import React from "react";
import CRUDBookmarkFacade from "../../../lib/CRUDBookmarkFacade";
import { BookmarkNode } from "../../../lib/typesFacade";
import { isAFolder } from "../../../utils/ifHasChildrenFolders";
import {
ContextMenuActionTypes,
ContextMenuContextAction,
} from "../../context-menu/ContextMenuContext";
import { PathAction } from "../../path/PathContext";
import { viewDetailsColNumber } from "../../table/columns";

export function runDoubleClickSideEffects(
col: number,
contextMenuDispatch: React.Dispatch<ContextMenuContextAction>,
isFolder: boolean,
b: BookmarkNode,
pathDispatch: React.Dispatch<PathAction>,
): void {
if (col === viewDetailsColNumber) {
contextMenuDispatch({
type: isFolder ? "folder" : "single-bookmark",
direction: "open",
// todo change that hardcoded value for position, should be some start of the table maybe
position: [550, 550],
things: [b],
});
} else if (isFolder) {
CRUDBookmarkFacade.getPath(b).then((newPath) => {
console.debug("path:", newPath);
pathDispatch({
type: "full",
nodes: newPath,
});
});
} else {
chrome.tabs.create({ url: b.url });
}
}

// todo here logic does not seem to work actualyl
export function decideContextType(
selectedBookmarks: BookmarkNode[],
): ContextMenuActionTypes {
return selectedBookmarks.length === 1
? "mixed"
: isAFolder(selectedBookmarks[0])
? "folder"
: "single-bookmark";
}

export function getNodesFromTableSelection(
rows: BookmarkNode[],
selection: GridSelection,
cell: Item,
): BookmarkNode[] {
const includes: boolean = gridSelectionHasItem(selection, cell);
if (!includes) return [];
const start: number = selection.current?.range.y ?? 0;
const end: number = start + (selection.current?.range.height ?? 0);
const selectedBookmarks = rows.slice(start, end);
return selectedBookmarks;
}
2 changes: 1 addition & 1 deletion app/src/features/sorting/sorting.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TraverseArgs, globalTraverse } from "./utils/traversalFunctions/traverseTree";
import { TraverseArgs } from "../../lib/TraverseBookmarkFacade";

export const defaultSorterOptions: SorterOptions = {
autosort: false,
Expand Down
1 change: 1 addition & 0 deletions app/src/features/table/columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import "@glideapps/glide-data-grid/dist/index.css";
import "@toast-ui/editor/dist/toastui-editor.css";
import { CellGetter, myCols } from "./columnDefinitions";
import { BookmarkNode } from "../../lib/typesFacade";

const columnNumberToGridCell: Map<number, CellGetter> = new Map(
myCols.map((v, i) => [i, v.columnGetter]),
Expand Down
12 changes: 7 additions & 5 deletions app/todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ modules

## base refatoring
- [ ] ask ChatGPT to regfactor the table component
- [ ] move the refactoring from search results talbe also to the main table
- [ ] in search results change the path whenever the highlighted bookmark shows. do not just jump between things in the side tree
- [ ] deal with dual drop handling in the bookmark table
- [ ] make a separate search result component, reusing maybe half of the logic, but also a filter component there instead of path stuff
- [ ] try a simpler, local context menu for clicking from a tutorial and see if all works there. right now both the location and the passing of props do not work for edition
- [ ] possibly use a custom event listener for all the history and path changes - for these to be automatic

## dev features
- [ ] useSearchParams
- [ ] maybe history shouldn't be updated directly, but only downstream from the Path context object?
- [ ] test empirically summary-detail html5 instead of unrolled boolean
- [ ] lower panel is invisible rn if search results mode
## questionabl dev decision
- [ ] useSearchParams - might be too weird, but the native bookmark manager uses that
- [ ] maybe history shouldn't be updated directly, but only downstream from the Path context object? - check this out!
- [ ] test empirically summary-detail html5 instead of unrolled boolean - ask on stack overflow and also

## current errors
- [x] edit folder does not use the name first
Expand Down

0 comments on commit 8a5489c

Please sign in to comment.