merge non-normalized user subtypes fixing #645

This commit is contained in:
keyan 2023-11-20 09:04:38 -06:00
parent 44e12a4b44
commit af59e3ef54
16 changed files with 42 additions and 39 deletions

View File

@ -35,8 +35,8 @@ export default function WelcomeBanner () {
}
})
useEffect(() => {
setHidden(me?.hideWelcomeBanner || (!me && window.localStorage.getItem('hideWelcomeBanner')))
}, [me?.hideWelcomeBanner])
setHidden(me?.privates?.hideWelcomeBanner || (!me && window.localStorage.getItem('hideWelcomeBanner')))
}, [me?.privates?.hideWelcomeBanner])
if (hidden) return

View File

@ -99,7 +99,7 @@ export default function Comment ({
}) {
const [edit, setEdit] = useState()
const me = useMe()
const isHiddenFreebie = !me?.wildWestMode && !me?.greeterMode && !item.mine && item.freebie && item.wvotes <= 0
const isHiddenFreebie = !me?.privates?.wildWestMode && !me?.privates?.greeterMode && !item.mine && item.freebie && item.wvotes <= 0
const [collapse, setCollapse] = useState(
isHiddenFreebie || item?.user?.meMute
? 'yep'

View File

@ -99,7 +99,7 @@ export function DiscussionForm ({
initial={{
title: item?.title || shareTitle || '',
text: item?.text || '',
crosspost: me?.nostrCrossposting,
crosspost: me?.privates?.nostrCrossposting,
...AdvPostInitial({ forward: normalizeForwards(item?.forwards), boost: item?.boost }),
...SubSelectInitial({ sub: item?.subName || sub?.name })
}}

View File

@ -115,7 +115,7 @@ export default function FeeButton ({ ChildButton = SubmitButton, variant, text,
// freebies: there's only a base cost, it's less than 10, and we have less than 10 sats
const free = total === lines.baseCost?.modifier(0) &&
total <= FREEBIE_BASE_COST_THRESHOLD &&
me?.sats < FREEBIE_BASE_COST_THRESHOLD
me?.privates?.sats < FREEBIE_BASE_COST_THRESHOLD
const feeText = free
? 'free'
: total > 1

View File

@ -29,10 +29,10 @@ import { clearNotifications } from '../lib/badge'
function WalletSummary ({ me }) {
if (!me) return null
if (me.hideWalletBalance) {
if (me.privates?.hideWalletBalance) {
return <HiddenWalletSummary abbreviate fixedWidth />
}
return `${abbrNum(me.sats)}`
return `${abbrNum(me.privates?.sats)}`
}
function Back () {

View File

@ -18,7 +18,7 @@ export default function HiddenWalletSummary ({ abbreviate, fixedWidth }) {
ref={ref} style={{ width: fixedWidth ? width : undefined }}
className='d-inline-block text-monospace' align='right' onPointerEnter={() => setHover(true)} onPointerLeave={() => setHover(false)}
>
{hover ? (abbreviate ? abbrNum(me.sats) : numWithUnits(me.sats, { abbreviate: false, format: true })) : '******'}
{hover ? (abbreviate ? abbrNum(me.privates?.sats) : numWithUnits(me.privates?.sats, { abbreviate: false, format: true })) : '******'}
</span>
)
}

View File

@ -23,7 +23,7 @@ function ImageOriginal ({ src, topLevel, nofollow, tab, children, onClick, ...pr
const [showImage, setShowImage] = useState(false)
useEffect(() => {
if (me?.imgproxyOnly && tab !== 'preview') return
if (me?.privates?.imgproxyOnly && tab !== 'preview') return
// make sure it's not a false negative by trying to load URL as <img>
const img = new window.Image()
img.onload = () => setShowImage(true)

View File

@ -67,7 +67,7 @@ export default function ItemAct ({ onClose, itemId, act, down, strike }) {
return (
<Form
initial={{
amount: me?.tipDefault || defaultTips[0],
amount: me?.privates?.tipDefault || defaultTips[0],
default: false
}}
schema={amountSchema}

View File

@ -55,7 +55,7 @@ export function LoggerProvider ({ children }) {
const log = useCallback(level => {
return async (message, context) => {
if (!me || !me.diagnostics) return
if (!me || !me.privates?.diagnostics) return
const env = {
userAgent: window.navigator.userAgent,
// os may not be initialized yet
@ -78,7 +78,7 @@ export function LoggerProvider ({ children }) {
body: JSON.stringify(body)
}).catch(console.error)
}
}, [me?.diagnostics, name, os])
}, [me?.privates?.diagnostics, name, os])
const logger = useMemo(() => ({
info: log('info'),

View File

@ -1,4 +1,4 @@
import React, { useContext, useMemo } from 'react'
import React, { useContext } from 'react'
import { useQuery } from '@apollo/client'
import { ME } from '../fragments/users'
import { SSR } from '../lib/constants'
@ -9,22 +9,14 @@ export const MeContext = React.createContext({
export function MeProvider ({ me, children }) {
const { data } = useQuery(ME, SSR ? {} : { pollInterval: 1000, nextFetchPolicy: 'cache-and-network' })
const futureMe = data?.me || me
const contextValue = useMemo(() => ({
me: futureMe
? { ...futureMe, ...futureMe.privates, ...futureMe.optional }
: null
}), [me, data])
return (
<MeContext.Provider value={contextValue}>
<MeContext.Provider value={data?.me || me}>
{children}
</MeContext.Provider>
)
}
export function useMe () {
const { me } = useContext(MeContext)
return me
return useContext(MeContext)
}

View File

@ -18,7 +18,7 @@ export function usePrice () {
export function PriceProvider ({ price, children }) {
const me = useMe()
const fiatCurrency = me?.fiatCurrency
const fiatCurrency = me?.privates?.fiatCurrency
const { data } = useQuery(PRICE, {
variables: { fiatCurrency },
...(SSR
@ -32,7 +32,7 @@ export function PriceProvider ({ price, children }) {
const contextValue = useMemo(() => ({
price: data?.price || price,
fiatSymbol: CURRENCY_SYMBOLS[fiatCurrency] || '$'
}), [data?.price, price, me?.fiatCurrency])
}), [data?.price, price, me?.privates?.fiatCurrency])
return (
<PriceContext.Provider value={contextValue}>

View File

@ -39,7 +39,7 @@ const UpvotePopover = ({ target, show, handleClose }) => {
<button type='button' className='btn-close' onClick={handleClose}><span className='visually-hidden-focusable'>Close alert</span></button>
</Popover.Header>
<Popover.Body>
<div className='mb-2'>Press the bolt again to zap {me?.tipDefault || 1} more sat{me?.tipDefault > 1 ? 's' : ''}.</div>
<div className='mb-2'>Press the bolt again to zap {me?.privates?.tipDefault || 1} more sat{me?.privates?.tipDefault > 1 ? 's' : ''}.</div>
<div>Repeatedly press the bolt to zap more sats.</div>
</Popover.Body>
</Popover>
@ -83,7 +83,7 @@ export default function UpVote ({ item, className, pendingSats, setPendingSats }
if (!me) return
// if they haven't seen the walkthrough and they have sats
if (yes && !me.upvotePopover && me.sats) {
if (yes && !me.privates?.upvotePopover && me.privates?.sats) {
_setVoteShow(true)
}
@ -97,7 +97,7 @@ export default function UpVote ({ item, className, pendingSats, setPendingSats }
if (!me) return
// if we want to show it, yet we still haven't shown
if (yes && !me.tipPopover && me.sats) {
if (yes && !me.privates?.tipPopover && me.privates?.sats) {
_setTipShow(true)
}
@ -124,7 +124,7 @@ export default function UpVote ({ item, className, pendingSats, setPendingSats }
},
meSats: me
? (existingSats = 0) => {
if (sats <= me.sats) {
if (sats <= me.privates?.sats) {
if (existingSats === 0) {
setVoteShow(true)
} else {
@ -186,8 +186,8 @@ export default function UpVote ({ item, className, pendingSats, setPendingSats }
const meSats = (item?.meSats || item?.meAnonSats || 0) + pendingSats
// what should our next tip be?
let sats = me?.tipDefault || 1
if (me?.turboTipping) {
let sats = me?.privates?.tipDefault || 1
if (me?.privates?.turboTipping) {
let raiseTip = sats
while (meSats >= raiseTip) {
raiseTip *= 10
@ -197,7 +197,7 @@ export default function UpVote ({ item, className, pendingSats, setPendingSats }
}
return [meSats, sats, me ? numWithUnits(sats, { abbreviate: false }) : 'zap it', getColor(meSats)]
}, [item?.meSats, item?.meAnonSats, pendingSats, me?.tipDefault, me?.turboDefault])
}, [item?.meSats, item?.meAnonSats, pendingSats, me?.privates?.tipDefault, me?.privates?.turboDefault])
return (
<LightningConsumer>

View File

@ -33,6 +33,17 @@ function getClient (uri) {
cache: new InMemoryCache({
freezeResults: true,
typePolicies: {
User: {
// https://www.apollographql.com/docs/react/caching/cache-field-behavior/#merging-non-normalized-objects
fields: {
privates: {
merge: true
},
optional: {
merge: true
}
}
},
Query: {
fields: {
sub: {

View File

@ -277,7 +277,7 @@ export default function Settings ({ ssrData }) {
name='hideBookmarks'
groupClassName='mb-0'
/>
{me.isContributor &&
{me.optional?.isContributor &&
<Checkbox
label={<>hide that I'm a stacker.news contributor</>}
name='hideIsContributor'

View File

@ -62,9 +62,9 @@ function YouHaveSats () {
<h2 className={`${me ? 'visible' : 'invisible'} text-success`}>
you have{' '}
<span className='text-monospace'>{me && (
me.hideWalletBalance
me.privates?.hideWalletBalance
? <HiddenWalletSummary />
: numWithUnits(me.sats, { abbreviate: false, format: true })
: numWithUnits(me.privates?.sats, { abbreviate: false, format: true })
)}
</span>
</h2>
@ -201,7 +201,7 @@ export function InvWithdrawal () {
const [createWithdrawl, { called, error }] = useMutation(CREATE_WITHDRAWL)
const maxFeeDefault = me?.withdrawMaxFeeDefault
const maxFeeDefault = me?.privates?.withdrawMaxFeeDefault
useEffect(() => {
async function effect () {
@ -209,7 +209,7 @@ export function InvWithdrawal () {
const provider = await requestProvider()
const { paymentRequest: invoice } = await provider.makeInvoice({
defaultMemo: `Withdrawal for @${me.name} on SN`,
maximumAmount: Math.max(me.sats - maxFeeDefault, 0)
maximumAmount: Math.max(me.privates?.sats - maxFeeDefault, 0)
})
const { data } = await createWithdrawl({ variables: { invoice, maxFee: maxFeeDefault } })
router.push(`/withdrawals/${data.createWithdrawl.id}`)
@ -340,7 +340,7 @@ export function LnAddrWithdrawal () {
const defaultOptions = { min: 1 }
const [addrOptions, setAddrOptions] = useState(defaultOptions)
const [formSchema, setFormSchema] = useState(lnAddrSchema())
const maxFeeDefault = me?.withdrawMaxFeeDefault
const maxFeeDefault = me?.privates?.withdrawMaxFeeDefault
const onAddrChange = useDebounceCallback(async (formik, e) => {
if (!e?.target?.value) {

View File

@ -120,7 +120,7 @@ function PrivacyOption ({ wd }) {
if (!oldEnough) {
return (
<span className='text-muted fst-italic'>
{`this invoice ${me.autoDropBolt11s ? 'will be auto-deleted' : 'can be deleted'} in ${timeLeft(keepUntil)}`}
{`this invoice ${me.privates?.autoDropBolt11s ? 'will be auto-deleted' : 'can be deleted'} in ${timeLeft(keepUntil)}`}
</span>
)
}