From 0854af3794f7fb2b7712c9b30ab1147cc18583a3 Mon Sep 17 00:00:00 2001 From: Alec Armbruster Date: Fri, 16 Jun 2023 17:07:55 -0400 Subject: [PATCH 01/19] break out all role utils --- src/client/index.tsx | 6 +- src/shared/components/app/navbar.tsx | 4 +- .../components/comment/comment-node.tsx | 12 +- src/shared/components/community/sidebar.tsx | 6 +- src/shared/components/home/home.tsx | 2 +- src/shared/components/modlog.tsx | 4 +- src/shared/components/person/profile.tsx | 6 +- src/shared/components/person/reports.tsx | 2 +- src/shared/components/post/post-listing.tsx | 16 +-- src/shared/utils.ts | 112 ------------------ src/shared/utils/roles/am-admin.ts | 5 + .../utils/roles/am-community-creator.ts | 12 ++ src/shared/utils/roles/am-mod.ts | 10 ++ src/shared/utils/roles/am-site-creator.ts | 11 ++ src/shared/utils/roles/am-top-mod.ts | 9 ++ src/shared/utils/roles/can-admin.ts | 12 ++ .../utils/roles/can-create-community.ts | 12 ++ src/shared/utils/roles/can-mod.ts | 31 +++++ src/shared/utils/roles/is-admin.ts | 5 + src/shared/utils/roles/is-banned.ts | 16 +++ src/shared/utils/roles/is-mod.ts | 8 ++ 21 files changed, 160 insertions(+), 141 deletions(-) create mode 100644 src/shared/utils/roles/am-admin.ts create mode 100644 src/shared/utils/roles/am-community-creator.ts create mode 100644 src/shared/utils/roles/am-mod.ts create mode 100644 src/shared/utils/roles/am-site-creator.ts create mode 100644 src/shared/utils/roles/am-top-mod.ts create mode 100644 src/shared/utils/roles/can-admin.ts create mode 100644 src/shared/utils/roles/can-create-community.ts create mode 100644 src/shared/utils/roles/can-mod.ts create mode 100644 src/shared/utils/roles/is-admin.ts create mode 100644 src/shared/utils/roles/is-banned.ts create mode 100644 src/shared/utils/roles/is-mod.ts diff --git a/src/client/index.tsx b/src/client/index.tsx index 7b6b6b1c..860c0756 100644 --- a/src/client/index.tsx +++ b/src/client/index.tsx @@ -1,14 +1,13 @@ import { hydrate } from "inferno-hydrate"; import { Router } from "inferno-router"; import { App } from "../shared/components/app/app"; +import { HistoryService } from "../shared/services/HistoryService"; import { initializeSite } from "../shared/utils"; import "bootstrap/js/dist/collapse"; import "bootstrap/js/dist/dropdown"; -import { HistoryService } from "../shared/services/HistoryService"; -const site = window.isoData.site_res; -initializeSite(site); +initializeSite(window.isoData.site_res); const wrapper = ( @@ -17,6 +16,7 @@ const wrapper = ( ); const root = document.getElementById("root"); + if (root) { hydrate(wrapper, root); } diff --git a/src/shared/components/app/navbar.tsx b/src/shared/components/app/navbar.tsx index 6d310eef..8c7c9202 100644 --- a/src/shared/components/app/navbar.tsx +++ b/src/shared/components/app/navbar.tsx @@ -10,8 +10,6 @@ import { i18n } from "../../i18next"; import { UserService } from "../../services"; import { HttpService, RequestState } from "../../services/HttpService"; import { - amAdmin, - canCreateCommunity, donateLemmyUrl, isBrowser, myAuth, @@ -21,6 +19,8 @@ import { toast, updateUnreadCountsInterval, } from "../../utils"; +import { amAdmin } from "../../utils/roles/am-admin"; +import { canCreateCommunity } from "../../utils/roles/can-create-community"; import { Icon } from "../common/icon"; import { PictrsImage } from "../common/pictrs-image"; diff --git a/src/shared/components/comment/comment-node.tsx b/src/shared/components/comment/comment-node.tsx index 0380a726..10b13c12 100644 --- a/src/shared/components/comment/comment-node.tsx +++ b/src/shared/components/comment/comment-node.tsx @@ -40,16 +40,10 @@ import { } from "../../interfaces"; import { UserService } from "../../services"; import { - amCommunityCreator, - canAdmin, - canMod, colorList, commentTreeMaxDepth, futureDaysToUnixTime, getCommentParentId, - isAdmin, - isBanned, - isMod, mdToHtml, mdToHtmlNoImages, myAuth, @@ -59,6 +53,12 @@ import { setupTippy, showScores, } from "../../utils"; +import { amCommunityCreator } from "../../utils/roles/am-community-creator"; +import { canAdmin } from "../../utils/roles/can-admin"; +import { canMod } from "../../utils/roles/can-mod"; +import { isAdmin } from "../../utils/roles/is-admin"; +import { isBanned } from "../../utils/roles/is-banned"; +import { isMod } from "../../utils/roles/is-mod"; import { Icon, PurgeWarning, Spinner } from "../common/icon"; import { MomentTime } from "../common/moment-time"; import { CommunityLink } from "../community/community-link"; diff --git a/src/shared/components/community/sidebar.tsx b/src/shared/components/community/sidebar.tsx index a5c620f3..2774747b 100644 --- a/src/shared/components/community/sidebar.tsx +++ b/src/shared/components/community/sidebar.tsx @@ -16,15 +16,15 @@ import { import { i18n } from "../../i18next"; import { UserService } from "../../services"; import { - amAdmin, - amMod, - amTopMod, getUnixTime, hostname, mdToHtml, myAuthRequired, numToSI, } from "../../utils"; +import { amAdmin } from "../../utils/roles/am-admin"; +import { amMod } from "../../utils/roles/am-mod"; +import { amTopMod } from "../../utils/roles/am-top-mod"; import { BannerIconHeader } from "../common/banner-icon-header"; import { Icon, PurgeWarning, Spinner } from "../common/icon"; import { CommunityForm } from "../community/community-form"; diff --git a/src/shared/components/home/home.tsx b/src/shared/components/home/home.tsx index 1abb1ee3..4e23c03c 100644 --- a/src/shared/components/home/home.tsx +++ b/src/shared/components/home/home.tsx @@ -57,7 +57,6 @@ import { UserService } from "../../services"; import { FirstLoadService } from "../../services/FirstLoadService"; import { HttpService, RequestState } from "../../services/HttpService"; import { - canCreateCommunity, commentsToFlatNodes, editComment, editPost, @@ -85,6 +84,7 @@ import { trendingFetchLimit, updatePersonBlock, } from "../../utils"; +import { canCreateCommunity } from "../../utils/roles/can-create-community"; 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/modlog.tsx b/src/shared/components/modlog.tsx index d917f5f3..4ab5ec56 100644 --- a/src/shared/components/modlog.tsx +++ b/src/shared/components/modlog.tsx @@ -34,8 +34,6 @@ import { HttpService, RequestState } from "../services/HttpService"; import { Choice, QueryParams, - amAdmin, - amMod, debounce, fetchLimit, fetchUsers, @@ -48,6 +46,8 @@ import { personToChoice, setIsoData, } from "../utils"; +import { amAdmin } from "../utils/roles/am-admin"; +import { amMod } from "../utils/roles/am-mod"; 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 f80d5b90..4d9f8f77 100644 --- a/src/shared/components/person/profile.tsx +++ b/src/shared/components/person/profile.tsx @@ -54,7 +54,6 @@ import { FirstLoadService } from "../../services/FirstLoadService"; import { HttpService, RequestState } from "../../services/HttpService"; import { QueryParams, - canMod, capitalizeFirstLetter, editComment, editPost, @@ -67,8 +66,6 @@ import { getPageFromString, getQueryParams, getQueryString, - isAdmin, - isBanned, mdToHtml, myAuth, myAuthRequired, @@ -81,6 +78,9 @@ import { toast, updatePersonBlock, } from "../../utils"; +import { canMod } from "../../utils/roles/can-mod"; +import { isAdmin } from "../../utils/roles/is-admin"; +import { isBanned } from "../../utils/roles/is-banned"; 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/person/reports.tsx b/src/shared/components/person/reports.tsx index 29daa3ff..187fe4c2 100644 --- a/src/shared/components/person/reports.tsx +++ b/src/shared/components/person/reports.tsx @@ -23,7 +23,6 @@ import { HttpService, UserService } from "../../services"; import { FirstLoadService } from "../../services/FirstLoadService"; import { RequestState } from "../../services/HttpService"; import { - amAdmin, editCommentReport, editPostReport, editPrivateMessageReport, @@ -31,6 +30,7 @@ import { myAuthRequired, setIsoData, } from "../../utils"; +import { amAdmin } from "../../utils/roles/am-admin"; import { CommentReport } from "../comment/comment-report"; import { HtmlTags } from "../common/html-tags"; import { Spinner } from "../common/icon"; diff --git a/src/shared/components/post/post-listing.tsx b/src/shared/components/post/post-listing.tsx index 60e188a3..3ec1a456 100644 --- a/src/shared/components/post/post-listing.tsx +++ b/src/shared/components/post/post-listing.tsx @@ -28,18 +28,10 @@ import { i18n } from "../../i18next"; import { BanType, PostFormParams, PurgeType, VoteType } from "../../interfaces"; import { UserService } from "../../services"; import { - amAdmin, - amCommunityCreator, - amMod, - canAdmin, - canMod, canShare, futureDaysToUnixTime, hostname, - isAdmin, - isBanned, isImage, - isMod, isVideo, mdNoImages, mdToHtml, @@ -52,6 +44,14 @@ import { share, showScores, } from "../../utils"; +import { amAdmin } from "../../utils/roles/am-admin"; +import { amCommunityCreator } from "../../utils/roles/am-community-creator"; +import { amMod } from "../../utils/roles/am-mod"; +import { canAdmin } from "../../utils/roles/can-admin"; +import { canMod } from "../../utils/roles/can-mod"; +import { isAdmin } from "../../utils/roles/is-admin"; +import { isBanned } from "../../utils/roles/is-banned"; +import { isMod } from "../../utils/roles/is-mod"; import { Icon, PurgeWarning, Spinner } from "../common/icon"; import { MomentTime } from "../common/moment-time"; import { PictrsImage } from "../common/pictrs-image"; diff --git a/src/shared/utils.ts b/src/shared/utils.ts index 4a3b298a..b1b06e2e 100644 --- a/src/shared/utils.ts +++ b/src/shared/utils.ts @@ -9,7 +9,6 @@ import { CommentReportView, CommentSortType, CommentView, - CommunityModeratorView, CommunityView, CustomEmojiView, GetSiteMetadata, @@ -17,7 +16,6 @@ import { Language, LemmyHttp, MyUserInfo, - Person, PersonMentionView, PersonView, PostReportView, @@ -228,92 +226,6 @@ export function futureDaysToUnixTime(days?: number): number | undefined { : undefined; } -export function canMod( - creator_id: number, - mods?: CommunityModeratorView[], - admins?: PersonView[], - myUserInfo = UserService.Instance.myUserInfo, - onSelf = false -): boolean { - // You can do moderator actions only on the mods added after you. - let adminsThenMods = - admins - ?.map(a => a.person.id) - .concat(mods?.map(m => m.moderator.id) ?? []) ?? []; - - if (myUserInfo) { - const myIndex = adminsThenMods.findIndex( - id => id == myUserInfo.local_user_view.person.id - ); - if (myIndex == -1) { - return false; - } else { - // onSelf +1 on mod actions not for yourself, IE ban, remove, etc - adminsThenMods = adminsThenMods.slice(0, myIndex + (onSelf ? 0 : 1)); - return !adminsThenMods.includes(creator_id); - } - } else { - return false; - } -} - -export function canAdmin( - creatorId: number, - admins?: PersonView[], - myUserInfo = UserService.Instance.myUserInfo, - onSelf = false -): boolean { - return canMod(creatorId, undefined, admins, myUserInfo, onSelf); -} - -export function isMod( - creatorId: number, - mods?: CommunityModeratorView[] -): boolean { - return mods?.map(m => m.moderator.id).includes(creatorId) ?? false; -} - -export function amMod( - mods?: CommunityModeratorView[], - myUserInfo = UserService.Instance.myUserInfo -): boolean { - return myUserInfo ? isMod(myUserInfo.local_user_view.person.id, mods) : false; -} - -export function isAdmin(creatorId: number, admins?: PersonView[]): boolean { - return admins?.map(a => a.person.id).includes(creatorId) ?? false; -} - -export function amAdmin(myUserInfo = UserService.Instance.myUserInfo): boolean { - return myUserInfo?.local_user_view.person.admin ?? false; -} - -export function amCommunityCreator( - creator_id: number, - mods?: CommunityModeratorView[], - myUserInfo = UserService.Instance.myUserInfo -): boolean { - const myId = myUserInfo?.local_user_view.person.id; - // Don't allow mod actions on yourself - return myId == mods?.at(0)?.moderator.id && myId != creator_id; -} - -export function amSiteCreator( - creator_id: number, - admins?: PersonView[], - myUserInfo = UserService.Instance.myUserInfo -): boolean { - const myId = myUserInfo?.local_user_view.person.id; - return myId == admins?.at(0)?.person.id && myId != creator_id; -} - -export function amTopMod( - mods: CommunityModeratorView[], - myUserInfo = UserService.Instance.myUserInfo -): boolean { - return mods.at(0)?.moderator.id == myUserInfo?.local_user_view.person.id; -} - const imageRegex = /(http)?s?:?(\/\/[^"']*\.(?:jpg|jpeg|gif|png|svg|webp))/; const videoRegex = /(http)?s?:?(\/\/[^"']*\.(?:mp4|webm))/; @@ -1291,21 +1203,6 @@ export function numToSI(value: number): string { return SHORTNUM_SI_FORMAT.format(value); } -export function isBanned(ps: Person): boolean { - const expires = ps.ban_expires; - // Add Z to convert from UTC date - // TODO this check probably isn't necessary anymore - if (expires) { - if (ps.banned && new Date(expires + "Z") > new Date()) { - return true; - } else { - return false; - } - } else { - return ps.banned; - } -} - export function myAuth(): string | undefined { return UserService.Instance.auth(); } @@ -1337,15 +1234,6 @@ export function postToCommentSortType(sort: SortType): CommentSortType { } } -export function canCreateCommunity( - siteRes: GetSiteResponse, - myUserInfo = UserService.Instance.myUserInfo -): boolean { - const adminOnly = siteRes.site_view.local_site.community_creation_admin_only; - // TODO: Make this check if user is logged on as well - return !adminOnly || amAdmin(myUserInfo); -} - export function isPostBlocked( pv: PostView, myUserInfo: MyUserInfo | undefined = UserService.Instance.myUserInfo diff --git a/src/shared/utils/roles/am-admin.ts b/src/shared/utils/roles/am-admin.ts new file mode 100644 index 00000000..aadf52ce --- /dev/null +++ b/src/shared/utils/roles/am-admin.ts @@ -0,0 +1,5 @@ +import { UserService } from "../../services"; + +export function amAdmin(myUserInfo = UserService.Instance.myUserInfo): boolean { + return myUserInfo?.local_user_view.person.admin ?? false; +} diff --git a/src/shared/utils/roles/am-community-creator.ts b/src/shared/utils/roles/am-community-creator.ts new file mode 100644 index 00000000..20f9b1dd --- /dev/null +++ b/src/shared/utils/roles/am-community-creator.ts @@ -0,0 +1,12 @@ +import { CommunityModeratorView } from "lemmy-js-client"; +import { UserService } from "../../services"; + +export function amCommunityCreator( + creator_id: number, + mods?: CommunityModeratorView[], + myUserInfo = UserService.Instance.myUserInfo +): boolean { + const myId = myUserInfo?.local_user_view.person.id; + // Don't allow mod actions on yourself + return myId == mods?.at(0)?.moderator.id && myId != creator_id; +} diff --git a/src/shared/utils/roles/am-mod.ts b/src/shared/utils/roles/am-mod.ts new file mode 100644 index 00000000..7b792b39 --- /dev/null +++ b/src/shared/utils/roles/am-mod.ts @@ -0,0 +1,10 @@ +import { CommunityModeratorView } from "lemmy-js-client"; +import { UserService } from "../../services"; +import { isMod } from "./is-mod"; + +export function amMod( + mods?: CommunityModeratorView[], + myUserInfo = UserService.Instance.myUserInfo +): boolean { + return myUserInfo ? isMod(myUserInfo.local_user_view.person.id, mods) : false; +} diff --git a/src/shared/utils/roles/am-site-creator.ts b/src/shared/utils/roles/am-site-creator.ts new file mode 100644 index 00000000..323ac0a4 --- /dev/null +++ b/src/shared/utils/roles/am-site-creator.ts @@ -0,0 +1,11 @@ +import { PersonView } from "lemmy-js-client"; +import { UserService } from "../../services"; + +export function amSiteCreator( + creator_id: number, + admins?: PersonView[], + myUserInfo = UserService.Instance.myUserInfo +): boolean { + const myId = myUserInfo?.local_user_view.person.id; + return myId == admins?.at(0)?.person.id && myId != creator_id; +} diff --git a/src/shared/utils/roles/am-top-mod.ts b/src/shared/utils/roles/am-top-mod.ts new file mode 100644 index 00000000..4b942da7 --- /dev/null +++ b/src/shared/utils/roles/am-top-mod.ts @@ -0,0 +1,9 @@ +import { CommunityModeratorView } from "lemmy-js-client"; +import { UserService } from "../../services"; + +export function amTopMod( + mods: CommunityModeratorView[], + myUserInfo = UserService.Instance.myUserInfo +): boolean { + return mods.at(0)?.moderator.id == myUserInfo?.local_user_view.person.id; +} diff --git a/src/shared/utils/roles/can-admin.ts b/src/shared/utils/roles/can-admin.ts new file mode 100644 index 00000000..080c7acc --- /dev/null +++ b/src/shared/utils/roles/can-admin.ts @@ -0,0 +1,12 @@ +import { PersonView } from "lemmy-js-client"; +import { UserService } from "../../services"; +import { canMod } from "./can-mod"; + +export function canAdmin( + creatorId: number, + admins?: PersonView[], + myUserInfo = UserService.Instance.myUserInfo, + onSelf = false +): boolean { + return canMod(creatorId, undefined, admins, myUserInfo, onSelf); +} diff --git a/src/shared/utils/roles/can-create-community.ts b/src/shared/utils/roles/can-create-community.ts new file mode 100644 index 00000000..202290d2 --- /dev/null +++ b/src/shared/utils/roles/can-create-community.ts @@ -0,0 +1,12 @@ +import { GetSiteResponse } from "lemmy-js-client"; +import { UserService } from "../../services"; +import { amAdmin } from "./am-admin"; + +export function canCreateCommunity( + siteRes: GetSiteResponse, + myUserInfo = UserService.Instance.myUserInfo +): boolean { + const adminOnly = siteRes.site_view.local_site.community_creation_admin_only; + // TODO: Make this check if user is logged on as well + return !adminOnly || amAdmin(myUserInfo); +} diff --git a/src/shared/utils/roles/can-mod.ts b/src/shared/utils/roles/can-mod.ts new file mode 100644 index 00000000..2892304d --- /dev/null +++ b/src/shared/utils/roles/can-mod.ts @@ -0,0 +1,31 @@ +import { CommunityModeratorView, PersonView } from "lemmy-js-client"; +import { UserService } from "../../services"; + +export function canMod( + creator_id: number, + mods?: CommunityModeratorView[], + admins?: PersonView[], + myUserInfo = UserService.Instance.myUserInfo, + onSelf = false +): boolean { + // You can do moderator actions only on the mods added after you. + let adminsThenMods = + admins + ?.map(a => a.person.id) + .concat(mods?.map(m => m.moderator.id) ?? []) ?? []; + + if (myUserInfo) { + const myIndex = adminsThenMods.findIndex( + id => id == myUserInfo.local_user_view.person.id + ); + if (myIndex == -1) { + return false; + } else { + // onSelf +1 on mod actions not for yourself, IE ban, remove, etc + adminsThenMods = adminsThenMods.slice(0, myIndex + (onSelf ? 0 : 1)); + return !adminsThenMods.includes(creator_id); + } + } else { + return false; + } +} diff --git a/src/shared/utils/roles/is-admin.ts b/src/shared/utils/roles/is-admin.ts new file mode 100644 index 00000000..fbf662b8 --- /dev/null +++ b/src/shared/utils/roles/is-admin.ts @@ -0,0 +1,5 @@ +import { PersonView } from "lemmy-js-client"; + +export function isAdmin(creatorId: number, admins?: PersonView[]): boolean { + return admins?.map(a => a.person.id).includes(creatorId) ?? false; +} diff --git a/src/shared/utils/roles/is-banned.ts b/src/shared/utils/roles/is-banned.ts new file mode 100644 index 00000000..dd5ffe6c --- /dev/null +++ b/src/shared/utils/roles/is-banned.ts @@ -0,0 +1,16 @@ +import { Person } from "lemmy-js-client"; + +export function isBanned(ps: Person): boolean { + const expires = ps.ban_expires; + // Add Z to convert from UTC date + // TODO this check probably isn't necessary anymore + if (expires) { + if (ps.banned && new Date(expires + "Z") > new Date()) { + return true; + } else { + return false; + } + } else { + return ps.banned; + } +} diff --git a/src/shared/utils/roles/is-mod.ts b/src/shared/utils/roles/is-mod.ts new file mode 100644 index 00000000..87311072 --- /dev/null +++ b/src/shared/utils/roles/is-mod.ts @@ -0,0 +1,8 @@ +import { CommunityModeratorView } from "lemmy-js-client"; + +export function isMod( + creatorId: number, + mods?: CommunityModeratorView[] +): boolean { + return mods?.map(m => m.moderator.id).includes(creatorId) ?? false; +} From 976ed12d077e89298b6fc574d59f23455c211a15 Mon Sep 17 00:00:00 2001 From: Alec Armbruster Date: Fri, 16 Jun 2023 17:25:53 -0400 Subject: [PATCH 02/19] break out browser and helper methods --- src/shared/components/app/navbar.tsx | 4 +- .../components/common/markdown-textarea.tsx | 3 +- .../components/community/communities.tsx | 6 +- src/shared/components/community/community.tsx | 7 +- src/shared/components/home/home.tsx | 6 +- src/shared/components/home/login.tsx | 3 +- src/shared/components/home/signup.tsx | 2 +- src/shared/components/modlog.tsx | 6 +- src/shared/components/person/profile.tsx | 6 +- src/shared/components/post/create-post.tsx | 4 +- src/shared/components/post/post-listing.tsx | 4 +- src/shared/components/post/post.tsx | 2 +- src/shared/components/search.tsx | 6 +- src/shared/env.ts | 2 +- src/shared/services/UserService.ts | 3 +- src/shared/utils.ts | 73 +------------------ src/shared/utils/browser/can-share.ts | 5 ++ src/shared/utils/browser/is-browser.ts | 3 + src/shared/utils/browser/share.ts | 7 ++ src/shared/utils/helpers/get-query-params.ts | 19 +++++ src/shared/utils/helpers/get-query-string.ts | 10 +++ src/shared/utils/helpers/group-by.ts | 8 ++ src/shared/utils/helpers/poll.ts | 12 +++ src/shared/utils/helpers/sleep.ts | 3 + src/shared/utils/types/query-params.ts | 3 + 25 files changed, 104 insertions(+), 103 deletions(-) create mode 100644 src/shared/utils/browser/can-share.ts create mode 100644 src/shared/utils/browser/is-browser.ts create mode 100644 src/shared/utils/browser/share.ts create mode 100644 src/shared/utils/helpers/get-query-params.ts create mode 100644 src/shared/utils/helpers/get-query-string.ts create mode 100644 src/shared/utils/helpers/group-by.ts create mode 100644 src/shared/utils/helpers/poll.ts create mode 100644 src/shared/utils/helpers/sleep.ts create mode 100644 src/shared/utils/types/query-params.ts diff --git a/src/shared/components/app/navbar.tsx b/src/shared/components/app/navbar.tsx index 8c7c9202..20d4f251 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 9318d3bb..c1e85243 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 62326943..b98bf251 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 7dc150f3..cb7e9517 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; postsRes: RequestState; diff --git a/src/shared/components/home/home.tsx b/src/shared/components/home/home.tsx index 4e23c03c..ed3afd01 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 381c13bb..d7e20ec7 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 16a3cc6d..192393db 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 4ab5ec56..cd0cfcb9 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 4d9f8f77..c12114bc 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 71fac79a..ed1df4c1 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 3ec1a456..4c36f651 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 9c68532b..250c08a7 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 8097dbde..59bbf616 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 576c6c58..969f8761 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 57c8aecf..8f553aba 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 b1b06e2e..54b5a3f4 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 = ( - 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> = { - [key in keyof T]?: string; -}; - -export function getQueryParams>(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>( - 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 { - 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 00000000..bec7e805 --- /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 00000000..4139b25d --- /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 00000000..b1d1b5be --- /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 00000000..213d3521 --- /dev/null +++ b/src/shared/utils/helpers/get-query-params.ts @@ -0,0 +1,19 @@ +import { isBrowser } from "../browser/is-browser"; + +export function getQueryParams>(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 00000000..a66b5af4 --- /dev/null +++ b/src/shared/utils/helpers/get-query-string.ts @@ -0,0 +1,10 @@ +export function getQueryString>( + 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 00000000..4dd5d5db --- /dev/null +++ b/src/shared/utils/helpers/group-by.ts @@ -0,0 +1,8 @@ +export const groupBy = ( + 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 00000000..055f17f4 --- /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 00000000..5b7c5388 --- /dev/null +++ b/src/shared/utils/helpers/sleep.ts @@ -0,0 +1,3 @@ +export function sleep(millis: number): Promise { + 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 00000000..37705bd8 --- /dev/null +++ b/src/shared/utils/types/query-params.ts @@ -0,0 +1,3 @@ +export type QueryParams> = { + [key in keyof T]?: string; +}; From 6126900bd54a302c871cc52f332a725fe159d04b Mon Sep 17 00:00:00 2001 From: Alec Armbruster Date: Fri, 16 Jun 2023 18:49:28 -0400 Subject: [PATCH 03/19] forgot debounce --- src/shared/components/modlog.tsx | 2 +- src/shared/components/person/settings.tsx | 2 +- src/shared/components/post/post-form.tsx | 2 +- src/shared/components/post/post.tsx | 2 +- src/shared/components/search.tsx | 2 +- src/shared/utils.ts | 46 +---------------------- src/shared/utils/helpers/debounce.ts | 44 ++++++++++++++++++++++ 7 files changed, 50 insertions(+), 50 deletions(-) create mode 100644 src/shared/utils/helpers/debounce.ts diff --git a/src/shared/components/modlog.tsx b/src/shared/components/modlog.tsx index cd0cfcb9..99f15e50 100644 --- a/src/shared/components/modlog.tsx +++ b/src/shared/components/modlog.tsx @@ -33,7 +33,6 @@ import { FirstLoadService } from "../services/FirstLoadService"; import { HttpService, RequestState } from "../services/HttpService"; import { Choice, - debounce, fetchLimit, fetchUsers, getIdFromString, @@ -43,6 +42,7 @@ import { personToChoice, setIsoData, } from "../utils"; +import { debounce } from "../utils/helpers/debounce"; import { getQueryParams } from "../utils/helpers/get-query-params"; import { getQueryString } from "../utils/helpers/get-query-string"; import { amAdmin } from "../utils/roles/am-admin"; diff --git a/src/shared/components/person/settings.tsx b/src/shared/components/person/settings.tsx index a29f61b0..564daff4 100644 --- a/src/shared/components/person/settings.tsx +++ b/src/shared/components/person/settings.tsx @@ -18,7 +18,6 @@ import { Choice, capitalizeFirstLetter, communityToChoice, - debounce, elementUrl, emDash, enableNsfw, @@ -38,6 +37,7 @@ import { updateCommunityBlock, updatePersonBlock, } from "../../utils"; +import { debounce } from "../../utils/helpers/debounce"; import { HtmlTags } from "../common/html-tags"; import { Icon, Spinner } from "../common/icon"; import { ImageUploadForm } from "../common/image-upload-form"; diff --git a/src/shared/components/post/post-form.tsx b/src/shared/components/post/post-form.tsx index 4640922d..c21a6e2b 100644 --- a/src/shared/components/post/post-form.tsx +++ b/src/shared/components/post/post-form.tsx @@ -18,7 +18,6 @@ import { archiveTodayUrl, capitalizeFirstLetter, communityToChoice, - debounce, fetchCommunities, getIdFromString, ghostArchiveUrl, @@ -33,6 +32,7 @@ import { validURL, webArchiveUrl, } from "../../utils"; +import { debounce } from "../../utils/helpers/debounce"; import { Icon, Spinner } from "../common/icon"; import { LanguageSelect } from "../common/language-select"; import { MarkdownTextArea } from "../common/markdown-textarea"; diff --git a/src/shared/components/post/post.tsx b/src/shared/components/post/post.tsx index 250c08a7..2c61f79e 100644 --- a/src/shared/components/post/post.tsx +++ b/src/shared/components/post/post.tsx @@ -64,7 +64,6 @@ import { buildCommentsTree, commentsToFlatNodes, commentTreeMaxDepth, - debounce, editComment, editWith, enableDownvotes, @@ -84,6 +83,7 @@ import { updatePersonBlock, } from "../../utils"; import { isBrowser } from "../../utils/browser/is-browser"; +import { debounce } from "../../utils/helpers/debounce"; 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 59bbf616..d32e4087 100644 --- a/src/shared/components/search.tsx +++ b/src/shared/components/search.tsx @@ -29,7 +29,6 @@ import { capitalizeFirstLetter, commentsToFlatNodes, communityToChoice, - debounce, enableDownvotes, enableNsfw, fetchCommunities, @@ -46,6 +45,7 @@ import { setIsoData, showLocal, } from "../utils"; +import { debounce } from "../utils/helpers/debounce"; import { getQueryParams } from "../utils/helpers/get-query-params"; import { getQueryString } from "../utils/helpers/get-query-string"; import type { QueryParams } from "../utils/types/query-params"; diff --git a/src/shared/utils.ts b/src/shared/utils.ts index 54b5a3f4..c0caad88 100644 --- a/src/shared/utils.ts +++ b/src/shared/utils.ts @@ -44,6 +44,7 @@ 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 { debounce } from "./utils/helpers/debounce"; import { groupBy } from "./utils/helpers/group-by"; let Tribute: any; @@ -268,51 +269,6 @@ export function getDataTypeString(dt: DataType) { return dt === DataType.Post ? "Post" : "Comment"; } -export function debounce( - func: (...e: T) => R, - wait = 1000, - immediate = false -) { - // 'private' variable for instance - // The returned function will be able to reference this due to closure. - // Each call to the returned function will share this common timer. - let timeout: NodeJS.Timeout | null; - - // Calling debounce returns a new anonymous function - return function () { - // reference the context and args for the setTimeout function - const args = arguments; - - // Should the function be called now? If immediate is true - // and not already in a timeout then the answer is: Yes - const callNow = immediate && !timeout; - - // This is the basic debounce behavior where you can call this - // function several times, but it will only execute once - // [before or after imposing a delay]. - // Each time the returned function is called, the timer starts over. - clearTimeout(timeout ?? undefined); - - // Set the new timeout - timeout = setTimeout(function () { - // Inside the timeout function, clear the timeout variable - // which will let the next execution run when in 'immediate' mode - timeout = null; - - // Check if the function already ran with the immediate flag - if (!immediate) { - // Call the original function with apply - // apply lets you define the 'this' object as well as the arguments - // (both captured before setTimeout) - func.apply(this, args); - } - }, wait); - - // Immediate mode and no wait timer? Execute the function.. - if (callNow) func.apply(this, args); - } as (...e: T) => R; -} - export function getLanguages( override?: string, myUserInfo = UserService.Instance.myUserInfo diff --git a/src/shared/utils/helpers/debounce.ts b/src/shared/utils/helpers/debounce.ts new file mode 100644 index 00000000..7a1e8b19 --- /dev/null +++ b/src/shared/utils/helpers/debounce.ts @@ -0,0 +1,44 @@ +export function debounce( + func: (...e: T) => R, + wait = 1000, + immediate = false +) { + // 'private' variable for instance + // The returned function will be able to reference this due to closure. + // Each call to the returned function will share this common timer. + let timeout: NodeJS.Timeout | null; + + // Calling debounce returns a new anonymous function + return function () { + // reference the context and args for the setTimeout function + const args = arguments; + + // Should the function be called now? If immediate is true + // and not already in a timeout then the answer is: Yes + const callNow = immediate && !timeout; + + // This is the basic debounce behavior where you can call this + // function several times, but it will only execute once + // [before or after imposing a delay]. + // Each time the returned function is called, the timer starts over. + clearTimeout(timeout ?? undefined); + + // Set the new timeout + timeout = setTimeout(function () { + // Inside the timeout function, clear the timeout variable + // which will let the next execution run when in 'immediate' mode + timeout = null; + + // Check if the function already ran with the immediate flag + if (!immediate) { + // Call the original function with apply + // apply lets you define the 'this' object as well as the arguments + // (both captured before setTimeout) + func.apply(this, args); + } + }, wait); + + // Immediate mode and no wait timer? Execute the function.. + if (callNow) func.apply(this, args); + } as (...e: T) => R; +} From 6c6ddd5b5112afcab1641d49b2462083142bf265 Mon Sep 17 00:00:00 2001 From: Alec Armbruster Date: Fri, 16 Jun 2023 18:56:23 -0400 Subject: [PATCH 04/19] reset, merge issues --- .github/CODEOWNERS | 2 +- .github/ISSUE_TEMPLATE/BUG_REPORT.yml | 2 +- .github/ISSUE_TEMPLATE/FEATURE_REQUEST.yml | 2 +- Dockerfile | 2 +- dev.dockerfile | 2 +- package.json | 2 +- src/assets/css/main.css | 24 +++++ src/shared/components/common/html-tags.tsx | 6 +- .../components/common/markdown-textarea.tsx | 98 +++++++++---------- src/shared/components/common/moment-time.tsx | 6 +- src/shared/components/modlog.tsx | 2 +- src/shared/components/person/settings.tsx | 2 +- src/shared/components/post/post-form.tsx | 2 +- src/shared/components/post/post.tsx | 2 +- src/shared/components/search.tsx | 2 +- src/shared/i18next.ts | 26 +---- src/shared/utils.ts | 75 +++++--------- src/shared/utils/helpers/debounce.ts | 44 +++++++++ yarn.lock | 8 +- 19 files changed, 164 insertions(+), 145 deletions(-) create mode 100644 src/shared/utils/helpers/debounce.ts diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 76916e60..6df17d57 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1 +1 @@ -* @dessalines @SleeplessOne1917 @alectrocute +* @dessalines @SleeplessOne1917 diff --git a/.github/ISSUE_TEMPLATE/BUG_REPORT.yml b/.github/ISSUE_TEMPLATE/BUG_REPORT.yml index ae2d4e51..2273a138 100644 --- a/.github/ISSUE_TEMPLATE/BUG_REPORT.yml +++ b/.github/ISSUE_TEMPLATE/BUG_REPORT.yml @@ -21,7 +21,7 @@ body: - label: Is this only a single bug? Do not put multiple bugs in one issue. required: true - label: Is this a server side (not related to the UI) issue? Use the [Lemmy back end](https://github.com/LemmyNet/lemmy) repo. - required: false + required: true - type: textarea id: summary attributes: diff --git a/.github/ISSUE_TEMPLATE/FEATURE_REQUEST.yml b/.github/ISSUE_TEMPLATE/FEATURE_REQUEST.yml index 3c75050a..2f6f3fc1 100644 --- a/.github/ISSUE_TEMPLATE/FEATURE_REQUEST.yml +++ b/.github/ISSUE_TEMPLATE/FEATURE_REQUEST.yml @@ -19,7 +19,7 @@ body: - label: Is this only a feature request? Do not put multiple feature requests in one issue. required: true - label: Is this a server side (not related to the UI) issue? Use the [Lemmy back end](https://github.com/LemmyNet/lemmy) repo. - required: false + required: true - type: textarea id: problem attributes: diff --git a/Dockerfile b/Dockerfile index 2b36581d..3d6d6212 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM node:20.2-alpine as builder +FROM node:alpine as builder RUN apk update && apk add curl yarn python3 build-base gcc wget git --no-cache RUN curl -sf https://gobinaries.com/tj/node-prune | sh diff --git a/dev.dockerfile b/dev.dockerfile index 3bfc10da..0e925c0a 100644 --- a/dev.dockerfile +++ b/dev.dockerfile @@ -1,4 +1,4 @@ -FROM node:20.2-alpine as builder +FROM node:alpine as builder RUN apk update && apk add curl yarn python3 build-base gcc wget git --no-cache WORKDIR /usr/src/app diff --git a/package.json b/package.json index b7c48c79..2298d9e1 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "inferno-server": "^8.1.1", "isomorphic-cookie": "^1.2.4", "jwt-decode": "^3.1.2", - "lemmy-js-client": "0.18.0-rc.1", + "lemmy-js-client": "0.17.2-rc.24", "lodash": "^4.17.21", "markdown-it": "^13.0.1", "markdown-it-container": "^3.0.0", diff --git a/src/assets/css/main.css b/src/assets/css/main.css index 82f8433e..e1adfc53 100644 --- a/src/assets/css/main.css +++ b/src/assets/css/main.css @@ -80,6 +80,30 @@ overflow-x: auto; } +.md-div table { + border-collapse: collapse; + width: 100%; + margin-bottom: 1rem; + border: 1px solid var(--dark); +} + +.md-div table th, +.md-div table td { + padding: 0.3rem; + vertical-align: top; + border-top: 1px solid var(--dark); + border: 1px solid var(--dark); +} + +.md-div table thead th { + vertical-align: bottom; + border-bottom: 2px solid var(--dark); +} + +.md-div table tbody + tbody { + border-top: 2px solid var(--dark); +} + .vote-bar { margin-top: -6.5px; } diff --git a/src/shared/components/common/html-tags.tsx b/src/shared/components/common/html-tags.tsx index f32b0fc0..0e6cb2d0 100644 --- a/src/shared/components/common/html-tags.tsx +++ b/src/shared/components/common/html-tags.tsx @@ -2,8 +2,7 @@ import { htmlToText } from "html-to-text"; import { Component } from "inferno"; import { Helmet } from "inferno-helmet"; import { httpExternalPath } from "../../env"; -import { i18n } from "../../i18next"; -import { md } from "../../utils"; +import { getLanguages, md } from "../../utils"; interface HtmlTagsProps { title: string; @@ -18,10 +17,11 @@ export class HtmlTags extends Component { const url = httpExternalPath(this.props.path); const desc = this.props.description; const image = this.props.image; + const lang = getLanguages()[0]; return ( - + {["title", "og:title", "twitter:title"].map(t => ( diff --git a/src/shared/components/common/markdown-textarea.tsx b/src/shared/components/common/markdown-textarea.tsx index 4e1bca11..c1e85243 100644 --- a/src/shared/components/common/markdown-textarea.tsx +++ b/src/shared/components/common/markdown-textarea.tsx @@ -183,6 +183,53 @@ export class MarkdownTextArea extends Component<
+ {this.props.buttonTitle && ( + + )} + {this.props.replyType && ( + + )} + {this.state.content && ( + + )} + {/* A flex expander */} +
+ + {this.props.showLanguage && ( + + )} {this.getFormatButton("bold", this.handleInsertBold)} {this.getFormatButton("italic", this.handleInsertItalic)} {this.getFormatButton("link", this.handleInsertLink)} @@ -235,57 +282,6 @@ export class MarkdownTextArea extends Component<
- -
- {this.props.showLanguage && ( - - )} - - {/* A flex expander */} -
- - {this.props.buttonTitle && ( - - )} - {this.props.replyType && ( - - )} - {this.state.content && ( - - )} -
); diff --git a/src/shared/components/common/moment-time.tsx b/src/shared/components/common/moment-time.tsx index 30c1682c..10714f5b 100644 --- a/src/shared/components/common/moment-time.tsx +++ b/src/shared/components/common/moment-time.tsx @@ -1,7 +1,7 @@ import { Component } from "inferno"; import moment from "moment"; import { i18n } from "../../i18next"; -import { capitalizeFirstLetter } from "../../utils"; +import { capitalizeFirstLetter, getLanguages } from "../../utils"; import { Icon } from "./icon"; interface MomentTimeProps { @@ -15,7 +15,9 @@ export class MomentTime extends Component { constructor(props: any, context: any) { super(props, context); - moment.locale([...i18n.languages]); + const lang = getLanguages(); + + moment.locale(lang); } createdAndModifiedTimes() { diff --git a/src/shared/components/modlog.tsx b/src/shared/components/modlog.tsx index cd0cfcb9..99f15e50 100644 --- a/src/shared/components/modlog.tsx +++ b/src/shared/components/modlog.tsx @@ -33,7 +33,6 @@ import { FirstLoadService } from "../services/FirstLoadService"; import { HttpService, RequestState } from "../services/HttpService"; import { Choice, - debounce, fetchLimit, fetchUsers, getIdFromString, @@ -43,6 +42,7 @@ import { personToChoice, setIsoData, } from "../utils"; +import { debounce } from "../utils/helpers/debounce"; import { getQueryParams } from "../utils/helpers/get-query-params"; import { getQueryString } from "../utils/helpers/get-query-string"; import { amAdmin } from "../utils/roles/am-admin"; diff --git a/src/shared/components/person/settings.tsx b/src/shared/components/person/settings.tsx index 56d57a7a..3c8e9fcd 100644 --- a/src/shared/components/person/settings.tsx +++ b/src/shared/components/person/settings.tsx @@ -18,7 +18,6 @@ import { Choice, capitalizeFirstLetter, communityToChoice, - debounce, elementUrl, emDash, enableNsfw, @@ -37,6 +36,7 @@ import { updateCommunityBlock, updatePersonBlock, } from "../../utils"; +import { debounce } from "../../utils/helpers/debounce"; import { HtmlTags } from "../common/html-tags"; import { Icon, Spinner } from "../common/icon"; import { ImageUploadForm } from "../common/image-upload-form"; diff --git a/src/shared/components/post/post-form.tsx b/src/shared/components/post/post-form.tsx index 4640922d..c21a6e2b 100644 --- a/src/shared/components/post/post-form.tsx +++ b/src/shared/components/post/post-form.tsx @@ -18,7 +18,6 @@ import { archiveTodayUrl, capitalizeFirstLetter, communityToChoice, - debounce, fetchCommunities, getIdFromString, ghostArchiveUrl, @@ -33,6 +32,7 @@ import { validURL, webArchiveUrl, } from "../../utils"; +import { debounce } from "../../utils/helpers/debounce"; import { Icon, Spinner } from "../common/icon"; import { LanguageSelect } from "../common/language-select"; import { MarkdownTextArea } from "../common/markdown-textarea"; diff --git a/src/shared/components/post/post.tsx b/src/shared/components/post/post.tsx index 05e4d9b9..a471e649 100644 --- a/src/shared/components/post/post.tsx +++ b/src/shared/components/post/post.tsx @@ -64,7 +64,6 @@ import { buildCommentsTree, commentsToFlatNodes, commentTreeMaxDepth, - debounce, editComment, editWith, enableDownvotes, @@ -84,6 +83,7 @@ import { updatePersonBlock, } from "../../utils"; import { isBrowser } from "../../utils/browser/is-browser"; +import { debounce } from "../../utils/helpers/debounce"; 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 59bbf616..d32e4087 100644 --- a/src/shared/components/search.tsx +++ b/src/shared/components/search.tsx @@ -29,7 +29,6 @@ import { capitalizeFirstLetter, commentsToFlatNodes, communityToChoice, - debounce, enableDownvotes, enableNsfw, fetchCommunities, @@ -46,6 +45,7 @@ import { setIsoData, showLocal, } from "../utils"; +import { debounce } from "../utils/helpers/debounce"; import { getQueryParams } from "../utils/helpers/get-query-params"; import { getQueryString } from "../utils/helpers/get-query-string"; import type { QueryParams } from "../utils/types/query-params"; diff --git a/src/shared/i18next.ts b/src/shared/i18next.ts index 47ca6501..eaedbbf8 100644 --- a/src/shared/i18next.ts +++ b/src/shared/i18next.ts @@ -1,5 +1,4 @@ import i18next, { i18nTyped, Resource } from "i18next"; -import { UserService } from "./services"; import { ar } from "./translations/ar"; import { bg } from "./translations/bg"; import { ca } from "./translations/ca"; @@ -31,7 +30,7 @@ import { sv } from "./translations/sv"; import { vi } from "./translations/vi"; import { zh } from "./translations/zh"; import { zh_Hant } from "./translations/zh_Hant"; -import { isBrowser } from "./utils"; +import { getLanguages } from "./utils"; export const languages = [ { resource: ar, code: "ar", name: "العربية" }, @@ -74,31 +73,12 @@ function format(value: any, format: any): any { return format === "uppercase" ? value.toUpperCase() : value; } -class LanguageDetector { - static readonly type = "languageDetector"; - - detect() { - const langs: string[] = []; - - const myLang = - UserService.Instance.myUserInfo?.local_user_view.local_user - .interface_language ?? "browser"; - - if (myLang !== "browser") langs.push(myLang); - - if (isBrowser()) langs.push(...navigator.languages); - - return langs; - } -} - -i18next.use(LanguageDetector).init({ +i18next.init({ debug: false, compatibilityJSON: "v3", - supportedLngs: languages.map(l => l.code), - nonExplicitSupportedLngs: true, // load: 'languageOnly', // initImmediate: false, + lng: getLanguages()[0], fallbackLng: "en", resources, interpolation: { format }, diff --git a/src/shared/utils.ts b/src/shared/utils.ts index bc6e76f8..c0caad88 100644 --- a/src/shared/utils.ts +++ b/src/shared/utils.ts @@ -40,10 +40,11 @@ import moment from "moment"; import tippy from "tippy.js"; import Toastify from "toastify-js"; import { getHttpBase } from "./env"; -import { i18n } from "./i18next"; +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 { debounce } from "./utils/helpers/debounce"; import { groupBy } from "./utils/helpers/group-by"; let Tribute: any; @@ -230,7 +231,6 @@ export function futureDaysToUnixTime(days?: number): number | undefined { const imageRegex = /(http)?s?:?(\/\/[^"']*\.(?:jpg|jpeg|gif|png|svg|webp))/; const videoRegex = /(http)?s?:?(\/\/[^"']*\.(?:mp4|webm))/; -const tldRegex = /([a-z0-9]+\.)*[a-z0-9]+\.[a-z]+/; export function isImage(url: string) { return imageRegex.test(url); @@ -244,10 +244,6 @@ export function validURL(str: string) { return !!new URL(str); } -export function validInstanceTLD(str: string) { - return tldRegex.test(str); -} - export function communityRSSUrl(actorId: string, sort: string): string { const url = new URL(actorId); return `${url.origin}/feeds${url.pathname}.xml?sort=${sort}`; @@ -273,49 +269,29 @@ export function getDataTypeString(dt: DataType) { return dt === DataType.Post ? "Post" : "Comment"; } -export function debounce( - func: (...e: T) => R, - wait = 1000, - immediate = false -) { - // 'private' variable for instance - // The returned function will be able to reference this due to closure. - // Each call to the returned function will share this common timer. - let timeout: NodeJS.Timeout | null; +export function getLanguages( + override?: string, + myUserInfo = UserService.Instance.myUserInfo +): string[] { + const myLang = myUserInfo?.local_user_view.local_user.interface_language; + const lang = override || myLang || "browser"; - // Calling debounce returns a new anonymous function - return function () { - // reference the context and args for the setTimeout function - const args = arguments; + if (lang == "browser" && isBrowser()) { + return getBrowserLanguages(); + } else { + return [lang]; + } +} - // Should the function be called now? If immediate is true - // and not already in a timeout then the answer is: Yes - const callNow = immediate && !timeout; +function getBrowserLanguages(): string[] { + // Intersect lemmy's langs, with the browser langs + const langs = languages ? languages.map(l => l.code) : ["en"]; - // This is the basic debounce behavior where you can call this - // function several times, but it will only execute once - // [before or after imposing a delay]. - // Each time the returned function is called, the timer starts over. - clearTimeout(timeout ?? undefined); - - // Set the new timeout - timeout = setTimeout(function () { - // Inside the timeout function, clear the timeout variable - // which will let the next execution run when in 'immediate' mode - timeout = null; - - // Check if the function already ran with the immediate flag - if (!immediate) { - // Call the original function with apply - // apply lets you define the 'this' object as well as the arguments - // (both captured before setTimeout) - func.apply(this, args); - } - }, wait); - - // Immediate mode and no wait timer? Execute the function.. - if (callNow) func.apply(this, args); - } as (...e: T) => R; + // NOTE, mobile browsers seem to be missing this list, so append en + const allowedLangs = navigator.languages + .concat("en") + .filter(v => langs.includes(v)); + return allowedLangs; } export async function fetchThemeList(): Promise { @@ -633,7 +609,7 @@ function setupMarkdown() { defs: emojiDefs, }) .disable("image"); - const defaultRenderer = md.renderer.rules.image; + var defaultRenderer = md.renderer.rules.image; md.renderer.rules.image = function ( tokens: Token[], idx: number, @@ -652,9 +628,6 @@ function setupMarkdown() { const alt_text = item.content; return `${alt_text}`; }; - md.renderer.rules.table_open = function () { - return ''; - }; } export function getEmojiMart( @@ -1166,7 +1139,7 @@ export function personSelectName({ export function initializeSite(site?: GetSiteResponse) { UserService.Instance.myUserInfo = site?.my_user; - i18n.changeLanguage(); + i18n.changeLanguage(getLanguages()[0]); if (site) { setupEmojiDataModel(site.custom_emojis ?? []); } diff --git a/src/shared/utils/helpers/debounce.ts b/src/shared/utils/helpers/debounce.ts new file mode 100644 index 00000000..7a1e8b19 --- /dev/null +++ b/src/shared/utils/helpers/debounce.ts @@ -0,0 +1,44 @@ +export function debounce( + func: (...e: T) => R, + wait = 1000, + immediate = false +) { + // 'private' variable for instance + // The returned function will be able to reference this due to closure. + // Each call to the returned function will share this common timer. + let timeout: NodeJS.Timeout | null; + + // Calling debounce returns a new anonymous function + return function () { + // reference the context and args for the setTimeout function + const args = arguments; + + // Should the function be called now? If immediate is true + // and not already in a timeout then the answer is: Yes + const callNow = immediate && !timeout; + + // This is the basic debounce behavior where you can call this + // function several times, but it will only execute once + // [before or after imposing a delay]. + // Each time the returned function is called, the timer starts over. + clearTimeout(timeout ?? undefined); + + // Set the new timeout + timeout = setTimeout(function () { + // Inside the timeout function, clear the timeout variable + // which will let the next execution run when in 'immediate' mode + timeout = null; + + // Check if the function already ran with the immediate flag + if (!immediate) { + // Call the original function with apply + // apply lets you define the 'this' object as well as the arguments + // (both captured before setTimeout) + func.apply(this, args); + } + }, wait); + + // Immediate mode and no wait timer? Execute the function.. + if (callNow) func.apply(this, args); + } as (...e: T) => R; +} diff --git a/yarn.lock b/yarn.lock index 7cd64474..f783f07f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5615,10 +5615,10 @@ leac@^0.6.0: resolved "https://registry.yarnpkg.com/leac/-/leac-0.6.0.tgz#dcf136e382e666bd2475f44a1096061b70dc0912" integrity sha512-y+SqErxb8h7nE/fiEX07jsbuhrpO9lL8eca7/Y1nuWV2moNlXhyd59iDGcRf6moVyDMbmTNzL40SUyrFU/yDpg== -lemmy-js-client@0.18.0-rc.1: - version "0.18.0-rc.1" - resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.18.0-rc.1.tgz#fd0c88810572d90413696011ebaed19e3b8162d8" - integrity sha512-lQe443Nr5UCSoY+IxmT7mBe0IRF6EAZ/4PJSRoPSL+U8A+egMMBPbuxnisHzLsC+eDOWRUIgOqZlwlaRnbmuig== +lemmy-js-client@0.17.2-rc.24: + version "0.17.2-rc.24" + resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.17.2-rc.24.tgz#3b09233a6d89286e559be2e840d81c0c549562ad" + integrity sha512-aSHz7UTcwnwnNd9poY8tEXP7RA9ieZm9MAfSljcbCNU5ds9CASXYNodmraUVJiqCmT4HWnj7IeVmBC9r7nTHnw== dependencies: cross-fetch "^3.1.5" form-data "^4.0.0" From e164a3b9a1d0477cd8441072c4683c3fdaebb8e3 Mon Sep 17 00:00:00 2001 From: Alec Armbruster Date: Fri, 16 Jun 2023 19:10:25 -0400 Subject: [PATCH 05/19] attempt to fix crazy merge fiasco --- .github/CODEOWNERS | 2 +- .github/ISSUE_TEMPLATE/BUG_REPORT.yml | 2 +- .github/ISSUE_TEMPLATE/FEATURE_REQUEST.yml | 2 +- Dockerfile | 2 +- dev.dockerfile | 2 +- package.json | 2 +- src/assets/css/main.css | 24 ---------------------- src/shared/components/common/html-tags.tsx | 7 +++---- 8 files changed, 9 insertions(+), 34 deletions(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 6df17d57..76916e60 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1 +1 @@ -* @dessalines @SleeplessOne1917 +* @dessalines @SleeplessOne1917 @alectrocute diff --git a/.github/ISSUE_TEMPLATE/BUG_REPORT.yml b/.github/ISSUE_TEMPLATE/BUG_REPORT.yml index 2273a138..ae2d4e51 100644 --- a/.github/ISSUE_TEMPLATE/BUG_REPORT.yml +++ b/.github/ISSUE_TEMPLATE/BUG_REPORT.yml @@ -21,7 +21,7 @@ body: - label: Is this only a single bug? Do not put multiple bugs in one issue. required: true - label: Is this a server side (not related to the UI) issue? Use the [Lemmy back end](https://github.com/LemmyNet/lemmy) repo. - required: true + required: false - type: textarea id: summary attributes: diff --git a/.github/ISSUE_TEMPLATE/FEATURE_REQUEST.yml b/.github/ISSUE_TEMPLATE/FEATURE_REQUEST.yml index 2f6f3fc1..3c75050a 100644 --- a/.github/ISSUE_TEMPLATE/FEATURE_REQUEST.yml +++ b/.github/ISSUE_TEMPLATE/FEATURE_REQUEST.yml @@ -19,7 +19,7 @@ body: - label: Is this only a feature request? Do not put multiple feature requests in one issue. required: true - label: Is this a server side (not related to the UI) issue? Use the [Lemmy back end](https://github.com/LemmyNet/lemmy) repo. - required: true + required: false - type: textarea id: problem attributes: diff --git a/Dockerfile b/Dockerfile index 3d6d6212..2b36581d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM node:alpine as builder +FROM node:20.2-alpine as builder RUN apk update && apk add curl yarn python3 build-base gcc wget git --no-cache RUN curl -sf https://gobinaries.com/tj/node-prune | sh diff --git a/dev.dockerfile b/dev.dockerfile index 0e925c0a..3bfc10da 100644 --- a/dev.dockerfile +++ b/dev.dockerfile @@ -1,4 +1,4 @@ -FROM node:alpine as builder +FROM node:20.2-alpine as builder RUN apk update && apk add curl yarn python3 build-base gcc wget git --no-cache WORKDIR /usr/src/app diff --git a/package.json b/package.json index 2298d9e1..b7c48c79 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "inferno-server": "^8.1.1", "isomorphic-cookie": "^1.2.4", "jwt-decode": "^3.1.2", - "lemmy-js-client": "0.17.2-rc.24", + "lemmy-js-client": "0.18.0-rc.1", "lodash": "^4.17.21", "markdown-it": "^13.0.1", "markdown-it-container": "^3.0.0", diff --git a/src/assets/css/main.css b/src/assets/css/main.css index e1adfc53..82f8433e 100644 --- a/src/assets/css/main.css +++ b/src/assets/css/main.css @@ -80,30 +80,6 @@ overflow-x: auto; } -.md-div table { - border-collapse: collapse; - width: 100%; - margin-bottom: 1rem; - border: 1px solid var(--dark); -} - -.md-div table th, -.md-div table td { - padding: 0.3rem; - vertical-align: top; - border-top: 1px solid var(--dark); - border: 1px solid var(--dark); -} - -.md-div table thead th { - vertical-align: bottom; - border-bottom: 2px solid var(--dark); -} - -.md-div table tbody + tbody { - border-top: 2px solid var(--dark); -} - .vote-bar { margin-top: -6.5px; } diff --git a/src/shared/components/common/html-tags.tsx b/src/shared/components/common/html-tags.tsx index 0e6cb2d0..387cd041 100644 --- a/src/shared/components/common/html-tags.tsx +++ b/src/shared/components/common/html-tags.tsx @@ -2,8 +2,8 @@ import { htmlToText } from "html-to-text"; import { Component } from "inferno"; import { Helmet } from "inferno-helmet"; import { httpExternalPath } from "../../env"; -import { getLanguages, md } from "../../utils"; - +import { i18n } from "../../i18next"; +import { md } from "../../utils"; interface HtmlTagsProps { title: string; path: string; @@ -17,11 +17,10 @@ export class HtmlTags extends Component { const url = httpExternalPath(this.props.path); const desc = this.props.description; const image = this.props.image; - const lang = getLanguages()[0]; return ( - + {["title", "og:title", "twitter:title"].map(t => ( From 8fcde4bdd08280b2565a39bbb005bf49686c7295 Mon Sep 17 00:00:00 2001 From: Alec Armbruster Date: Fri, 16 Jun 2023 19:14:35 -0400 Subject: [PATCH 06/19] more cleanup --- src/shared/components/common/html-tags.tsx | 1 + .../components/common/markdown-textarea.tsx | 98 ++++++++++--------- src/shared/components/common/moment-time.tsx | 6 +- 3 files changed, 54 insertions(+), 51 deletions(-) diff --git a/src/shared/components/common/html-tags.tsx b/src/shared/components/common/html-tags.tsx index 387cd041..f32b0fc0 100644 --- a/src/shared/components/common/html-tags.tsx +++ b/src/shared/components/common/html-tags.tsx @@ -4,6 +4,7 @@ import { Helmet } from "inferno-helmet"; import { httpExternalPath } from "../../env"; import { i18n } from "../../i18next"; import { md } from "../../utils"; + interface HtmlTagsProps { title: string; path: string; diff --git a/src/shared/components/common/markdown-textarea.tsx b/src/shared/components/common/markdown-textarea.tsx index 36f6283d..55e73617 100644 --- a/src/shared/components/common/markdown-textarea.tsx +++ b/src/shared/components/common/markdown-textarea.tsx @@ -184,53 +184,6 @@ export class MarkdownTextArea extends Component<
- {this.props.buttonTitle && ( - - )} - {this.props.replyType && ( - - )} - {this.state.content && ( - - )} - {/* A flex expander */} -
- - {this.props.showLanguage && ( - - )} {this.getFormatButton("bold", this.handleInsertBold)} {this.getFormatButton("italic", this.handleInsertItalic)} {this.getFormatButton("link", this.handleInsertLink)} @@ -283,6 +236,57 @@ export class MarkdownTextArea extends Component<
+ +
+ {this.props.showLanguage && ( + + )} + + {/* A flex expander */} +
+ + {this.props.buttonTitle && ( + + )} + {this.props.replyType && ( + + )} + {this.state.content && ( + + )} +
); diff --git a/src/shared/components/common/moment-time.tsx b/src/shared/components/common/moment-time.tsx index 10714f5b..30c1682c 100644 --- a/src/shared/components/common/moment-time.tsx +++ b/src/shared/components/common/moment-time.tsx @@ -1,7 +1,7 @@ import { Component } from "inferno"; import moment from "moment"; import { i18n } from "../../i18next"; -import { capitalizeFirstLetter, getLanguages } from "../../utils"; +import { capitalizeFirstLetter } from "../../utils"; import { Icon } from "./icon"; interface MomentTimeProps { @@ -15,9 +15,7 @@ export class MomentTime extends Component { constructor(props: any, context: any) { super(props, context); - const lang = getLanguages(); - - moment.locale(lang); + moment.locale([...i18n.languages]); } createdAndModifiedTimes() { From 768ed8ea94ffcd418ff4644a312e9b904af37a7e Mon Sep 17 00:00:00 2001 From: Alec Armbruster Date: Fri, 16 Jun 2023 19:16:00 -0400 Subject: [PATCH 07/19] even more cleanup --- src/shared/i18next.ts | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/shared/i18next.ts b/src/shared/i18next.ts index eaedbbf8..0a705ae6 100644 --- a/src/shared/i18next.ts +++ b/src/shared/i18next.ts @@ -1,4 +1,5 @@ import i18next, { i18nTyped, Resource } from "i18next"; +import { UserService } from "./services"; import { ar } from "./translations/ar"; import { bg } from "./translations/bg"; import { ca } from "./translations/ca"; @@ -30,7 +31,7 @@ import { sv } from "./translations/sv"; import { vi } from "./translations/vi"; import { zh } from "./translations/zh"; import { zh_Hant } from "./translations/zh_Hant"; -import { getLanguages } from "./utils"; +import { isBrowser } from "./utils/browser/is-browser"; export const languages = [ { resource: ar, code: "ar", name: "العربية" }, @@ -73,12 +74,31 @@ function format(value: any, format: any): any { return format === "uppercase" ? value.toUpperCase() : value; } -i18next.init({ +class LanguageDetector { + static readonly type = "languageDetector"; + + detect() { + const langs: string[] = []; + + const myLang = + UserService.Instance.myUserInfo?.local_user_view.local_user + .interface_language ?? "browser"; + + if (myLang !== "browser") langs.push(myLang); + + if (isBrowser()) langs.push(...navigator.languages); + + return langs; + } +} + +i18next.use(LanguageDetector).init({ debug: false, compatibilityJSON: "v3", + supportedLngs: languages.map(l => l.code), + nonExplicitSupportedLngs: true, // load: 'languageOnly', // initImmediate: false, - lng: getLanguages()[0], fallbackLng: "en", resources, interpolation: { format }, From fbc13249ca2add7be9608402841b5a85f740065d Mon Sep 17 00:00:00 2001 From: Alec Armbruster Date: Fri, 16 Jun 2023 19:18:06 -0400 Subject: [PATCH 08/19] more cleanup --- src/shared/utils.ts | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/src/shared/utils.ts b/src/shared/utils.ts index c0caad88..6019e4d6 100644 --- a/src/shared/utils.ts +++ b/src/shared/utils.ts @@ -40,7 +40,7 @@ import moment from "moment"; import tippy from "tippy.js"; import Toastify from "toastify-js"; import { getHttpBase } from "./env"; -import { i18n, languages } from "./i18next"; +import { i18n } from "./i18next"; import { CommentNodeI, DataType, IsoData, VoteType } from "./interfaces"; import { HttpService, UserService } from "./services"; import { isBrowser } from "./utils/browser/is-browser"; @@ -231,6 +231,7 @@ export function futureDaysToUnixTime(days?: number): number | undefined { const imageRegex = /(http)?s?:?(\/\/[^"']*\.(?:jpg|jpeg|gif|png|svg|webp))/; const videoRegex = /(http)?s?:?(\/\/[^"']*\.(?:mp4|webm))/; +const tldRegex = /([a-z0-9]+\.)*[a-z0-9]+\.[a-z]+/; export function isImage(url: string) { return imageRegex.test(url); @@ -244,6 +245,10 @@ export function validURL(str: string) { return !!new URL(str); } +export function validInstanceTLD(str: string) { + return tldRegex.test(str); +} + export function communityRSSUrl(actorId: string, sort: string): string { const url = new URL(actorId); return `${url.origin}/feeds${url.pathname}.xml?sort=${sort}`; @@ -269,31 +274,6 @@ export function getDataTypeString(dt: DataType) { return dt === DataType.Post ? "Post" : "Comment"; } -export function getLanguages( - override?: string, - myUserInfo = UserService.Instance.myUserInfo -): string[] { - const myLang = myUserInfo?.local_user_view.local_user.interface_language; - const lang = override || myLang || "browser"; - - if (lang == "browser" && isBrowser()) { - return getBrowserLanguages(); - } else { - return [lang]; - } -} - -function getBrowserLanguages(): string[] { - // Intersect lemmy's langs, with the browser langs - const langs = languages ? languages.map(l => l.code) : ["en"]; - - // NOTE, mobile browsers seem to be missing this list, so append en - const allowedLangs = navigator.languages - .concat("en") - .filter(v => langs.includes(v)); - return allowedLangs; -} - export async function fetchThemeList(): Promise { return fetch("/css/themelist").then(res => res.json()); } From b3a25a3bb38ca6b80922a3472cfb96d91211c42c Mon Sep 17 00:00:00 2001 From: Alec Armbruster Date: Fri, 16 Jun 2023 19:20:43 -0400 Subject: [PATCH 09/19] hopefully last merge fiasco cleanup --- src/shared/utils.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/shared/utils.ts b/src/shared/utils.ts index 6019e4d6..6f6e61c7 100644 --- a/src/shared/utils.ts +++ b/src/shared/utils.ts @@ -589,7 +589,7 @@ function setupMarkdown() { defs: emojiDefs, }) .disable("image"); - var defaultRenderer = md.renderer.rules.image; + const defaultRenderer = md.renderer.rules.image; md.renderer.rules.image = function ( tokens: Token[], idx: number, @@ -608,6 +608,9 @@ function setupMarkdown() { const alt_text = item.content; return `${alt_text}`; }; + md.renderer.rules.table_open = function () { + return '
'; + }; } export function getEmojiMart( @@ -1119,7 +1122,7 @@ export function personSelectName({ export function initializeSite(site?: GetSiteResponse) { UserService.Instance.myUserInfo = site?.my_user; - i18n.changeLanguage(getLanguages()[0]); + i18n.changeLanguage(); if (site) { setupEmojiDataModel(site.custom_emojis ?? []); } From b0c39ad5f5960cbf8f332f963fcb67846a6fe66e Mon Sep 17 00:00:00 2001 From: Alec Armbruster Date: Fri, 16 Jun 2023 19:22:12 -0400 Subject: [PATCH 10/19] and of course, yarn.lock --- yarn.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/yarn.lock b/yarn.lock index f783f07f..7cd64474 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5615,10 +5615,10 @@ leac@^0.6.0: resolved "https://registry.yarnpkg.com/leac/-/leac-0.6.0.tgz#dcf136e382e666bd2475f44a1096061b70dc0912" integrity sha512-y+SqErxb8h7nE/fiEX07jsbuhrpO9lL8eca7/Y1nuWV2moNlXhyd59iDGcRf6moVyDMbmTNzL40SUyrFU/yDpg== -lemmy-js-client@0.17.2-rc.24: - version "0.17.2-rc.24" - resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.17.2-rc.24.tgz#3b09233a6d89286e559be2e840d81c0c549562ad" - integrity sha512-aSHz7UTcwnwnNd9poY8tEXP7RA9ieZm9MAfSljcbCNU5ds9CASXYNodmraUVJiqCmT4HWnj7IeVmBC9r7nTHnw== +lemmy-js-client@0.18.0-rc.1: + version "0.18.0-rc.1" + resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.18.0-rc.1.tgz#fd0c88810572d90413696011ebaed19e3b8162d8" + integrity sha512-lQe443Nr5UCSoY+IxmT7mBe0IRF6EAZ/4PJSRoPSL+U8A+egMMBPbuxnisHzLsC+eDOWRUIgOqZlwlaRnbmuig== dependencies: cross-fetch "^3.1.5" form-data "^4.0.0" From 571b1faf70c3ed7d5dc1a086c491c63ea7dfbede Mon Sep 17 00:00:00 2001 From: Alec Armbruster Date: Sat, 17 Jun 2023 08:46:19 -0400 Subject: [PATCH 11/19] remove comments --- src/shared/utils/helpers/debounce.ts | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/src/shared/utils/helpers/debounce.ts b/src/shared/utils/helpers/debounce.ts index 7a1e8b19..d5cd7017 100644 --- a/src/shared/utils/helpers/debounce.ts +++ b/src/shared/utils/helpers/debounce.ts @@ -3,42 +3,22 @@ export function debounce( wait = 1000, immediate = false ) { - // 'private' variable for instance - // The returned function will be able to reference this due to closure. - // Each call to the returned function will share this common timer. let timeout: NodeJS.Timeout | null; - // Calling debounce returns a new anonymous function return function () { - // reference the context and args for the setTimeout function const args = arguments; - - // Should the function be called now? If immediate is true - // and not already in a timeout then the answer is: Yes const callNow = immediate && !timeout; - // This is the basic debounce behavior where you can call this - // function several times, but it will only execute once - // [before or after imposing a delay]. - // Each time the returned function is called, the timer starts over. clearTimeout(timeout ?? undefined); - // Set the new timeout timeout = setTimeout(function () { - // Inside the timeout function, clear the timeout variable - // which will let the next execution run when in 'immediate' mode timeout = null; - // Check if the function already ran with the immediate flag if (!immediate) { - // Call the original function with apply - // apply lets you define the 'this' object as well as the arguments - // (both captured before setTimeout) func.apply(this, args); } }, wait); - // Immediate mode and no wait timer? Execute the function.. if (callNow) func.apply(this, args); } as (...e: T) => R; } From f8fd90cc2edc77af4dc6d463da8adb499a77b971 Mon Sep 17 00:00:00 2001 From: Alec Armbruster Date: Sat, 17 Jun 2023 08:47:43 -0400 Subject: [PATCH 12/19] fix accidental changes --- src/shared/components/common/markdown-textarea.tsx | 1 + src/shared/components/community/community.tsx | 1 + 2 files changed, 2 insertions(+) diff --git a/src/shared/components/common/markdown-textarea.tsx b/src/shared/components/common/markdown-textarea.tsx index 094a8dd3..c03c68eb 100644 --- a/src/shared/components/common/markdown-textarea.tsx +++ b/src/shared/components/common/markdown-textarea.tsx @@ -26,6 +26,7 @@ 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/community.tsx b/src/shared/components/community/community.tsx index 5f1b37af..54ecfcd7 100644 --- a/src/shared/components/community/community.tsx +++ b/src/shared/components/community/community.tsx @@ -62,6 +62,7 @@ import { UserService } from "../../services"; import { FirstLoadService } from "../../services/FirstLoadService"; import { HttpService, RequestState } from "../../services/HttpService"; import { + RouteDataResponse, commentsToFlatNodes, communityRSSUrl, editComment, From b1b0fdf5f86cc406922de5704ba5ebe45aed3b39 Mon Sep 17 00:00:00 2001 From: Alec Armbruster <35377827+alectrocute@users.noreply.github.com> Date: Mon, 19 Jun 2023 19:10:53 -0400 Subject: [PATCH 13/19] fix missing imports --- src/shared/components/community/communities.tsx | 1 + src/shared/components/modlog.tsx | 1 + src/shared/components/person/profile.tsx | 1 + 3 files changed, 3 insertions(+) diff --git a/src/shared/components/community/communities.tsx b/src/shared/components/community/communities.tsx index 9ce4f492..eef064c7 100644 --- a/src/shared/components/community/communities.tsx +++ b/src/shared/components/community/communities.tsx @@ -11,6 +11,7 @@ import { InitialFetchRequest } from "../../interfaces"; import { FirstLoadService } from "../../services/FirstLoadService"; import { HttpService, RequestState } from "../../services/HttpService"; import { + RouteDataResponse, editCommunity, getPageFromString, myAuth, diff --git a/src/shared/components/modlog.tsx b/src/shared/components/modlog.tsx index 2db3d4b1..8538a4f5 100644 --- a/src/shared/components/modlog.tsx +++ b/src/shared/components/modlog.tsx @@ -34,6 +34,7 @@ import { FirstLoadService } from "../services/FirstLoadService"; import { HttpService, RequestState } from "../services/HttpService"; import { Choice, + RouteDataResponse, fetchLimit, fetchUsers, getIdFromString, diff --git a/src/shared/components/person/profile.tsx b/src/shared/components/person/profile.tsx index b6a3200d..c8060ce6 100644 --- a/src/shared/components/person/profile.tsx +++ b/src/shared/components/person/profile.tsx @@ -53,6 +53,7 @@ import { UserService } from "../../services"; import { FirstLoadService } from "../../services/FirstLoadService"; import { HttpService, RequestState } from "../../services/HttpService"; import { + RouteDataResponse, capitalizeFirstLetter, editComment, editPost, From f53d5db0073c1ee8439365b1ec21f484dc5a2611 Mon Sep 17 00:00:00 2001 From: Alec Armbruster <35377827+alectrocute@users.noreply.github.com> Date: Mon, 19 Jun 2023 21:12:42 -0400 Subject: [PATCH 14/19] export default everything, will fix type errors next --- src/shared/components/comment/comment-node.tsx | 12 ++++++------ src/shared/utils/browser/can-share.ts | 4 ++-- src/shared/utils/browser/is-browser.ts | 2 +- src/shared/utils/browser/share.ts | 4 ++-- src/shared/utils/helpers/debounce.ts | 2 +- src/shared/utils/helpers/get-query-params.ts | 6 ++++-- src/shared/utils/helpers/get-query-string.ts | 6 +++--- src/shared/utils/helpers/poll.ts | 4 ++-- src/shared/utils/helpers/sleep.ts | 2 +- src/shared/utils/roles/am-admin.ts | 4 +++- src/shared/utils/roles/am-community-creator.ts | 2 +- src/shared/utils/roles/am-mod.ts | 4 ++-- src/shared/utils/roles/am-site-creator.ts | 2 +- src/shared/utils/roles/am-top-mod.ts | 2 +- src/shared/utils/roles/can-admin.ts | 4 ++-- src/shared/utils/roles/can-create-community.ts | 4 ++-- src/shared/utils/roles/can-mod.ts | 2 +- src/shared/utils/roles/is-admin.ts | 5 ++++- src/shared/utils/roles/is-banned.ts | 2 +- src/shared/utils/roles/is-mod.ts | 2 +- 20 files changed, 41 insertions(+), 34 deletions(-) diff --git a/src/shared/components/comment/comment-node.tsx b/src/shared/components/comment/comment-node.tsx index ebf7cb2e..bcbd6050 100644 --- a/src/shared/components/comment/comment-node.tsx +++ b/src/shared/components/comment/comment-node.tsx @@ -53,12 +53,12 @@ import { setupTippy, showScores, } from "../../utils"; -import { amCommunityCreator } from "../../utils/roles/am-community-creator"; -import { canAdmin } from "../../utils/roles/can-admin"; -import { canMod } from "../../utils/roles/can-mod"; -import { isAdmin } from "../../utils/roles/is-admin"; -import { isBanned } from "../../utils/roles/is-banned"; -import { isMod } from "../../utils/roles/is-mod"; +import amCommunityCreator from "../../utils/roles/am-community-creator"; +import canAdmin from "../../utils/roles/can-admin"; +import canMod from "../../utils/roles/can-mod"; +import isAdmin from "../../utils/roles/is-admin"; +import isBanned from "../../utils/roles/is-banned"; +import isMod from "../../utils/roles/is-mod"; import { Icon, PurgeWarning, Spinner } from "../common/icon"; import { MomentTime } from "../common/moment-time"; import { CommunityLink } from "../community/community-link"; diff --git a/src/shared/utils/browser/can-share.ts b/src/shared/utils/browser/can-share.ts index bec7e805..f141fe50 100644 --- a/src/shared/utils/browser/can-share.ts +++ b/src/shared/utils/browser/can-share.ts @@ -1,5 +1,5 @@ -import { isBrowser } from "./is-browser"; +import isBrowser from "./is-browser"; -export function canShare() { +export default function canShare() { return isBrowser() && !!navigator.canShare; } diff --git a/src/shared/utils/browser/is-browser.ts b/src/shared/utils/browser/is-browser.ts index 4139b25d..cc6ba882 100644 --- a/src/shared/utils/browser/is-browser.ts +++ b/src/shared/utils/browser/is-browser.ts @@ -1,3 +1,3 @@ -export function isBrowser() { +export default function isBrowser() { return typeof window !== "undefined"; } diff --git a/src/shared/utils/browser/share.ts b/src/shared/utils/browser/share.ts index b1d1b5be..9ae94974 100644 --- a/src/shared/utils/browser/share.ts +++ b/src/shared/utils/browser/share.ts @@ -1,6 +1,6 @@ -import { isBrowser } from "./is-browser"; +import isBrowser from "./is-browser"; -export function share(shareData: ShareData) { +export default function share(shareData: ShareData) { if (isBrowser()) { navigator.share(shareData); } diff --git a/src/shared/utils/helpers/debounce.ts b/src/shared/utils/helpers/debounce.ts index d5cd7017..7e3b6f03 100644 --- a/src/shared/utils/helpers/debounce.ts +++ b/src/shared/utils/helpers/debounce.ts @@ -1,4 +1,4 @@ -export function debounce( +export default function debounce( func: (...e: T) => R, wait = 1000, immediate = false diff --git a/src/shared/utils/helpers/get-query-params.ts b/src/shared/utils/helpers/get-query-params.ts index 213d3521..5488c6ea 100644 --- a/src/shared/utils/helpers/get-query-params.ts +++ b/src/shared/utils/helpers/get-query-params.ts @@ -1,6 +1,8 @@ -import { isBrowser } from "../browser/is-browser"; +import isBrowser from "../browser/is-browser"; -export function getQueryParams>(processors: { +export default function getQueryParams< + T extends Record +>(processors: { [K in keyof T]: (param: string) => T[K]; }): T { if (isBrowser()) { diff --git a/src/shared/utils/helpers/get-query-string.ts b/src/shared/utils/helpers/get-query-string.ts index a66b5af4..4b7bdbb5 100644 --- a/src/shared/utils/helpers/get-query-string.ts +++ b/src/shared/utils/helpers/get-query-string.ts @@ -1,6 +1,6 @@ -export function getQueryString>( - obj: T -) { +export default function getQueryString< + T extends Record +>(obj: T) { return Object.entries(obj) .filter(([, val]) => val !== undefined && val !== null) .reduce( diff --git a/src/shared/utils/helpers/poll.ts b/src/shared/utils/helpers/poll.ts index 055f17f4..8f30e91b 100644 --- a/src/shared/utils/helpers/poll.ts +++ b/src/shared/utils/helpers/poll.ts @@ -1,9 +1,9 @@ -import { sleep } from "./sleep"; +import sleep from "./sleep"; /** * Polls / repeatedly runs a promise, every X milliseconds */ -export async function poll(promiseFn: any, millis: number) { +export default async function poll(promiseFn: any, millis: number) { if (window.document.visibilityState !== "hidden") { await promiseFn(); } diff --git a/src/shared/utils/helpers/sleep.ts b/src/shared/utils/helpers/sleep.ts index 5b7c5388..6529b522 100644 --- a/src/shared/utils/helpers/sleep.ts +++ b/src/shared/utils/helpers/sleep.ts @@ -1,3 +1,3 @@ -export function sleep(millis: number): Promise { +export default function sleep(millis: number): Promise { return new Promise(resolve => setTimeout(resolve, millis)); } diff --git a/src/shared/utils/roles/am-admin.ts b/src/shared/utils/roles/am-admin.ts index aadf52ce..69139d37 100644 --- a/src/shared/utils/roles/am-admin.ts +++ b/src/shared/utils/roles/am-admin.ts @@ -1,5 +1,7 @@ import { UserService } from "../../services"; -export function amAdmin(myUserInfo = UserService.Instance.myUserInfo): boolean { +export default function amAdmin( + myUserInfo = UserService.Instance.myUserInfo +): boolean { return myUserInfo?.local_user_view.person.admin ?? false; } diff --git a/src/shared/utils/roles/am-community-creator.ts b/src/shared/utils/roles/am-community-creator.ts index 20f9b1dd..3671ef20 100644 --- a/src/shared/utils/roles/am-community-creator.ts +++ b/src/shared/utils/roles/am-community-creator.ts @@ -1,7 +1,7 @@ import { CommunityModeratorView } from "lemmy-js-client"; import { UserService } from "../../services"; -export function amCommunityCreator( +export default function amCommunityCreator( creator_id: number, mods?: CommunityModeratorView[], myUserInfo = UserService.Instance.myUserInfo diff --git a/src/shared/utils/roles/am-mod.ts b/src/shared/utils/roles/am-mod.ts index 7b792b39..85483dae 100644 --- a/src/shared/utils/roles/am-mod.ts +++ b/src/shared/utils/roles/am-mod.ts @@ -1,8 +1,8 @@ import { CommunityModeratorView } from "lemmy-js-client"; import { UserService } from "../../services"; -import { isMod } from "./is-mod"; +import isMod from "./is-mod"; -export function amMod( +export default function amMod( mods?: CommunityModeratorView[], myUserInfo = UserService.Instance.myUserInfo ): boolean { diff --git a/src/shared/utils/roles/am-site-creator.ts b/src/shared/utils/roles/am-site-creator.ts index 323ac0a4..9da2840e 100644 --- a/src/shared/utils/roles/am-site-creator.ts +++ b/src/shared/utils/roles/am-site-creator.ts @@ -1,7 +1,7 @@ import { PersonView } from "lemmy-js-client"; import { UserService } from "../../services"; -export function amSiteCreator( +export default function amSiteCreator( creator_id: number, admins?: PersonView[], myUserInfo = UserService.Instance.myUserInfo diff --git a/src/shared/utils/roles/am-top-mod.ts b/src/shared/utils/roles/am-top-mod.ts index 4b942da7..9163d7ca 100644 --- a/src/shared/utils/roles/am-top-mod.ts +++ b/src/shared/utils/roles/am-top-mod.ts @@ -1,7 +1,7 @@ import { CommunityModeratorView } from "lemmy-js-client"; import { UserService } from "../../services"; -export function amTopMod( +export default function amTopMod( mods: CommunityModeratorView[], myUserInfo = UserService.Instance.myUserInfo ): boolean { diff --git a/src/shared/utils/roles/can-admin.ts b/src/shared/utils/roles/can-admin.ts index 080c7acc..cac451e9 100644 --- a/src/shared/utils/roles/can-admin.ts +++ b/src/shared/utils/roles/can-admin.ts @@ -1,8 +1,8 @@ import { PersonView } from "lemmy-js-client"; import { UserService } from "../../services"; -import { canMod } from "./can-mod"; +import canMod from "./can-mod"; -export function canAdmin( +export default function canAdmin( creatorId: number, admins?: PersonView[], myUserInfo = UserService.Instance.myUserInfo, diff --git a/src/shared/utils/roles/can-create-community.ts b/src/shared/utils/roles/can-create-community.ts index 202290d2..b0bfeddc 100644 --- a/src/shared/utils/roles/can-create-community.ts +++ b/src/shared/utils/roles/can-create-community.ts @@ -1,8 +1,8 @@ import { GetSiteResponse } from "lemmy-js-client"; import { UserService } from "../../services"; -import { amAdmin } from "./am-admin"; +import amAdmin from "./am-admin"; -export function canCreateCommunity( +export default function canCreateCommunity( siteRes: GetSiteResponse, myUserInfo = UserService.Instance.myUserInfo ): boolean { diff --git a/src/shared/utils/roles/can-mod.ts b/src/shared/utils/roles/can-mod.ts index 2892304d..df639b7f 100644 --- a/src/shared/utils/roles/can-mod.ts +++ b/src/shared/utils/roles/can-mod.ts @@ -1,7 +1,7 @@ import { CommunityModeratorView, PersonView } from "lemmy-js-client"; import { UserService } from "../../services"; -export function canMod( +export default function canMod( creator_id: number, mods?: CommunityModeratorView[], admins?: PersonView[], diff --git a/src/shared/utils/roles/is-admin.ts b/src/shared/utils/roles/is-admin.ts index fbf662b8..bc0332ea 100644 --- a/src/shared/utils/roles/is-admin.ts +++ b/src/shared/utils/roles/is-admin.ts @@ -1,5 +1,8 @@ import { PersonView } from "lemmy-js-client"; -export function isAdmin(creatorId: number, admins?: PersonView[]): boolean { +export default function isAdmin( + creatorId: number, + admins?: PersonView[] +): boolean { return admins?.map(a => a.person.id).includes(creatorId) ?? false; } diff --git a/src/shared/utils/roles/is-banned.ts b/src/shared/utils/roles/is-banned.ts index dd5ffe6c..d71f6f4f 100644 --- a/src/shared/utils/roles/is-banned.ts +++ b/src/shared/utils/roles/is-banned.ts @@ -1,6 +1,6 @@ import { Person } from "lemmy-js-client"; -export function isBanned(ps: Person): boolean { +export default function isBanned(ps: Person): boolean { const expires = ps.ban_expires; // Add Z to convert from UTC date // TODO this check probably isn't necessary anymore diff --git a/src/shared/utils/roles/is-mod.ts b/src/shared/utils/roles/is-mod.ts index 87311072..018b721e 100644 --- a/src/shared/utils/roles/is-mod.ts +++ b/src/shared/utils/roles/is-mod.ts @@ -1,6 +1,6 @@ import { CommunityModeratorView } from "lemmy-js-client"; -export function isMod( +export default function isMod( creatorId: number, mods?: CommunityModeratorView[] ): boolean { From 69a123b6d80766c48a64c15f7d7e1545605bad47 Mon Sep 17 00:00:00 2001 From: Alec Armbruster <35377827+alectrocute@users.noreply.github.com> Date: Mon, 19 Jun 2023 21:48:38 -0400 Subject: [PATCH 15/19] update imports --- src/shared/components/app/navbar.tsx | 8 ++++---- .../components/common/markdown-textarea.tsx | 2 +- .../components/community/communities.tsx | 4 ++-- src/shared/components/community/community.tsx | 4 ++-- src/shared/components/community/sidebar.tsx | 13 ++++-------- src/shared/components/home/home.tsx | 6 +++--- src/shared/components/home/login.tsx | 2 +- src/shared/components/home/signup.tsx | 2 +- src/shared/components/modlog.tsx | 10 +++++----- src/shared/components/person/profile.tsx | 10 +++++----- src/shared/components/person/reports.tsx | 2 +- src/shared/components/person/settings.tsx | 2 +- src/shared/components/post/create-post.tsx | 2 +- src/shared/components/post/post-form.tsx | 2 +- src/shared/components/post/post-listing.tsx | 20 +++++++++---------- src/shared/components/post/post.tsx | 4 ++-- src/shared/components/search.tsx | 6 +++--- src/shared/env.ts | 2 +- src/shared/i18next.ts | 2 +- src/shared/services/UserService.ts | 2 +- src/shared/utils.ts | 6 +++--- 21 files changed, 53 insertions(+), 58 deletions(-) diff --git a/src/shared/components/app/navbar.tsx b/src/shared/components/app/navbar.tsx index d0943af2..9f68656a 100644 --- a/src/shared/components/app/navbar.tsx +++ b/src/shared/components/app/navbar.tsx @@ -17,10 +17,10 @@ import { 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 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"; import { PictrsImage } from "../common/pictrs-image"; diff --git a/src/shared/components/common/markdown-textarea.tsx b/src/shared/components/common/markdown-textarea.tsx index c03c68eb..36700214 100644 --- a/src/shared/components/common/markdown-textarea.tsx +++ b/src/shared/components/common/markdown-textarea.tsx @@ -20,7 +20,7 @@ import { setupTribute, toast, } from "../../utils"; -import { isBrowser } from "../../utils/browser/is-browser"; +import isBrowser from "../../utils/browser/is-browser"; import { EmojiPicker } from "./emoji-picker"; import { Icon, Spinner } from "./icon"; import { LanguageSelect } from "./language-select"; diff --git a/src/shared/components/community/communities.tsx b/src/shared/components/community/communities.tsx index eef064c7..6a2ae6ad 100644 --- a/src/shared/components/community/communities.tsx +++ b/src/shared/components/community/communities.tsx @@ -20,8 +20,8 @@ import { setIsoData, showLocal, } from "../../utils"; -import { getQueryParams } from "../../utils/helpers/get-query-params"; -import { getQueryString } from "../../utils/helpers/get-query-string"; +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"; diff --git a/src/shared/components/community/community.tsx b/src/shared/components/community/community.tsx index 54ecfcd7..42a77836 100644 --- a/src/shared/components/community/community.tsx +++ b/src/shared/components/community/community.tsx @@ -86,8 +86,8 @@ import { updateCommunityBlock, updatePersonBlock, } from "../../utils"; -import { getQueryParams } from "../../utils/helpers/get-query-params"; -import { getQueryString } from "../../utils/helpers/get-query-string"; +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"; diff --git a/src/shared/components/community/sidebar.tsx b/src/shared/components/community/sidebar.tsx index 508e5a0d..72c94c09 100644 --- a/src/shared/components/community/sidebar.tsx +++ b/src/shared/components/community/sidebar.tsx @@ -16,15 +16,10 @@ import { } from "lemmy-js-client"; import { i18n } from "../../i18next"; import { UserService } from "../../services"; -import { - getUnixTime, - hostname, - mdToHtml, - myAuthRequired, -} from "../../utils"; -import { amAdmin } from "../../utils/roles/am-admin"; -import { amMod } from "../../utils/roles/am-mod"; -import { amTopMod } from "../../utils/roles/am-top-mod"; +import { getUnixTime, hostname, mdToHtml, myAuthRequired } from "../../utils"; +import amAdmin from "../../utils/roles/am-admin"; +import amMod from "../../utils/roles/am-mod"; +import amTopMod from "../../utils/roles/am-top-mod"; import { Badges } from "../common/badges"; import { BannerIconHeader } from "../common/banner-icon-header"; import { Icon, PurgeWarning, Spinner } from "../common/icon"; diff --git a/src/shared/components/home/home.tsx b/src/shared/components/home/home.tsx index 39753c48..4a1e9d70 100644 --- a/src/shared/components/home/home.tsx +++ b/src/shared/components/home/home.tsx @@ -82,9 +82,9 @@ 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 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"; diff --git a/src/shared/components/home/login.tsx b/src/shared/components/home/login.tsx index 6a270899..e25222e0 100644 --- a/src/shared/components/home/login.tsx +++ b/src/shared/components/home/login.tsx @@ -4,7 +4,7 @@ import { i18n } from "../../i18next"; import { UserService } from "../../services"; import { HttpService, RequestState } from "../../services/HttpService"; import { myAuth, setIsoData, toast, validEmail } from "../../utils"; -import { isBrowser } from "../../utils/browser/is-browser"; +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 c7306950..287bdb7b 100644 --- a/src/shared/components/home/signup.tsx +++ b/src/shared/components/home/signup.tsx @@ -20,7 +20,7 @@ import { toast, validEmail, } from "../../utils"; -import { isBrowser } from "../../utils/browser/is-browser"; +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 8538a4f5..3ea910c5 100644 --- a/src/shared/components/modlog.tsx +++ b/src/shared/components/modlog.tsx @@ -44,11 +44,11 @@ import { personToChoice, setIsoData, } from "../utils"; -import { debounce } from "../utils/helpers/debounce"; -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 debounce from "../utils/helpers/debounce"; +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"; diff --git a/src/shared/components/person/profile.tsx b/src/shared/components/person/profile.tsx index c8060ce6..d99ab124 100644 --- a/src/shared/components/person/profile.tsx +++ b/src/shared/components/person/profile.tsx @@ -76,11 +76,11 @@ 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 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"; diff --git a/src/shared/components/person/reports.tsx b/src/shared/components/person/reports.tsx index 0be75379..ce9d215a 100644 --- a/src/shared/components/person/reports.tsx +++ b/src/shared/components/person/reports.tsx @@ -31,7 +31,7 @@ import { myAuthRequired, setIsoData, } from "../../utils"; -import { amAdmin } from "../../utils/roles/am-admin"; +import amAdmin from "../../utils/roles/am-admin"; import { CommentReport } from "../comment/comment-report"; import { HtmlTags } from "../common/html-tags"; import { Spinner } from "../common/icon"; diff --git a/src/shared/components/person/settings.tsx b/src/shared/components/person/settings.tsx index 971ec891..7cb9308c 100644 --- a/src/shared/components/person/settings.tsx +++ b/src/shared/components/person/settings.tsx @@ -35,7 +35,7 @@ import { updateCommunityBlock, updatePersonBlock, } from "../../utils"; -import { debounce } from "../../utils/helpers/debounce"; +import debounce from "../../utils/helpers/debounce"; import { HtmlTags } from "../common/html-tags"; import { Icon, Spinner } from "../common/icon"; import { ImageUploadForm } from "../common/image-upload-form"; diff --git a/src/shared/components/post/create-post.tsx b/src/shared/components/post/create-post.tsx index 7df628b2..4f367712 100644 --- a/src/shared/components/post/create-post.tsx +++ b/src/shared/components/post/create-post.tsx @@ -24,7 +24,7 @@ import { myAuth, setIsoData, } from "../../utils"; -import { getQueryParams } from "../../utils/helpers/get-query-params"; +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"; diff --git a/src/shared/components/post/post-form.tsx b/src/shared/components/post/post-form.tsx index c21a6e2b..02a59067 100644 --- a/src/shared/components/post/post-form.tsx +++ b/src/shared/components/post/post-form.tsx @@ -32,7 +32,7 @@ import { validURL, webArchiveUrl, } from "../../utils"; -import { debounce } from "../../utils/helpers/debounce"; +import debounce from "../../utils/helpers/debounce"; import { Icon, Spinner } from "../common/icon"; import { LanguageSelect } from "../common/language-select"; import { MarkdownTextArea } from "../common/markdown-textarea"; diff --git a/src/shared/components/post/post-listing.tsx b/src/shared/components/post/post-listing.tsx index 3c90c976..914c16d3 100644 --- a/src/shared/components/post/post-listing.tsx +++ b/src/shared/components/post/post-listing.tsx @@ -42,16 +42,16 @@ import { setupTippy, 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"; -import { canAdmin } from "../../utils/roles/can-admin"; -import { canMod } from "../../utils/roles/can-mod"; -import { isAdmin } from "../../utils/roles/is-admin"; -import { isBanned } from "../../utils/roles/is-banned"; -import { isMod } from "../../utils/roles/is-mod"; +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"; +import canAdmin from "../../utils/roles/can-admin"; +import canMod from "../../utils/roles/can-mod"; +import isAdmin from "../../utils/roles/is-admin"; +import isBanned from "../../utils/roles/is-banned"; +import isMod from "../../utils/roles/is-mod"; import { Icon, PurgeWarning, Spinner } from "../common/icon"; import { MomentTime } from "../common/moment-time"; import { PictrsImage } from "../common/pictrs-image"; diff --git a/src/shared/components/post/post.tsx b/src/shared/components/post/post.tsx index 3dee31a7..8b10c058 100644 --- a/src/shared/components/post/post.tsx +++ b/src/shared/components/post/post.tsx @@ -83,8 +83,8 @@ import { updateCommunityBlock, updatePersonBlock, } from "../../utils"; -import { isBrowser } from "../../utils/browser/is-browser"; -import { debounce } from "../../utils/helpers/debounce"; +import isBrowser from "../../utils/browser/is-browser"; +import debounce from "../../utils/helpers/debounce"; 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 e56056ac..844737cc 100644 --- a/src/shared/components/search.tsx +++ b/src/shared/components/search.tsx @@ -46,9 +46,9 @@ import { setIsoData, showLocal, } from "../utils"; -import { debounce } from "../utils/helpers/debounce"; -import { getQueryParams } from "../utils/helpers/get-query-params"; -import { getQueryString } from "../utils/helpers/get-query-string"; +import debounce from "../utils/helpers/debounce"; +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"; diff --git a/src/shared/env.ts b/src/shared/env.ts index 969f8761..2f3a5cf1 100644 --- a/src/shared/env.ts +++ b/src/shared/env.ts @@ -1,4 +1,4 @@ -import { isBrowser } from "./utils/browser/is-browser"; +import isBrowser from "./utils/browser/is-browser"; const testHost = "0.0.0.0:8536"; diff --git a/src/shared/i18next.ts b/src/shared/i18next.ts index 0a705ae6..d0a4e587 100644 --- a/src/shared/i18next.ts +++ b/src/shared/i18next.ts @@ -31,7 +31,7 @@ import { sv } from "./translations/sv"; import { vi } from "./translations/vi"; import { zh } from "./translations/zh"; import { zh_Hant } from "./translations/zh_Hant"; -import { isBrowser } from "./utils/browser/is-browser"; +import isBrowser from "./utils/browser/is-browser"; export const languages = [ { resource: ar, code: "ar", name: "العربية" }, diff --git a/src/shared/services/UserService.ts b/src/shared/services/UserService.ts index 8f553aba..18d0804a 100644 --- a/src/shared/services/UserService.ts +++ b/src/shared/services/UserService.ts @@ -5,7 +5,7 @@ import { LoginResponse, MyUserInfo } from "lemmy-js-client"; import { isHttps } from "../env"; import { i18n } from "../i18next"; import { isAuthPath, toast } from "../utils"; -import { isBrowser } from "../utils/browser/is-browser"; +import isBrowser from "../utils/browser/is-browser"; interface Claims { sub: number; diff --git a/src/shared/utils.ts b/src/shared/utils.ts index be461dcd..35e30a36 100644 --- a/src/shared/utils.ts +++ b/src/shared/utils.ts @@ -49,10 +49,10 @@ import { VoteType, } from "./interfaces"; import { HttpService, UserService } from "./services"; -import { isBrowser } from "./utils/browser/is-browser"; -import { debounce } from "./utils/helpers/debounce"; -import { groupBy } from "./utils/helpers/group-by"; import { RequestState } from "./services/HttpService"; +import isBrowser from "./utils/browser/is-browser"; +import debounce from "./utils/helpers/debounce"; +import { groupBy } from "./utils/helpers/group-by"; let Tribute: any; if (isBrowser()) { From e4f9c31cfa17196a908d98757319e83353ed6639 Mon Sep 17 00:00:00 2001 From: Alec Armbruster <35377827+alectrocute@users.noreply.github.com> Date: Tue, 20 Jun 2023 10:17:16 -0400 Subject: [PATCH 16/19] make suggested changes --- src/shared/components/app/navbar.tsx | 7 +++--- .../components/comment/comment-node.tsx | 14 ++++++----- .../components/community/communities.tsx | 5 ++-- src/shared/components/community/community.tsx | 5 ++-- src/shared/components/community/sidebar.tsx | 4 +-- src/shared/components/home/home.tsx | 11 ++++---- src/shared/components/home/login.tsx | 2 +- src/shared/components/home/signup.tsx | 2 +- src/shared/components/modlog.tsx | 9 +++---- src/shared/components/person/profile.tsx | 9 +++---- src/shared/components/person/reports.tsx | 2 +- src/shared/components/person/settings.tsx | 2 +- src/shared/components/post/create-post.tsx | 4 +-- src/shared/components/post/post-listing.tsx | 21 ++++++++-------- src/shared/components/post/post.tsx | 4 +-- src/shared/components/search.tsx | 6 ++--- src/shared/utils/browser/can-share.ts | 2 +- src/shared/utils/browser/index.ts | 5 ++++ src/shared/utils/browser/share.ts | 2 +- src/shared/utils/helpers/get-query-params.ts | 2 +- src/shared/utils/helpers/index.ts | 8 ++++++ src/shared/utils/roles/am-mod.ts | 2 +- src/shared/utils/roles/can-admin.ts | 2 +- .../utils/roles/can-create-community.ts | 2 +- src/shared/utils/roles/index.ts | 25 +++++++++++++++++++ src/shared/utils/types/index.ts | 3 +++ tsconfig.json | 8 +++++- 27 files changed, 102 insertions(+), 66 deletions(-) create mode 100644 src/shared/utils/browser/index.ts create mode 100644 src/shared/utils/helpers/index.ts create mode 100644 src/shared/utils/roles/index.ts create mode 100644 src/shared/utils/types/index.ts diff --git a/src/shared/components/app/navbar.tsx b/src/shared/components/app/navbar.tsx index 9f68656a..d412e006 100644 --- a/src/shared/components/app/navbar.tsx +++ b/src/shared/components/app/navbar.tsx @@ -1,3 +1,6 @@ +import { isBrowser } from "@utils/browser"; +import { poll } from "@utils/helpers"; +import { amAdmin, canCreateCommunity } from "@utils/roles"; import { Component, createRef, linkEvent } from "inferno"; import { NavLink } from "inferno-router"; import { @@ -17,10 +20,6 @@ import { 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"; import { PictrsImage } from "../common/pictrs-image"; diff --git a/src/shared/components/comment/comment-node.tsx b/src/shared/components/comment/comment-node.tsx index bcbd6050..e60ad437 100644 --- a/src/shared/components/comment/comment-node.tsx +++ b/src/shared/components/comment/comment-node.tsx @@ -1,3 +1,11 @@ +import { + amCommunityCreator, + canAdmin, + canMod, + isAdmin, + isBanned, + isMod, +} from "@utils/roles"; import classNames from "classnames"; import { Component, InfernoNode, linkEvent } from "inferno"; import { Link } from "inferno-router"; @@ -53,12 +61,6 @@ import { setupTippy, showScores, } from "../../utils"; -import amCommunityCreator from "../../utils/roles/am-community-creator"; -import canAdmin from "../../utils/roles/can-admin"; -import canMod from "../../utils/roles/can-mod"; -import isAdmin from "../../utils/roles/is-admin"; -import isBanned from "../../utils/roles/is-banned"; -import isMod from "../../utils/roles/is-mod"; import { Icon, PurgeWarning, Spinner } from "../common/icon"; import { MomentTime } from "../common/moment-time"; import { CommunityLink } from "../community/community-link"; diff --git a/src/shared/components/community/communities.tsx b/src/shared/components/community/communities.tsx index 6a2ae6ad..a64b9e96 100644 --- a/src/shared/components/community/communities.tsx +++ b/src/shared/components/community/communities.tsx @@ -1,3 +1,5 @@ +import { getQueryParams, getQueryString } from "@utils/helpers"; +import type { QueryParams } from "@utils/types"; import { Component, linkEvent } from "inferno"; import { CommunityResponse, @@ -20,9 +22,6 @@ 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 { 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 42a77836..203288c5 100644 --- a/src/shared/components/community/community.tsx +++ b/src/shared/components/community/community.tsx @@ -1,3 +1,5 @@ +import { getQueryParams, getQueryString } from "@utils/helpers"; +import type { QueryParams } from "@utils/types"; import { Component, linkEvent } from "inferno"; import { RouteComponentProps } from "inferno-router/dist/Route"; import { @@ -86,9 +88,6 @@ 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"; diff --git a/src/shared/components/community/sidebar.tsx b/src/shared/components/community/sidebar.tsx index 72c94c09..2396b527 100644 --- a/src/shared/components/community/sidebar.tsx +++ b/src/shared/components/community/sidebar.tsx @@ -1,3 +1,4 @@ +import { amAdmin, amMod, amTopMod } from "@utils/roles"; import { Component, InfernoNode, linkEvent } from "inferno"; import { T } from "inferno-i18next-dess"; import { Link } from "inferno-router"; @@ -17,9 +18,6 @@ import { import { i18n } from "../../i18next"; import { UserService } from "../../services"; import { getUnixTime, hostname, mdToHtml, myAuthRequired } from "../../utils"; -import amAdmin from "../../utils/roles/am-admin"; -import amMod from "../../utils/roles/am-mod"; -import amTopMod from "../../utils/roles/am-top-mod"; import { Badges } from "../common/badges"; import { BannerIconHeader } from "../common/banner-icon-header"; import { Icon, PurgeWarning, Spinner } from "../common/icon"; diff --git a/src/shared/components/home/home.tsx b/src/shared/components/home/home.tsx index 4a1e9d70..deca4589 100644 --- a/src/shared/components/home/home.tsx +++ b/src/shared/components/home/home.tsx @@ -1,5 +1,8 @@ +import { getQueryParams, getQueryString } from "@utils/helpers"; +import { canCreateCommunity } from "@utils/roles"; +import type { QueryParams } from "@utils/types"; import { NoOptionI18nKeys } from "i18next"; -import { Component, linkEvent, MouseEventHandler } from "inferno"; +import { Component, MouseEventHandler, linkEvent } from "inferno"; import { T } from "inferno-i18next-dess"; import { Link } from "inferno-router"; import { @@ -57,6 +60,7 @@ import { UserService } from "../../services"; import { FirstLoadService } from "../../services/FirstLoadService"; import { HttpService, RequestState } from "../../services/HttpService"; import { + RouteDataResponse, commentsToFlatNodes, editComment, editPost, @@ -73,7 +77,6 @@ import { postToCommentSortType, relTags, restoreScrollPosition, - RouteDataResponse, saveScrollPosition, setIsoData, setupTippy, @@ -82,10 +85,6 @@ 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 e25222e0..d1b3abd0 100644 --- a/src/shared/components/home/login.tsx +++ b/src/shared/components/home/login.tsx @@ -1,10 +1,10 @@ +import { isBrowser } from "@utils/browser"; import { Component, linkEvent } from "inferno"; import { GetSiteResponse, LoginResponse } from "lemmy-js-client"; import { i18n } from "../../i18next"; import { UserService } from "../../services"; import { HttpService, RequestState } from "../../services/HttpService"; 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 287bdb7b..6961a14a 100644 --- a/src/shared/components/home/signup.tsx +++ b/src/shared/components/home/signup.tsx @@ -1,3 +1,4 @@ +import { isBrowser } from "@utils/browser"; import { Options, passwordStrength } from "check-password-strength"; import { NoOptionI18nKeys } from "i18next"; import { Component, linkEvent } from "inferno"; @@ -20,7 +21,6 @@ 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 3ea910c5..a37486f9 100644 --- a/src/shared/components/modlog.tsx +++ b/src/shared/components/modlog.tsx @@ -1,3 +1,6 @@ +import { debounce, getQueryParams, getQueryString } from "@utils/helpers"; +import { amAdmin, amMod } from "@utils/roles"; +import type { QueryParams } from "@utils/types"; import { NoOptionI18nKeys } from "i18next"; import { Component, linkEvent } from "inferno"; import { T } from "inferno-i18next-dess"; @@ -44,12 +47,6 @@ import { personToChoice, setIsoData, } from "../utils"; -import debounce from "../utils/helpers/debounce"; -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 d99ab124..b4bec066 100644 --- a/src/shared/components/person/profile.tsx +++ b/src/shared/components/person/profile.tsx @@ -1,3 +1,6 @@ +import { getQueryParams, getQueryString } from "@utils/helpers"; +import { canMod, isAdmin, isBanned } from "@utils/roles"; +import type { QueryParams } from "@utils/types"; import classNames from "classnames"; import { NoOptionI18nKeys } from "i18next"; import { Component, linkEvent } from "inferno"; @@ -76,12 +79,6 @@ 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/person/reports.tsx b/src/shared/components/person/reports.tsx index ce9d215a..de303a87 100644 --- a/src/shared/components/person/reports.tsx +++ b/src/shared/components/person/reports.tsx @@ -1,3 +1,4 @@ +import { amAdmin } from "@utils/roles"; import { Component, linkEvent } from "inferno"; import { CommentReportResponse, @@ -31,7 +32,6 @@ import { myAuthRequired, setIsoData, } from "../../utils"; -import amAdmin from "../../utils/roles/am-admin"; import { CommentReport } from "../comment/comment-report"; import { HtmlTags } from "../common/html-tags"; import { Spinner } from "../common/icon"; diff --git a/src/shared/components/person/settings.tsx b/src/shared/components/person/settings.tsx index 7cb9308c..08567dcb 100644 --- a/src/shared/components/person/settings.tsx +++ b/src/shared/components/person/settings.tsx @@ -1,3 +1,4 @@ +import { debounce } from "@utils/helpers"; import { NoOptionI18nKeys } from "i18next"; import { Component, linkEvent } from "inferno"; import { @@ -35,7 +36,6 @@ import { updateCommunityBlock, updatePersonBlock, } from "../../utils"; -import debounce from "../../utils/helpers/debounce"; import { HtmlTags } from "../common/html-tags"; import { Icon, Spinner } from "../common/icon"; import { ImageUploadForm } from "../common/image-upload-form"; diff --git a/src/shared/components/post/create-post.tsx b/src/shared/components/post/create-post.tsx index 4f367712..ad231346 100644 --- a/src/shared/components/post/create-post.tsx +++ b/src/shared/components/post/create-post.tsx @@ -1,3 +1,5 @@ +import { getQueryParams } from "@utils/helpers"; +import type { QueryParams } from "@utils/types"; import { Component } from "inferno"; import { RouteComponentProps } from "inferno-router/dist/Route"; import { @@ -24,8 +26,6 @@ import { 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 914c16d3..928c1ece 100644 --- a/src/shared/components/post/post-listing.tsx +++ b/src/shared/components/post/post-listing.tsx @@ -1,3 +1,14 @@ +import { canShare, share } from "@utils/browser"; +import { + amAdmin, + amCommunityCreator, + amMod, + canAdmin, + canMod, + isAdmin, + isBanned, + isMod, +} from "@utils/roles"; import classNames from "classnames"; import { Component, linkEvent } from "inferno"; import { Link } from "inferno-router"; @@ -42,16 +53,6 @@ import { setupTippy, 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"; -import canAdmin from "../../utils/roles/can-admin"; -import canMod from "../../utils/roles/can-mod"; -import isAdmin from "../../utils/roles/is-admin"; -import isBanned from "../../utils/roles/is-banned"; -import isMod from "../../utils/roles/is-mod"; import { Icon, PurgeWarning, Spinner } from "../common/icon"; import { MomentTime } from "../common/moment-time"; import { PictrsImage } from "../common/pictrs-image"; diff --git a/src/shared/components/post/post.tsx b/src/shared/components/post/post.tsx index 8b10c058..60059fee 100644 --- a/src/shared/components/post/post.tsx +++ b/src/shared/components/post/post.tsx @@ -1,3 +1,5 @@ +import { isBrowser } from "@utils/browser"; +import { debounce } from "@utils/helpers"; import autosize from "autosize"; import { Component, createRef, linkEvent, RefObject } from "inferno"; import { @@ -83,8 +85,6 @@ import { updateCommunityBlock, updatePersonBlock, } from "../../utils"; -import isBrowser from "../../utils/browser/is-browser"; -import debounce from "../../utils/helpers/debounce"; 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 844737cc..0dc4eb77 100644 --- a/src/shared/components/search.tsx +++ b/src/shared/components/search.tsx @@ -1,3 +1,5 @@ +import { debounce, getQueryParams, getQueryString } from "@utils/helpers"; +import type { QueryParams } from "@utils/types"; import type { NoOptionI18nKeys } from "i18next"; import { Component, linkEvent } from "inferno"; import { @@ -46,10 +48,6 @@ import { setIsoData, showLocal, } from "../utils"; -import debounce from "../utils/helpers/debounce"; -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/utils/browser/can-share.ts b/src/shared/utils/browser/can-share.ts index f141fe50..77de9838 100644 --- a/src/shared/utils/browser/can-share.ts +++ b/src/shared/utils/browser/can-share.ts @@ -1,4 +1,4 @@ -import isBrowser from "./is-browser"; +import { isBrowser } from "@utils/browser"; export default function canShare() { return isBrowser() && !!navigator.canShare; diff --git a/src/shared/utils/browser/index.ts b/src/shared/utils/browser/index.ts new file mode 100644 index 00000000..a7a08a50 --- /dev/null +++ b/src/shared/utils/browser/index.ts @@ -0,0 +1,5 @@ +import canShare from "./can-share"; +import isBrowser from "./is-browser"; +import share from "./share"; + +export { canShare, isBrowser, share }; diff --git a/src/shared/utils/browser/share.ts b/src/shared/utils/browser/share.ts index 9ae94974..98d17712 100644 --- a/src/shared/utils/browser/share.ts +++ b/src/shared/utils/browser/share.ts @@ -1,4 +1,4 @@ -import isBrowser from "./is-browser"; +import { isBrowser } from "@utils/browser"; export default function share(shareData: ShareData) { if (isBrowser()) { diff --git a/src/shared/utils/helpers/get-query-params.ts b/src/shared/utils/helpers/get-query-params.ts index 5488c6ea..627341e4 100644 --- a/src/shared/utils/helpers/get-query-params.ts +++ b/src/shared/utils/helpers/get-query-params.ts @@ -1,4 +1,4 @@ -import isBrowser from "../browser/is-browser"; +import { isBrowser } from "@utils/browser"; export default function getQueryParams< T extends Record diff --git a/src/shared/utils/helpers/index.ts b/src/shared/utils/helpers/index.ts new file mode 100644 index 00000000..663afbf9 --- /dev/null +++ b/src/shared/utils/helpers/index.ts @@ -0,0 +1,8 @@ +import debounce from "./debounce"; +import getQueryParams from "./get-query-params"; +import getQueryString from "./get-query-string"; +import { groupBy } from "./group-by"; +import poll from "./poll"; +import sleep from "./sleep"; + +export { debounce, getQueryParams, getQueryString, groupBy, poll, sleep }; diff --git a/src/shared/utils/roles/am-mod.ts b/src/shared/utils/roles/am-mod.ts index 85483dae..c0632f78 100644 --- a/src/shared/utils/roles/am-mod.ts +++ b/src/shared/utils/roles/am-mod.ts @@ -1,6 +1,6 @@ +import { isMod } from "@utils/roles"; import { CommunityModeratorView } from "lemmy-js-client"; import { UserService } from "../../services"; -import isMod from "./is-mod"; export default function amMod( mods?: CommunityModeratorView[], diff --git a/src/shared/utils/roles/can-admin.ts b/src/shared/utils/roles/can-admin.ts index cac451e9..55bfd1cf 100644 --- a/src/shared/utils/roles/can-admin.ts +++ b/src/shared/utils/roles/can-admin.ts @@ -1,6 +1,6 @@ +import { canMod } from "@utils/roles"; import { PersonView } from "lemmy-js-client"; import { UserService } from "../../services"; -import canMod from "./can-mod"; export default function canAdmin( creatorId: number, diff --git a/src/shared/utils/roles/can-create-community.ts b/src/shared/utils/roles/can-create-community.ts index b0bfeddc..1b5cf05c 100644 --- a/src/shared/utils/roles/can-create-community.ts +++ b/src/shared/utils/roles/can-create-community.ts @@ -1,6 +1,6 @@ +import { amAdmin } from "@utils/roles"; import { GetSiteResponse } from "lemmy-js-client"; import { UserService } from "../../services"; -import amAdmin from "./am-admin"; export default function canCreateCommunity( siteRes: GetSiteResponse, diff --git a/src/shared/utils/roles/index.ts b/src/shared/utils/roles/index.ts new file mode 100644 index 00000000..4762637a --- /dev/null +++ b/src/shared/utils/roles/index.ts @@ -0,0 +1,25 @@ +import amAdmin from "./am-admin"; +import amCommunityCreator from "./am-community-creator"; +import amMod from "./am-mod"; +import amSiteCreator from "./am-site-creator"; +import amTopMod from "./am-top-mod"; +import canAdmin from "./can-admin"; +import canCreateCommunity from "./can-create-community"; +import canMod from "./can-mod"; +import isAdmin from "./is-admin"; +import isBanned from "./is-banned"; +import isMod from "./is-mod"; + +export { + amAdmin, + amCommunityCreator, + amMod, + amSiteCreator, + amTopMod, + canAdmin, + canCreateCommunity, + canMod, + isAdmin, + isBanned, + isMod, +}; diff --git a/src/shared/utils/types/index.ts b/src/shared/utils/types/index.ts new file mode 100644 index 00000000..9b4a1cec --- /dev/null +++ b/src/shared/utils/types/index.ts @@ -0,0 +1,3 @@ +import { QueryParams } from "./query-params"; + +export { QueryParams }; diff --git a/tsconfig.json b/tsconfig.json index 3b7d3e41..600d33aa 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -18,7 +18,13 @@ "noImplicitReturns": true, "experimentalDecorators": true, "strictNullChecks": true, - "noFallthroughCasesInSwitch": true + "noFallthroughCasesInSwitch": true, + "paths": { + "@utils/roles": ["./shared/utils/roles/index"], + "@utils/browser": ["./shared/utils/browser/index"], + "@utils/helpers": ["./shared/utils/helpers/index"], + "@utils/types": ["./shared/utils/types/index"], + } }, "include": [ "src/**/*.ts", From 2d4c9dd1c207432b5e8aaaa15efb9ea5e94aecb9 Mon Sep 17 00:00:00 2001 From: Alec Armbruster <35377827+alectrocute@users.noreply.github.com> Date: Tue, 20 Jun 2023 10:21:31 -0400 Subject: [PATCH 17/19] make suggested changes --- lemmy-translations | 2 +- src/shared/components/post/post-form.tsx | 2 +- src/shared/env.ts | 2 +- src/shared/i18next.ts | 2 +- src/shared/services/UserService.ts | 2 +- src/shared/utils.ts | 5 ++--- 6 files changed, 7 insertions(+), 8 deletions(-) diff --git a/lemmy-translations b/lemmy-translations index 7fc71d08..c9a07885 160000 --- a/lemmy-translations +++ b/lemmy-translations @@ -1 +1 @@ -Subproject commit 7fc71d0860bbe5c6d620ec27112350ffe5b9229c +Subproject commit c9a07885f35cf334d3cf167cb57587a8177fc3fb diff --git a/src/shared/components/post/post-form.tsx b/src/shared/components/post/post-form.tsx index 02a59067..9742b345 100644 --- a/src/shared/components/post/post-form.tsx +++ b/src/shared/components/post/post-form.tsx @@ -1,3 +1,4 @@ +import { debounce } from "@utils/helpers"; import autosize from "autosize"; import { Component, InfernoNode, linkEvent } from "inferno"; import { @@ -32,7 +33,6 @@ import { validURL, webArchiveUrl, } from "../../utils"; -import debounce from "../../utils/helpers/debounce"; import { Icon, Spinner } from "../common/icon"; import { LanguageSelect } from "../common/language-select"; import { MarkdownTextArea } from "../common/markdown-textarea"; diff --git a/src/shared/env.ts b/src/shared/env.ts index 2f3a5cf1..287912d1 100644 --- a/src/shared/env.ts +++ b/src/shared/env.ts @@ -1,4 +1,4 @@ -import isBrowser from "./utils/browser/is-browser"; +import { isBrowser } from "@utils/browser"; const testHost = "0.0.0.0:8536"; diff --git a/src/shared/i18next.ts b/src/shared/i18next.ts index d0a4e587..ff5f77f1 100644 --- a/src/shared/i18next.ts +++ b/src/shared/i18next.ts @@ -1,3 +1,4 @@ +import { isBrowser } from "@utils/browser"; import i18next, { i18nTyped, Resource } from "i18next"; import { UserService } from "./services"; import { ar } from "./translations/ar"; @@ -31,7 +32,6 @@ import { sv } from "./translations/sv"; import { vi } from "./translations/vi"; import { zh } from "./translations/zh"; import { zh_Hant } from "./translations/zh_Hant"; -import isBrowser from "./utils/browser/is-browser"; export const languages = [ { resource: ar, code: "ar", name: "العربية" }, diff --git a/src/shared/services/UserService.ts b/src/shared/services/UserService.ts index 18d0804a..346d833a 100644 --- a/src/shared/services/UserService.ts +++ b/src/shared/services/UserService.ts @@ -1,11 +1,11 @@ // import Cookies from 'js-cookie'; +import { isBrowser } from "@utils/browser"; import IsomorphicCookie from "isomorphic-cookie"; import jwt_decode from "jwt-decode"; import { LoginResponse, MyUserInfo } from "lemmy-js-client"; import { isHttps } from "../env"; import { i18n } from "../i18next"; 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 35e30a36..ad1e8a47 100644 --- a/src/shared/utils.ts +++ b/src/shared/utils.ts @@ -1,3 +1,5 @@ +import { isBrowser } from "@utils/browser"; +import { debounce, groupBy } from "@utils/helpers"; import { Picker } from "emoji-mart"; import emojiShortName from "emoji-short-name"; import { @@ -50,9 +52,6 @@ import { } from "./interfaces"; import { HttpService, UserService } from "./services"; import { RequestState } from "./services/HttpService"; -import isBrowser from "./utils/browser/is-browser"; -import debounce from "./utils/helpers/debounce"; -import { groupBy } from "./utils/helpers/group-by"; let Tribute: any; if (isBrowser()) { From fe8368548bbd3d15b618655042c47e591bbb7fc3 Mon Sep 17 00:00:00 2001 From: Alec Armbruster <35377827+alectrocute@users.noreply.github.com> Date: Tue, 20 Jun 2023 15:48:42 -0400 Subject: [PATCH 18/19] add tsconfigpathsplugin to resolve --- lemmy-translations | 2 +- package.json | 1 + webpack.config.js | 2 ++ yarn.lock | 33 ++++++++++++++++++++++++++++++++- 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/lemmy-translations b/lemmy-translations index c9a07885..7fc71d08 160000 --- a/lemmy-translations +++ b/lemmy-translations @@ -1 +1 @@ -Subproject commit c9a07885f35cf334d3cf167cb57587a8177fc3fb +Subproject commit 7fc71d0860bbe5c6d620ec27112350ffe5b9229c diff --git a/package.json b/package.json index 2def768f..0fac882a 100644 --- a/package.json +++ b/package.json @@ -121,6 +121,7 @@ "sortpack": "^2.3.4", "style-loader": "^3.3.2", "terser": "^5.17.3", + "tsconfig-paths-webpack-plugin": "^4.0.1", "typescript": "^5.0.4", "webpack-dev-server": "4.15.0" }, diff --git a/webpack.config.js b/webpack.config.js index 67b10a98..fd210f05 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -5,6 +5,7 @@ const CopyPlugin = require("copy-webpack-plugin"); const RunNodeWebpackPlugin = require("run-node-webpack-plugin"); const merge = require("lodash/merge"); const { ServiceWorkerPlugin } = require("service-worker-webpack"); +const TsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin"); const banner = ` hash:[contentHash], chunkhash:[chunkhash], name:[name], filebase:[base], query:[query], file:[file] Source code: https://github.com/LemmyNet/lemmy-ui @@ -19,6 +20,7 @@ const base = { hashFunction: "xxhash64", }, resolve: { + plugins: [new TsconfigPathsPlugin()], extensions: [".js", ".jsx", ".ts", ".tsx"], }, performance: { diff --git a/yarn.lock b/yarn.lock index 8ed82982..1e31d7cf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2714,7 +2714,7 @@ chalk@^2.0.0, chalk@^2.0.1: escape-string-regexp "^1.0.5" supports-color "^5.3.0" -chalk@^4.0.0, chalk@^4.0.2: +chalk@^4.0.0, chalk@^4.0.2, chalk@^4.1.0: version "4.1.2" resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.2.tgz#aac4e2b7734a740867aeb16bf02aad556a1e7a01" integrity sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA== @@ -3578,6 +3578,14 @@ enhanced-resolve@^5.14.0: graceful-fs "^4.2.4" tapable "^2.2.0" +enhanced-resolve@^5.7.0: + version "5.15.0" + resolved "https://registry.yarnpkg.com/enhanced-resolve/-/enhanced-resolve-5.15.0.tgz#1af946c7d93603eb88e9896cee4904dc012e9c35" + integrity sha512-LXYT42KJ7lpIKECr2mAXIaMldcNCh/7E0KBKOu4KSfkHmP+mZmSs+8V5gBAqisWBy0OO4W5Oyys0GO1Y8KtdKg== + dependencies: + graceful-fs "^4.2.4" + tapable "^2.2.0" + entities@^4.2.0, entities@^4.4.0: version "4.5.0" resolved "https://registry.yarnpkg.com/entities/-/entities-4.5.0.tgz#5d268ea5e7113ec74c4d033b79ea5a35a488fb48" @@ -8686,6 +8694,11 @@ strip-ansi@^7.0.1: dependencies: ansi-regex "^6.0.1" +strip-bom@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-3.0.0.tgz#2334c18e9c759f7bdd56fdef7e9ae3d588e68ed3" + integrity sha512-vavAMRXOgBVNF6nyEEmL3DBK19iRpDcoIwW+swQ+CbGiu7lju6t+JklA1MHweoWtadgt4ISVUsXLyDq34ddcwA== + strip-comments@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/strip-comments/-/strip-comments-2.0.1.tgz#4ad11c3fbcac177a67a40ac224ca339ca1c1ba9b" @@ -8938,6 +8951,24 @@ tributejs@^5.1.3: resolved "https://registry.yarnpkg.com/tributejs/-/tributejs-5.1.3.tgz#980600fc72865be5868893078b4bfde721129eae" integrity sha512-B5CXihaVzXw+1UHhNFyAwUTMDk1EfoLP5Tj1VhD9yybZ1I8DZJEv8tZ1l0RJo0t0tk9ZhR8eG5tEsaCvRigmdQ== +tsconfig-paths-webpack-plugin@^4.0.1: + version "4.0.1" + resolved "https://registry.yarnpkg.com/tsconfig-paths-webpack-plugin/-/tsconfig-paths-webpack-plugin-4.0.1.tgz#a24651d0f69668a1abad38d3c2489855c257460d" + integrity sha512-m5//KzLoKmqu2MVix+dgLKq70MnFi8YL8sdzQZ6DblmCdfuq/y3OqvJd5vMndg2KEVCOeNz8Es4WVZhYInteLw== + dependencies: + chalk "^4.1.0" + enhanced-resolve "^5.7.0" + tsconfig-paths "^4.1.2" + +tsconfig-paths@^4.1.2: + version "4.2.0" + resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-4.2.0.tgz#ef78e19039133446d244beac0fd6a1632e2d107c" + integrity sha512-NoZ4roiN7LnbKn9QqE1amc9DJfzvZXxF4xDavcOWt1BPkdx+m+0gJuPM+S0vCe7zTJMYUP0R8pO2XMr+Y8oLIg== + dependencies: + json5 "^2.2.2" + minimist "^1.2.6" + strip-bom "^3.0.0" + tslib@^1.8.1: version "1.14.1" resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00" From cc4ecb585ea7ed0fb297eace80f9948db25f9661 Mon Sep 17 00:00:00 2001 From: Alec Armbruster <35377827+alectrocute@users.noreply.github.com> Date: Tue, 20 Jun 2023 15:51:37 -0400 Subject: [PATCH 19/19] forgot an import --- src/shared/components/common/markdown-textarea.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shared/components/common/markdown-textarea.tsx b/src/shared/components/common/markdown-textarea.tsx index 6e378f96..1746716c 100644 --- a/src/shared/components/common/markdown-textarea.tsx +++ b/src/shared/components/common/markdown-textarea.tsx @@ -1,3 +1,4 @@ +import { isBrowser } from "@utils/browser"; import autosize from "autosize"; import classNames from "classnames"; import { NoOptionI18nKeys } from "i18next"; @@ -20,7 +21,6 @@ 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";