Skip to content

Commit

Permalink
sort by new works
Browse files Browse the repository at this point in the history
  • Loading branch information
nemanjam committed Apr 23, 2024
1 parent ebcafb0 commit efaa2ca
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 30 deletions.
7 changes: 5 additions & 2 deletions docs/work-notes/todo2.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ onScroll, onKeyDown treba attach samo jednom
3. wait - retry for comments to load or error, or timeout, on dropdown change too
4. onCommentsLoaded, onThreadArrived, onNewThreadArrived, onThreadLeft dispatch custom events

debounce koji ne ceka na prvi call
debounce koji ne ceka na prvi call - leading lodash

attach listeners only once lock
fix highlight on focus
remove delayExecution(markAsRead) - needed, cancels on debounce

isElementInViewport broken - fixed with debounceTrailing
sortByNew
sortByNew

use retry-async with exception
poenta je ista kao u cypress ili react-testing-library retry da async element loaded, cela filozofija
5 changes: 2 additions & 3 deletions source/reddit-comments/constants/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export const threadPostIdRegexValidate = /^t3_[a-z0-9]+$/;
export const threadWithZeroCommentsSelector =
'comment-forest-empty-state[post-id^="t3_"]';

export const brokenCommentsThreadSelector = '#main-content > shreddit-forbidden';

/*----------------- header ----------------*/

export const pageHeaderSelector = 'reddit-header-large';
Expand All @@ -57,6 +59,3 @@ export const sortMenuClickSelector = '#comment-sort-button';

// dropdown item new
export const sortByNewMenuItemSelector = 'div[slot="dropdown-items"] span.text-14';

// for blur, outside of shadow dom, simple selector
export const mainContentForBlurSelector = '#main-content';
9 changes: 2 additions & 7 deletions source/reddit-comments/dom/sort-by-new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
sortByNewMenuItemSelector,
sortMenuShadowHostSelector,
sortMenuClickSelector,
mainContentForBlurSelector,
} from '../constants/selectors';
import { MyElementNotFoundDOMException } from '../exceptions';
import { wait } from '../utils';
Expand All @@ -16,6 +15,7 @@ export const clickSortByNewMenuItem = async (): Promise<boolean> => {
// same shadowHost is reused for currently selected item and dropdown menu click
const shadowHost = document.querySelector(sortMenuShadowHostSelector);
const shadowRoot = shadowHost?.shadowRoot;

if (!shadowRoot)
throw new MyElementNotFoundDOMException(
`shadowRoot not found. sortMenuShadowHostSelector: ${sortMenuShadowHostSelector}`
Expand Down Expand Up @@ -61,12 +61,7 @@ export const clickSortByNewMenuItem = async (): Promise<boolean> => {
(sortByNewMenuItem as HTMLElement).click();

// remove :focus-visible border
const mainContent = document.querySelector<HTMLElement>(mainContentForBlurSelector);
if (!mainContent)
throw new MyElementNotFoundDOMException(
`mainContent not found. mainContentForBlurSelector: ${mainContentForBlurSelector}`
);
mainContent?.click();
sortMenu.blur();
}

return true;
Expand Down
4 changes: 4 additions & 0 deletions source/reddit-comments/dom/thread.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
brokenCommentsThreadSelector,
threadPostSelector,
threadWithZeroCommentsSelector,
} from '../constants/selectors';
Expand All @@ -21,3 +22,6 @@ export const getThreadIdFromDom = (): string => {

export const isZeroCommentsThread = (): boolean =>
Boolean(document.querySelector<HTMLElement>(threadWithZeroCommentsSelector));

export const isBrokenCommentsThread = (): boolean =>
Boolean(document.querySelector<HTMLElement>(brokenCommentsThreadSelector));
27 changes: 17 additions & 10 deletions source/reddit-comments/dom/wait-for-comments.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defaultRetryOptions, RetryOptions } from '../constants/config';
import { getElapsedTime, isActiveTabAndRedditThreadAndHasComments, wait } from '../utils';
import { isZeroCommentsThread } from './thread';
import { isBrokenCommentsThread, isZeroCommentsThread } from './thread';

export interface RetryAndWaitResult {
/** if should proceed with highlight */
Expand All @@ -11,6 +11,7 @@ export interface RetryAndWaitResult {
| 'timeout'
| 'left-thread'
| 'zero-comments'
| 'broken-comments'
| 'initial';
elapsedTime: number;
retryIndex: number;
Expand All @@ -20,41 +21,47 @@ export type RetryAndWaitCallbackResult = Pick<RetryAndWaitResult, 'reason' | 'is
export type RetryAndWaitCallback = () => RetryAndWaitCallbackResult;

export const waitForCommentsCallback: RetryAndWaitCallback = () => {
let result: RetryAndWaitCallbackResult = {
isSuccess: false,
reason: 'initial',
};

// check 0 comments
const hasZeroComments = isZeroCommentsThread();

if (hasZeroComments) {
result = {
return {
isSuccess: false,
reason: 'zero-comments',
};
}

// select broken comments dom
const hasBrokenComments = isBrokenCommentsThread();

if (hasBrokenComments) {
return {
isSuccess: false,
reason: 'broken-comments',
};
}

const { isOk, isActiveTab, isRedditThread } =
isActiveTabAndRedditThreadAndHasComments();

if (isOk) {
result = {
return {
isSuccess: true,
reason: 'has-comments',
};
}

if (!(isActiveTab && isRedditThread)) {
result = {
return {
isSuccess: false,
reason: 'left-thread',
};
}

return result;
return {
isSuccess: false,
reason: 'initial',
};
};

export const retryAndWaitForCommentsToLoad = () =>
Expand Down
15 changes: 7 additions & 8 deletions source/reddit-comments/events/on-thread.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { threadArrivedDebounceWait } from '../constants/config';
import { debounceLeading } from '../utils';
import { waitAfterSortByNew } from '../constants/config';
import { getSettings } from '../database/models/settings';
import { openDatabase } from '../database/schema';
import { highlightByDate } from '../dom/highlight-by-date';
Expand All @@ -12,7 +11,6 @@ import { clickSortByNewMenuItem } from '../dom/sort-by-new';
import {
isActiveTabAndRedditThread,
isActiveTabAndRedditThreadAndHasComments,
wait,
} from '../utils';
import logger from '../logger';
import { ARRIVED_TO_REDDIT_THREAD_EVENT_NAME } from '../constants/events';
Expand All @@ -27,19 +25,20 @@ export const handleArrivedToRedditThread = async () => {
try {
const db = await openDatabase();
const { sortAllByNew } = await getSettings(db);

if (sortAllByNew) {
const hasSorted = await clickSortByNewMenuItem();
if (hasSorted) {
// delay must be AFTER sort
await wait(waitAfterSortByNew);
}
// when comments are loaded, sort menu is loaded too
const { isSuccess } = await retryAndWaitForCommentsToLoad();
if (!isSuccess) return;

await clickSortByNewMenuItem();
}

// wait for sorted comments to reload
const { isSuccess } = await retryAndWaitForCommentsToLoad();
if (!isSuccess) return;

//! important, must select element AFTER sort
// only root check, child functions must have commentElements array filled
const { isOk, commentElements } = isActiveTabAndRedditThreadAndHasComments();
if (!isOk) return;

Expand Down

0 comments on commit efaa2ca

Please sign in to comment.