From 5478885dc6d65e475332072255eba40195da3cd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Le=C5=9Bniak?= Date: Wed, 13 Jan 2021 18:50:46 +0000 Subject: [PATCH 1/4] Convert common helpers to typescript --- .../common/helpers/{avatar.js => avatar.tsx} | 19 +++++++++++-------- .../helpers/{listItems.js => listItems.tsx} | 16 +++++++--------- .../helpers/{userOnline.js => userOnline.tsx} | 7 +++---- .../helpers/{username.js => username.tsx} | 9 +++++---- 4 files changed, 26 insertions(+), 25 deletions(-) rename js/src/common/helpers/{avatar.js => avatar.tsx} (65%) rename js/src/common/helpers/{listItems.js => listItems.tsx} (72%) rename js/src/common/helpers/{userOnline.js => userOnline.tsx} (63%) rename js/src/common/helpers/{username.js => username.tsx} (70%) diff --git a/js/src/common/helpers/avatar.js b/js/src/common/helpers/avatar.tsx similarity index 65% rename from js/src/common/helpers/avatar.js rename to js/src/common/helpers/avatar.tsx index f5c0245643..a0d504904b 100644 --- a/js/src/common/helpers/avatar.js +++ b/js/src/common/helpers/avatar.tsx @@ -1,26 +1,29 @@ +import * as Mithril from "mithril"; +import User from "../models/User"; + /** * The `avatar` helper displays a user's avatar. * - * @param {User} user - * @param {Object} attrs Attributes to apply to the avatar element - * @return {Object} + * @param user + * @param attrs Attributes to apply to the avatar element */ -export default function avatar(user, attrs = {}) { + +export default function avatar(user: User, attrs: Object = {}): Mithril.Vnode { attrs.className = 'Avatar ' + (attrs.className || ''); - let content = ''; + let content: string = ''; // If the `title` attribute is set to null or false, we don't want to give the // avatar a title. On the other hand, if it hasn't been given at all, we can // safely default it to the user's username. - const hasTitle = attrs.title === 'undefined' || attrs.title; + const hasTitle: boolean | string = attrs.title === 'undefined' || attrs.title; if (!hasTitle) delete attrs.title; // If a user has been passed, then we will set up an avatar using their // uploaded image, or the first letter of their username if they haven't // uploaded one. if (user) { - const username = user.displayName() || '?'; - const avatarUrl = user.avatarUrl(); + const username: string = user.displayName() || '?'; + const avatarUrl: string = user.avatarUrl(); if (hasTitle) attrs.title = attrs.title || username; diff --git a/js/src/common/helpers/listItems.js b/js/src/common/helpers/listItems.tsx similarity index 72% rename from js/src/common/helpers/listItems.js rename to js/src/common/helpers/listItems.tsx index da9232fde9..4f7c5f2c35 100644 --- a/js/src/common/helpers/listItems.js +++ b/js/src/common/helpers/listItems.tsx @@ -1,15 +1,16 @@ +import * as Mithril from "mithril"; import Separator from '../components/Separator'; import classList from '../utils/classList'; -function isSeparator(item) { +function isSeparator(item): boolean { return item.tag === Separator; } -function withoutUnnecessarySeparators(items) { +function withoutUnnecessarySeparators(items: Array): Array { const newItems = []; let prevItem; - items.filter(Boolean).forEach((item, i) => { + items.filter(Boolean).forEach((item: Mithril.Vnode, i: number) => { if (!isSeparator(item) || (prevItem && !isSeparator(prevItem) && i !== items.length - 1)) { prevItem = item; newItems.push(item); @@ -22,14 +23,11 @@ function withoutUnnecessarySeparators(items) { /** * The `listItems` helper wraps a collection of components in
  • tags, * stripping out any unnecessary `Separator` components. - * - * @param {*} items - * @return {Array} */ -export default function listItems(items) { +export default function listItems(items: Mithril.Vnode | Array): Array { if (!(items instanceof Array)) items = [items]; - return withoutUnnecessarySeparators(items).map((item) => { + return withoutUnnecessarySeparators(items).map((item: Mithril.Vnode) => { const isListItem = item.tag && item.tag.isListItem; const active = item.tag && item.tag.isActive && item.tag.isActive(item.attrs); const className = (item.attrs && item.attrs.itemClassName) || item.itemClassName; @@ -40,7 +38,7 @@ export default function listItems(items) { item.key = item.attrs.key; } - const node = isListItem ? ( + const node: Mithril.Vnode = isListItem ? ( item ) : (
  • {icon('fas fa-circle')}; } diff --git a/js/src/common/helpers/username.js b/js/src/common/helpers/username.tsx similarity index 70% rename from js/src/common/helpers/username.js rename to js/src/common/helpers/username.tsx index 7e05ca4d6d..7bec93df2d 100644 --- a/js/src/common/helpers/username.js +++ b/js/src/common/helpers/username.tsx @@ -1,11 +1,12 @@ +import * as Mithril from "mithril"; +import User from "../models/User"; + /** * The `username` helper displays a user's username in a * tag. If the user doesn't exist, the username will be displayed as [deleted]. - * - * @param {User} user - * @return {Object} */ -export default function username(user) { + +export default function username(user: User): Mithril.Vnode { const name = (user && user.displayName()) || app.translator.trans('core.lib.username.deleted_text'); return {name}; From fc71ab710db0e990ab9ea479b53b04451aa7211e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Le=C5=9Bniak?= Date: Wed, 13 Jan 2021 19:07:08 +0000 Subject: [PATCH 2/4] Remove unnecessary parameter from JSDoc --- js/src/common/helpers/avatar.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/js/src/common/helpers/avatar.tsx b/js/src/common/helpers/avatar.tsx index a0d504904b..11e9d32aa6 100644 --- a/js/src/common/helpers/avatar.tsx +++ b/js/src/common/helpers/avatar.tsx @@ -4,7 +4,6 @@ import User from "../models/User"; /** * The `avatar` helper displays a user's avatar. * - * @param user * @param attrs Attributes to apply to the avatar element */ From 520323e85d17f491c4e20be5d113ce433566d7d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Le=C5=9Bniak?= Date: Wed, 13 Jan 2021 19:14:10 +0000 Subject: [PATCH 3/4] Pretty code --- js/src/common/helpers/avatar.tsx | 4 ++-- js/src/common/helpers/listItems.tsx | 2 +- js/src/common/helpers/userOnline.tsx | 4 ++-- js/src/common/helpers/username.tsx | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/js/src/common/helpers/avatar.tsx b/js/src/common/helpers/avatar.tsx index 11e9d32aa6..ca842adb90 100644 --- a/js/src/common/helpers/avatar.tsx +++ b/js/src/common/helpers/avatar.tsx @@ -1,5 +1,5 @@ -import * as Mithril from "mithril"; -import User from "../models/User"; +import * as Mithril from 'mithril'; +import User from '../models/User'; /** * The `avatar` helper displays a user's avatar. diff --git a/js/src/common/helpers/listItems.tsx b/js/src/common/helpers/listItems.tsx index 4f7c5f2c35..3232d236a1 100644 --- a/js/src/common/helpers/listItems.tsx +++ b/js/src/common/helpers/listItems.tsx @@ -1,4 +1,4 @@ -import * as Mithril from "mithril"; +import * as Mithril from 'mithril'; import Separator from '../components/Separator'; import classList from '../utils/classList'; diff --git a/js/src/common/helpers/userOnline.tsx b/js/src/common/helpers/userOnline.tsx index 4af3609fa0..b89109db32 100644 --- a/js/src/common/helpers/userOnline.tsx +++ b/js/src/common/helpers/userOnline.tsx @@ -1,5 +1,5 @@ -import * as Mithril from "mithril"; -import User from "../models/User"; +import * as Mithril from 'mithril'; +import User from '../models/User'; import icon from './icon'; /** diff --git a/js/src/common/helpers/username.tsx b/js/src/common/helpers/username.tsx index 7bec93df2d..1ceffd15b6 100644 --- a/js/src/common/helpers/username.tsx +++ b/js/src/common/helpers/username.tsx @@ -1,5 +1,5 @@ -import * as Mithril from "mithril"; -import User from "../models/User"; +import * as Mithril from 'mithril'; +import User from '../models/User'; /** * The `username` helper displays a user's username in a From 6a57780950a4546bd4892ed29e28afb970b1a2c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Le=C5=9Bniak?= Date: Fri, 15 Jan 2021 12:24:47 +0000 Subject: [PATCH 4/4] Fix formatting, add missing JSDoc params --- js/src/common/helpers/avatar.tsx | 2 +- js/src/common/helpers/username.tsx | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/js/src/common/helpers/avatar.tsx b/js/src/common/helpers/avatar.tsx index ca842adb90..a60872b43c 100644 --- a/js/src/common/helpers/avatar.tsx +++ b/js/src/common/helpers/avatar.tsx @@ -4,9 +4,9 @@ import User from '../models/User'; /** * The `avatar` helper displays a user's avatar. * + * @param user * @param attrs Attributes to apply to the avatar element */ - export default function avatar(user: User, attrs: Object = {}): Mithril.Vnode { attrs.className = 'Avatar ' + (attrs.className || ''); let content: string = ''; diff --git a/js/src/common/helpers/username.tsx b/js/src/common/helpers/username.tsx index 1ceffd15b6..249a2fdb31 100644 --- a/js/src/common/helpers/username.tsx +++ b/js/src/common/helpers/username.tsx @@ -5,7 +5,6 @@ import User from '../models/User'; * The `username` helper displays a user's username in a * tag. If the user doesn't exist, the username will be displayed as [deleted]. */ - export default function username(user: User): Mithril.Vnode { const name = (user && user.displayName()) || app.translator.trans('core.lib.username.deleted_text');