Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup of the frontend code #1563

Merged
merged 1 commit into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
cleanup of the frontend code
- replace undocumented `substr` with `substring`
- remove unused code
- inline a few variables
- simplify ifs when possible
- improve saveCollapsedComments documentation
- cleanup the unused imports
- remove unused variables and types
  • Loading branch information
paskal committed Jan 8, 2023
commit 3d12f0705deba52e47227526c53e673bb5332b1b
2 changes: 0 additions & 2 deletions frontend/apps/remark42/app/common/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ export const getConfig = (): Promise<Config> => apiFetcher.get('/config');

export const getPostComments = (sort: Sorting) => apiFetcher.get<Tree>('/find', { url, sort, format: 'tree' });

export const getComment = (id: Comment['id']): Promise<Comment> => apiFetcher.get(`/id/${id}`, { url });

export const getUserComments = (
userId: User['id'],
config: { limit: number; skip?: number } = { limit: 10, skip: 0 }
Expand Down
4 changes: 0 additions & 4 deletions frontend/apps/remark42/app/common/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,3 @@ export function getCookie(name: string) {

return matches ? decodeURIComponent(matches[1]) : undefined;
}

export function deleteCookie(name: string) {
setCookie(name, '', { expires: -1 });
}
4 changes: 1 addition & 3 deletions frontend/apps/remark42/app/common/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ const createFetcher = (baseUrl: string = ''): Methods => {
// TODO: it should be clarified when frontend gets this header and what could be in it to simplify this logic and cover by tests
const date = (res.headers.has('date') && res.headers.get('date')) || '';
const timestamp = isNaN(Date.parse(date)) ? 0 : Date.parse(date);
const timeDiff = (new Date().getTime() - timestamp) / 1000;

StaticStore.serverClientTimeDiff = timeDiff;
StaticStore.serverClientTimeDiff = (new Date().getTime() - timestamp) / 1000;

// backend could update jwt in any time. so, we should handle it
if (res.headers.has(JWT_HEADER)) {
Expand Down
6 changes: 0 additions & 6 deletions frontend/apps/remark42/app/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ export interface Comment {
*/
hidden?: boolean;
}

export interface CommentsResponse {
comments: Comment[];
count: number;
}

export interface Node {
comment: Comment;
replies?: Node[];
Expand Down
2 changes: 1 addition & 1 deletion frontend/apps/remark42/app/components/auth/auth.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export function useErrorMessage(): [string | null, (e: unknown) => void] {
}

const errorReason =
err instanceof RequestError || (isObject(err) && typeof (err as Record<string, string>).error === 'string')
err instanceof RequestError || isObject(err)
? (err as Record<'error', string>).error
: err instanceof Error
? err.message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const emailRegexp = /[^@]+@[^.]+\..+/;
enum Step {
Email,
Token,
Final,
Close,
Subscribed,
Unsubscribed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class CommentForm extends Component<Props, State> {

onInput = (e: Event) => {
const { value } = e.target as HTMLInputElement;
const text = value.substr(0, StaticStore.config.max_comment_size);
const text = value.substring(0, StaticStore.config.max_comment_size);

updatePersistedComments(this.props.id, value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('<CommentVote />', () => {
['downvote', -1, 'Vote down', 'Vote up', 'downVoteButtonActive'],
])(
'should go throught voting process and communicate with store when %s button is clicked',
async (_, increment, activeButtonText, secondButtonText, activeButtonClass) => {
async (_, increment, activeButtonText, secondButtonText) => {
const putCommentVoteSpy = jest
.spyOn(api, 'putCommentVote')
.mockImplementationOnce(({ vote }) => Promise.resolve({ id: '1', score: 10 + vote }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Props = {
disabled?: boolean;
};

export function CommentVotes({ id, votes, vote, disabled, controversy = 0 }: Props) {
export function CommentVotes({ id, votes, vote, disabled }: Props) {
const intl = useIntl();
const dispatch = useDispatch();
const [loadingState, setLoadingState] = useState<{ vote: number; votes: number } | null>(null);
Expand Down
15 changes: 2 additions & 13 deletions frontend/apps/remark42/app/components/comment/comment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export interface State {
}

export class Comment extends Component<CommentProps, State> {
votingPromise: Promise<unknown> = Promise.resolve();
/** comment text node. Used in comment text copying */
textNode = createRef<HTMLDivElement>();

Expand Down Expand Up @@ -175,16 +174,6 @@ export class Comment extends Component<CommentProps, State> {
}
};

onBlockUserClick = (evt: Event) => {
const target = evt.currentTarget;

if (target instanceof HTMLOptionElement) {
// we have to debounce the blockUser function calls otherwise it will be
// called 2 times (by change event and by blur event)
this.blockUser(target.value as BlockTTL);
}
};

blockUser = debounce((ttl: BlockTTL): void => {
const { user } = this.props.data;
const blockingDurations = getBlockingDurations(this.props.intl);
Expand Down Expand Up @@ -427,7 +416,7 @@ export class Comment extends Component<CommentProps, State> {
/>
</button>
)}
{!isAdmin && !!o.user.verified && props.view !== 'user' && (
{!isAdmin && o.user.verified && props.view !== 'user' && (
<VerificationIcon className={styles.verificationIcon} title={intl.formatMessage(messages.verifiedUser)} />
)}
{o.user.paid_sub && (
Expand Down Expand Up @@ -560,7 +549,7 @@ function getTextSnippet(html: string) {
tmp.innerHTML = html.replace('</p><p>', ' ');

const result = tmp.innerText || '';
const snippet = result.substr(0, LENGTH);
const snippet = result.substring(0, LENGTH);

return snippet.length === LENGTH && result.length !== LENGTH ? `${snippet}...` : snippet;
}
Expand Down
5 changes: 1 addition & 4 deletions frontend/apps/remark42/app/components/root/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { COMMENT_NODE_CLASSNAME_PREFIX, MAX_SHOWN_ROOT_COMMENTS, THEMES, IS_MOBI
import { maxShownComments, url } from 'common/settings';

import {
setUser,
fetchUser,
blockUser,
unblockUser,
Expand All @@ -20,7 +19,7 @@ import {
unhideUser,
signout,
} from 'store/user/actions';
import { fetchComments, addComment, updateComment, unsetCommentMode } from 'store/comments/actions';
import { fetchComments, addComment, updateComment } from 'store/comments/actions';
import { setCommentsReadOnlyState } from 'store/post-info/actions';
import { setTheme } from 'store/theme/actions';

Expand Down Expand Up @@ -73,7 +72,6 @@ const mapStateToProps = (state: StoreState) => ({

const boundActions = bindActions({
fetchComments,
setUser,
fetchUser,
fetchBlockedUsers,
setTheme,
Expand All @@ -85,7 +83,6 @@ const boundActions = bindActions({
addComment,
updateComment,
setCollapse,
unsetCommentMode,
signout,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ class SettingsComponent extends Component<Props, State> {
};

__isUserHidden = (user: User): boolean => {
if (this.state.unhiddenUsers.indexOf(user.id) === -1) return true;
return false;
return !this.state.unhiddenUsers.includes(user.id);
};

render({ user, theme }: Props, { blockedUsers, unblockedUsers, unhiddenUsers }: State) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { h, JSX } from 'preact';
import { forwardRef } from 'preact/compat';
import { useEffect, useRef } from 'preact/hooks';

function autoResize(textarea: HTMLTextAreaElement, onResize?: () => void) {
function autoResize(textarea: HTMLTextAreaElement) {
textarea.style.height = '';
textarea.style.height = `${textarea.scrollHeight}px`;
}
Expand Down
4 changes: 1 addition & 3 deletions frontend/apps/remark42/app/hooks/useTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@ import { StoreState } from 'store';
import { Theme } from 'common/types';

export function useTheme() {
const theme = useSelector<StoreState, Theme>(({ theme }) => theme);

return theme;
return useSelector<StoreState, Theme>(({ theme }) => theme);
}
15 changes: 0 additions & 15 deletions frontend/apps/remark42/app/typings/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,4 @@ declare global {
| undefined;
};
}

namespace NodeJS {
interface Global {
Headers: typeof Headers;
localStorage: typeof Storage;
}
}
}

/**
* Variable responsive for dynamic setting public path for
* assets. Dynamic imports with relative url will be resolved over this path.
*
* https://webpack.js.org/guides/public-path/#on-the-fly
*/
declare let __webpack_public_path__: string;
8 changes: 0 additions & 8 deletions frontend/apps/remark42/app/utils/bench.ts

This file was deleted.

22 changes: 0 additions & 22 deletions frontend/apps/remark42/app/utils/debug-node.ts

This file was deleted.

2 changes: 0 additions & 2 deletions frontend/apps/remark42/app/utils/derequire.ts

This file was deleted.

2 changes: 1 addition & 1 deletion frontend/apps/remark42/app/utils/replaceSelection.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export function replaceSelection(text: string, selection: [number, number], replacement: string): string {
return text.substr(0, selection[0]) + replacement + text.substr(selection[1]);
return text.substring(0, selection[0]) + replacement + text.substring(selection[1]);
}
9 changes: 0 additions & 9 deletions frontend/apps/remark42/app/utils/shallowCompare.ts

This file was deleted.

2 changes: 1 addition & 1 deletion frontend/apps/remark42/templates/comments.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
var query = (function () {
if (window.location.search.length === 0) return {};
return window.location.search
.substr(1)
.substring(1)
.split('&')
.map(function (item) {
return item.split('=');
Expand Down
2 changes: 1 addition & 1 deletion frontend/apps/remark42/templates/iframe.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
window.location.search.length < 2
? {}
: window.location.search
.substr(1)
.substring(1)
.split('&')
.reduce(function (c, x) {
var splitted = x.split('=');
Expand Down