stacker.news/components/item-act.js

85 lines
2.2 KiB
JavaScript
Raw Normal View History

import { Button, InputGroup } from 'react-bootstrap'
import React, { useState, useRef, useEffect } from 'react'
import { Form, Input, SubmitButton } from './form'
2021-09-12 16:55:38 +00:00
import { useMe } from './me'
2022-12-09 20:13:31 +00:00
import UpBolt from '../svgs/bolt.svg'
2023-02-08 19:38:04 +00:00
import { amountSchema } from '../lib/validate'
2021-09-10 18:55:36 +00:00
2023-05-14 23:33:49 +00:00
const defaultTips = [100, 1000, 10000, 100000]
2023-05-14 23:11:31 +00:00
const Tips = ({ setOValue }) => {
const tips = [...getCustomTips(), ...defaultTips].sort((a, b) => a - b)
return tips.map(num =>
2023-05-14 23:11:31 +00:00
<Button
size='sm'
className={`${num > 1 ? 'ml-2' : ''} mb-2`}
key={num}
onClick={() => { setOValue(num) }}
>
<UpBolt
className='mr-1'
width={14}
height={14}
/>{num}
</Button>)
}
const getCustomTips = () => JSON.parse(localStorage.getItem('custom-tips')) || []
const addCustomTip = (amount) => {
2023-05-14 23:33:49 +00:00
if (defaultTips.includes(amount)) return
2023-05-14 23:11:31 +00:00
let customTips = Array.from(new Set([amount, ...getCustomTips()]))
if (customTips.length > 3) {
customTips = customTips.slice(0, 3)
}
localStorage.setItem('custom-tips', JSON.stringify(customTips))
}
export default function ItemAct ({ onClose, itemId, act, strike }) {
2021-09-10 18:55:36 +00:00
const inputRef = useRef(null)
2021-09-12 16:55:38 +00:00
const me = useMe()
2022-12-09 20:13:31 +00:00
const [oValue, setOValue] = useState()
2021-09-10 18:55:36 +00:00
useEffect(() => {
inputRef.current?.focus()
}, [onClose, itemId])
2021-09-10 18:55:36 +00:00
return (
<Form
initial={{
amount: me?.tipDefault,
default: false
}}
2023-02-08 19:38:04 +00:00
schema={amountSchema}
onSubmit={async ({ amount }) => {
await act({
variables: {
id: itemId,
sats: Number(amount)
}
})
await strike()
2023-05-14 23:11:31 +00:00
addCustomTip(Number(amount))
onClose()
2021-09-10 18:55:36 +00:00
}}
>
<Input
label='amount'
name='amount'
2023-05-14 23:24:45 +00:00
type='number'
innerRef={inputRef}
overrideValue={oValue}
required
autoFocus
append={<InputGroup.Text className='text-monospace'>sats</InputGroup.Text>}
/>
<div>
2023-05-14 23:11:31 +00:00
<Tips setOValue={setOValue} />
</div>
<div className='d-flex'>
2023-06-19 18:21:55 +00:00
<SubmitButton variant='success' className='ml-auto mt-1 px-4' value='TIP'>zap</SubmitButton>
</div>
</Form>
2021-09-10 18:55:36 +00:00
)
}