From cb31e0a8c7a22561584682b0d8246094ce5eda17 Mon Sep 17 00:00:00 2001 From: Martti Malmi Date: Tue, 24 Jan 2023 21:27:23 +0200 Subject: [PATCH] badges, badges everywhere --- src/css/style.css | 6 +++- src/js/components/Badge.tsx | 43 +++++++++++++++++++++++++ src/js/components/{Name.ts => Name.tsx} | 18 +++++++---- src/js/components/PublicMessage.js | 42 ++++++------------------ src/js/components/SearchBox.tsx | 3 +- 5 files changed, 71 insertions(+), 41 deletions(-) create mode 100644 src/js/components/Badge.tsx rename src/js/components/{Name.ts => Name.tsx} (60%) diff --git a/src/css/style.css b/src/css/style.css index 1d993ab1..839df4ea 100644 --- a/src/css/style.css +++ b/src/css/style.css @@ -1183,11 +1183,15 @@ a.msg { align-items: center; } -.msg-sender-link .badge { +.badge { margin-left: 5px; text-shadow: white 0 0 1px; } +.text-color { + color: var(--text-color); +} + .public-messages-view .msg-sender .msg-menu-btn { text-align: right; flex: 1; diff --git a/src/js/components/Badge.tsx b/src/js/components/Badge.tsx new file mode 100644 index 00000000..ec1bc5db --- /dev/null +++ b/src/js/components/Badge.tsx @@ -0,0 +1,43 @@ +import iris from 'iris-lib'; + +import Icons from '../Icons'; +import Nostr from '../Nostr'; +import { translate as t } from '../translations/Translation'; + +export default function Badge(props) { + const myPub = iris.session.getKey().secp256k1.rpub; + const hexAddress = Nostr.toNostrHexAddress(props.pub); + if (hexAddress === myPub) { + return ( + + {Icons.checkmark} + {t('you')} + + ); + } + if (!hexAddress) { + return null; + } + const following = Nostr.followedByUser.get(myPub)?.has(hexAddress); + if (following) { + return ( + + {Icons.checkmark} + {t('following')} + + ); + } else { + const count = Nostr.followedByFriendsCount(hexAddress); + if (count > 0) { + const className = count > 10 ? 'neutral' : 'text-color'; + return ( + + {Icons.checkmark} + + {count} {t('friends_following')} + + + ); + } + } +} diff --git a/src/js/components/Name.ts b/src/js/components/Name.tsx similarity index 60% rename from src/js/components/Name.ts rename to src/js/components/Name.tsx index 9498c918..a1a9a196 100644 --- a/src/js/components/Name.ts +++ b/src/js/components/Name.tsx @@ -4,16 +4,19 @@ import Component from '../BaseComponent'; import Helpers from '../Helpers'; import Nostr from '../Nostr'; +import Badge from './Badge'; + type Props = { pub: string; placeholder?: string; + hideBadge?: boolean; }; type State = { name: string; }; -class Name extends Component { +export default class Name extends Component { componentDidMount(): void { const nostrAddr = Nostr.toNostrHexAddress(this.props.pub); if (nostrAddr) { @@ -27,11 +30,14 @@ class Name extends Component { render() { return ( - this.state.name ?? - this.props.placeholder ?? - Helpers.generateName(Nostr.toNostrBech32Address(this.props.pub, 'npub') || this.props.pub) + <> + {this.state.name ?? + this.props.placeholder ?? + Helpers.generateName( + Nostr.toNostrBech32Address(this.props.pub, 'npub') || this.props.pub, + )} + {!this.props.hideBadge && } + ); } } - -export default Name; diff --git a/src/js/components/PublicMessage.js b/src/js/components/PublicMessage.js index 04c805a0..6303fa01 100644 --- a/src/js/components/PublicMessage.js +++ b/src/js/components/PublicMessage.js @@ -9,6 +9,7 @@ import Icons from '../Icons'; import Nostr from '../Nostr'; import { translate as t } from '../translations/Translation'; +import Badge from './Badge'; import CopyButton from './CopyButton'; import Dropdown from './Dropdown'; import FeedMessageForm from './FeedMessageForm'; @@ -419,37 +420,6 @@ class PublicMessage extends Message { this.setState({ showBoosts: !this.state.showBoosts, showLikes: false }); } - getBadge() { - const myPub = iris.session.getKey().secp256k1.rpub; - const hexAddress = Nostr.toNostrHexAddress(this.state.msg.info.from); - if (hexAddress === myPub) { - return null; - } - if (!hexAddress) { - return null; - } - const following = Nostr.followedByUser.get(myPub)?.has(hexAddress); - if (following) { - return html` - - ${Icons.checkmark} - ${t('following')} - - `; - } else { - const count = Nostr.followedByFriendsCount(hexAddress); - if (count > 0) { - const className = count > 10 ? 'neutral' : ''; - return html` - - ${Icons.checkmark} - ${count} ${t('friends_following')} - - `; - } - } - } - render() { const isThumbnail = this.props.thumbnail ? 'thumbnail-item' : ''; if (!this.state.msg) { @@ -582,8 +552,14 @@ class PublicMessage extends Message {