Skip to content

Commit

Permalink
feat: adding toggle handler
Browse files Browse the repository at this point in the history
  • Loading branch information
devpanther committed Mar 5, 2024
1 parent 28ad633 commit 623627b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
36 changes: 36 additions & 0 deletions .husky/_/husky.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env sh
if [ -z "$husky_skip_init" ]; then
debug () {
if [ "$HUSKY_DEBUG" = "1" ]; then
echo "husky (debug) - $1"
fi
}

readonly hook_name="$(basename -- "$0")"
debug "starting $hook_name..."

if [ "$HUSKY" = "0" ]; then
debug "HUSKY env variable is set to 0, skipping hook"
exit 0
fi

if [ -f ~/.huskyrc ]; then
debug "sourcing ~/.huskyrc"
. ~/.huskyrc
fi

readonly husky_skip_init=1
export husky_skip_init
sh -e "$0" "$@"
exitCode="$?"

if [ $exitCode != 0 ]; then
echo "husky - $hook_name hook exited with code $exitCode (error)"
fi

if [ $exitCode = 127 ]; then
echo "husky - command not found in PATH=$PATH"
fi

exit $exitCode
fi
3 changes: 2 additions & 1 deletion src/home/sorting/generate-sorting-buttons.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { SortingManager } from "./sorting-manager";

export const SORTING_OPTIONS = ["price", "time", "priority", "activity"] as const;
export type Sorting = (typeof SORTING_OPTIONS)[number];
export type PrivateSorting = (typeof SORTING_OPTIONS)[number];
export type Sorting = PrivateSorting & { private?: never };

export function generateSortingToolbar() {
const sortingManager = new SortingManager("filters", SORTING_OPTIONS);
Expand Down
6 changes: 5 additions & 1 deletion src/home/sorting/sort-issues-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import { Sorting } from "./generate-sorting-buttons";
import { sortIssuesBy } from "./sort-issues-by";
import { sortIssuesByPriority } from "./sort-issues-by-priority";
import { sortIssuesByTime } from "./sort-issues-by-time";
import { sortPrivateIssues } from "./sort-private-issues";

export function sortIssuesController(tasks: TaskMaybeFull[], sorting?: Sorting, options = { ordering: "normal" }) {
let sortedIssues = tasks;

if (sorting) {
if (sorting && sorting !== ("private" as Sorting)) {
sortedIssues = sortIssuesBy(sortedIssues, sorting);
} else if (sorting === ("private" as Sorting)) {
const sortedIssuesByPrivacy = sortPrivateIssues(sortedIssues);
sortedIssues = sortedIssuesByPrivacy;
} else {
const sortedIssuesByTime = sortIssuesByTime(sortedIssues);
const sortedIssuesByPriority = sortIssuesByPriority(sortedIssuesByTime);
Expand Down
8 changes: 8 additions & 0 deletions src/home/sorting/sort-private-issues.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { TaskMaybeFull } from "../fetch-github/preview-to-full-mapping";

export function sortPrivateIssues(tasks: TaskMaybeFull[]) {
return tasks.filter(task => {
// Check if task's issue body contains "private: true"
return task.preview.private;
});
}
13 changes: 13 additions & 0 deletions src/home/sorting/sorting-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export class SortingManager {
toggleCheckbox.id = "cb4";
toggleCheckbox.type = "checkbox";

toggleCheckbox.addEventListener("click", () => this._handleToggleClick(toggleCheckbox));

return toggleCheckbox;
}

Expand Down Expand Up @@ -102,6 +104,17 @@ export class SortingManager {
return label;
}

private async _handleToggleClick(input: HTMLInputElement) {
const ordering = input === this._lastChecked ? "reverse" : "normal";
let option: Sorting | undefined;
if(input.checked){
option = "private" as Sorting;
}

// instantly load from cache
fetchAndDisplayPreviewsFromCache(option, { ordering }).catch((error) => console.error(error));
}

private async _handleSortingClick(input: HTMLInputElement, option: string) {
const ordering = input === this._lastChecked ? "reverse" : "normal";
input.checked = input !== this._lastChecked;
Expand Down

0 comments on commit 623627b

Please sign in to comment.