stacker.news/pages/[name]/index.js

126 lines
3.6 KiB
JavaScript
Raw Normal View History

import Layout from '@/components/layout'
2021-09-24 21:28:21 +00:00
import { gql, useMutation, useQuery } from '@apollo/client'
import UserHeader from '@/components/user-header'
2023-07-24 18:35:05 +00:00
import Button from 'react-bootstrap/Button'
import styles from '@/styles/user.module.css'
2021-09-23 17:42:00 +00:00
import { useState } from 'react'
import ItemFull from '@/components/item-full'
import { Form, MarkdownInput } from '@/components/form'
import { useMe } from '@/components/me'
import { USER_FULL } from '@/fragments/users'
import { ITEM_FIELDS } from '@/fragments/items'
import { getGetServerSideProps } from '@/api/ssrApollo'
import { FeeButtonProvider } from '@/components/fee-button'
import { bioSchema } from '@/lib/validate'
import { useRouter } from 'next/router'
import PageLoading from '@/components/page-loading'
import { ItemButtonBar } from '@/components/post'
2021-04-22 22:14:32 +00:00
export const getServerSideProps = getGetServerSideProps({
query: USER_FULL,
notFound: data => !data.user
})
2021-04-22 22:14:32 +00:00
export function BioForm ({ handleDone, bio }) {
2021-09-24 21:28:21 +00:00
const [upsertBio] = useMutation(
2021-09-23 17:42:00 +00:00
gql`
2021-09-24 21:28:21 +00:00
${ITEM_FIELDS}
mutation upsertBio($bio: String!) {
upsertBio(bio: $bio) {
2021-09-23 17:42:00 +00:00
id
2021-09-24 21:28:21 +00:00
bio {
...ItemFields
text
}
2021-09-23 17:42:00 +00:00
}
}`, {
2021-09-24 21:28:21 +00:00
update (cache, { data: { upsertBio } }) {
2021-09-23 17:42:00 +00:00
cache.modify({
2021-09-24 21:28:21 +00:00
id: `User:${upsertBio.id}`,
2021-09-23 17:42:00 +00:00
fields: {
bio () {
2021-09-24 21:28:21 +00:00
return upsertBio.bio
2021-09-23 17:42:00 +00:00
}
}
})
}
}
)
return (
<div className={styles.createFormContainer}>
2023-11-13 02:31:39 +00:00
<FeeButtonProvider>
<Form
initial={{
bio: bio?.text || ''
}}
schema={bioSchema}
onSubmit={async values => {
const { error } = await upsertBio({ variables: values })
backend payment optimism (#1195) * wip backend optimism * another inch * make action state transitions only happen once * another inch * almost ready for testing * use interactive txs * another inch * ready for basic testing * lint fix * inches * wip item update * get item update to work * donate and downzap * inchy inch * fix territory paid actions * wip usePaidMutation * usePaidMutation error handling * PENDING_HELD and HELD transitions, gql paidAction return types * mostly working pessimism * make sure invoice field is present in optimisticResponse * inches * show optimistic values to current me * first pass at notifications and payment status reporting * fix migration to have withdrawal hash * reverse optimism on payment failure * Revert "Optimistic updates via pending sats in item context (#1229)" This reverts commit 93713b33df9bc3701dc5a692b86a04ff64e8cfb1. * add onCompleted to usePaidMutation * onPaid and onPayError for new comments * use 'IS DISTINCT FROM' for NULL invoiceActionState columns * make usePaidMutation easier to read * enhance invoice qr * prevent actions on unpaid items * allow navigation to action's invoice * retry create item * start edit window after item is paid for * fix ux of retries from notifications * refine retries * fix optimistic downzaps * remember item updates can't be retried * store reference to action item in invoice * remove invoice modal layout shift * fix destructuring * fix zap undos * make sure ItemAct is paid in aggregate queries * dont toast on long press zap undo * fix delete and remindme bots * optimistic poll votes with retries * fix retry notifications and invoice item context * fix pessimisitic typo * item mentions and mention notifications * dont show payment retry on item popover * make bios work * refactor paidAction transitions * remove stray console.log * restore docker compose nwc settings * add new todos * persist qr modal on post submission + unify item form submission * fix post edit threshold * make bounty payments work * make job posting work * remove more store procedure usage ... document serialization concerns * dont use dynamic imports for paid action modules * inline comment denormalization * create item starts with median votes * fix potential of serialization anomalies in zaps * dont trigger notification indicator on successful paid action invoices * ignore invoiceId on territory actions and add optimistic concurrency control * begin docs for paid actions * better error toasts and fix apollo cache warnings * small documentation enhancements * improve paid action docs * optimistic concurrency control for territory updates * use satsToMsats and msatsToSats helpers * explictly type raw query template parameters * improve consistency of nested relation names * complete paid action docs * useEffect for canEdit on payment * make sure invoiceId is provided when required * don't return null when expecting array * remove buy credits * move verifyPayment to paidAction * fix comments invoicePaidAt time zone * close nwc connections once * grouped logs for paid actions * stop invoiceWaitUntilPaid if not attempting to pay * allow actionState to transition directly from HELD to PAID * make paid mutation wait until pessimistic are fully paid * change button text when form submits/pays * pulsing form submit button * ignore me in notification indicator for territory subscription * filter unpaid items from more queries * fix donation stike timing * fix pending poll vote * fix recent item notifcation padding * no default form submitting button text * don't show paying on submit button on free edits * fix territory autorenew with fee credits * reorg readme * allow jobs to be editted forever * fix image uploads * more filter fixes for aggregate views * finalize paid action invoice expirations * remove unnecessary async * keep clientside cache normal/consistent * add more detail to paid action doc * improve paid action table * remove actionType guard * fix top territories * typo api/paidAction/README.md Co-authored-by: ekzyis <ek@stacker.news> * typo components/use-paid-mutation.js Co-authored-by: ekzyis <ek@stacker.news> * Apply suggestions from code review Co-authored-by: ekzyis <ek@stacker.news> * encorporate ek feeback * more ek suggestions * fix 'cost to post' hover on items * Apply suggestions from code review Co-authored-by: ekzyis <ek@stacker.news> --------- Co-authored-by: ekzyis <ek@stacker.news>
2024-07-01 17:02:29 +00:00
if (error) throw error
2023-11-13 02:31:39 +00:00
handleDone?.()
}}
>
<MarkdownInput
topLevel
name='bio'
minRows={6}
/>
2023-11-19 20:16:35 +00:00
<ItemButtonBar createText='save' onCancel={handleDone} />
2023-11-13 02:31:39 +00:00
</Form>
</FeeButtonProvider>
2021-09-23 17:42:00 +00:00
</div>
)
}
2024-02-23 15:32:20 +00:00
export function UserLayout ({ user, children, containClassName }) {
return (
<Layout user={user} footer footerLinks={false} containClassName={containClassName}>
<UserHeader user={user} />
{children}
</Layout>
)
}
export default function User ({ ssrData }) {
2021-09-23 17:42:00 +00:00
const [create, setCreate] = useState(false)
2021-09-24 21:28:21 +00:00
const [edit, setEdit] = useState(false)
const router = useRouter()
Account Switching (#644) * WIP: Account switching * Fix empty USER query ANON_USER_ID was undefined and thus the query for @anon had no variables. * Apply multiAuthMiddleware in /api/graphql * Fix 'you must be logged in' query error on switch to anon * Add smart 'switch account' button "smart" means that it only shows if there are accounts to which one can switch * Fix multiAuth not set in backend * Comment fixes, minor changes * Use fw-bold instead of 'selected' * Close dropdown and offcanvas Inside a dropdown, we can rely on autoClose but need to wrap the buttons with <Dropdown.Item> for that to work. For the offcanvas, we need to pass down handleClose. * Use button to add account * Some pages require hard reload on account switch * Reinit settings form on account switch * Also don't refetch WalletHistory * Formatting * Use width: fit-content for standalone SignUpButton * Remove unused className * Use fw-bold and text-underline on selected * Fix inconsistent padding of login buttons * Fix duplicate redirect from /settings on anon switch * Never throw during refetch * Throw errors which extend GraphQLError * Only use meAnonSats if logged out * Use reactive variable for meAnonSats The previous commit broke the UI update after anon zaps because we actually updated item.meSats in the cache and not item.meAnonSats. Updating item.meAnonSats was not possible because it's a local field. For that, one needs to use reactive variables. We do this now and thus also don't need the useEffect hack in item-info.js anymore. * Switch to new user * Fix missing cleanup during logout If we logged in but never switched to any other account, the 'multi_auth.user-id' cookie was not set. This meant that during logout, the other 'multi_auth.*' cookies were not deleted. This broke the account switch modal. This is fixed by setting the 'multi_auth.user-id' cookie on login. Additionally, we now cleanup if cookie pointer OR session is set (instead of only if both are set). * Fix comments in middleware * Remove unnecessary effect dependencies setState is stable and thus only noise in effect dependencies * Show but disable unavailable auth methods * make signup button consistent with others * Always reload page on switch * refine account switch styling * logout barrier --------- Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com> Co-authored-by: k00b <k00b@stacker.news>
2024-09-12 18:05:11 +00:00
const { me } = useMe()
2021-09-30 15:46:58 +00:00
2023-07-26 00:45:35 +00:00
const { data } = useQuery(USER_FULL, { variables: { ...router.query } })
if (!data && !ssrData) return <PageLoading />
2021-09-24 21:28:21 +00:00
const { user } = data || ssrData
2021-09-24 21:28:21 +00:00
const mine = me?.name === user.name
2021-04-22 22:14:32 +00:00
return (
2024-02-23 15:32:20 +00:00
<UserLayout user={user} containClassName={!user.bio && mine && styles.contain}>
2021-09-23 17:42:00 +00:00
{user.bio
2021-09-24 21:28:21 +00:00
? (edit
? (
<div className={styles.create}>
<BioForm bio={user.bio} handleDone={() => setEdit(false)} />
2021-09-24 21:28:21 +00:00
</div>)
: <ItemFull item={user.bio} bio handleClick={setEdit} />
)
: (mine &&
2021-09-23 17:42:00 +00:00
<div className={styles.create}>
{create
? <BioForm handleDone={() => setCreate(false)} />
2021-09-23 17:42:00 +00:00
: (
2021-09-24 21:28:21 +00:00
mine &&
2021-11-23 21:23:25 +00:00
<div className='text-center'>
<Button
onClick={setCreate}
size='md' variant='secondary'
>create bio
</Button>
2023-07-09 17:37:12 +00:00
<small className='d-block mt-3 text-muted'>your bio is also a post introducing yourself to other stackers</small>
2021-11-23 21:23:25 +00:00
</div>
2021-09-23 17:42:00 +00:00
)}
</div>)}
</UserLayout>
2021-04-22 22:14:32 +00:00
)
2021-04-14 23:56:29 +00:00
}