Singular/Plural "Sats" labels (#380)

Co-authored-by: keyan <keyan.kousha+huumn@gmail.com>
This commit is contained in:
SatsAllDay 2023-08-08 17:04:06 -04:00 committed by GitHub
parent 67a0de3ea5
commit 6e05cd38f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 79 additions and 44 deletions

View File

@ -9,7 +9,7 @@ import {
MAX_TITLE_LENGTH, ITEM_FILTER_THRESHOLD,
DONT_LIKE_THIS_COST, COMMENT_DEPTH_LIMIT, COMMENT_TYPE_QUERY
} from '../../lib/constants'
import { msatsToSats } from '../../lib/format'
import { msatsToSats, numWithUnits } from '../../lib/format'
import { parse } from 'tldts'
import uu from 'url-unshort'
import { amountSchema, bountySchema, commentSchema, discussionSchema, jobSchema, linkSchema, pollSchema, ssValidate } from '../../lib/validate'
@ -731,7 +731,7 @@ export default {
const [{ item_act: vote }] = await serialize(models, models.$queryRaw`SELECT item_act(${Number(id)}::INTEGER, ${me.id}::INTEGER, 'TIP', ${Number(sats)}::INTEGER)`)
const updatedItem = await models.item.findUnique({ where: { id: Number(id) } })
const title = `your ${updatedItem.title ? 'post' : 'reply'} ${updatedItem.fwdUser ? 'forwarded' : 'stacked'} ${Math.floor(Number(updatedItem.msats) / 1000)} sats${updatedItem.fwdUser ? ` to @${updatedItem.fwdUser.name}` : ''}`
const title = `your ${updatedItem.title ? 'post' : 'reply'} ${updatedItem.fwdUser ? 'forwarded' : 'stacked'} ${numWithUnits(msatsToSats(updatedItem.msats))}${updatedItem.fwdUser ? ` to @${updatedItem.fwdUser.name}` : ''}`
sendUserNotification(updatedItem.userId, {
title,
body: updatedItem.title ? updatedItem.title : updatedItem.text,

View File

@ -3,6 +3,7 @@ import { Input, InputUserSuggest } from './form'
import InputGroup from 'react-bootstrap/InputGroup'
import { BOOST_MIN } from '../lib/constants'
import Info from './info'
import { numWithUnits } from '../lib/format'
export function AdvPostInitial ({ forward }) {
return {
@ -23,10 +24,10 @@ export default function AdvPostForm ({ edit }) {
<Info>
<ol className='fw-bold'>
<li>Boost ranks posts higher temporarily based on the amount</li>
<li>The minimum boost is {BOOST_MIN} sats</li>
<li>Each {BOOST_MIN} sats of boost is equivalent to one trusted upvote
<li>The minimum boost is {numWithUnits(BOOST_MIN, { abbreviate: false })}</li>
<li>Each {numWithUnits(BOOST_MIN, { abbreviate: false })} of boost is equivalent to one trusted upvote
<ul>
<li>e.g. {BOOST_MIN * 2} sats is like 2 votes</li>
<li>e.g. {numWithUnits(BOOST_MIN * 2, { abbreviate: false })} is like 2 votes</li>
</ul>
</li>
<li>The decay of boost "votes" increases at 2x the rate of organic votes

View File

@ -15,7 +15,7 @@ import PayBounty from './pay-bounty'
import BountyIcon from '../svgs/bounty-bag.svg'
import ActionTooltip from './action-tooltip'
import Flag from '../svgs/flag-fill.svg'
import { abbrNum } from '../lib/format'
import { numWithUnits } from '../lib/format'
import Share from './share'
import ItemInfo from './item-info'
import Badge from 'react-bootstrap/Badge'
@ -154,7 +154,7 @@ export default function Comment ({
<>
{includeParent && <Parent item={item} rootText={rootText} />}
{bountyPaid &&
<ActionTooltip notForm overlayText={`${abbrNum(root.bounty)} sats paid`}>
<ActionTooltip notForm overlayText={`${numWithUnits(root.bounty)} paid`}>
<BountyIcon className={`${styles.bountyIcon} ${'fill-success vertical-align-middle'}`} height={16} width={16} />
</ActionTooltip>}
</>

View File

@ -2,7 +2,7 @@ import Comment, { CommentSkeleton } from './comment'
import styles from './header.module.css'
import Nav from 'react-bootstrap/Nav'
import Navbar from 'react-bootstrap/Navbar'
import { abbrNum } from '../lib/format'
import { numWithUnits } from '../lib/format'
import { defaultCommentSort } from '../lib/item'
import { useRouter } from 'next/router'
@ -23,7 +23,7 @@ export function CommentsHeader ({ handleSort, pinned, bio, parentCreatedAt, comm
activeKey={sort}
>
<Nav.Item className='text-muted'>
{abbrNum(commentSats)} sats
{numWithUnits(commentSats)}
</Nav.Item>
<div className='ms-auto d-flex'>
<Nav.Item>

View File

@ -5,13 +5,14 @@ import styles from './fee-button.module.css'
import { gql, useQuery } from '@apollo/client'
import { useFormikContext } from 'formik'
import { SSR } from '../lib/constants'
import { numWithUnits } from '../lib/format'
function Receipt ({ cost, repetition, hasImgLink, baseFee, parentId, boost }) {
return (
<Table className={styles.receipt} borderless size='sm'>
<tbody>
<tr>
<td>{baseFee} sats</td>
<td>{numWithUnits(baseFee, { abbreviate: false })}</td>
<td align='right' className='font-weight-light'>{parentId ? 'reply' : 'post'} fee</td>
</tr>
{hasImgLink &&
@ -26,13 +27,13 @@ function Receipt ({ cost, repetition, hasImgLink, baseFee, parentId, boost }) {
</tr>}
{boost > 0 &&
<tr>
<td>+ {boost} sats</td>
<td>+ {numWithUnits(boost, { abbreviate: false })}</td>
<td className='font-weight-light' align='right'>boost</td>
</tr>}
</tbody>
<tfoot>
<tr>
<td className='fw-bold'>{cost} sats</td>
<td className='fw-bold'>{numWithUnits(cost, { abbreviate: false })}</td>
<td align='right' className='font-weight-light'>total fee</td>
</tr>
</tfoot>
@ -53,8 +54,8 @@ export default function FeeButton ({ parentId, hasImgLink, baseFee, ChildButton,
const show = alwaysShow || !formik?.isSubmitting
return (
<div className='d-flex align-items-center'>
<ActionTooltip overlayText={`${cost} sats`}>
<ChildButton variant={variant} disabled={disabled}>{text}{cost > baseFee && show && <small> {cost} sats</small>}</ChildButton>
<ActionTooltip overlayText={numWithUnits(cost, { abbreviate: false })}>
<ChildButton variant={variant} disabled={disabled}>{text}{cost > baseFee && show && <small> {numWithUnits(cost, { abbreviate: false })}</small>}</ChildButton>
</ActionTooltip>
{cost > baseFee && show &&
<Info>
@ -71,7 +72,7 @@ function EditReceipt ({ cost, paidSats, addImgLink, boost, parentId }) {
{addImgLink &&
<>
<tr>
<td>{paidSats} sats</td>
<td>{numWithUnits(paidSats, { abbreviate: false })}</td>
<td align='right' className='font-weight-light'>{parentId ? 'reply' : 'post'} fee</td>
</tr>
<tr>
@ -79,19 +80,19 @@ function EditReceipt ({ cost, paidSats, addImgLink, boost, parentId }) {
<td align='right' className='font-weight-light'>image/link fee</td>
</tr>
<tr>
<td>- {paidSats} sats</td>
<td>- {numWithUnits(paidSats, { abbreviate: false })}</td>
<td align='right' className='font-weight-light'>already paid</td>
</tr>
</>}
{boost > 0 &&
<tr>
<td>+ {boost} sats</td>
<td>+ {numWithUnits(boost, { abbreviate: false })}</td>
<td className='font-weight-light' align='right'>boost</td>
</tr>}
</tbody>
<tfoot>
<tr>
<td className='fw-bold'>{cost} sats</td>
<td className='fw-bold'>{numWithUnits(cost)}</td>
<td align='right' className='font-weight-light'>total fee</td>
</tr>
</tfoot>
@ -108,8 +109,8 @@ export function EditFeeButton ({ paidSats, hadImgLink, hasImgLink, ChildButton,
const show = alwaysShow || !formik?.isSubmitting
return (
<div className='d-flex align-items-center'>
<ActionTooltip overlayText={`${cost} sats`}>
<ChildButton variant={variant}>{text}{cost > 0 && show && <small> {cost} sats</small>}</ChildButton>
<ActionTooltip overlayText={numWithUnits(cost, { abbreviate: false })}>
<ChildButton variant={variant}>{text}{cost > 0 && show && <small> {numWithUnits(cost, { abbreviate: false })}</small>}</ChildButton>
</ActionTooltip>
{cost > 0 && show &&
<Info>

View File

@ -1,5 +1,6 @@
import AccordianItem from './accordian-item'
import Qr from './qr'
import { numWithUnits } from '../lib/format'
export function Invoice ({ invoice }) {
let variant = 'default'
@ -7,7 +8,7 @@ export function Invoice ({ invoice }) {
let webLn = true
if (invoice.confirmedAt) {
variant = 'confirmed'
status = `${invoice.satsReceived} sats deposited`
status = `${numWithUnits(invoice.satsReceived, { abbreviate: false })} deposited`
webLn = false
} else if (invoice.cancelled) {
variant = 'failed'

View File

@ -23,6 +23,7 @@ import Toc from './table-of-contents'
import Link from 'next/link'
import { RootProvider } from './root'
import { IMGPROXY_URL_REGEXP } from '../lib/url'
import { numWithUnits } from '../lib/format'
function BioItem ({ item, handleClick }) {
const me = useMe()
@ -139,11 +140,11 @@ function TopLevelItem ({ item, noReply, ...props }) {
{item.bountyPaidTo?.length
? (
<div className='px-3 py-1 d-inline-block bg-grey-medium rounded text-success'>
<Check className='fill-success' /> {item.bounty} sats paid
<Check className='fill-success' /> {numWithUnits(item.bounty, { abbreviate: false })} paid
</div>)
: (
<div className='px-3 py-1 d-inline-block bg-grey-darkmode rounded text-light'>
{item.bounty} sats bounty
{numWithUnits(item.bounty, { abbreviate: false })} bounty
</div>)}
</div>}
</div>

View File

@ -4,7 +4,7 @@ import { useEffect, useState } from 'react'
import Badge from 'react-bootstrap/Badge'
import Dropdown from 'react-bootstrap/Dropdown'
import Countdown from './countdown'
import { abbrNum } from '../lib/format'
import { abbrNum, numWithUnits } from '../lib/format'
import { newComments, commentsViewedAt } from '../lib/new-comments'
import { timeSince } from '../lib/time'
import CowboyHat from './cowboy-hat'
@ -34,7 +34,12 @@ export default function ItemInfo ({ item, pendingSats, full, commentsText, class
<div className={className || `${styles.other}`}>
{!item.position &&
<>
<span title={`from ${item.upvotes} stackers ${item.mine ? `\\ ${item.meSats} sats to post` : `(${item.meSats + pendingSats} sats from me)`} `}>{abbrNum(item.sats + pendingSats)} sats</span>
<span title={`from ${item.upvotes} stackers ${item.mine
? `\\ ${numWithUnits(item.meSats, { abbreviate: false })} to post`
: `(${numWithUnits(item.meSats + pendingSats, { abbreviate: false })} from me)`} `}
>
{numWithUnits(item.sats + pendingSats)}
</span>
<span> \ </span>
</>}
{item.boost > 0 &&
@ -51,7 +56,7 @@ export default function ItemInfo ({ item, pendingSats, full, commentsText, class
`/items/${item.id}?commentsViewedAt=${viewedAt}`,
`/items/${item.id}`)
}
}} title={`${item.commentSats} sats`} className='text-reset position-relative'
}} title={numWithUnits(item.commentSats)} className='text-reset position-relative'
>
{item.ncomments} {commentsText || 'comments'}
{hasNewComments &&

View File

@ -10,7 +10,7 @@ import BountyIcon from '../svgs/bounty-bag.svg'
import ActionTooltip from './action-tooltip'
import Flag from '../svgs/flag-fill.svg'
import ImageIcon from '../svgs/image-fill.svg'
import { abbrNum } from '../lib/format'
import { numWithUnits } from '../lib/format'
import ItemInfo from './item-info'
import { commentsViewedAt } from '../lib/new-comments'
import { useRouter } from 'next/router'
@ -58,7 +58,7 @@ export default function Item ({ item, rank, belowTitle, right, full, children, s
{item.pollCost && <span className={styles.icon}> <PollIcon className='fill-grey ms-1' height={14} width={14} /></span>}
{item.bounty > 0 &&
<span className={styles.icon}>
<ActionTooltip notForm overlayText={`${abbrNum(item.bounty)} ${item.bountyPaidTo?.length ? 'sats paid' : 'sats bounty'}`}>
<ActionTooltip notForm overlayText={`${numWithUnits(item.bounty)} ${item.bountyPaidTo?.length ? ' paid' : ' bounty'}`}>
<BountyIcon className={`${styles.bountyIcon} ${item.bountyPaidTo?.length ? 'fill-success' : 'fill-grey'}`} height={16} width={16} />
</ActionTooltip>
</span>}

View File

@ -24,6 +24,7 @@ import { useData } from './use-data'
import { nostrZapDetails } from '../lib/nostr'
import Text from './text'
import NostrIcon from '../svgs/nostr.svg'
import { numWithUnits } from '../lib/format'
function Notification ({ n, fresh }) {
const type = n.__typename
@ -155,14 +156,14 @@ function EarnNotification ({ n }) {
<HandCoin className='align-self-center fill-boost mx-1' width={24} height={24} style={{ flex: '0 0 24px', transform: 'rotateY(180deg)' }} />
<div className='ms-2'>
<div className='fw-bold text-boost'>
you stacked {n.earnedSats} sats in rewards<small className='text-muted ms-1 fw-normal' suppressHydrationWarning>{timeSince(new Date(n.sortTime))}</small>
you stacked {numWithUnits(n.earnedSats, { abbreviate: false })} in rewards<small className='text-muted ms-1 fw-normal' suppressHydrationWarning>{timeSince(new Date(n.sortTime))}</small>
</div>
{n.sources &&
<div style={{ fontSize: '80%', color: 'var(--theme-grey)' }}>
{n.sources.posts > 0 && <span>{n.sources.posts} sats for top posts</span>}
{n.sources.comments > 0 && <span>{n.sources.posts > 0 && ' \\ '}{n.sources.comments} sats for top comments</span>}
{n.sources.tipPosts > 0 && <span>{(n.sources.comments > 0 || n.sources.posts > 0) && ' \\ '}{n.sources.tipPosts} sats for zapping top posts early</span>}
{n.sources.tipComments > 0 && <span>{(n.sources.comments > 0 || n.sources.posts > 0 || n.sources.tipPosts > 0) && ' \\ '}{n.sources.tipComments} sats for zapping top comments early</span>}
{n.sources.posts > 0 && <span>{numWithUnits(n.sources.posts, { abbreviate: false })} for top posts</span>}
{n.sources.comments > 0 && <span>{n.sources.posts > 0 && ' \\ '}{numWithUnits(n.sources.comments, { abbreviate: false })} for top comments</span>}
{n.sources.tipPosts > 0 && <span>{(n.sources.comments > 0 || n.sources.posts > 0) && ' \\ '}{numWithUnits(n.sources.tipPosts, { abbreviate: false })} for zapping top posts early</span>}
{n.sources.tipComments > 0 && <span>{(n.sources.comments > 0 || n.sources.posts > 0 || n.sources.tipPosts > 0) && ' \\ '}{numWithUnits(n.sources.tipComments, { abbreviate: false })} for zapping top comments early</span>}
</div>}
<div className='pb-1' style={{ lineHeight: '140%' }}>
SN distributes the sats it earns back to its best stackers daily. These sats come from <Link href='/~jobs'>jobs</Link>, boosts, posting fees, and donations. You can see the daily rewards pool and make a donation <Link href='/rewards'>here</Link>.
@ -217,7 +218,7 @@ function NostrZap ({ n }) {
function InvoicePaid ({ n }) {
return (
<div className='fw-bold text-info ms-2 py-1'>
<Check className='fill-info me-1' />{n.earnedSats} sats were deposited in your account
<Check className='fill-info me-1' />{numWithUnits(n.earnedSats, { abbreviate: false })} were deposited in your account
<small className='text-muted ms-1 fw-normal' suppressHydrationWarning>{timeSince(new Date(n.sortTime))}</small>
</div>
)
@ -236,7 +237,7 @@ function Votification ({ n }) {
return (
<>
<small className='fw-bold text-success ms-2'>
your {n.item.title ? 'post' : 'reply'} {n.item.fwdUser ? 'forwarded' : 'stacked'} {n.earnedSats} sats{n.item.fwdUser && ` to @${n.item.fwdUser.name}`}
your {n.item.title ? 'post' : 'reply'} {n.item.fwdUser ? 'forwarded' : 'stacked'} {numWithUnits(n.earnedSats, { abbreviate: false })}{n.item.fwdUser && ` to @${n.item.fwdUser.name}`}
</small>
<div>
{n.item.title

View File

@ -4,7 +4,7 @@ import styles from './pay-bounty.module.css'
import ActionTooltip from './action-tooltip'
import { useMutation, gql } from '@apollo/client'
import { useMe } from './me'
import { abbrNum } from '../lib/format'
import { numWithUnits } from '../lib/format'
import { useShowModal } from './modal'
import FundError from './fund-error'
import { useRoot } from './root'
@ -90,7 +90,7 @@ export default function PayBounty ({ children, item }) {
return (
<ActionTooltip
notForm
overlayText={`${root.bounty} sats`}
overlayText={numWithUnits(root.bounty)}
>
<div
className={styles.pay} onClick={() => {
@ -101,7 +101,7 @@ export default function PayBounty ({ children, item }) {
</div>
<div className='text-center'>
<Button className='mt-4' variant='primary' onClick={() => handlePayBounty(onClose)}>
pay <small>{abbrNum(root.bounty)} sats</small>
pay <small>{numWithUnits(root.bounty)}</small>
</Button>
</div>
</>

View File

@ -1,6 +1,7 @@
import { NextSeo } from 'next-seo'
import { useRouter } from 'next/router'
import removeMd from 'remove-markdown'
import { numWithUnits } from '../lib/format'
export function SeoSearch ({ sub }) {
const router = useRouter()
@ -55,7 +56,7 @@ export default function Seo ({ sub, item, user }) {
desc = desc.replace(/\s+/g, ' ')
}
} else {
desc = `@${item.user.name} stacked ${item.sats} sats ${item.url ? `posting ${item.url}` : 'with this discussion'}`
desc = `@${item.user.name} stacked ${numWithUnits(item.sats)} ${item.url ? `posting ${item.url}` : 'with this discussion'}`
}
if (item.ncomments) {
desc += ` [${item.ncomments} comments`

View File

@ -6,6 +6,27 @@ export const abbrNum = n => {
if (n >= 1e12) return +(n / 1e12).toFixed(1) + 't'
}
/**
* Take a number that represents a count
* and return a formatted label e.g. 0 sats, 1 sat, 2 sats
*
* @param n The number of sats
* @param opts Options
* @param opts.abbreviate Whether to abbreviate the number
* @param opts.unitSingular The singular unit label
* @param opts.unitPlural The plural unit label
*/
export const numWithUnits = (n, {
abbreviate = true,
unitSingular = 'sat',
unitPlural = 'sats'
} = {}) => {
if (isNaN(n)) {
return `${n} ${unitPlural}`
}
return `${abbreviate ? abbrNum(n) : n} ${n === 1 ? unitSingular : unitPlural}`
}
export const fixedDecimal = (n, f) => {
return Number.parseFloat(n).toFixed(f)
}

View File

@ -9,6 +9,7 @@ import { useQuery } from '@apollo/client'
import PageLoading from '../../components/page-loading'
import { WHENS } from '../../lib/constants'
import dynamic from 'next/dynamic'
import { numWithUnits } from '../../lib/format'
const WhenComposedChart = dynamic(() => import('../../components/charts').then(mod => mod.WhenComposedChart), {
loading: () => <div>Loading...</div>
@ -44,7 +45,7 @@ export default function Referrals ({ ssrData }) {
return (
<CenterLayout footerLinks>
<h4 className='fw-bold text-muted text-center pt-5 pb-3 d-flex align-items-center justify-content-center'>
{totalReferrals} referrals & {totalSats} sats in the last
{totalReferrals} referrals & {numWithUnits(totalSats, { abbreviate: false })} in the last
<Select
groupClassName='mb-0 ms-2'
className='w-auto'

View File

@ -9,7 +9,7 @@ import { useMutation, useQuery } from '@apollo/client'
import Link from 'next/link'
import { amountSchema } from '../lib/validate'
import Countdown from 'react-countdown'
import { abbrNum } from '../lib/format'
import { numWithUnits } from '../lib/format'
import PageLoading from '../components/page-loading'
import { useShowModal } from '../components/modal'
import dynamic from 'next/dynamic'
@ -52,7 +52,7 @@ export function RewardLine ({ total }) {
return (
<>
{abbrNum(total)} sats in rewards
{numWithUnits(total)} in rewards
{threshold &&
<Countdown
date={threshold}

View File

@ -15,6 +15,7 @@ import { CREATE_WITHDRAWL, SEND_TO_LNADDR } from '../fragments/wallet'
import { getGetServerSideProps } from '../api/ssrApollo'
import { amountSchema, lnAddrSchema, withdrawlSchema } from '../lib/validate'
import { SSR } from '../lib/constants'
import { numWithUnits } from '../lib/format'
export const getServerSideProps = getGetServerSideProps()
@ -30,7 +31,7 @@ function YouHaveSats () {
const me = useMe()
return (
<h2 className={`${me ? 'visible' : 'invisible'} text-success pb-5`}>
you have <span className='text-monospace'>{me && me.sats}</span> sats
you have <span className='text-monospace'>{me && numWithUnits(me.sats, { abbreviate: false })}</span>
</h2>
)
}

View File

@ -7,6 +7,7 @@ import { useRouter } from 'next/router'
import { WITHDRAWL } from '../../fragments/wallet'
import Link from 'next/link'
import { SSR } from '../../lib/constants'
import { numWithUnits } from '../../lib/format'
export default function Withdrawl () {
return (
@ -53,7 +54,7 @@ function LoadWithdrawl () {
let variant = 'default'
switch (data.withdrawl.status) {
case 'CONFIRMED':
status = `sent ${data.withdrawl.satsPaid} sats with ${data.withdrawl.satsFeePaid} sats in routing fees`
status = `sent ${numWithUnits(data.withdrawl.satsPaid, { abbreviate: false })} with ${numWithUnits(data.withdrawl.satsFeePaid, { abbreviate: false })} in routing fees`
variant = 'confirmed'
break
case 'INSUFFICIENT_BALANCE':