]> Untitled Git - lemmy-ui.git/blob - src/shared/utils.ts
Don't flash default theme on page load
[lemmy-ui.git] / src / shared / utils.ts
1 import 'moment/locale/es';
2 import 'moment/locale/el';
3 import 'moment/locale/eu';
4 import 'moment/locale/eo';
5 import 'moment/locale/de';
6 import 'moment/locale/zh-cn';
7 import 'moment/locale/fr';
8 import 'moment/locale/sv';
9 import 'moment/locale/ru';
10 import 'moment/locale/nl';
11 import 'moment/locale/it';
12 import 'moment/locale/fi';
13 import 'moment/locale/ca';
14 import 'moment/locale/fa';
15 import 'moment/locale/pl';
16 import 'moment/locale/pt-br';
17 import 'moment/locale/ja';
18 import 'moment/locale/ka';
19 import 'moment/locale/hi';
20 import 'moment/locale/gl';
21 import 'moment/locale/tr';
22 import 'moment/locale/hu';
23 import 'moment/locale/uk';
24 import 'moment/locale/sq';
25 import 'moment/locale/km';
26 import 'moment/locale/ga';
27 import 'moment/locale/sr';
28 import 'moment/locale/ko';
29
30 import {
31   UserOperation,
32   Comment,
33   CommentNode as CommentNodeI,
34   Post,
35   PrivateMessage,
36   User,
37   SortType,
38   ListingType,
39   SearchType,
40   WebSocketResponse,
41   WebSocketJsonResponse,
42   SearchForm,
43   SearchResponse,
44   CommentResponse,
45   PostResponse,
46   LemmyHttp,
47 } from 'lemmy-js-client';
48
49 import { httpUri } from './env';
50
51 import { CommentSortType, DataType, IsoData } from './interfaces';
52 import { UserService, WebSocketService } from './services';
53
54 var Tribute;
55 if (isBrowser()) {
56   Tribute = require('tributejs');
57 }
58 import markdown_it from 'markdown-it';
59 import markdown_it_sub from 'markdown-it-sub';
60 import markdown_it_sup from 'markdown-it-sup';
61 import markdownitEmoji from 'markdown-it-emoji/light';
62 import markdown_it_container from 'markdown-it-container';
63 import emojiShortName from 'emoji-short-name';
64 import Toastify from 'toastify-js';
65 import tippy from 'tippy.js';
66 import moment from 'moment';
67 import { Subscription } from 'rxjs';
68 import { retryWhen, delay, take } from 'rxjs/operators';
69
70 export const favIconUrl = '/static/assets/favicon.svg';
71 export const favIconPngUrl = '/static/assets/apple-touch-icon.png';
72 // TODO
73 // export const defaultFavIcon = `${window.location.protocol}//${window.location.host}${favIconPngUrl}`;
74 export const defaultFavIcon = 'test';
75 export const repoUrl = 'https://github.com/LemmyNet';
76 export const joinLemmyUrl = 'https://join.lemmy.ml';
77 export const helpGuideUrl = '/docs/about_guide.html';
78 export const markdownHelpUrl = `${helpGuideUrl}#markdown-guide`;
79 export const sortingHelpUrl = `${helpGuideUrl}#sorting`;
80 export const archiveUrl = 'https://archive.is';
81 export const elementUrl = 'https://element.io/';
82
83 export const postRefetchSeconds: number = 60 * 1000;
84 export const fetchLimit: number = 20;
85 export const mentionDropdownFetchLimit = 10;
86
87 export const lemmyHttp = new LemmyHttp(httpUri);
88
89 export const languages = [
90   { code: 'ca', name: 'Català' },
91   { code: 'en', name: 'English' },
92   { code: 'el', name: 'Ελληνικά' },
93   { code: 'eu', name: 'Euskara' },
94   { code: 'eo', name: 'Esperanto' },
95   { code: 'es', name: 'Español' },
96   { code: 'de', name: 'Deutsch' },
97   { code: 'ga', name: 'Gaeilge' },
98   { code: 'gl', name: 'Galego' },
99   { code: 'hu', name: 'Magyar Nyelv' },
100   { code: 'ka', name: 'ქართული ენა' },
101   { code: 'ko', name: '한국어' },
102   { code: 'km', name: 'ភាសាខ្មែរ' },
103   { code: 'hi', name: 'मानक हिन्दी' },
104   { code: 'fa', name: 'فارسی' },
105   { code: 'ja', name: '日本語' },
106   { code: 'pl', name: 'Polski' },
107   { code: 'pt_BR', name: 'Português Brasileiro' },
108   { code: 'zh', name: '中文' },
109   { code: 'fi', name: 'Suomi' },
110   { code: 'fr', name: 'Français' },
111   { code: 'sv', name: 'Svenska' },
112   { code: 'sq', name: 'Shqip' },
113   { code: 'sr_Latn', name: 'srpski' },
114   { code: 'tr', name: 'Türkçe' },
115   { code: 'uk', name: 'Українська Mова' },
116   { code: 'ru', name: 'Русский' },
117   { code: 'nl', name: 'Nederlands' },
118   { code: 'it', name: 'Italiano' },
119 ];
120
121 export const themes = [
122   'litera',
123   'materia',
124   'minty',
125   'solar',
126   'united',
127   'cyborg',
128   'darkly',
129   'journal',
130   'sketchy',
131   'vaporwave',
132   'vaporwave-dark',
133   'i386',
134   'litely',
135 ];
136
137 const DEFAULT_ALPHABET =
138   'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
139
140 function getRandomCharFromAlphabet(alphabet: string): string {
141   return alphabet.charAt(Math.floor(Math.random() * alphabet.length));
142 }
143
144 export function randomStr(
145   idDesiredLength: number = 20,
146   alphabet = DEFAULT_ALPHABET
147 ): string {
148   /**
149    * Create n-long array and map it to random chars from given alphabet.
150    * Then join individual chars as string
151    */
152   return Array.from({ length: idDesiredLength })
153     .map(() => {
154       return getRandomCharFromAlphabet(alphabet);
155     })
156     .join('');
157 }
158
159 export function wsJsonToRes(msg: WebSocketJsonResponse): WebSocketResponse {
160   let opStr: string = msg.op;
161   return {
162     op: UserOperation[opStr],
163     data: msg.data,
164   };
165 }
166
167 export const md = new markdown_it({
168   html: false,
169   linkify: true,
170   typographer: true,
171 })
172   .use(markdown_it_sub)
173   .use(markdown_it_sup)
174   .use(markdown_it_container, 'spoiler', {
175     validate: function (params: any) {
176       return params.trim().match(/^spoiler\s+(.*)$/);
177     },
178
179     render: function (tokens: any, idx: any) {
180       var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
181
182       if (tokens[idx].nesting === 1) {
183         // opening tag
184         return `<details><summary> ${md.utils.escapeHtml(m[1])} </summary>\n`;
185       } else {
186         // closing tag
187         return '</details>\n';
188       }
189     },
190   })
191   .use(markdownitEmoji, {
192     defs: objectFlip(emojiShortName),
193   });
194
195 export function hotRankComment(comment: Comment): number {
196   return hotRank(comment.score, comment.published);
197 }
198
199 export function hotRankPost(post: Post): number {
200   return hotRank(post.score, post.newest_activity_time);
201 }
202
203 export function hotRank(score: number, timeStr: string): number {
204   // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
205   let date: Date = new Date(timeStr + 'Z'); // Add Z to convert from UTC date
206   let now: Date = new Date();
207   let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
208
209   let rank =
210     (10000 * Math.log10(Math.max(1, 3 + score))) /
211     Math.pow(hoursElapsed + 2, 1.8);
212
213   // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
214
215   return rank;
216 }
217
218 export function mdToHtml(text: string) {
219   return { __html: md.render(text) };
220 }
221
222 export function getUnixTime(text: string): number {
223   return text ? new Date(text).getTime() / 1000 : undefined;
224 }
225
226 export function addTypeInfo<T>(
227   arr: T[],
228   name: string
229 ): { type_: string; data: T }[] {
230   return arr.map(e => {
231     return { type_: name, data: e };
232   });
233 }
234
235 export function canMod(
236   user: User,
237   modIds: number[],
238   creator_id: number,
239   onSelf: boolean = false
240 ): boolean {
241   // You can do moderator actions only on the mods added after you.
242   if (user) {
243     let yourIndex = modIds.findIndex(id => id == user.id);
244     if (yourIndex == -1) {
245       return false;
246     } else {
247       // onSelf +1 on mod actions not for yourself, IE ban, remove, etc
248       modIds = modIds.slice(0, yourIndex + (onSelf ? 0 : 1));
249       return !modIds.includes(creator_id);
250     }
251   } else {
252     return false;
253   }
254 }
255
256 export function isMod(modIds: number[], creator_id: number): boolean {
257   return modIds.includes(creator_id);
258 }
259
260 const imageRegex = new RegExp(
261   /(http)?s?:?(\/\/[^"']*\.(?:jpg|jpeg|gif|png|svg|webp))/
262 );
263 const videoRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:mp4))`);
264
265 export function isImage(url: string) {
266   return imageRegex.test(url);
267 }
268
269 export function isVideo(url: string) {
270   return videoRegex.test(url);
271 }
272
273 // TODO this broke
274 export function validURL(str: string) {
275   // try {
276   return !!new URL(str);
277   // } catch {
278   // return false;
279   // }
280 }
281
282 export function communityRSSUrl(actorId: string, sort: string): string {
283   let url = new URL(actorId);
284   return `${url.origin}/feeds${url.pathname}.xml?sort=${sort}`;
285 }
286
287 export function validEmail(email: string) {
288   let re = /^(([^\s"(),.:;<>@[\\\]]+(\.[^\s"(),.:;<>@[\\\]]+)*)|(".+"))@((\[(?:\d{1,3}\.){3}\d{1,3}])|(([\dA-Za-z\-]+\.)+[A-Za-z]{2,}))$/;
289   return re.test(String(email).toLowerCase());
290 }
291
292 export function capitalizeFirstLetter(str: string): string {
293   return str.charAt(0).toUpperCase() + str.slice(1);
294 }
295
296 export function routeSortTypeToEnum(sort: string): SortType {
297   return SortType[sort];
298 }
299
300 export function routeListingTypeToEnum(type: string): ListingType {
301   return ListingType[type];
302 }
303
304 export function routeDataTypeToEnum(type: string): DataType {
305   return DataType[capitalizeFirstLetter(type)];
306 }
307
308 export function routeSearchTypeToEnum(type: string): SearchType {
309   return SearchType[type];
310 }
311
312 export async function getPageTitle(url: string) {
313   let res = await fetch(`/iframely/oembed?url=${url}`).then(res => res.json());
314   let title = await res.title;
315   return title;
316 }
317
318 export function debounce(
319   func: any,
320   wait: number = 1000,
321   immediate: boolean = false
322 ) {
323   // 'private' variable for instance
324   // The returned function will be able to reference this due to closure.
325   // Each call to the returned function will share this common timer.
326   let timeout: any;
327
328   // Calling debounce returns a new anonymous function
329   return function () {
330     // reference the context and args for the setTimeout function
331     var context = this,
332       args = arguments;
333
334     // Should the function be called now? If immediate is true
335     //   and not already in a timeout then the answer is: Yes
336     var callNow = immediate && !timeout;
337
338     // This is the basic debounce behaviour where you can call this
339     //   function several times, but it will only execute once
340     //   [before or after imposing a delay].
341     //   Each time the returned function is called, the timer starts over.
342     clearTimeout(timeout);
343
344     // Set the new timeout
345     timeout = setTimeout(function () {
346       // Inside the timeout function, clear the timeout variable
347       // which will let the next execution run when in 'immediate' mode
348       timeout = null;
349
350       // Check if the function already ran with the immediate flag
351       if (!immediate) {
352         // Call the original function with apply
353         // apply lets you define the 'this' object as well as the arguments
354         //    (both captured before setTimeout)
355         func.apply(context, args);
356       }
357     }, wait);
358
359     // Immediate mode and no wait timer? Execute the function..
360     if (callNow) func.apply(context, args);
361   };
362 }
363
364 // TODO
365 export function getLanguage(override?: string): string {
366   let user = UserService.Instance.user;
367   let lang = override || (user && user.lang ? user.lang : 'browser');
368
369   if (lang == 'browser' && isBrowser()) {
370     return getBrowserLanguage();
371   } else {
372     return lang;
373   }
374 }
375
376 // TODO
377 export function getBrowserLanguage(): string {
378   return navigator.language;
379 }
380
381 export function getMomentLanguage(): string {
382   let lang = getLanguage();
383   if (lang.startsWith('zh')) {
384     lang = 'zh-cn';
385   } else if (lang.startsWith('sv')) {
386     lang = 'sv';
387   } else if (lang.startsWith('fr')) {
388     lang = 'fr';
389   } else if (lang.startsWith('de')) {
390     lang = 'de';
391   } else if (lang.startsWith('ru')) {
392     lang = 'ru';
393   } else if (lang.startsWith('es')) {
394     lang = 'es';
395   } else if (lang.startsWith('eo')) {
396     lang = 'eo';
397   } else if (lang.startsWith('nl')) {
398     lang = 'nl';
399   } else if (lang.startsWith('it')) {
400     lang = 'it';
401   } else if (lang.startsWith('fi')) {
402     lang = 'fi';
403   } else if (lang.startsWith('ca')) {
404     lang = 'ca';
405   } else if (lang.startsWith('fa')) {
406     lang = 'fa';
407   } else if (lang.startsWith('pl')) {
408     lang = 'pl';
409   } else if (lang.startsWith('pt')) {
410     lang = 'pt-br';
411   } else if (lang.startsWith('ja')) {
412     lang = 'ja';
413   } else if (lang.startsWith('ka')) {
414     lang = 'ka';
415   } else if (lang.startsWith('hi')) {
416     lang = 'hi';
417   } else if (lang.startsWith('el')) {
418     lang = 'el';
419   } else if (lang.startsWith('eu')) {
420     lang = 'eu';
421   } else if (lang.startsWith('gl')) {
422     lang = 'gl';
423   } else if (lang.startsWith('tr')) {
424     lang = 'tr';
425   } else if (lang.startsWith('hu')) {
426     lang = 'hu';
427   } else if (lang.startsWith('uk')) {
428     lang = 'uk';
429   } else if (lang.startsWith('sq')) {
430     lang = 'sq';
431   } else if (lang.startsWith('km')) {
432     lang = 'km';
433   } else if (lang.startsWith('ga')) {
434     lang = 'ga';
435   } else if (lang.startsWith('sr')) {
436     lang = 'sr';
437   } else if (lang.startsWith('ko')) {
438     lang = 'ko';
439   } else {
440     lang = 'en';
441   }
442   return lang;
443 }
444
445 export function setTheme(theme: string, forceReload: boolean = false) {
446   if (!isBrowser()) {
447     return;
448   }
449   if (theme === 'browser' && !forceReload) {
450     return;
451   }
452   // This is only run on a force reload
453   if (theme == 'browser') {
454     theme = 'darkly';
455   }
456
457   // Unload all the other themes
458   for (var i = 0; i < themes.length; i++) {
459     let styleSheet = document.getElementById(themes[i]);
460     if (styleSheet) {
461       styleSheet.setAttribute('disabled', 'disabled');
462     }
463   }
464
465   document
466     .getElementById('default-light')
467     ?.setAttribute('disabled', 'disabled');
468   document.getElementById('default-dark')?.setAttribute('disabled', 'disabled');
469
470   // Load the theme dynamically
471   let cssLoc = `/static/assets/css/themes/${theme}.min.css`;
472   loadCss(theme, cssLoc);
473   document.getElementById(theme).removeAttribute('disabled');
474 }
475
476 export function loadCss(id: string, loc: string) {
477   if (!document.getElementById(id)) {
478     var head = document.getElementsByTagName('head')[0];
479     var link = document.createElement('link');
480     link.id = id;
481     link.rel = 'stylesheet';
482     link.type = 'text/css';
483     link.href = loc;
484     link.media = 'all';
485     head.appendChild(link);
486   }
487 }
488
489 export function objectFlip(obj: any) {
490   const ret = {};
491   Object.keys(obj).forEach(key => {
492     ret[obj[key]] = key;
493   });
494   return ret;
495 }
496
497 export function showAvatars(): boolean {
498   return (
499     (UserService.Instance.user && UserService.Instance.user.show_avatars) ||
500     !UserService.Instance.user
501   );
502 }
503
504 export function isCakeDay(published: string): boolean {
505   // moment(undefined) or moment.utc(undefined) returns the current date/time
506   // moment(null) or moment.utc(null) returns null
507   const userCreationDate = moment.utc(published || null).local();
508   const currentDate = moment(new Date());
509
510   return (
511     userCreationDate.date() === currentDate.date() &&
512     userCreationDate.month() === currentDate.month() &&
513     userCreationDate.year() !== currentDate.year()
514   );
515 }
516
517 export function isCommentType(
518   item: Comment | PrivateMessage | Post
519 ): item is Comment {
520   return (
521     (item as Comment).community_id !== undefined &&
522     (item as Comment).content !== undefined
523   );
524 }
525
526 export function isPostType(
527   item: Comment | PrivateMessage | Post
528 ): item is Post {
529   return (item as Post).stickied !== undefined;
530 }
531
532 export function toast(text: string, background: string = 'success') {
533   let backgroundColor = `var(--${background})`;
534   Toastify({
535     text: text,
536     backgroundColor: backgroundColor,
537     gravity: 'bottom',
538     position: 'left',
539   }).showToast();
540 }
541
542 export function pictrsDeleteToast(
543   clickToDeleteText: string,
544   deletePictureText: string,
545   deleteUrl: string
546 ) {
547   let backgroundColor = `var(--light)`;
548   let toast = Toastify({
549     text: clickToDeleteText,
550     backgroundColor: backgroundColor,
551     gravity: 'top',
552     position: 'right',
553     duration: 10000,
554     onClick: () => {
555       if (toast) {
556         window.location.replace(deleteUrl);
557         alert(deletePictureText);
558         toast.hideToast();
559       }
560     },
561     close: true,
562   }).showToast();
563 }
564
565 interface NotifyInfo {
566   name: string;
567   icon: string;
568   link: string;
569   body: string;
570 }
571
572 export function messageToastify(info: NotifyInfo, router: any) {
573   let htmlBody = info.body ? md.render(info.body) : '';
574   let backgroundColor = `var(--light)`;
575
576   let toast = Toastify({
577     text: `${htmlBody}<br />${info.name}`,
578     avatar: info.icon,
579     backgroundColor: backgroundColor,
580     className: 'text-dark',
581     close: true,
582     gravity: 'top',
583     position: 'right',
584     duration: 5000,
585     onClick: () => {
586       if (toast) {
587         toast.hideToast();
588         router.history.push(info.link);
589       }
590     },
591   }).showToast();
592 }
593
594 export function notifyPost(post: Post, router: any) {
595   let info: NotifyInfo = {
596     name: post.community_name,
597     icon: post.community_icon ? post.community_icon : defaultFavIcon,
598     link: `/post/${post.id}`,
599     body: post.name,
600   };
601   notify(info, router);
602 }
603
604 export function notifyComment(comment: Comment, router: any) {
605   let info: NotifyInfo = {
606     name: comment.creator_name,
607     icon: comment.creator_avatar ? comment.creator_avatar : defaultFavIcon,
608     link: `/post/${comment.post_id}/comment/${comment.id}`,
609     body: comment.content,
610   };
611   notify(info, router);
612 }
613
614 export function notifyPrivateMessage(pm: PrivateMessage, router: any) {
615   let info: NotifyInfo = {
616     name: pm.creator_name,
617     icon: pm.creator_avatar ? pm.creator_avatar : defaultFavIcon,
618     link: `/inbox`,
619     body: pm.content,
620   };
621   notify(info, router);
622 }
623
624 function notify(info: NotifyInfo, router: any) {
625   messageToastify(info, router);
626
627   if (Notification.permission !== 'granted') Notification.requestPermission();
628   else {
629     var notification = new Notification(info.name, {
630       icon: info.icon,
631       body: info.body,
632     });
633
634     notification.onclick = () => {
635       event.preventDefault();
636       router.history.push(info.link);
637     };
638   }
639 }
640
641 export function setupTribute() {
642   return new Tribute({
643     noMatchTemplate: function () {
644       return '';
645     },
646     collection: [
647       // Emojis
648       {
649         trigger: ':',
650         menuItemTemplate: (item: any) => {
651           let shortName = `:${item.original.key}:`;
652           return `${item.original.val} ${shortName}`;
653         },
654         selectTemplate: (item: any) => {
655           return `:${item.original.key}:`;
656         },
657         values: Object.entries(emojiShortName).map(e => {
658           return { key: e[1], val: e[0] };
659         }),
660         allowSpaces: false,
661         autocompleteMode: true,
662         // TODO
663         // menuItemLimit: mentionDropdownFetchLimit,
664         menuShowMinLength: 2,
665       },
666       // Users
667       {
668         trigger: '@',
669         selectTemplate: (item: any) => {
670           let link = item.original.local
671             ? `[${item.original.key}](/u/${item.original.name})`
672             : `[${item.original.key}](/user/${item.original.id})`;
673           return link;
674         },
675         values: (text: string, cb: any) => {
676           userSearch(text, (users: any) => cb(users));
677         },
678         allowSpaces: false,
679         autocompleteMode: true,
680         // TODO
681         // menuItemLimit: mentionDropdownFetchLimit,
682         menuShowMinLength: 2,
683       },
684
685       // Communities
686       {
687         trigger: '!',
688         selectTemplate: (item: any) => {
689           let link = item.original.local
690             ? `[${item.original.key}](/c/${item.original.name})`
691             : `[${item.original.key}](/community/${item.original.id})`;
692           return link;
693         },
694         values: (text: string, cb: any) => {
695           communitySearch(text, (communities: any) => cb(communities));
696         },
697         allowSpaces: false,
698         autocompleteMode: true,
699         // TODO
700         // menuItemLimit: mentionDropdownFetchLimit,
701         menuShowMinLength: 2,
702       },
703     ],
704   });
705 }
706
707 var tippyInstance;
708 if (isBrowser()) {
709   tippyInstance = tippy('[data-tippy-content]');
710 }
711
712 export function setupTippy() {
713   if (isBrowser()) {
714     tippyInstance.forEach(e => e.destroy());
715     tippyInstance = tippy('[data-tippy-content]', {
716       delay: [500, 0],
717       // Display on "long press"
718       touch: ['hold', 500],
719     });
720   }
721 }
722
723 function userSearch(text: string, cb: any) {
724   if (text) {
725     let form: SearchForm = {
726       q: text,
727       type_: SearchType.Users,
728       sort: SortType.TopAll,
729       page: 1,
730       limit: mentionDropdownFetchLimit,
731     };
732
733     WebSocketService.Instance.search(form);
734
735     let userSub = WebSocketService.Instance.subject.subscribe(
736       msg => {
737         let res = wsJsonToRes(msg);
738         if (res.op == UserOperation.Search) {
739           let data = res.data as SearchResponse;
740           let users = data.users.map(u => {
741             return {
742               key: `@${u.name}@${hostname(u.actor_id)}`,
743               name: u.name,
744               local: u.local,
745               id: u.id,
746             };
747           });
748           cb(users);
749           userSub.unsubscribe();
750         }
751       },
752       err => console.error(err),
753       () => console.log('complete')
754     );
755   } else {
756     cb([]);
757   }
758 }
759
760 function communitySearch(text: string, cb: any) {
761   if (text) {
762     let form: SearchForm = {
763       q: text,
764       type_: SearchType.Communities,
765       sort: SortType.TopAll,
766       page: 1,
767       limit: mentionDropdownFetchLimit,
768     };
769
770     WebSocketService.Instance.search(form);
771
772     let communitySub = WebSocketService.Instance.subject.subscribe(
773       msg => {
774         let res = wsJsonToRes(msg);
775         if (res.op == UserOperation.Search) {
776           let data = res.data as SearchResponse;
777           let communities = data.communities.map(c => {
778             return {
779               key: `!${c.name}@${hostname(c.actor_id)}`,
780               name: c.name,
781               local: c.local,
782               id: c.id,
783             };
784           });
785           cb(communities);
786           communitySub.unsubscribe();
787         }
788       },
789       err => console.error(err),
790       () => console.log('complete')
791     );
792   } else {
793     cb([]);
794   }
795 }
796
797 export function getListingTypeFromProps(props: any): ListingType {
798   return props.match.params.listing_type
799     ? routeListingTypeToEnum(props.match.params.listing_type)
800     : UserService.Instance.user
801     ? Object.values(ListingType)[UserService.Instance.user.default_listing_type]
802     : ListingType.Local;
803 }
804
805 // TODO might need to add a user setting for this too
806 export function getDataTypeFromProps(props: any): DataType {
807   return props.match.params.data_type
808     ? routeDataTypeToEnum(props.match.params.data_type)
809     : DataType.Post;
810 }
811
812 export function getSortTypeFromProps(props: any): SortType {
813   return props.match.params.sort
814     ? routeSortTypeToEnum(props.match.params.sort)
815     : UserService.Instance.user
816     ? Object.values(SortType)[UserService.Instance.user.default_sort_type]
817     : SortType.Active;
818 }
819
820 export function getPageFromProps(props: any): number {
821   return props.match.params.page ? Number(props.match.params.page) : 1;
822 }
823
824 export function getRecipientIdFromProps(props: any): number {
825   return props.match.params.recipient_id
826     ? Number(props.match.params.recipient_id)
827     : 1;
828 }
829
830 export function getIdFromProps(props: any): number {
831   return Number(props.match.params.id);
832 }
833
834 export function getCommentIdFromProps(props: any): number {
835   return Number(props.match.params.comment_id);
836 }
837
838 export function getUsernameFromProps(props: any): string {
839   return props.match.params.username;
840 }
841
842 export function editCommentRes(data: CommentResponse, comments: Comment[]) {
843   let found = comments.find(c => c.id == data.comment.id);
844   if (found) {
845     found.content = data.comment.content;
846     found.updated = data.comment.updated;
847     found.removed = data.comment.removed;
848     found.deleted = data.comment.deleted;
849     found.upvotes = data.comment.upvotes;
850     found.downvotes = data.comment.downvotes;
851     found.score = data.comment.score;
852   }
853 }
854
855 export function saveCommentRes(data: CommentResponse, comments: Comment[]) {
856   let found = comments.find(c => c.id == data.comment.id);
857   if (found) {
858     found.saved = data.comment.saved;
859   }
860 }
861
862 export function createCommentLikeRes(
863   data: CommentResponse,
864   comments: Comment[]
865 ) {
866   let found: Comment = comments.find(c => c.id === data.comment.id);
867   if (found) {
868     found.score = data.comment.score;
869     found.upvotes = data.comment.upvotes;
870     found.downvotes = data.comment.downvotes;
871     if (data.comment.my_vote !== null) {
872       found.my_vote = data.comment.my_vote;
873     }
874   }
875 }
876
877 export function createPostLikeFindRes(data: PostResponse, posts: Post[]) {
878   let found = posts.find(c => c.id == data.post.id);
879   if (found) {
880     createPostLikeRes(data, found);
881   }
882 }
883
884 export function createPostLikeRes(data: PostResponse, post: Post) {
885   if (post) {
886     post.score = data.post.score;
887     post.upvotes = data.post.upvotes;
888     post.downvotes = data.post.downvotes;
889     if (data.post.my_vote !== null) {
890       post.my_vote = data.post.my_vote;
891     }
892   }
893 }
894
895 export function editPostFindRes(data: PostResponse, posts: Post[]) {
896   let found = posts.find(c => c.id == data.post.id);
897   if (found) {
898     editPostRes(data, found);
899   }
900 }
901
902 export function editPostRes(data: PostResponse, post: Post) {
903   if (post) {
904     post.url = data.post.url;
905     post.name = data.post.name;
906     post.nsfw = data.post.nsfw;
907     post.deleted = data.post.deleted;
908     post.removed = data.post.removed;
909     post.stickied = data.post.stickied;
910     post.body = data.post.body;
911     post.locked = data.post.locked;
912     post.saved = data.post.saved;
913   }
914 }
915
916 export function commentsToFlatNodes(comments: Comment[]): CommentNodeI[] {
917   let nodes: CommentNodeI[] = [];
918   for (let comment of comments) {
919     nodes.push({ comment: comment });
920   }
921   return nodes;
922 }
923
924 export function commentSort(tree: CommentNodeI[], sort: CommentSortType) {
925   // First, put removed and deleted comments at the bottom, then do your other sorts
926   if (sort == CommentSortType.Top) {
927     tree.sort(
928       (a, b) =>
929         +a.comment.removed - +b.comment.removed ||
930         +a.comment.deleted - +b.comment.deleted ||
931         b.comment.score - a.comment.score
932     );
933   } else if (sort == CommentSortType.New) {
934     tree.sort(
935       (a, b) =>
936         +a.comment.removed - +b.comment.removed ||
937         +a.comment.deleted - +b.comment.deleted ||
938         b.comment.published.localeCompare(a.comment.published)
939     );
940   } else if (sort == CommentSortType.Old) {
941     tree.sort(
942       (a, b) =>
943         +a.comment.removed - +b.comment.removed ||
944         +a.comment.deleted - +b.comment.deleted ||
945         a.comment.published.localeCompare(b.comment.published)
946     );
947   } else if (sort == CommentSortType.Hot) {
948     tree.sort(
949       (a, b) =>
950         +a.comment.removed - +b.comment.removed ||
951         +a.comment.deleted - +b.comment.deleted ||
952         hotRankComment(b.comment) - hotRankComment(a.comment)
953     );
954   }
955
956   // Go through the children recursively
957   for (let node of tree) {
958     if (node.children) {
959       commentSort(node.children, sort);
960     }
961   }
962 }
963
964 export function commentSortSortType(tree: CommentNodeI[], sort: SortType) {
965   commentSort(tree, convertCommentSortType(sort));
966 }
967
968 function convertCommentSortType(sort: SortType): CommentSortType {
969   if (
970     sort == SortType.TopAll ||
971     sort == SortType.TopDay ||
972     sort == SortType.TopWeek ||
973     sort == SortType.TopMonth ||
974     sort == SortType.TopYear
975   ) {
976     return CommentSortType.Top;
977   } else if (sort == SortType.New) {
978     return CommentSortType.New;
979   } else if (sort == SortType.Hot || sort == SortType.Active) {
980     return CommentSortType.Hot;
981   } else {
982     return CommentSortType.Hot;
983   }
984 }
985
986 export function postSort(
987   posts: Post[],
988   sort: SortType,
989   communityType: boolean
990 ) {
991   // First, put removed and deleted comments at the bottom, then do your other sorts
992   if (
993     sort == SortType.TopAll ||
994     sort == SortType.TopDay ||
995     sort == SortType.TopWeek ||
996     sort == SortType.TopMonth ||
997     sort == SortType.TopYear
998   ) {
999     posts.sort(
1000       (a, b) =>
1001         +a.removed - +b.removed ||
1002         +a.deleted - +b.deleted ||
1003         (communityType && +b.stickied - +a.stickied) ||
1004         b.score - a.score
1005     );
1006   } else if (sort == SortType.New) {
1007     posts.sort(
1008       (a, b) =>
1009         +a.removed - +b.removed ||
1010         +a.deleted - +b.deleted ||
1011         (communityType && +b.stickied - +a.stickied) ||
1012         b.published.localeCompare(a.published)
1013     );
1014   } else if (sort == SortType.Hot) {
1015     posts.sort(
1016       (a, b) =>
1017         +a.removed - +b.removed ||
1018         +a.deleted - +b.deleted ||
1019         (communityType && +b.stickied - +a.stickied) ||
1020         b.hot_rank - a.hot_rank
1021     );
1022   } else if (sort == SortType.Active) {
1023     posts.sort(
1024       (a, b) =>
1025         +a.removed - +b.removed ||
1026         +a.deleted - +b.deleted ||
1027         (communityType && +b.stickied - +a.stickied) ||
1028         b.hot_rank_active - a.hot_rank_active
1029     );
1030   }
1031 }
1032
1033 export const colorList: string[] = [
1034   hsl(0),
1035   hsl(100),
1036   hsl(150),
1037   hsl(200),
1038   hsl(250),
1039   hsl(300),
1040 ];
1041
1042 function hsl(num: number) {
1043   return `hsla(${num}, 35%, 50%, 1)`;
1044 }
1045
1046 // function randomHsl() {
1047 //   return `hsla(${Math.random() * 360}, 100%, 50%, 1)`;
1048 // }
1049
1050 export function previewLines(
1051   text: string,
1052   maxChars: number = 300,
1053   maxLines: number = 1
1054 ): string {
1055   return (
1056     text
1057       .slice(0, maxChars)
1058       .split('\n')
1059       // Use lines * 2 because markdown requires 2 lines
1060       .slice(0, maxLines * 2)
1061       .join('\n') + '...'
1062   );
1063 }
1064
1065 export function hostname(url: string): string {
1066   let cUrl = new URL(url);
1067   return cUrl.port ? `${cUrl.hostname}:${cUrl.port}` : `${cUrl.hostname}`;
1068 }
1069
1070 export function validTitle(title?: string): boolean {
1071   // Initial title is null, minimum length is taken care of by textarea's minLength={3}
1072   if (title === null || title.length < 3) return true;
1073
1074   const regex = new RegExp(/.*\S.*/, 'g');
1075
1076   return regex.test(title);
1077 }
1078
1079 export function siteBannerCss(banner: string): string {
1080   return ` \
1081     background-image: linear-gradient( rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.8) ) ,url("${banner}"); \
1082     background-attachment: fixed; \
1083     background-position: top; \
1084     background-repeat: no-repeat; \
1085     background-size: 100% cover; \
1086
1087     width: 100%; \
1088     max-height: 100vh; \
1089     `;
1090 }
1091
1092 export function isBrowser() {
1093   return typeof window !== 'undefined';
1094 }
1095
1096 export function setAuth(obj: any, auth: string) {
1097   if (auth) {
1098     obj.auth = auth;
1099   }
1100 }
1101
1102 export function setIsoData(context: any): IsoData {
1103   let isoData: IsoData = isBrowser()
1104     ? window.isoData
1105     : context.router.staticContext;
1106   return isoData;
1107 }
1108
1109 export function wsSubscribe(parseMessage: any): Subscription {
1110   if (isBrowser()) {
1111     return WebSocketService.Instance.subject
1112       .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
1113       .subscribe(
1114         msg => parseMessage(msg),
1115         err => console.error(err),
1116         () => console.log('complete')
1117       );
1118   } else {
1119     return null;
1120   }
1121 }
1122
1123 moment.updateLocale('en', {
1124   relativeTime: {
1125     future: 'in %s',
1126     past: '%s ago',
1127     s: '<1m',
1128     ss: '%ds',
1129     m: '1m',
1130     mm: '%dm',
1131     h: '1h',
1132     hh: '%dh',
1133     d: '1d',
1134     dd: '%dd',
1135     w: '1w',
1136     ww: '%dw',
1137     M: '1M',
1138     MM: '%dM',
1139     y: '1Y',
1140     yy: '%dY',
1141   },
1142 });