Compare commits

..

2 Commits

Author SHA1 Message Date
NostrDev
3a94fbc0ae chore(types): used KeyboardCode enum
All checks were successful
Open PR on Staging / audit_and_check (pull_request) Successful in 31s
2024-11-01 11:23:05 +03:00
NostrDev
e37f90d6db feat(pdf-fields): add logic to hide signers on ESC 2024-11-01 11:22:31 +03:00
5 changed files with 106 additions and 59 deletions

View File

@ -9,7 +9,7 @@ import {
} from '@mui/material' } from '@mui/material'
import styles from './style.module.scss' import styles from './style.module.scss'
import React, { useEffect, useState } from 'react' import React, { useEffect, useState } from 'react'
import { ProfileMetadata, User, UserRole } from '../../types' import { ProfileMetadata, User, UserRole, KeyboardCode } from '../../types'
import { MouseState, PdfPage, DrawnField, DrawTool } from '../../types/drawing' import { MouseState, PdfPage, DrawnField, DrawTool } from '../../types/drawing'
import { hexToNpub, npubToHex, getProfileUsername } from '../../utils' import { hexToNpub, npubToHex, getProfileUsername } from '../../utils'
import { SigitFile } from '../../utils/file' import { SigitFile } from '../../utils/file'
@ -41,6 +41,10 @@ export const DrawPDFFields = (props: Props) => {
const signers = users.filter((u) => u.role === UserRole.signer) const signers = users.filter((u) => u.role === UserRole.signer)
const defaultSignerNpub = signers.length ? hexToNpub(signers[0].pubkey) : '' const defaultSignerNpub = signers.length ? hexToNpub(signers[0].pubkey) : ''
const [lastSigner, setLastSigner] = useState(defaultSignerNpub) const [lastSigner, setLastSigner] = useState(defaultSignerNpub)
const [hideSignersForDrawnField, setHideSignersForDrawnField] = useState<{
[key: number]: boolean
} | null>()
/** /**
* Return first pubkey that is present in the signers list * Return first pubkey that is present in the signers list
* @param pubkeys * @param pubkeys
@ -361,6 +365,7 @@ export const DrawPDFFields = (props: Props) => {
rect rect
} }
} }
/** /**
* Renders the pdf pages and drawing elements * Renders the pdf pages and drawing elements
*/ */
@ -492,62 +497,90 @@ export const DrawPDFFields = (props: Props) => {
fileIndex, fileIndex,
pageIndex, pageIndex,
drawnFieldIndex drawnFieldIndex
) && ( ) &&
<div (!hideSignersForDrawnField ||
onPointerDown={handleUserSelectPointerDown} !hideSignersForDrawnField[drawnFieldIndex]) && (
className={styles.userSelect} <div
> onPointerDown={handleUserSelectPointerDown}
<FormControl fullWidth size="small"> className={styles.userSelect}
<InputLabel id="counterparts">Counterpart</InputLabel> >
<Select <FormControl fullWidth size="small">
value={getAvailableSigner(drawnField.counterpart)} <InputLabel id="counterparts">
onChange={(event) => { Counterpart
drawnField.counterpart = event.target.value </InputLabel>
setLastSigner(event.target.value) <Select
refreshPdfFiles() value={getAvailableSigner(drawnField.counterpart)}
}} onChange={(event) => {
labelId="counterparts" drawnField.counterpart = event.target.value
label="Counterparts" setLastSigner(event.target.value)
sx={{ refreshPdfFiles()
background: 'white' }}
}} labelId="counterparts"
renderValue={(value) => label="Counterparts"
renderCounterpartValue(value) sx={{
} background: 'white'
> }}
{signers.map((signer, index) => { renderValue={(value) =>
const npub = hexToNpub(signer.pubkey) renderCounterpartValue(value)
const metadata = props.metadata[signer.pubkey] }
const displayValue = getProfileUsername( onClose={(event: React.SyntheticEvent) => {
npub, // get native event
metadata const { nativeEvent } = event
)
return ( // check if event is a keyboard event
<MenuItem key={index} value={npub}> if (nativeEvent instanceof KeyboardEvent) {
<ListItemIcon> // check if event code is Escape
<AvatarIconButton if (
src={metadata?.picture} nativeEvent.code === KeyboardCode.Escape
hexKey={signer.pubkey} ) {
aria-label={`account of user ${displayValue}`} // set hide signers for this DrawnField
color="inherit" if (hideSignersForDrawnField) {
sx={{ setHideSignersForDrawnField((prev) => ({
padding: 0, ...prev,
'> img': { [drawnFieldIndex]: true
width: '30px', }))
height: '30px' } else {
} setHideSignersForDrawnField({
}} [drawnFieldIndex]: true
/> })
</ListItemIcon> }
<ListItemText>{displayValue}</ListItemText> }
</MenuItem> }
) }}
})} >
</Select> {signers.map((signer, index) => {
</FormControl> const npub = hexToNpub(signer.pubkey)
</div> const metadata = props.metadata[signer.pubkey]
)} const displayValue = getProfileUsername(
npub,
metadata
)
return (
<MenuItem key={index} value={npub}>
<ListItemIcon>
<AvatarIconButton
src={metadata?.picture}
hexKey={signer.pubkey}
aria-label={`account of user ${displayValue}`}
color="inherit"
sx={{
padding: 0,
'> img': {
width: '30px',
height: '30px'
}
}}
/>
</ListItemIcon>
<ListItemText>{displayValue}</ListItemText>
</MenuItem>
)
})}
</Select>
</FormControl>
</div>
)}
</div> </div>
) )
})} })}

View File

@ -21,7 +21,8 @@ import {
ProfileMetadata, ProfileMetadata,
SignedEvent, SignedEvent,
User, User,
UserRole UserRole,
KeyboardCode
} from '../../types' } from '../../types'
import { import {
encryptArrayBuffer, encryptArrayBuffer,
@ -87,7 +88,10 @@ export const CreatePage = () => {
const [userInput, setUserInput] = useState('') const [userInput, setUserInput] = useState('')
const handleInputKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => { const handleInputKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
if (event.code === 'Enter' || event.code === 'NumpadEnter') { if (
event.code === KeyboardCode.Enter ||
event.code === KeyboardCode.NumpadEnter
) {
event.preventDefault() event.preventDefault()
handleAddUser() handleAddUser()
} }

View File

@ -9,6 +9,7 @@ import { toast } from 'react-toastify'
import { LoadingSpinner } from '../../components/LoadingSpinner' import { LoadingSpinner } from '../../components/LoadingSpinner'
import { AuthController } from '../../controllers' import { AuthController } from '../../controllers'
import { updateKeyPair, updateLoginMethod } from '../../store/actions' import { updateKeyPair, updateLoginMethod } from '../../store/actions'
import { KeyboardCode } from '../../types'
import { LoginMethod } from '../../store/auth/types' import { LoginMethod } from '../../store/auth/types'
import { hexToBytes } from '@noble/hashes/utils' import { hexToBytes } from '@noble/hashes/utils'
@ -52,7 +53,10 @@ export const Nostr = () => {
* Call login function when enter is pressed * Call login function when enter is pressed
*/ */
const handleInputKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => { const handleInputKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
if (event.code === 'Enter' || event.code === 'NumpadEnter') { if (
event.code === KeyboardCode.Enter ||
event.code === KeyboardCode.NumpadEnter
) {
event.preventDefault() event.preventDefault()
login() login()
} }

5
src/types/event.ts Normal file
View File

@ -0,0 +1,5 @@
export enum KeyboardCode {
Escape = 'Escape',
Enter = 'Enter',
NumpadEnter = 'NumpadEnter'
}

View File

@ -4,3 +4,4 @@ export * from './nostr'
export * from './profile' export * from './profile'
export * from './relay' export * from './relay'
export * from './zip' export * from './zip'
export * from './event'