-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:ChainSafe/Multix
- Loading branch information
Showing
17 changed files
with
364 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
import { Grid } from '@mui/material' | ||
import { styled } from '@mui/material/styles' | ||
import { SubmittableExtrinsic } from '@polkadot/api/types' | ||
import { ISubmittableResult } from '@polkadot/types/types' | ||
import { useCallback, useEffect, useMemo, useState } from 'react' | ||
import { useApi } from '../../contexts/ApiContext' | ||
import { TextField } from '../library' | ||
import { useIdentity } from '../../hooks/useIdentity' | ||
|
||
interface Props { | ||
className?: string | ||
from: string | ||
onSetExtrinsic: (ext?: SubmittableExtrinsic<'promise', ISubmittableResult>) => void | ||
onSetErrorMessage: React.Dispatch<React.SetStateAction<string>> | ||
} | ||
|
||
type IdentityFields = { | ||
display: string | undefined | ||
legal: string | undefined | ||
web: string | undefined | ||
riot: string | undefined | ||
email: string | undefined | ||
twitter: string | undefined | ||
} | ||
|
||
const getRawOrNone = (val: string | undefined) => { | ||
return val | ||
? { | ||
Raw: val | ||
} | ||
: { none: null } | ||
} | ||
const getExtrinsicsArgs = ({ legal, display, email, riot, twitter, web }: IdentityFields) => { | ||
return { | ||
additional: [], | ||
display: getRawOrNone(display), | ||
legal: getRawOrNone(legal), | ||
web: getRawOrNone(web), | ||
riot: getRawOrNone(riot), | ||
email: getRawOrNone(email), | ||
pgpFingerprint: null, | ||
twitter: getRawOrNone(twitter) | ||
} | ||
} | ||
|
||
const fieldNameAndPlaceholder = (fieldName: keyof IdentityFields) => { | ||
switch (fieldName) { | ||
case 'display': | ||
return { | ||
field: 'Display name', | ||
placeholder: 'Luke', | ||
required: true | ||
} | ||
|
||
case 'legal': | ||
return { | ||
field: 'Legal name', | ||
placeholder: 'Luke Skylwalker', | ||
required: false | ||
} | ||
|
||
case 'riot': | ||
return { | ||
field: 'Element handle', | ||
placeholder: '@luke:matrix.org', | ||
required: false | ||
} | ||
|
||
case 'web': | ||
return { | ||
field: 'Website', | ||
placeholder: 'https://luke.sky', | ||
required: false | ||
} | ||
|
||
case 'twitter': | ||
return { | ||
field: 'Twitter/X handle', | ||
placeholder: '@luke', | ||
required: false | ||
} | ||
|
||
case 'email': | ||
return { | ||
field: 'Email', | ||
placeholder: '[email protected]', | ||
required: false | ||
} | ||
|
||
default: | ||
return { | ||
field: fieldName, | ||
placeholder: '', | ||
required: false | ||
} | ||
} | ||
} | ||
|
||
const MAX_ALLOWED_VAL_LENGTH = 32 | ||
|
||
const SetIdentity = ({ className, onSetExtrinsic, from, onSetErrorMessage }: Props) => { | ||
const { api, chainInfo } = useApi() | ||
const [identityFields, setIdentityFields] = useState<IdentityFields | undefined>() | ||
const chainIdentity = useIdentity(from) | ||
const [hasChangedAtLeastAField, setHasChangedAtLeastAField] = useState(false) | ||
const fieldtooLongError = useMemo(() => { | ||
const res: (keyof IdentityFields)[] = [] | ||
identityFields && | ||
Object.entries(identityFields).forEach(([field, value]) => { | ||
if (typeof value === 'string' && value.length >= MAX_ALLOWED_VAL_LENGTH) { | ||
res.push(field as keyof IdentityFields) | ||
} | ||
}) | ||
|
||
return res | ||
}, [identityFields]) | ||
|
||
useEffect(() => { | ||
if (fieldtooLongError.length > 0) { | ||
onSetErrorMessage(`A field exceeds the ${MAX_ALLOWED_VAL_LENGTH} character limit`) | ||
return | ||
} | ||
|
||
if (!identityFields?.display && hasChangedAtLeastAField) { | ||
onSetErrorMessage('Display name is required') | ||
return | ||
} | ||
|
||
onSetErrorMessage('') | ||
}, [fieldtooLongError, hasChangedAtLeastAField, identityFields, onSetErrorMessage]) | ||
|
||
useEffect(() => { | ||
if (chainIdentity) { | ||
const { display, email, legal, web, riot, twitter } = chainIdentity | ||
setIdentityFields({ | ||
display, | ||
legal, | ||
web, | ||
riot, | ||
email, | ||
}) | ||
} else { | ||
setIdentityFields({ | ||
display: undefined, | ||
legal: undefined, | ||
web: undefined, | ||
riot: undefined, | ||
email: undefined, | ||
twitter: undefined | ||
}) | ||
} | ||
}, [chainIdentity]) | ||
|
||
useEffect(() => { | ||
if (!api) { | ||
onSetExtrinsic(undefined) | ||
return | ||
} | ||
|
||
if (!identityFields) { | ||
onSetExtrinsic(undefined) | ||
return | ||
} | ||
|
||
if (fieldtooLongError.length > 0) { | ||
onSetExtrinsic(undefined) | ||
return | ||
} | ||
|
||
const extrinsicsArgs = getExtrinsicsArgs(identityFields) | ||
onSetExtrinsic(api.tx.identity.setIdentity(extrinsicsArgs)) | ||
}, [api, chainInfo, fieldtooLongError, identityFields, onSetErrorMessage, onSetExtrinsic]) | ||
|
||
const onChangeField = useCallback((field: keyof IdentityFields, value: string) => { | ||
setHasChangedAtLeastAField(true) | ||
setIdentityFields((prev) => (prev ? { ...prev, [field]: value } : undefined)) | ||
}, []) | ||
|
||
return ( | ||
<Grid | ||
className={className} | ||
container | ||
spacing={1} | ||
> | ||
{identityFields && | ||
Object.entries(identityFields).map(([fieldName, value]) => { | ||
const { field, placeholder, required } = fieldNameAndPlaceholder( | ||
fieldName as keyof IdentityFields | ||
) | ||
const isFieldError = fieldtooLongError.includes(fieldName as keyof IdentityFields) | ||
const isDiplayNameError = fieldName === 'display' && !value && hasChangedAtLeastAField | ||
return ( | ||
<Grid | ||
item | ||
xs={12} | ||
sm={6} | ||
md={6} | ||
alignItems="center" | ||
> | ||
<TextFieldStyled | ||
data-cy={`${fieldName}-identity`} | ||
label={field} | ||
placeholder={placeholder} | ||
onChange={(val) => | ||
onChangeField(fieldName as keyof IdentityFields, val.target.value) | ||
} | ||
value={value || ''} | ||
required={required} | ||
helperText={isFieldError && `Field has more than ${MAX_ALLOWED_VAL_LENGTH} chars`} | ||
error={isFieldError || isDiplayNameError} | ||
/> | ||
</Grid> | ||
) | ||
})} | ||
</Grid> | ||
) | ||
} | ||
|
||
const TextFieldStyled = styled(TextField)` | ||
.MuiFormHelperText-root.Mui-error { | ||
position: initial; | ||
} | ||
` | ||
|
||
export default styled(SetIdentity)` | ||
margin-top: 0.5rem; | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.