From: Alec Armbruster <alectrocute@gmail.com> Date: Fri, 16 Jun 2023 21:25:53 +0000 (-0400) Subject: break out browser and helper methods X-Git-Url: http://these/git/%7B%24%7B%60data:application/%22%7Burl%7D/readmes/README.zh.hant.md?a=commitdiff_plain;h=976ed12d077e89298b6fc574d59f23455c211a15;p=lemmy-ui.git break out browser and helper methods --- diff --git a/src/shared/components/app/navbar.tsx b/src/shared/components/app/navbar.tsx index 8c7c920..20d4f25 100644 --- a/src/shared/components/app/navbar.tsx +++ b/src/shared/components/app/navbar.tsx @@ -11,14 +11,14 @@ import { UserService } from "../../services"; import { HttpService, RequestState } from "../../services/HttpService"; import { donateLemmyUrl, - isBrowser, myAuth, numToSI, - poll, showAvatars, toast, updateUnreadCountsInterval, } from "../../utils"; +import { isBrowser } from "../../utils/browser/is-browser"; +import { poll } from "../../utils/helpers/poll"; import { amAdmin } from "../../utils/roles/am-admin"; import { canCreateCommunity } from "../../utils/roles/can-create-community"; import { Icon } from "../common/icon"; diff --git a/src/shared/components/common/markdown-textarea.tsx b/src/shared/components/common/markdown-textarea.tsx index 9318d3b..c1e8524 100644 --- a/src/shared/components/common/markdown-textarea.tsx +++ b/src/shared/components/common/markdown-textarea.tsx @@ -7,7 +7,6 @@ import { HttpService, UserService } from "../../services"; import { concurrentImageUpload, customEmojisLookup, - isBrowser, markdownFieldCharacterLimit, markdownHelpUrl, maxUploadImages, @@ -20,12 +19,12 @@ import { setupTribute, toast, } from "../../utils"; +import { isBrowser } from "../../utils/browser/is-browser"; import { EmojiPicker } from "./emoji-picker"; import { Icon, Spinner } from "./icon"; import { LanguageSelect } from "./language-select"; import NavigationPrompt from "./navigation-prompt"; import ProgressBar from "./progress-bar"; - interface MarkdownTextAreaProps { initialContent?: string; initialLanguageId?: number; diff --git a/src/shared/components/community/communities.tsx b/src/shared/components/community/communities.tsx index 6232694..b98bf25 100644 --- a/src/shared/components/community/communities.tsx +++ b/src/shared/components/community/communities.tsx @@ -11,17 +11,17 @@ import { InitialFetchRequest } from "../../interfaces"; import { FirstLoadService } from "../../services/FirstLoadService"; import { HttpService, RequestState } from "../../services/HttpService"; import { - QueryParams, editCommunity, getPageFromString, - getQueryParams, - getQueryString, myAuth, myAuthRequired, numToSI, setIsoData, showLocal, } from "../../utils"; +import { getQueryParams } from "../../utils/helpers/get-query-params"; +import { getQueryString } from "../../utils/helpers/get-query-string"; +import type { QueryParams } from "../../utils/types/query-params"; import { HtmlTags } from "../common/html-tags"; import { Spinner } from "../common/icon"; import { ListingTypeSelect } from "../common/listing-type-select"; diff --git a/src/shared/components/community/community.tsx b/src/shared/components/community/community.tsx index 7dc150f..cb7e951 100644 --- a/src/shared/components/community/community.tsx +++ b/src/shared/components/community/community.tsx @@ -62,7 +62,6 @@ import { UserService } from "../../services"; import { FirstLoadService } from "../../services/FirstLoadService"; import { HttpService, RequestState } from "../../services/HttpService"; import { - QueryParams, commentsToFlatNodes, communityRSSUrl, editComment, @@ -74,8 +73,6 @@ import { getCommentParentId, getDataTypeString, getPageFromString, - getQueryParams, - getQueryString, myAuth, postToCommentSortType, relTags, @@ -88,6 +85,9 @@ import { updateCommunityBlock, updatePersonBlock, } from "../../utils"; +import { getQueryParams } from "../../utils/helpers/get-query-params"; +import { getQueryString } from "../../utils/helpers/get-query-string"; +import type { QueryParams } from "../../utils/types/query-params"; import { CommentNodes } from "../comment/comment-nodes"; import { BannerIconHeader } from "../common/banner-icon-header"; import { DataTypeSelect } from "../common/data-type-select"; @@ -99,7 +99,6 @@ import { Sidebar } from "../community/sidebar"; import { SiteSidebar } from "../home/site-sidebar"; import { PostListings } from "../post/post-listings"; import { CommunityLink } from "./community-link"; - interface State { communityRes: RequestState<GetCommunityResponse>; postsRes: RequestState<GetPostsResponse>; diff --git a/src/shared/components/home/home.tsx b/src/shared/components/home/home.tsx index 4e23c03..ed3afd0 100644 --- a/src/shared/components/home/home.tsx +++ b/src/shared/components/home/home.tsx @@ -67,13 +67,10 @@ import { getCommentParentId, getDataTypeString, getPageFromString, - getQueryParams, - getQueryString, getRandomFromList, mdToHtml, myAuth, postToCommentSortType, - QueryParams, relTags, restoreScrollPosition, saveScrollPosition, @@ -84,7 +81,10 @@ import { trendingFetchLimit, updatePersonBlock, } from "../../utils"; +import { getQueryParams } from "../../utils/helpers/get-query-params"; +import { getQueryString } from "../../utils/helpers/get-query-string"; import { canCreateCommunity } from "../../utils/roles/can-create-community"; +import type { QueryParams } from "../../utils/types/query-params"; import { CommentNodes } from "../comment/comment-nodes"; import { DataTypeSelect } from "../common/data-type-select"; import { HtmlTags } from "../common/html-tags"; diff --git a/src/shared/components/home/login.tsx b/src/shared/components/home/login.tsx index 381c13b..d7e20ec 100644 --- a/src/shared/components/home/login.tsx +++ b/src/shared/components/home/login.tsx @@ -3,7 +3,8 @@ import { GetSiteResponse, LoginResponse } from "lemmy-js-client"; import { i18n } from "../../i18next"; import { UserService } from "../../services"; import { HttpService, RequestState } from "../../services/HttpService"; -import { isBrowser, myAuth, setIsoData, toast, validEmail } from "../../utils"; +import { myAuth, setIsoData, toast, validEmail } from "../../utils"; +import { isBrowser } from "../../utils/browser/is-browser"; import { HtmlTags } from "../common/html-tags"; import { Spinner } from "../common/icon"; diff --git a/src/shared/components/home/signup.tsx b/src/shared/components/home/signup.tsx index 16a3cc6..192393d 100644 --- a/src/shared/components/home/signup.tsx +++ b/src/shared/components/home/signup.tsx @@ -13,7 +13,6 @@ import { i18n } from "../../i18next"; import { UserService } from "../../services"; import { HttpService, RequestState } from "../../services/HttpService"; import { - isBrowser, joinLemmyUrl, mdToHtml, myAuth, @@ -21,6 +20,7 @@ import { toast, validEmail, } from "../../utils"; +import { isBrowser } from "../../utils/browser/is-browser"; import { HtmlTags } from "../common/html-tags"; import { Icon, Spinner } from "../common/icon"; import { MarkdownTextArea } from "../common/markdown-textarea"; diff --git a/src/shared/components/modlog.tsx b/src/shared/components/modlog.tsx index 4ab5ec5..cd0cfcb 100644 --- a/src/shared/components/modlog.tsx +++ b/src/shared/components/modlog.tsx @@ -33,21 +33,21 @@ import { FirstLoadService } from "../services/FirstLoadService"; import { HttpService, RequestState } from "../services/HttpService"; import { Choice, - QueryParams, debounce, fetchLimit, fetchUsers, getIdFromString, getPageFromString, - getQueryParams, - getQueryString, getUpdatedSearchId, myAuth, personToChoice, setIsoData, } from "../utils"; +import { getQueryParams } from "../utils/helpers/get-query-params"; +import { getQueryString } from "../utils/helpers/get-query-string"; import { amAdmin } from "../utils/roles/am-admin"; import { amMod } from "../utils/roles/am-mod"; +import type { QueryParams } from "../utils/types/query-params"; import { HtmlTags } from "./common/html-tags"; import { Icon, Spinner } from "./common/icon"; import { MomentTime } from "./common/moment-time"; diff --git a/src/shared/components/person/profile.tsx b/src/shared/components/person/profile.tsx index 4d9f8f7..c12114b 100644 --- a/src/shared/components/person/profile.tsx +++ b/src/shared/components/person/profile.tsx @@ -53,7 +53,6 @@ import { UserService } from "../../services"; import { FirstLoadService } from "../../services/FirstLoadService"; import { HttpService, RequestState } from "../../services/HttpService"; import { - QueryParams, capitalizeFirstLetter, editComment, editPost, @@ -64,8 +63,6 @@ import { futureDaysToUnixTime, getCommentParentId, getPageFromString, - getQueryParams, - getQueryString, mdToHtml, myAuth, myAuthRequired, @@ -78,9 +75,12 @@ import { toast, updatePersonBlock, } from "../../utils"; +import { getQueryParams } from "../../utils/helpers/get-query-params"; +import { getQueryString } from "../../utils/helpers/get-query-string"; import { canMod } from "../../utils/roles/can-mod"; import { isAdmin } from "../../utils/roles/is-admin"; import { isBanned } from "../../utils/roles/is-banned"; +import type { QueryParams } from "../../utils/types/query-params"; import { BannerIconHeader } from "../common/banner-icon-header"; import { HtmlTags } from "../common/html-tags"; import { Icon, Spinner } from "../common/icon"; diff --git a/src/shared/components/post/create-post.tsx b/src/shared/components/post/create-post.tsx index 71fac79..ed1df4c 100644 --- a/src/shared/components/post/create-post.tsx +++ b/src/shared/components/post/create-post.tsx @@ -16,14 +16,14 @@ import { } from "../../services/HttpService"; import { Choice, - QueryParams, enableDownvotes, enableNsfw, getIdFromString, - getQueryParams, myAuth, setIsoData, } from "../../utils"; +import { getQueryParams } from "../../utils/helpers/get-query-params"; +import type { QueryParams } from "../../utils/types/query-params"; import { HtmlTags } from "../common/html-tags"; import { Spinner } from "../common/icon"; import { PostForm } from "./post-form"; diff --git a/src/shared/components/post/post-listing.tsx b/src/shared/components/post/post-listing.tsx index 3ec1a45..4c36f65 100644 --- a/src/shared/components/post/post-listing.tsx +++ b/src/shared/components/post/post-listing.tsx @@ -28,7 +28,6 @@ import { i18n } from "../../i18next"; import { BanType, PostFormParams, PurgeType, VoteType } from "../../interfaces"; import { UserService } from "../../services"; import { - canShare, futureDaysToUnixTime, hostname, isImage, @@ -41,9 +40,10 @@ import { numToSI, relTags, setupTippy, - share, showScores, } from "../../utils"; +import { canShare } from "../../utils/browser/can-share"; +import { share } from "../../utils/browser/share"; import { amAdmin } from "../../utils/roles/am-admin"; import { amCommunityCreator } from "../../utils/roles/am-community-creator"; import { amMod } from "../../utils/roles/am-mod"; diff --git a/src/shared/components/post/post.tsx b/src/shared/components/post/post.tsx index 9c68532..250c08a 100644 --- a/src/shared/components/post/post.tsx +++ b/src/shared/components/post/post.tsx @@ -73,7 +73,6 @@ import { getCommentParentId, getDepthFromComment, getIdFromProps, - isBrowser, isImage, myAuth, restoreScrollPosition, @@ -84,6 +83,7 @@ import { updateCommunityBlock, updatePersonBlock, } from "../../utils"; +import { isBrowser } from "../../utils/browser/is-browser"; import { CommentForm } from "../comment/comment-form"; import { CommentNodes } from "../comment/comment-nodes"; import { HtmlTags } from "../common/html-tags"; diff --git a/src/shared/components/search.tsx b/src/shared/components/search.tsx index 8097dbd..59bbf61 100644 --- a/src/shared/components/search.tsx +++ b/src/shared/components/search.tsx @@ -26,7 +26,6 @@ import { FirstLoadService } from "../services/FirstLoadService"; import { HttpService, RequestState } from "../services/HttpService"; import { Choice, - QueryParams, capitalizeFirstLetter, commentsToFlatNodes, communityToChoice, @@ -38,8 +37,6 @@ import { fetchUsers, getIdFromString, getPageFromString, - getQueryParams, - getQueryString, getUpdatedSearchId, myAuth, numToSI, @@ -49,6 +46,9 @@ import { setIsoData, showLocal, } from "../utils"; +import { getQueryParams } from "../utils/helpers/get-query-params"; +import { getQueryString } from "../utils/helpers/get-query-string"; +import type { QueryParams } from "../utils/types/query-params"; import { CommentNodes } from "./comment/comment-nodes"; import { HtmlTags } from "./common/html-tags"; import { Spinner } from "./common/icon"; diff --git a/src/shared/env.ts b/src/shared/env.ts index 576c6c5..969f876 100644 --- a/src/shared/env.ts +++ b/src/shared/env.ts @@ -1,4 +1,4 @@ -import { isBrowser } from "./utils"; +import { isBrowser } from "./utils/browser/is-browser"; const testHost = "0.0.0.0:8536"; diff --git a/src/shared/services/UserService.ts b/src/shared/services/UserService.ts index 57c8aec..8f553ab 100644 --- a/src/shared/services/UserService.ts +++ b/src/shared/services/UserService.ts @@ -4,7 +4,8 @@ import jwt_decode from "jwt-decode"; import { LoginResponse, MyUserInfo } from "lemmy-js-client"; import { isHttps } from "../env"; import { i18n } from "../i18next"; -import { isAuthPath, isBrowser, toast } from "../utils"; +import { isAuthPath, toast } from "../utils"; +import { isBrowser } from "../utils/browser/is-browser"; interface Claims { sub: number; diff --git a/src/shared/utils.ts b/src/shared/utils.ts index b1b06e2..54b5a3f 100644 --- a/src/shared/utils.ts +++ b/src/shared/utils.ts @@ -43,6 +43,8 @@ import { getHttpBase } from "./env"; import { i18n, languages } from "./i18next"; import { CommentNodeI, DataType, IsoData, VoteType } from "./interfaces"; import { HttpService, UserService } from "./services"; +import { isBrowser } from "./utils/browser/is-browser"; +import { groupBy } from "./utils/helpers/group-by"; let Tribute: any; if (isBrowser()) { @@ -1070,10 +1072,6 @@ export function siteBannerCss(banner: string): string { `; } -export function isBrowser() { - return typeof window !== "undefined"; -} - export function setIsoData(context: any): IsoData { // If its the browser, you need to deserialize the data from the window if (isBrowser()) { @@ -1314,64 +1312,12 @@ interface EmojiMartSkin { src: string; } -const groupBy = <T>( - array: T[], - predicate: (value: T, index: number, array: T[]) => string -) => - array.reduce((acc, value, index, array) => { - (acc[predicate(value, index, array)] ||= []).push(value); - return acc; - }, {} as { [key: string]: T[] }); - -export type QueryParams<T extends Record<string, any>> = { - [key in keyof T]?: string; -}; - -export function getQueryParams<T extends Record<string, any>>(processors: { - [K in keyof T]: (param: string) => T[K]; -}): T { - if (isBrowser()) { - const searchParams = new URLSearchParams(window.location.search); - - return Array.from(Object.entries(processors)).reduce( - (acc, [key, process]) => ({ - ...acc, - [key]: process(searchParams.get(key)), - }), - {} as T - ); - } - - return {} as T; -} - -export function getQueryString<T extends Record<string, string | undefined>>( - obj: T -) { - return Object.entries(obj) - .filter(([, val]) => val !== undefined && val !== null) - .reduce( - (acc, [key, val], index) => `${acc}${index > 0 ? "&" : ""}${key}=${val}`, - "?" - ); -} - export function isAuthPath(pathname: string) { return /create_.*|inbox|settings|admin|reports|registration_applications/g.test( pathname ); } -export function canShare() { - return isBrowser() && !!navigator.canShare; -} - -export function share(shareData: ShareData) { - if (isBrowser()) { - navigator.share(shareData); - } -} - export function newVote(voteType: VoteType, myVote?: number): number { if (voteType == VoteType.Upvote) { return myVote == 1 ? 0 : 1; @@ -1379,18 +1325,3 @@ export function newVote(voteType: VoteType, myVote?: number): number { return myVote == -1 ? 0 : -1; } } - -function sleep(millis: number): Promise<void> { - return new Promise(resolve => setTimeout(resolve, millis)); -} - -/** - * Polls / repeatedly runs a promise, every X milliseconds - */ -export async function poll(promiseFn: any, millis: number) { - if (window.document.visibilityState !== "hidden") { - await promiseFn(); - } - await sleep(millis); - return poll(promiseFn, millis); -} diff --git a/src/shared/utils/browser/can-share.ts b/src/shared/utils/browser/can-share.ts new file mode 100644 index 0000000..bec7e80 --- /dev/null +++ b/src/shared/utils/browser/can-share.ts @@ -0,0 +1,5 @@ +import { isBrowser } from "./is-browser"; + +export function canShare() { + return isBrowser() && !!navigator.canShare; +} diff --git a/src/shared/utils/browser/is-browser.ts b/src/shared/utils/browser/is-browser.ts new file mode 100644 index 0000000..4139b25 --- /dev/null +++ b/src/shared/utils/browser/is-browser.ts @@ -0,0 +1,3 @@ +export function isBrowser() { + return typeof window !== "undefined"; +} diff --git a/src/shared/utils/browser/share.ts b/src/shared/utils/browser/share.ts new file mode 100644 index 0000000..b1d1b5b --- /dev/null +++ b/src/shared/utils/browser/share.ts @@ -0,0 +1,7 @@ +import { isBrowser } from "./is-browser"; + +export function share(shareData: ShareData) { + if (isBrowser()) { + navigator.share(shareData); + } +} diff --git a/src/shared/utils/helpers/get-query-params.ts b/src/shared/utils/helpers/get-query-params.ts new file mode 100644 index 0000000..213d352 --- /dev/null +++ b/src/shared/utils/helpers/get-query-params.ts @@ -0,0 +1,19 @@ +import { isBrowser } from "../browser/is-browser"; + +export function getQueryParams<T extends Record<string, any>>(processors: { + [K in keyof T]: (param: string) => T[K]; +}): T { + if (isBrowser()) { + const searchParams = new URLSearchParams(window.location.search); + + return Array.from(Object.entries(processors)).reduce( + (acc, [key, process]) => ({ + ...acc, + [key]: process(searchParams.get(key)), + }), + {} as T + ); + } + + return {} as T; +} diff --git a/src/shared/utils/helpers/get-query-string.ts b/src/shared/utils/helpers/get-query-string.ts new file mode 100644 index 0000000..a66b5af --- /dev/null +++ b/src/shared/utils/helpers/get-query-string.ts @@ -0,0 +1,10 @@ +export function getQueryString<T extends Record<string, string | undefined>>( + obj: T +) { + return Object.entries(obj) + .filter(([, val]) => val !== undefined && val !== null) + .reduce( + (acc, [key, val], index) => `${acc}${index > 0 ? "&" : ""}${key}=${val}`, + "?" + ); +} diff --git a/src/shared/utils/helpers/group-by.ts b/src/shared/utils/helpers/group-by.ts new file mode 100644 index 0000000..4dd5d5d --- /dev/null +++ b/src/shared/utils/helpers/group-by.ts @@ -0,0 +1,8 @@ +export const groupBy = <T>( + array: T[], + predicate: (value: T, index: number, array: T[]) => string +) => + array.reduce((acc, value, index, array) => { + (acc[predicate(value, index, array)] ||= []).push(value); + return acc; + }, {} as { [key: string]: T[] }); diff --git a/src/shared/utils/helpers/poll.ts b/src/shared/utils/helpers/poll.ts new file mode 100644 index 0000000..055f17f --- /dev/null +++ b/src/shared/utils/helpers/poll.ts @@ -0,0 +1,12 @@ +import { sleep } from "./sleep"; + +/** + * Polls / repeatedly runs a promise, every X milliseconds + */ +export async function poll(promiseFn: any, millis: number) { + if (window.document.visibilityState !== "hidden") { + await promiseFn(); + } + await sleep(millis); + return poll(promiseFn, millis); +} diff --git a/src/shared/utils/helpers/sleep.ts b/src/shared/utils/helpers/sleep.ts new file mode 100644 index 0000000..5b7c538 --- /dev/null +++ b/src/shared/utils/helpers/sleep.ts @@ -0,0 +1,3 @@ +export function sleep(millis: number): Promise<void> { + return new Promise(resolve => setTimeout(resolve, millis)); +} diff --git a/src/shared/utils/types/query-params.ts b/src/shared/utils/types/query-params.ts new file mode 100644 index 0000000..37705bd --- /dev/null +++ b/src/shared/utils/types/query-params.ts @@ -0,0 +1,3 @@ +export type QueryParams<T extends Record<string, any>> = { + [key in keyof T]?: string; +};