mirror of
https://github.com/LemmyNet/lemmy-ui.git
synced 2025-01-01 13:08:09 +00:00
break out browser and helper methods
This commit is contained in:
parent
0854af3794
commit
976ed12d07
|
@ -11,14 +11,14 @@ import { UserService } from "../../services";
|
||||||
import { HttpService, RequestState } from "../../services/HttpService";
|
import { HttpService, RequestState } from "../../services/HttpService";
|
||||||
import {
|
import {
|
||||||
donateLemmyUrl,
|
donateLemmyUrl,
|
||||||
isBrowser,
|
|
||||||
myAuth,
|
myAuth,
|
||||||
numToSI,
|
numToSI,
|
||||||
poll,
|
|
||||||
showAvatars,
|
showAvatars,
|
||||||
toast,
|
toast,
|
||||||
updateUnreadCountsInterval,
|
updateUnreadCountsInterval,
|
||||||
} from "../../utils";
|
} from "../../utils";
|
||||||
|
import { isBrowser } from "../../utils/browser/is-browser";
|
||||||
|
import { poll } from "../../utils/helpers/poll";
|
||||||
import { amAdmin } from "../../utils/roles/am-admin";
|
import { amAdmin } from "../../utils/roles/am-admin";
|
||||||
import { canCreateCommunity } from "../../utils/roles/can-create-community";
|
import { canCreateCommunity } from "../../utils/roles/can-create-community";
|
||||||
import { Icon } from "../common/icon";
|
import { Icon } from "../common/icon";
|
||||||
|
|
|
@ -7,7 +7,6 @@ import { HttpService, UserService } from "../../services";
|
||||||
import {
|
import {
|
||||||
concurrentImageUpload,
|
concurrentImageUpload,
|
||||||
customEmojisLookup,
|
customEmojisLookup,
|
||||||
isBrowser,
|
|
||||||
markdownFieldCharacterLimit,
|
markdownFieldCharacterLimit,
|
||||||
markdownHelpUrl,
|
markdownHelpUrl,
|
||||||
maxUploadImages,
|
maxUploadImages,
|
||||||
|
@ -20,12 +19,12 @@ import {
|
||||||
setupTribute,
|
setupTribute,
|
||||||
toast,
|
toast,
|
||||||
} from "../../utils";
|
} from "../../utils";
|
||||||
|
import { isBrowser } from "../../utils/browser/is-browser";
|
||||||
import { EmojiPicker } from "./emoji-picker";
|
import { EmojiPicker } from "./emoji-picker";
|
||||||
import { Icon, Spinner } from "./icon";
|
import { Icon, Spinner } from "./icon";
|
||||||
import { LanguageSelect } from "./language-select";
|
import { LanguageSelect } from "./language-select";
|
||||||
import NavigationPrompt from "./navigation-prompt";
|
import NavigationPrompt from "./navigation-prompt";
|
||||||
import ProgressBar from "./progress-bar";
|
import ProgressBar from "./progress-bar";
|
||||||
|
|
||||||
interface MarkdownTextAreaProps {
|
interface MarkdownTextAreaProps {
|
||||||
initialContent?: string;
|
initialContent?: string;
|
||||||
initialLanguageId?: number;
|
initialLanguageId?: number;
|
||||||
|
|
|
@ -11,17 +11,17 @@ import { InitialFetchRequest } from "../../interfaces";
|
||||||
import { FirstLoadService } from "../../services/FirstLoadService";
|
import { FirstLoadService } from "../../services/FirstLoadService";
|
||||||
import { HttpService, RequestState } from "../../services/HttpService";
|
import { HttpService, RequestState } from "../../services/HttpService";
|
||||||
import {
|
import {
|
||||||
QueryParams,
|
|
||||||
editCommunity,
|
editCommunity,
|
||||||
getPageFromString,
|
getPageFromString,
|
||||||
getQueryParams,
|
|
||||||
getQueryString,
|
|
||||||
myAuth,
|
myAuth,
|
||||||
myAuthRequired,
|
myAuthRequired,
|
||||||
numToSI,
|
numToSI,
|
||||||
setIsoData,
|
setIsoData,
|
||||||
showLocal,
|
showLocal,
|
||||||
} from "../../utils";
|
} 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 { HtmlTags } from "../common/html-tags";
|
||||||
import { Spinner } from "../common/icon";
|
import { Spinner } from "../common/icon";
|
||||||
import { ListingTypeSelect } from "../common/listing-type-select";
|
import { ListingTypeSelect } from "../common/listing-type-select";
|
||||||
|
|
|
@ -62,7 +62,6 @@ import { UserService } from "../../services";
|
||||||
import { FirstLoadService } from "../../services/FirstLoadService";
|
import { FirstLoadService } from "../../services/FirstLoadService";
|
||||||
import { HttpService, RequestState } from "../../services/HttpService";
|
import { HttpService, RequestState } from "../../services/HttpService";
|
||||||
import {
|
import {
|
||||||
QueryParams,
|
|
||||||
commentsToFlatNodes,
|
commentsToFlatNodes,
|
||||||
communityRSSUrl,
|
communityRSSUrl,
|
||||||
editComment,
|
editComment,
|
||||||
|
@ -74,8 +73,6 @@ import {
|
||||||
getCommentParentId,
|
getCommentParentId,
|
||||||
getDataTypeString,
|
getDataTypeString,
|
||||||
getPageFromString,
|
getPageFromString,
|
||||||
getQueryParams,
|
|
||||||
getQueryString,
|
|
||||||
myAuth,
|
myAuth,
|
||||||
postToCommentSortType,
|
postToCommentSortType,
|
||||||
relTags,
|
relTags,
|
||||||
|
@ -88,6 +85,9 @@ import {
|
||||||
updateCommunityBlock,
|
updateCommunityBlock,
|
||||||
updatePersonBlock,
|
updatePersonBlock,
|
||||||
} from "../../utils";
|
} 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 { CommentNodes } from "../comment/comment-nodes";
|
||||||
import { BannerIconHeader } from "../common/banner-icon-header";
|
import { BannerIconHeader } from "../common/banner-icon-header";
|
||||||
import { DataTypeSelect } from "../common/data-type-select";
|
import { DataTypeSelect } from "../common/data-type-select";
|
||||||
|
@ -99,7 +99,6 @@ import { Sidebar } from "../community/sidebar";
|
||||||
import { SiteSidebar } from "../home/site-sidebar";
|
import { SiteSidebar } from "../home/site-sidebar";
|
||||||
import { PostListings } from "../post/post-listings";
|
import { PostListings } from "../post/post-listings";
|
||||||
import { CommunityLink } from "./community-link";
|
import { CommunityLink } from "./community-link";
|
||||||
|
|
||||||
interface State {
|
interface State {
|
||||||
communityRes: RequestState<GetCommunityResponse>;
|
communityRes: RequestState<GetCommunityResponse>;
|
||||||
postsRes: RequestState<GetPostsResponse>;
|
postsRes: RequestState<GetPostsResponse>;
|
||||||
|
|
|
@ -67,13 +67,10 @@ import {
|
||||||
getCommentParentId,
|
getCommentParentId,
|
||||||
getDataTypeString,
|
getDataTypeString,
|
||||||
getPageFromString,
|
getPageFromString,
|
||||||
getQueryParams,
|
|
||||||
getQueryString,
|
|
||||||
getRandomFromList,
|
getRandomFromList,
|
||||||
mdToHtml,
|
mdToHtml,
|
||||||
myAuth,
|
myAuth,
|
||||||
postToCommentSortType,
|
postToCommentSortType,
|
||||||
QueryParams,
|
|
||||||
relTags,
|
relTags,
|
||||||
restoreScrollPosition,
|
restoreScrollPosition,
|
||||||
saveScrollPosition,
|
saveScrollPosition,
|
||||||
|
@ -84,7 +81,10 @@ import {
|
||||||
trendingFetchLimit,
|
trendingFetchLimit,
|
||||||
updatePersonBlock,
|
updatePersonBlock,
|
||||||
} from "../../utils";
|
} 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 { canCreateCommunity } from "../../utils/roles/can-create-community";
|
||||||
|
import type { QueryParams } from "../../utils/types/query-params";
|
||||||
import { CommentNodes } from "../comment/comment-nodes";
|
import { CommentNodes } from "../comment/comment-nodes";
|
||||||
import { DataTypeSelect } from "../common/data-type-select";
|
import { DataTypeSelect } from "../common/data-type-select";
|
||||||
import { HtmlTags } from "../common/html-tags";
|
import { HtmlTags } from "../common/html-tags";
|
||||||
|
|
|
@ -3,7 +3,8 @@ import { GetSiteResponse, LoginResponse } from "lemmy-js-client";
|
||||||
import { i18n } from "../../i18next";
|
import { i18n } from "../../i18next";
|
||||||
import { UserService } from "../../services";
|
import { UserService } from "../../services";
|
||||||
import { HttpService, RequestState } from "../../services/HttpService";
|
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 { HtmlTags } from "../common/html-tags";
|
||||||
import { Spinner } from "../common/icon";
|
import { Spinner } from "../common/icon";
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,6 @@ import { i18n } from "../../i18next";
|
||||||
import { UserService } from "../../services";
|
import { UserService } from "../../services";
|
||||||
import { HttpService, RequestState } from "../../services/HttpService";
|
import { HttpService, RequestState } from "../../services/HttpService";
|
||||||
import {
|
import {
|
||||||
isBrowser,
|
|
||||||
joinLemmyUrl,
|
joinLemmyUrl,
|
||||||
mdToHtml,
|
mdToHtml,
|
||||||
myAuth,
|
myAuth,
|
||||||
|
@ -21,6 +20,7 @@ import {
|
||||||
toast,
|
toast,
|
||||||
validEmail,
|
validEmail,
|
||||||
} from "../../utils";
|
} from "../../utils";
|
||||||
|
import { isBrowser } from "../../utils/browser/is-browser";
|
||||||
import { HtmlTags } from "../common/html-tags";
|
import { HtmlTags } from "../common/html-tags";
|
||||||
import { Icon, Spinner } from "../common/icon";
|
import { Icon, Spinner } from "../common/icon";
|
||||||
import { MarkdownTextArea } from "../common/markdown-textarea";
|
import { MarkdownTextArea } from "../common/markdown-textarea";
|
||||||
|
|
|
@ -33,21 +33,21 @@ import { FirstLoadService } from "../services/FirstLoadService";
|
||||||
import { HttpService, RequestState } from "../services/HttpService";
|
import { HttpService, RequestState } from "../services/HttpService";
|
||||||
import {
|
import {
|
||||||
Choice,
|
Choice,
|
||||||
QueryParams,
|
|
||||||
debounce,
|
debounce,
|
||||||
fetchLimit,
|
fetchLimit,
|
||||||
fetchUsers,
|
fetchUsers,
|
||||||
getIdFromString,
|
getIdFromString,
|
||||||
getPageFromString,
|
getPageFromString,
|
||||||
getQueryParams,
|
|
||||||
getQueryString,
|
|
||||||
getUpdatedSearchId,
|
getUpdatedSearchId,
|
||||||
myAuth,
|
myAuth,
|
||||||
personToChoice,
|
personToChoice,
|
||||||
setIsoData,
|
setIsoData,
|
||||||
} from "../utils";
|
} 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 { amAdmin } from "../utils/roles/am-admin";
|
||||||
import { amMod } from "../utils/roles/am-mod";
|
import { amMod } from "../utils/roles/am-mod";
|
||||||
|
import type { QueryParams } from "../utils/types/query-params";
|
||||||
import { HtmlTags } from "./common/html-tags";
|
import { HtmlTags } from "./common/html-tags";
|
||||||
import { Icon, Spinner } from "./common/icon";
|
import { Icon, Spinner } from "./common/icon";
|
||||||
import { MomentTime } from "./common/moment-time";
|
import { MomentTime } from "./common/moment-time";
|
||||||
|
|
|
@ -53,7 +53,6 @@ import { UserService } from "../../services";
|
||||||
import { FirstLoadService } from "../../services/FirstLoadService";
|
import { FirstLoadService } from "../../services/FirstLoadService";
|
||||||
import { HttpService, RequestState } from "../../services/HttpService";
|
import { HttpService, RequestState } from "../../services/HttpService";
|
||||||
import {
|
import {
|
||||||
QueryParams,
|
|
||||||
capitalizeFirstLetter,
|
capitalizeFirstLetter,
|
||||||
editComment,
|
editComment,
|
||||||
editPost,
|
editPost,
|
||||||
|
@ -64,8 +63,6 @@ import {
|
||||||
futureDaysToUnixTime,
|
futureDaysToUnixTime,
|
||||||
getCommentParentId,
|
getCommentParentId,
|
||||||
getPageFromString,
|
getPageFromString,
|
||||||
getQueryParams,
|
|
||||||
getQueryString,
|
|
||||||
mdToHtml,
|
mdToHtml,
|
||||||
myAuth,
|
myAuth,
|
||||||
myAuthRequired,
|
myAuthRequired,
|
||||||
|
@ -78,9 +75,12 @@ import {
|
||||||
toast,
|
toast,
|
||||||
updatePersonBlock,
|
updatePersonBlock,
|
||||||
} from "../../utils";
|
} 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 { canMod } from "../../utils/roles/can-mod";
|
||||||
import { isAdmin } from "../../utils/roles/is-admin";
|
import { isAdmin } from "../../utils/roles/is-admin";
|
||||||
import { isBanned } from "../../utils/roles/is-banned";
|
import { isBanned } from "../../utils/roles/is-banned";
|
||||||
|
import type { QueryParams } from "../../utils/types/query-params";
|
||||||
import { BannerIconHeader } from "../common/banner-icon-header";
|
import { BannerIconHeader } from "../common/banner-icon-header";
|
||||||
import { HtmlTags } from "../common/html-tags";
|
import { HtmlTags } from "../common/html-tags";
|
||||||
import { Icon, Spinner } from "../common/icon";
|
import { Icon, Spinner } from "../common/icon";
|
||||||
|
|
|
@ -16,14 +16,14 @@ import {
|
||||||
} from "../../services/HttpService";
|
} from "../../services/HttpService";
|
||||||
import {
|
import {
|
||||||
Choice,
|
Choice,
|
||||||
QueryParams,
|
|
||||||
enableDownvotes,
|
enableDownvotes,
|
||||||
enableNsfw,
|
enableNsfw,
|
||||||
getIdFromString,
|
getIdFromString,
|
||||||
getQueryParams,
|
|
||||||
myAuth,
|
myAuth,
|
||||||
setIsoData,
|
setIsoData,
|
||||||
} from "../../utils";
|
} 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 { HtmlTags } from "../common/html-tags";
|
||||||
import { Spinner } from "../common/icon";
|
import { Spinner } from "../common/icon";
|
||||||
import { PostForm } from "./post-form";
|
import { PostForm } from "./post-form";
|
||||||
|
|
|
@ -28,7 +28,6 @@ import { i18n } from "../../i18next";
|
||||||
import { BanType, PostFormParams, PurgeType, VoteType } from "../../interfaces";
|
import { BanType, PostFormParams, PurgeType, VoteType } from "../../interfaces";
|
||||||
import { UserService } from "../../services";
|
import { UserService } from "../../services";
|
||||||
import {
|
import {
|
||||||
canShare,
|
|
||||||
futureDaysToUnixTime,
|
futureDaysToUnixTime,
|
||||||
hostname,
|
hostname,
|
||||||
isImage,
|
isImage,
|
||||||
|
@ -41,9 +40,10 @@ import {
|
||||||
numToSI,
|
numToSI,
|
||||||
relTags,
|
relTags,
|
||||||
setupTippy,
|
setupTippy,
|
||||||
share,
|
|
||||||
showScores,
|
showScores,
|
||||||
} from "../../utils";
|
} from "../../utils";
|
||||||
|
import { canShare } from "../../utils/browser/can-share";
|
||||||
|
import { share } from "../../utils/browser/share";
|
||||||
import { amAdmin } from "../../utils/roles/am-admin";
|
import { amAdmin } from "../../utils/roles/am-admin";
|
||||||
import { amCommunityCreator } from "../../utils/roles/am-community-creator";
|
import { amCommunityCreator } from "../../utils/roles/am-community-creator";
|
||||||
import { amMod } from "../../utils/roles/am-mod";
|
import { amMod } from "../../utils/roles/am-mod";
|
||||||
|
|
|
@ -73,7 +73,6 @@ import {
|
||||||
getCommentParentId,
|
getCommentParentId,
|
||||||
getDepthFromComment,
|
getDepthFromComment,
|
||||||
getIdFromProps,
|
getIdFromProps,
|
||||||
isBrowser,
|
|
||||||
isImage,
|
isImage,
|
||||||
myAuth,
|
myAuth,
|
||||||
restoreScrollPosition,
|
restoreScrollPosition,
|
||||||
|
@ -84,6 +83,7 @@ import {
|
||||||
updateCommunityBlock,
|
updateCommunityBlock,
|
||||||
updatePersonBlock,
|
updatePersonBlock,
|
||||||
} from "../../utils";
|
} from "../../utils";
|
||||||
|
import { isBrowser } from "../../utils/browser/is-browser";
|
||||||
import { CommentForm } from "../comment/comment-form";
|
import { CommentForm } from "../comment/comment-form";
|
||||||
import { CommentNodes } from "../comment/comment-nodes";
|
import { CommentNodes } from "../comment/comment-nodes";
|
||||||
import { HtmlTags } from "../common/html-tags";
|
import { HtmlTags } from "../common/html-tags";
|
||||||
|
|
|
@ -26,7 +26,6 @@ import { FirstLoadService } from "../services/FirstLoadService";
|
||||||
import { HttpService, RequestState } from "../services/HttpService";
|
import { HttpService, RequestState } from "../services/HttpService";
|
||||||
import {
|
import {
|
||||||
Choice,
|
Choice,
|
||||||
QueryParams,
|
|
||||||
capitalizeFirstLetter,
|
capitalizeFirstLetter,
|
||||||
commentsToFlatNodes,
|
commentsToFlatNodes,
|
||||||
communityToChoice,
|
communityToChoice,
|
||||||
|
@ -38,8 +37,6 @@ import {
|
||||||
fetchUsers,
|
fetchUsers,
|
||||||
getIdFromString,
|
getIdFromString,
|
||||||
getPageFromString,
|
getPageFromString,
|
||||||
getQueryParams,
|
|
||||||
getQueryString,
|
|
||||||
getUpdatedSearchId,
|
getUpdatedSearchId,
|
||||||
myAuth,
|
myAuth,
|
||||||
numToSI,
|
numToSI,
|
||||||
|
@ -49,6 +46,9 @@ import {
|
||||||
setIsoData,
|
setIsoData,
|
||||||
showLocal,
|
showLocal,
|
||||||
} from "../utils";
|
} 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 { CommentNodes } from "./comment/comment-nodes";
|
||||||
import { HtmlTags } from "./common/html-tags";
|
import { HtmlTags } from "./common/html-tags";
|
||||||
import { Spinner } from "./common/icon";
|
import { Spinner } from "./common/icon";
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { isBrowser } from "./utils";
|
import { isBrowser } from "./utils/browser/is-browser";
|
||||||
|
|
||||||
const testHost = "0.0.0.0:8536";
|
const testHost = "0.0.0.0:8536";
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,8 @@ import jwt_decode from "jwt-decode";
|
||||||
import { LoginResponse, MyUserInfo } from "lemmy-js-client";
|
import { LoginResponse, MyUserInfo } from "lemmy-js-client";
|
||||||
import { isHttps } from "../env";
|
import { isHttps } from "../env";
|
||||||
import { i18n } from "../i18next";
|
import { i18n } from "../i18next";
|
||||||
import { isAuthPath, isBrowser, toast } from "../utils";
|
import { isAuthPath, toast } from "../utils";
|
||||||
|
import { isBrowser } from "../utils/browser/is-browser";
|
||||||
|
|
||||||
interface Claims {
|
interface Claims {
|
||||||
sub: number;
|
sub: number;
|
||||||
|
|
|
@ -43,6 +43,8 @@ import { getHttpBase } from "./env";
|
||||||
import { i18n, languages } from "./i18next";
|
import { i18n, languages } from "./i18next";
|
||||||
import { CommentNodeI, DataType, IsoData, VoteType } from "./interfaces";
|
import { CommentNodeI, DataType, IsoData, VoteType } from "./interfaces";
|
||||||
import { HttpService, UserService } from "./services";
|
import { HttpService, UserService } from "./services";
|
||||||
|
import { isBrowser } from "./utils/browser/is-browser";
|
||||||
|
import { groupBy } from "./utils/helpers/group-by";
|
||||||
|
|
||||||
let Tribute: any;
|
let Tribute: any;
|
||||||
if (isBrowser()) {
|
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 {
|
export function setIsoData(context: any): IsoData {
|
||||||
// If its the browser, you need to deserialize the data from the window
|
// If its the browser, you need to deserialize the data from the window
|
||||||
if (isBrowser()) {
|
if (isBrowser()) {
|
||||||
|
@ -1314,64 +1312,12 @@ interface EmojiMartSkin {
|
||||||
src: string;
|
src: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const groupBy = <T>(
|
|
||||||
array: T[],
|
|
||||||
predicate: (value: T, index: number, array: T[]) => string
|
|
||||||
) =>
|
|
||||||
array.reduce((acc, value, index, array) => {
|
|
||||||
(acc[predicate(value, index, array)] ||= []).push(value);
|
|
||||||
return acc;
|
|
||||||
}, {} as { [key: string]: T[] });
|
|
||||||
|
|
||||||
export type QueryParams<T extends Record<string, any>> = {
|
|
||||||
[key in keyof T]?: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export function getQueryParams<T extends Record<string, any>>(processors: {
|
|
||||||
[K in keyof T]: (param: string) => T[K];
|
|
||||||
}): T {
|
|
||||||
if (isBrowser()) {
|
|
||||||
const searchParams = new URLSearchParams(window.location.search);
|
|
||||||
|
|
||||||
return Array.from(Object.entries(processors)).reduce(
|
|
||||||
(acc, [key, process]) => ({
|
|
||||||
...acc,
|
|
||||||
[key]: process(searchParams.get(key)),
|
|
||||||
}),
|
|
||||||
{} as T
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return {} as T;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getQueryString<T extends Record<string, string | undefined>>(
|
|
||||||
obj: T
|
|
||||||
) {
|
|
||||||
return Object.entries(obj)
|
|
||||||
.filter(([, val]) => val !== undefined && val !== null)
|
|
||||||
.reduce(
|
|
||||||
(acc, [key, val], index) => `${acc}${index > 0 ? "&" : ""}${key}=${val}`,
|
|
||||||
"?"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function isAuthPath(pathname: string) {
|
export function isAuthPath(pathname: string) {
|
||||||
return /create_.*|inbox|settings|admin|reports|registration_applications/g.test(
|
return /create_.*|inbox|settings|admin|reports|registration_applications/g.test(
|
||||||
pathname
|
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 {
|
export function newVote(voteType: VoteType, myVote?: number): number {
|
||||||
if (voteType == VoteType.Upvote) {
|
if (voteType == VoteType.Upvote) {
|
||||||
return myVote == 1 ? 0 : 1;
|
return myVote == 1 ? 0 : 1;
|
||||||
|
@ -1379,18 +1325,3 @@ export function newVote(voteType: VoteType, myVote?: number): number {
|
||||||
return myVote == -1 ? 0 : -1;
|
return myVote == -1 ? 0 : -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function sleep(millis: number): Promise<void> {
|
|
||||||
return new Promise(resolve => setTimeout(resolve, millis));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Polls / repeatedly runs a promise, every X milliseconds
|
|
||||||
*/
|
|
||||||
export async function poll(promiseFn: any, millis: number) {
|
|
||||||
if (window.document.visibilityState !== "hidden") {
|
|
||||||
await promiseFn();
|
|
||||||
}
|
|
||||||
await sleep(millis);
|
|
||||||
return poll(promiseFn, millis);
|
|
||||||
}
|
|
||||||
|
|
5
src/shared/utils/browser/can-share.ts
Normal file
5
src/shared/utils/browser/can-share.ts
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
import { isBrowser } from "./is-browser";
|
||||||
|
|
||||||
|
export function canShare() {
|
||||||
|
return isBrowser() && !!navigator.canShare;
|
||||||
|
}
|
3
src/shared/utils/browser/is-browser.ts
Normal file
3
src/shared/utils/browser/is-browser.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
export function isBrowser() {
|
||||||
|
return typeof window !== "undefined";
|
||||||
|
}
|
7
src/shared/utils/browser/share.ts
Normal file
7
src/shared/utils/browser/share.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import { isBrowser } from "./is-browser";
|
||||||
|
|
||||||
|
export function share(shareData: ShareData) {
|
||||||
|
if (isBrowser()) {
|
||||||
|
navigator.share(shareData);
|
||||||
|
}
|
||||||
|
}
|
19
src/shared/utils/helpers/get-query-params.ts
Normal file
19
src/shared/utils/helpers/get-query-params.ts
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
import { isBrowser } from "../browser/is-browser";
|
||||||
|
|
||||||
|
export function getQueryParams<T extends Record<string, any>>(processors: {
|
||||||
|
[K in keyof T]: (param: string) => T[K];
|
||||||
|
}): T {
|
||||||
|
if (isBrowser()) {
|
||||||
|
const searchParams = new URLSearchParams(window.location.search);
|
||||||
|
|
||||||
|
return Array.from(Object.entries(processors)).reduce(
|
||||||
|
(acc, [key, process]) => ({
|
||||||
|
...acc,
|
||||||
|
[key]: process(searchParams.get(key)),
|
||||||
|
}),
|
||||||
|
{} as T
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {} as T;
|
||||||
|
}
|
10
src/shared/utils/helpers/get-query-string.ts
Normal file
10
src/shared/utils/helpers/get-query-string.ts
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
export function getQueryString<T extends Record<string, string | undefined>>(
|
||||||
|
obj: T
|
||||||
|
) {
|
||||||
|
return Object.entries(obj)
|
||||||
|
.filter(([, val]) => val !== undefined && val !== null)
|
||||||
|
.reduce(
|
||||||
|
(acc, [key, val], index) => `${acc}${index > 0 ? "&" : ""}${key}=${val}`,
|
||||||
|
"?"
|
||||||
|
);
|
||||||
|
}
|
8
src/shared/utils/helpers/group-by.ts
Normal file
8
src/shared/utils/helpers/group-by.ts
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
export const groupBy = <T>(
|
||||||
|
array: T[],
|
||||||
|
predicate: (value: T, index: number, array: T[]) => string
|
||||||
|
) =>
|
||||||
|
array.reduce((acc, value, index, array) => {
|
||||||
|
(acc[predicate(value, index, array)] ||= []).push(value);
|
||||||
|
return acc;
|
||||||
|
}, {} as { [key: string]: T[] });
|
12
src/shared/utils/helpers/poll.ts
Normal file
12
src/shared/utils/helpers/poll.ts
Normal file
|
@ -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);
|
||||||
|
}
|
3
src/shared/utils/helpers/sleep.ts
Normal file
3
src/shared/utils/helpers/sleep.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
export function sleep(millis: number): Promise<void> {
|
||||||
|
return new Promise(resolve => setTimeout(resolve, millis));
|
||||||
|
}
|
3
src/shared/utils/types/query-params.ts
Normal file
3
src/shared/utils/types/query-params.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
export type QueryParams<T extends Record<string, any>> = {
|
||||||
|
[key in keyof T]?: string;
|
||||||
|
};
|
Loading…
Reference in a new issue