-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(idea/frontend): add code verifier (#1707)
- Loading branch information
1 parent
8bea095
commit 11614ec
Showing
51 changed files
with
845 additions
and
137 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
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,9 @@ | ||
const API_URL = import.meta.env.VITE_CODE_VERIFIER_API_URL as string; | ||
|
||
const METHOD = { | ||
VERIFY: 'verify', | ||
VERIFY_STATUS: 'verify/status', | ||
CODE: 'code', | ||
} as const; | ||
|
||
export { API_URL, METHOD }; |
54 changes: 54 additions & 0 deletions
54
idea/gear/frontend/src/features/code-verifier/api/hooks.ts
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,54 @@ | ||
import { HexString } from '@gear-js/api'; | ||
import { useAlert } from '@gear-js/react-hooks'; | ||
import { useMutation, useQuery } from '@tanstack/react-query'; | ||
import { STATUS_CODES } from 'http'; | ||
import { useEffect } from 'react'; | ||
|
||
import { getVerificationStatus, getVerifiedCode, verifyCode } from './requests'; | ||
|
||
function useVerifyCode() { | ||
return useMutation({ | ||
mutationKey: ['verify-code'], | ||
mutationFn: verifyCode, | ||
}); | ||
} | ||
|
||
function useIsCodeVerified(codeId: HexString | null | undefined) { | ||
const alert = useAlert(); | ||
|
||
const query = useQuery({ | ||
queryKey: ['code-verification-status', codeId], | ||
queryFn: () => getVerifiedCode(codeId!), | ||
select: (response) => Boolean(response), | ||
enabled: Boolean(codeId), | ||
}); | ||
|
||
const { error } = query; | ||
|
||
useEffect(() => { | ||
if (error && error.message !== STATUS_CODES[404]) alert.error(error.message); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [error]); | ||
|
||
return query; | ||
} | ||
|
||
function useVerificationStatus(id: string) { | ||
const alert = useAlert(); | ||
|
||
const query = useQuery({ | ||
queryKey: ['verification-status', id], | ||
queryFn: () => getVerificationStatus(id), | ||
}); | ||
|
||
const { error } = query; | ||
|
||
useEffect(() => { | ||
if (error) alert.error(error.message); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [error]); | ||
|
||
return query; | ||
} | ||
|
||
export { useVerifyCode, useIsCodeVerified, useVerificationStatus }; |
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,3 @@ | ||
import { useVerifyCode, useIsCodeVerified, useVerificationStatus } from './hooks'; | ||
|
||
export { useVerifyCode, useIsCodeVerified, useVerificationStatus }; |
13 changes: 13 additions & 0 deletions
13
idea/gear/frontend/src/features/code-verifier/api/requests.ts
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,13 @@ | ||
import { fetchWithGuard } from '@/shared/helpers'; | ||
import { CodeResponse, StatusResponse, VerifyParameters, VerifyResponse } from './types'; | ||
import { API_URL } from './consts'; | ||
|
||
const verifyCode = (parameters: VerifyParameters) => | ||
fetchWithGuard<VerifyResponse>(`${API_URL}/verify`, 'POST', parameters); | ||
|
||
const getVerificationStatus = (id: string) => | ||
fetchWithGuard<StatusResponse>(`${API_URL}/verify/status?id=${id}`, 'GET'); | ||
|
||
const getVerifiedCode = (id: string) => fetchWithGuard<CodeResponse>(`${API_URL}/code?id=${id}`, 'GET'); | ||
|
||
export { verifyCode, getVerificationStatus, getVerifiedCode }; |
29 changes: 29 additions & 0 deletions
29
idea/gear/frontend/src/features/code-verifier/api/types.ts
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,29 @@ | ||
import { HexString } from '@gear-js/api'; | ||
|
||
type VerifyParameters = { | ||
build_idl: boolean; | ||
code_id: HexString; | ||
network: string; | ||
project: { Name: string } | { PathToCargoToml: string }; | ||
repo_link: string; | ||
version: string; | ||
}; | ||
|
||
type VerifyResponse = { | ||
id: string; | ||
}; | ||
|
||
type StatusResponse = { | ||
status: 'pending' | 'verified' | 'failed' | 'in_progress'; | ||
failed_reason: string | null; | ||
created_at: number; | ||
}; | ||
|
||
type CodeResponse = { | ||
id: string; | ||
idl_hash: string | null; | ||
name: string; | ||
repo_link: string; | ||
}; | ||
|
||
export type { VerifyParameters, VerifyResponse, StatusResponse, CodeResponse }; |
10 changes: 10 additions & 0 deletions
10
idea/gear/frontend/src/features/code-verifier/assets/refresh.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions
5
idea/gear/frontend/src/features/code-verifier/components/index.ts
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,5 @@ | ||
import { VerifyLink } from './verify-link'; | ||
import { VerificationStatus } from './verification-status'; | ||
import { VerifyForm } from './verify-form'; | ||
|
||
export { VerifyLink, VerificationStatus, VerifyForm }; |
3 changes: 3 additions & 0 deletions
3
idea/gear/frontend/src/features/code-verifier/components/verification-status/index.ts
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,3 @@ | ||
import { VerificationStatus } from './verification-status'; | ||
|
||
export { VerificationStatus }; |
29 changes: 29 additions & 0 deletions
29
...src/features/code-verifier/components/verification-status/verification-status.module.scss
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,29 @@ | ||
.status { | ||
padding: 4px 8px; | ||
|
||
font-size: 12px; | ||
font-weight: 600; | ||
line-height: 15.6px; | ||
white-space: nowrap; | ||
|
||
border-radius: 20px; | ||
} | ||
|
||
.verified { | ||
color: #2bd071; | ||
|
||
background-color: rgba(#2bd071, 0.1); | ||
} | ||
|
||
.in_progress, | ||
.pending { | ||
color: #f2c94c; | ||
|
||
background-color: rgba(#f2c94c, 0.1); | ||
} | ||
|
||
.failed { | ||
color: #f24a4a; | ||
|
||
background-color: rgba(#f24a4a, 0.1); | ||
} |
28 changes: 28 additions & 0 deletions
28
...rontend/src/features/code-verifier/components/verification-status/verification-status.tsx
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,28 @@ | ||
import clsx from 'clsx'; | ||
|
||
import { Skeleton } from '@/shared/ui'; | ||
|
||
import styles from './verification-status.module.scss'; | ||
|
||
type Props = { | ||
value: 'verified' | 'failed' | 'pending' | 'in_progress'; | ||
}; | ||
|
||
const TEXT = { | ||
verified: 'Verified', | ||
failed: 'Failed', | ||
pending: 'Pending', | ||
in_progress: 'In Progress', | ||
} as const; | ||
|
||
function VerificationStatus({ value }: Props) { | ||
return <span className={clsx(styles.status, styles[value])}>{TEXT[value]}</span>; | ||
} | ||
|
||
function VerificationStatusSkeleton({ disabled }: { disabled: boolean }) { | ||
return <Skeleton width="5rem" className={styles.status} disabled={disabled} />; | ||
} | ||
|
||
VerificationStatus.Skeleton = VerificationStatusSkeleton; | ||
|
||
export { VerificationStatus }; |
77 changes: 77 additions & 0 deletions
77
idea/gear/frontend/src/features/code-verifier/components/verify-form/consts.ts
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,77 @@ | ||
import { z } from 'zod'; | ||
|
||
import { GENESIS } from '@/shared/config'; | ||
|
||
import { isCodeIdValid } from '../../utils'; | ||
|
||
const FIELD_NAME = { | ||
DOCKER_IMAGE_VERSION: 'version', | ||
CODE_ID: 'codeId', | ||
REPO_LINK: 'repoLink', | ||
PROJECT_ID_TYPE: 'projectIdType', | ||
PROJECT_ID: 'projectId', | ||
NETWORK: 'network', | ||
BUILD_IDL: 'buildIdl', | ||
} as const; | ||
|
||
const NETWORK = { | ||
[GENESIS.MAINNET]: 'vara_mainnet', | ||
[GENESIS.TESTNET]: 'vara_testnet', | ||
} as const; | ||
|
||
const NETWORK_OPTIONS = [ | ||
{ label: 'Mainnet', value: NETWORK[GENESIS.MAINNET] }, | ||
{ label: 'Testnet', value: NETWORK[GENESIS.TESTNET] }, | ||
] as const; | ||
|
||
const PROJECT_ID_TYPE = { | ||
NAME: 'name', | ||
CARGO_TOML_PATH: 'cargoTomlPath', | ||
} as const; | ||
|
||
const DEFAULT_VALUES = { | ||
[FIELD_NAME.DOCKER_IMAGE_VERSION]: '', | ||
[FIELD_NAME.CODE_ID]: '', | ||
[FIELD_NAME.REPO_LINK]: '', | ||
[FIELD_NAME.PROJECT_ID_TYPE]: PROJECT_ID_TYPE.NAME as (typeof PROJECT_ID_TYPE)[keyof typeof PROJECT_ID_TYPE], | ||
[FIELD_NAME.PROJECT_ID]: '', | ||
[FIELD_NAME.NETWORK]: NETWORK_OPTIONS[0].value as (typeof NETWORK)[keyof typeof NETWORK], | ||
[FIELD_NAME.BUILD_IDL]: false, | ||
}; | ||
|
||
const SEMVER_REGEX = /^\d+\.\d+\.\d+$/; | ||
const GITHUB_REPO_URL_REGEX = /^https?:\/\/(www\.)?github\.com\/([\w-]+)\/([\w-]+)(\/.*)?$/; | ||
const CARGO_TOML_PATH_REGEX = /^(?:\.\/)?(?:[^/]+\/)*Cargo\.toml$/; | ||
|
||
const SCHEMA = z | ||
.object({ | ||
[FIELD_NAME.DOCKER_IMAGE_VERSION]: z | ||
.string() | ||
.trim() | ||
.refine((value) => SEMVER_REGEX.test(value), { message: 'Invalid version format' }), | ||
|
||
[FIELD_NAME.CODE_ID]: z | ||
.string() | ||
.trim() | ||
.refine((value) => isCodeIdValid(value), { message: 'Invalid hex' }), | ||
|
||
[FIELD_NAME.REPO_LINK]: z | ||
.string() | ||
.trim() | ||
.refine((value) => GITHUB_REPO_URL_REGEX.test(value), { message: 'Invalid GitHub repository URL' }), | ||
|
||
[FIELD_NAME.PROJECT_ID_TYPE]: z.string(), | ||
[FIELD_NAME.PROJECT_ID]: z.string().trim().min(1), | ||
[FIELD_NAME.NETWORK]: z.string(), | ||
[FIELD_NAME.BUILD_IDL]: z.boolean(), | ||
}) | ||
.refine( | ||
({ projectIdType, projectId }) => | ||
projectIdType === PROJECT_ID_TYPE.CARGO_TOML_PATH ? CARGO_TOML_PATH_REGEX.test(projectId) : true, | ||
{ | ||
message: 'Invalid path to Cargo.toml', | ||
path: [FIELD_NAME.PROJECT_ID], | ||
}, | ||
); | ||
|
||
export { DEFAULT_VALUES, SCHEMA, NETWORK, FIELD_NAME, PROJECT_ID_TYPE, NETWORK_OPTIONS }; |
3 changes: 3 additions & 0 deletions
3
idea/gear/frontend/src/features/code-verifier/components/verify-form/index.ts
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,3 @@ | ||
import { VerifyForm } from './verify-form'; | ||
|
||
export { VerifyForm }; |
Oops, something went wrong.