diff --git a/packages/page-accounts/src/Vanity/Match.tsx b/packages/page-accounts/src/Vanity/Match.tsx index e35ed9efe4..8662663890 100644 --- a/packages/page-accounts/src/Vanity/Match.tsx +++ b/packages/page-accounts/src/Vanity/Match.tsx @@ -33,37 +33,40 @@ function Match ({ address, className, count, offset, onCreateToggle, onRemove, s ); return ( -
-
+ + -
-
- {address.slice(0, offset)}{address.slice(offset, count + offset)}{address.slice(count + offset)} -
-
- {hexSeed} -
+ + +
+ {address.slice(0, offset)}{address.slice(offset, count + offset)}{address.slice(count + offset)}
-
-
-
-
+ + + {hexSeed} + + +
- ); - } + if (checks.length === 0 || !mountedRef.current) { + return; + } - private checkMatches (): void { - const results = this.results; - - this.results = []; - - if (results.length === 0 || !this._isActive) { - return; - } - - this.setState( - ({ keyCount, keyTime, matches, startAt }: State): Pick => { - let newKeyCount = keyCount; - let newKeyTime = keyTime; - const newMatches = results - .reduce((result, { elapsed, found }): GeneratorMatch[] => { + setResults( + ({ isRunning, keyCount, keyTime, matches, startAt }: Results): Results => { + let newKeyCount = keyCount; + let newKeyTime = keyTime; + const newMatches = checks.reduce((result, { elapsed, found }): GeneratorMatch[] => { newKeyCount += found.length; newKeyTime += elapsed; return result.concat(found); - }, matches) - .sort(generatorSort) - .slice(0, 25); - const elapsed = Date.now() - startAt; + }, matches); - return { - elapsed, - keyCount: newKeyCount, - keyTime: newKeyTime, - matches: newMatches - }; - } - ); - } - - private executeGeneration = (): void => { - if (!this.state.isRunning) { - this.checkMatches(); - - return; - } - - setImmediate((): void => { - if (this._isActive) { - if (this.results.length === 25) { - this.checkMatches(); + return { + elapsed: Date.now() - startAt, + isRunning, + keyCount: newKeyCount, + keyTime: newKeyTime, + matches: newMatches.sort(generatorSort).slice(0, 25), + startAt + }; } + ); + }, + [mountedRef] + ); - const { match, type, withCase, withHex } = this.state; - - this.results.push( - generator({ - match, - runs: 10, - type, - withCase, - withHex - }) - ); - - this.executeGeneration(); + const _executeGeneration = useCallback( + (): void => { + if (!runningRef.current) { + return _checkMatches(); } - }); - } - private onCreateToggle = (createSeed: string): void => { - this.setState({ createSeed }); - } + setImmediate((): void => { + if (mountedRef.current) { + if (results.current.length === 25) { + _checkMatches(); + } - private onChangeCase = (withCase: boolean): void => { - this.setState({ withCase }); - } + results.current.push( + generator({ match, runs: 10, type, withCase, withHex: true }) + ); - private onChangeMatch = (match: string): void => { - this.setState({ + _executeGeneration(); + } + }); + }, + [_checkMatches, match, mountedRef, runningRef, type, withCase] + ); + + const _onChangeMatch = useCallback( + (match: string): void => setMatch({ isMatchValid: matchRegex.test(match) && (match.length !== 0) && (match.length < 31), match - }); - } + }), + [] + ); - private onChangeType = (type: KeypairType): void => { - this.setState({ type }); - } - - private onRemove = (address: string): void => { - this.setState( - ({ matches }: State): Pick => ({ - matches: matches.filter((item): boolean => - item.address !== address - ) + const _onRemove = useCallback( + (address: string): void => setResults( + (results: Results): Results => ({ + ...results, + matches: results.matches.filter((item) => item.address !== address) }) - ); - } + ), + [] + ); - private toggleStart = (): void => { - this.setState( - ({ isRunning, keyCount, keyTime, startAt }: State): Pick => { - this._isActive = !isRunning; + const _toggleStart = useCallback( + (): void => setResults( + ({ elapsed, isRunning, keyCount, keyTime, matches, startAt }: Results): Results => ({ + elapsed, + isRunning: !isRunning, + keyCount: isRunning ? keyCount : 0, + keyTime: isRunning ? keyTime : 0, + matches, + startAt: isRunning ? startAt : Date.now() + }) + ), + [] + ); - return { - isRunning: this._isActive, - keyCount: this._isActive ? 0 : keyCount, - keyTime: this._isActive ? 0 : keyTime, - startAt: this._isActive ? Date.now() : startAt - }; - }, - this.executeGeneration - ); - } + useEffect((): void => { + runningRef.current = isRunning; - private closeCreate = (): void => { - this.setState({ createSeed: null }); - } + if (isRunning) { + _executeGeneration(); + } + }, [_executeGeneration, isRunning]); + + return ( +
+
+ + +
+
+ +
+ +
+ ); } -export default translate( - styled(VanityApp)` - .vanity--App-matches { - padding: 1em 0; - } +export default React.memo(styled(VanityApp)` + .vanity--App-matches { + padding: 1em 0; + } - .vanity--App-stats { - padding: 1em 0 0 0; - opacity: 0.45; - text-align: center; - } - ` -); + .vanity--App-stats { + padding: 1em 0 0 0; + opacity: 0.45; + text-align: center; + } +`);