-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
react: add useValidateHandle hook (#826)
- Loading branch information
Showing
25 changed files
with
204 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
"@lens-protocol/blockchain-bindings": minor | ||
"@lens-protocol/api-bindings": minor | ||
"@lens-protocol/client": minor | ||
"@lens-protocol/react": minor | ||
"@lens-protocol/react-web": minor | ||
"@lens-protocol/react-native": minor | ||
--- | ||
|
||
**chore:** unified implementation and naming of `isValidHandle` helper among react and client SDKs. deprecated `isValidProfileHandle` in the client sdk. | ||
**feat:** added `useValidateHandle` hook |
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 |
---|---|---|
|
@@ -17,7 +17,7 @@ jobs: | |
uses: pnpm/[email protected] | ||
|
||
- name: Use Node.js | ||
uses: actions/setup-node@v3 | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version-file: ".nvmrc" | ||
cache: "pnpm" | ||
|
@@ -38,7 +38,7 @@ jobs: | |
uses: pnpm/[email protected] | ||
|
||
- name: Use Node.js for docs | ||
uses: actions/setup-node@v3 | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version-file: ".nvmrc" | ||
cache: "pnpm" | ||
|
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
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,45 @@ | ||
import { useValidateHandle } from '@lens-protocol/react-web'; | ||
import { toast } from 'react-hot-toast'; | ||
|
||
export function UseValidateHandle() { | ||
const { execute, loading } = useValidateHandle(); | ||
|
||
const validate = async (event: React.FormEvent<HTMLFormElement>) => { | ||
event.preventDefault(); | ||
const form = event.currentTarget; | ||
const formData = new FormData(form); | ||
|
||
const localName = formData.get('localName') as string; | ||
|
||
const result = await execute({ handle: localName }); | ||
|
||
if (result.isFailure()) { | ||
toast.error(result.error.message); | ||
return; | ||
} | ||
|
||
toast.success(`You can create a new profile with handle: ${localName}`); | ||
return; | ||
}; | ||
|
||
return ( | ||
<div> | ||
<h1> | ||
<code>useValidateHandle</code> | ||
</h1> | ||
|
||
<form onSubmit={validate}> | ||
<fieldset> | ||
<legend>New handle</legend> | ||
<label> | ||
Local name: | ||
<input type="text" name="localName" placeholder="wagmi" disabled={loading} /> | ||
</label> | ||
<div style={{ display: 'flex', flexDirection: 'row', justifyContent: 'flex-end' }}> | ||
<button disabled={loading}>Validate</button> | ||
</div> | ||
</fieldset> | ||
</form> | ||
</div> | ||
); | ||
} |
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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from './contracts'; | ||
export * from './data'; | ||
export * from './types'; | ||
export * from './profile'; | ||
export * from './TypedData'; | ||
export * from './types'; | ||
export * from './utils'; |
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,11 @@ | ||
/** | ||
* Checks if suggested profile handle name is valid. | ||
* | ||
* @param handle - profile handle to check | ||
* @returns true if the handle is valid | ||
*/ | ||
export function isValidHandle(handle: string): boolean { | ||
const validationRegex = /^[a-z](?:[a-z0-9_]{4,25})$/; | ||
|
||
return validationRegex.test(handle); | ||
} |
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 |
---|---|---|
@@ -1,11 +1,7 @@ | ||
/** | ||
* Checks if a profile handle is valid. | ||
* | ||
* @param handle - profile handle to check | ||
* @returns true if the handle is valid | ||
*/ | ||
export function isValidProfileHandle(handle: string): boolean { | ||
const validationRegex = /^[a-z](?:[a-z0-9_]{4,25})$/; | ||
|
||
return validationRegex.test(handle); | ||
} | ||
export { | ||
isValidHandle, | ||
/** | ||
* @deprecated Use `isValidHandle` instead | ||
*/ | ||
isValidHandle as isValidProfileHandle, | ||
} from '@lens-protocol/blockchain-bindings'; |
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,102 @@ | ||
import { UnspecifiedError, useHandleToAddressLazyQuery } from '@lens-protocol/api-bindings'; | ||
import { isValidHandle } from '@lens-protocol/blockchain-bindings'; | ||
import { PromiseResult, failure, invariant, success } from '@lens-protocol/shared-kernel'; | ||
|
||
import { useLensApolloClient } from '../helpers/arguments'; | ||
import { UseDeferredTask, useDeferredTask } from '../helpers/tasks'; | ||
import { useSharedDependencies } from '../shared'; | ||
|
||
export class HandleNotAvailableError extends Error { | ||
name = 'HandleNotAvailableError' as const; | ||
|
||
constructor(handle: string) { | ||
super(`Handle "${handle}" is already taken`); | ||
} | ||
} | ||
|
||
export class InvalidHandleError extends Error { | ||
name = 'InvalidHandleError' as const; | ||
|
||
constructor(handle: string) { | ||
super(`Handle "${handle}" is not valid`); | ||
} | ||
} | ||
|
||
export type ValidateHandleRequest = { | ||
/** Just the localname portion of a new handle */ | ||
handle: string; | ||
}; | ||
|
||
/** | ||
* Validate the proposed new handle, its format and availability. | ||
* | ||
* This hook will not execute until the returned function is called. | ||
* | ||
* @example | ||
* ```ts | ||
* const { called, error, loading, execute } = useValidateHandle(); | ||
* ``` | ||
* | ||
* Simple example: | ||
* ```ts | ||
* const { called, error, loading, execute } = useValidateHandle(); | ||
* | ||
* const callback = async () => { | ||
* const result = await execute({ handle: 'wagmi' }); | ||
* | ||
* if (result.isFailure()) { | ||
* console.error(result.error.message); // handle not valid or already taken | ||
* return; | ||
* } | ||
* | ||
* if (result.value === true) { | ||
* // success - handle is available | ||
* } | ||
* } | ||
* ``` | ||
* | ||
* @experimental This hook is experimental and may change in the future. | ||
* @category Misc | ||
* @group Hooks | ||
*/ | ||
export function useValidateHandle(): UseDeferredTask< | ||
void, | ||
UnspecifiedError | HandleNotAvailableError | InvalidHandleError, | ||
ValidateHandleRequest | ||
> { | ||
const [fetch] = useHandleToAddressLazyQuery(useLensApolloClient()); | ||
|
||
const { | ||
config: { environment }, | ||
} = useSharedDependencies(); | ||
|
||
return useDeferredTask( | ||
async ( | ||
request, | ||
): PromiseResult<void, UnspecifiedError | HandleNotAvailableError | InvalidHandleError> => { | ||
if (!isValidHandle(request.handle)) { | ||
return failure(new InvalidHandleError(request.handle)); | ||
} | ||
|
||
const { data, error } = await fetch({ | ||
variables: { | ||
request: { | ||
handle: environment.handleResolver(request.handle), | ||
}, | ||
}, | ||
}); | ||
|
||
if (error) { | ||
return failure(new UnspecifiedError(error)); | ||
} | ||
|
||
invariant(data, 'Data must be defined'); | ||
|
||
if (data.result) { | ||
return failure(new HandleNotAvailableError(request.handle)); | ||
} | ||
|
||
return success(); | ||
}, | ||
); | ||
} |
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