Skip to content

Commit

Permalink
refactor: rename functions and remove prefix underscore
Browse files Browse the repository at this point in the history
  • Loading branch information
Enter-tainer committed Sep 28, 2024
1 parent af73614 commit 64053b4
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 85 deletions.
12 changes: 6 additions & 6 deletions frontend/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { apiEndpoint } from "./const";

export let githubMeta: GitHubMeta;

export const _fetchGitHubMeta = async () => {
export const fetchGitHubMeta = async () => {
const res = await fetch(`${apiEndpoint}meta/github-app`, {
method: "GET",
});
Expand All @@ -15,7 +15,7 @@ export const _fetchGitHubMeta = async () => {
if (!githubMeta) githubMeta = (await res.json()).data;
};

export const _handleOAuthToken = () => {
export const handleOAuthToken = () => {
const url = new URL(window.location.href);
const token = url.searchParams.get("oauth_token");
if (!token) return;
Expand All @@ -24,16 +24,16 @@ export const _handleOAuthToken = () => {
window.history.replaceState(null, "", url.toString());
};

export const _getJWT = () => {
export const getJWT = () => {
// https://developer.mozilla.org/zh-CN/docs/Web/API/Document/cookie#%E7%A4%BA%E4%BE%8B_2_%E5%BE%97%E5%88%B0%E5%90%8D%E4%B8%BA_test2_%E7%9A%84_cookie
return document.cookie.replace(
/(?:(?:^|.*;\s*)oauth_token\s*\=\s*([^;]*).*$)|^.*$/,
"$1",
);
};

export const _decodeJWT = () => {
const jwt = _getJWT();
export const decodeJWT = () => {
const jwt = getJWT();
if (!jwt) return;
const raw = jwt.split(".")[1];

Expand All @@ -42,7 +42,7 @@ export const _decodeJWT = () => {
return JSON.parse(decodedString) as JWTPayload;
};

export const _logout = () => {
export const logout = () => {
document.cookie =
"oauth_token=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; secure";
};
78 changes: 39 additions & 39 deletions frontend/lib/dom/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import iconShare from "iconify/share";
import iconEdit from "iconify/edit";
import iconDelete from "iconify/delete";
import { Comment } from "../types";
import { _getJWT, _decodeJWT, _logout, githubMeta } from "../auth";
import { getJWT, decodeJWT, logout, githubMeta } from "../auth";
import { apiEndpoint } from "../const";
import { groupBy, dateTimeFormatter } from "../util";

Expand All @@ -26,7 +26,7 @@ export const setCommentsPanel = (panel: HTMLElement) => {
commentsPanel = panel;
};

export const _handleAnchor = async () => {
export const handleAnchor = async () => {
const url = new URL(window.location.href);
const anchor = url.hash;
if (!anchor) return;
Expand All @@ -35,7 +35,7 @@ export const _handleAnchor = async () => {
if (!rawCommentId) return;
const commentId = parseInt(rawCommentId);

await _fetchComments();
await fetchComments();
const comment = commentsCache?.find((it) => it.id === commentId);
if (!comment) return;

Expand All @@ -45,7 +45,7 @@ export const _handleAnchor = async () => {
);
if (!paragraph) return;

await _selectOffsetParagraph({
await selectOffsetParagraph({
el: paragraph,
focusReply: true,
});
Expand All @@ -61,7 +61,7 @@ export const _handleAnchor = async () => {
});
};

export const _registerDialog = ({
export const registerDialog = ({
idOrClass,
content,
actions = new Map<string, (el: HTMLElement) => void>(),
Expand Down Expand Up @@ -110,7 +110,7 @@ export const _registerDialog = ({
return dialog;
};

export const _selectOffsetParagraph = async ({
export const selectOffsetParagraph = async ({
el,
focusReply = false,
}: {
Expand All @@ -125,17 +125,17 @@ export const _selectOffsetParagraph = async ({
if (selectedOffset?.dataset.reviewHasComments || focusReply) {
delete selectedOffset.dataset.reviewFocused;
selectedOffset.dataset.reviewSelected = "true";
await _openCommentsPanel();
await openCommentsPanel();
}
};

export const _unselectOffsetParagraph = () => {
export const unselectOffsetParagraph = () => {
delete selectedOffset?.dataset.reviewSelected;
selectedOffset = null;
};

export const _openCommentsPanel = async () => {
const comments = [...(await _fetchComments())];
export const openCommentsPanel = async () => {
const comments = [...(await fetchComments())];

const selected = selectedOffset;

Expand Down Expand Up @@ -165,7 +165,7 @@ export const _openCommentsPanel = async () => {
});
}

await _renderComments(comments);
await renderComments(comments);
let selectedCommentsGroup = document.querySelector(
`#review-comments-panel .comments_group[data-original-document-start="${selectedOffset?.dataset.originalDocumentStart}"][data-original-document-end="${selectedOffset?.dataset.originalDocumentEnd}"]`,
) as HTMLElement;
Expand All @@ -186,12 +186,12 @@ export const _openCommentsPanel = async () => {
commentsPanel.classList.remove("review_hidden");
};

export const _closeCommentsPanel = () => {
export const closeCommentsPanel = () => {
commentsPanel.classList.add("review_hidden");
commentsButton.classList.remove("review_hidden");
};

export const _submitComment = async ({
export const submitComment = async ({
offsets,
content,
}: {
Expand All @@ -217,7 +217,7 @@ export const _submitComment = async ({
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${_getJWT()}`,
Authorization: `Bearer ${getJWT()}`,
},
body: JSON.stringify({
...comment,
Expand All @@ -231,9 +231,9 @@ export const _submitComment = async ({
commentsCache.push({
...comment,
commenter: {
name: _decodeJWT()?.name ?? "未知用户",
oauth_provider: _decodeJWT()?.provider as "github",
oauth_user_id: _decodeJWT()?.id ?? "-1",
name: decodeJWT()?.name ?? "未知用户",
oauth_provider: decodeJWT()?.provider as "github",
oauth_user_id: decodeJWT()?.id ?? "-1",
},
id: commentsCache.length,
created_time: new Date().toISOString(),
Expand All @@ -257,8 +257,8 @@ export const _submitComment = async ({
);
}

await _fetchComments(true);
_updateAvailableComments();
await fetchComments(true);
updateAvailableComments();
};

export const _modifyComment = async ({
Expand All @@ -274,7 +274,7 @@ export const _modifyComment = async ({
method: "PATCH",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${_getJWT()}`,
Authorization: `Bearer ${getJWT()}`,
},
body: JSON.stringify({
comment,
Expand Down Expand Up @@ -305,8 +305,8 @@ export const _modifyComment = async ({
);
}

await _fetchComments(true);
_updateAvailableComments();
await fetchComments(true);
updateAvailableComments();
};

export const _deleteComment = async ({ id }: { id: number }) => {
Expand All @@ -315,7 +315,7 @@ export const _deleteComment = async ({ id }: { id: number }) => {
{
method: "DELETE",
headers: {
Authorization: `Bearer ${_getJWT()}`,
Authorization: `Bearer ${getJWT()}`,
},
},
);
Expand All @@ -341,11 +341,11 @@ export const _deleteComment = async ({ id }: { id: number }) => {
commentsCache = commentsCache.filter((it) => it.id !== id);
}

await _fetchComments(true);
_updateAvailableComments();
await fetchComments(true);
updateAvailableComments();
};

export const _fetchComments = async (force: boolean = false) => {
export const fetchComments = async (force: boolean = false) => {
if (!force && commentsCache) {
return commentsCache;
}
Expand All @@ -363,7 +363,7 @@ export const _fetchComments = async (force: boolean = false) => {
return comments;
};

export const _renderComments = async (comments: Comment[]) => {
export const renderComments = async (comments: Comment[]) => {
const commentsEl = commentsPanel.querySelector(
".panel_main",
)! as HTMLDivElement;
Expand Down Expand Up @@ -443,7 +443,7 @@ export const _renderComments = async (comments: Comment[]) => {
"button.comment_actions_item[data-action='logout']",
) as HTMLButtonElement;

const userInfo = _decodeJWT();
const userInfo = decodeJWT();
if (!userInfo) {
username.textContent = "登录到 GitHub 以发表评论";
commentActionsLogout.style.display = "none";
Expand Down Expand Up @@ -646,7 +646,7 @@ export const _renderComments = async (comments: Comment[]) => {
} else if (e instanceof Response) {
if (e.status === 401) {
notification.textContent = "身份验证失效,请重新登录";
_logout();
logout();
return;
}
if (
Expand Down Expand Up @@ -691,7 +691,7 @@ export const _renderComments = async (comments: Comment[]) => {
break;
}
case "logout": {
_logout();
logout();
window.location.reload();
break;
}
Expand All @@ -714,7 +714,7 @@ export const _renderComments = async (comments: Comment[]) => {
notification.textContent = "";

try {
await _submitComment({
await submitComment({
offsets: [
parseInt(selectedOffset!.dataset.originalDocumentStart!),
parseInt(selectedOffset!.dataset.originalDocumentEnd!),
Expand All @@ -727,7 +727,7 @@ export const _renderComments = async (comments: Comment[]) => {
} catch (error) {
_handleError(error);
} finally {
await _openCommentsPanel();
await openCommentsPanel();

const newNotification = commentsPanel.querySelector(
"[data-review-selected] .comment_actions_notification",
Expand All @@ -744,7 +744,7 @@ export const _renderComments = async (comments: Comment[]) => {
}
}

await _openCommentsPanel();
await openCommentsPanel();
const newSubmitButton = commentsPanel.querySelector(
"[data-review-selected] button[data-action='submit']",
) as HTMLButtonElement;
Expand Down Expand Up @@ -815,7 +815,7 @@ export const _renderComments = async (comments: Comment[]) => {
} catch (error) {
_handleError(error);
} finally {
await _openCommentsPanel();
await openCommentsPanel();

const newNotification = commentsPanel.querySelector(
"[data-review-selected] .comment_actions_notification",
Expand All @@ -832,7 +832,7 @@ export const _renderComments = async (comments: Comment[]) => {
}
}

await _openCommentsPanel(); // ??
await openCommentsPanel(); // ??
break;
}
case "delete": {
Expand All @@ -847,7 +847,7 @@ export const _renderComments = async (comments: Comment[]) => {
_deleteComment({ id: parseInt(id) })
.catch(_handleError)
.finally(() => {
_openCommentsPanel().then(() => {
openCommentsPanel().then(() => {
const newNotification = commentsPanel.querySelector(
"[data-review-selected] .comment_actions_notification",
);
Expand All @@ -863,7 +863,7 @@ export const _renderComments = async (comments: Comment[]) => {
});
});

_openCommentsPanel().then(() => {
openCommentsPanel().then(() => {
const newNotification = commentsPanel.querySelector(
"[data-review-selected] .comment_actions_notification",
);
Expand Down Expand Up @@ -930,14 +930,14 @@ export const _renderComments = async (comments: Comment[]) => {
}
};

export const _updateAvailableComments = async () => {
export const updateAvailableComments = async () => {
const offsets = Array.from(
document.querySelectorAll<HTMLElement>(
"[data-review-enabled][data-original-document-start][data-original-document-end]",
),
);

await _fetchComments();
await fetchComments();

for (let offset of offsets) {
delete offset.dataset.reviewHasComments;
Expand Down
14 changes: 7 additions & 7 deletions frontend/lib/dom/ctx_menu.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { _selectOffsetParagraph } from "./comment";
import { _registerDialog } from "./util";
import { selectOffsetParagraph } from "./comment";
import { registerDialog } from "./util";
import iconAddComment from "iconify/add-comment-outline-rounded";

export const _createContextMenu = ({ el }: { el: HTMLElement }) => {
export const createContextMenu = ({ el }: { el: HTMLElement }) => {
if (el.querySelector(`.review-context-menu`)) return;
_registerDialog({
registerDialog({
idOrClass: "review-context-menu",
content: `
<button data-action="comment">
Expand All @@ -16,7 +16,7 @@ export const _createContextMenu = ({ el }: { el: HTMLElement }) => {
[
"comment",
() => {
_selectOffsetParagraph({
selectOffsetParagraph({
el,
focusReply: true,
});
Expand All @@ -36,7 +36,7 @@ export const _createContextMenu = ({ el }: { el: HTMLElement }) => {
});
};

export const _openContextMenu = ({ el }: { el: HTMLElement }) => {
export const openContextMenu = ({ el }: { el: HTMLElement }) => {
const contextMenu = el.querySelector(`.review-context-menu`) as
| HTMLDivElement
| undefined;
Expand All @@ -47,7 +47,7 @@ export const _openContextMenu = ({ el }: { el: HTMLElement }) => {
contextMenu.style.display = "";
};

export const _closeContextMenu = ({ el }: { el: HTMLElement }) => {
export const closeContextMenu = ({ el }: { el: HTMLElement }) => {
const contextMenu = el.querySelector(
`.review-context-menu:not([style*="display: none"])`,
) as HTMLDivElement | undefined;
Expand Down
2 changes: 1 addition & 1 deletion frontend/lib/dom/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const _registerDialog = ({
export const registerDialog = ({
idOrClass,
content,
actions = new Map<string, (el: HTMLElement) => void>(),
Expand Down
Loading

0 comments on commit 64053b4

Please sign in to comment.