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

120 lines
3.6 KiB
JavaScript
Raw Permalink Normal View History

import Layout from '@/components/layout'
2024-10-02 23:14:25 +00:00
import { 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 { 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'
2024-10-02 23:14:25 +00:00
import useItemSubmit from '@/components/use-item-submit'
import { UPSERT_BIO } from '@/fragments/paidAction'
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
2024-10-02 23:06:22 +00:00
export function BioForm ({ handleDone, bio, me }) {
2024-10-02 23:14:25 +00:00
const onSubmit = useItemSubmit(UPSERT_BIO, {
navigateOnSubmit: false,
paidMutationOptions: {
update (cache, { data: { upsertBio: { result, invoice } } }) {
if (!result) return
2021-09-23 17:42:00 +00:00
cache.modify({
2024-10-02 23:14:25 +00:00
id: `User:${me.id}`,
2021-09-23 17:42:00 +00:00
fields: {
bio () {
2024-10-02 23:14:25 +00:00
return result.text
2021-09-23 17:42:00 +00:00
}
}
})
}
2024-10-02 23:14:25 +00:00
},
onSuccessfulSubmit: (data, { resetForm }) => {
handleDone?.()
2021-09-23 17:42:00 +00:00
}
2024-10-02 23:14:25 +00:00
})
2021-09-23 17:42:00 +00:00
return (
<div className={styles.createFormContainer}>
2023-11-13 02:31:39 +00:00
<FeeButtonProvider>
<Form
initial={{
2024-10-02 23:14:25 +00:00
text: bio?.text || ''
2023-11-13 02:31:39 +00:00
}}
schema={bioSchema}
2024-10-02 23:14:25 +00:00
onSubmit={onSubmit}
2024-10-02 23:06:22 +00:00
storageKeyPrefix={`bio-${me.id}`}
2023-11-13 02:31:39 +00:00
>
<MarkdownInput
topLevel
2024-10-02 23:14:25 +00:00
name='text'
2023-11-13 02:31:39 +00:00
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}>
2024-10-02 23:06:22 +00:00
<BioForm bio={user.bio} me={me} 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
2024-10-02 23:06:22 +00:00
? <BioForm me={me} 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
}