-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: check if the feature is supported
- Loading branch information
Showing
11 changed files
with
178 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import React from 'react'; | ||
import { InfoCircledIcon } from '@radix-ui/react-icons'; | ||
|
||
import type { FeatureToCheck } from '@/utils'; | ||
import { useIsFeatureSupported } from '@/hooks/use-is-feature-supported'; | ||
import { cn } from '@/lib/utils'; | ||
import { useInstallSnap } from '@/hooks/use-install-snap'; | ||
|
||
export type FeatureGuardProps = { | ||
feature: FeatureToCheck; | ||
} & React.HTMLAttributes<HTMLDivElement>; | ||
|
||
export function FeatureGuard({ | ||
feature, | ||
children, | ||
className, | ||
...props | ||
}: FeatureGuardProps) { | ||
const isFeatureSupported = useIsFeatureSupported(feature); | ||
const installSnap = useInstallSnap(); | ||
|
||
return ( | ||
<div | ||
{...props} | ||
className={cn( | ||
'relative', | ||
!isFeatureSupported && 'min-h-[3rem]', | ||
className, | ||
)} | ||
> | ||
{isFeatureSupported ? ( | ||
children | ||
) : ( | ||
<div className="rounded-md bg-yellow-50 p-4"> | ||
<div className="flex"> | ||
<div className="flex-shrink-0"> | ||
<InfoCircledIcon | ||
className="h-5 w-5 text-yellow-400" | ||
aria-hidden="true" | ||
/> | ||
</div> | ||
<div className="ml-3 flex-1 md:flex md:justify-between"> | ||
<p className="text-sm text-yellow-700"> | ||
Current installed version does not support this feature. Click | ||
the Reinstall button to install the latest version. | ||
</p> | ||
<p className="mt-3 text-sm md:ml-6 md:mt-0"> | ||
<button | ||
onClick={installSnap} | ||
className="whitespace-nowrap font-medium text-yellow-700 hover:text-yellow-600" | ||
> | ||
Reinstall | ||
<span aria-hidden="true"> →</span> | ||
</button> | ||
</p> | ||
</div> | ||
</div> | ||
</div> | ||
)} | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React, { useContext } from 'react'; | ||
|
||
import { connectSnap, getSnap } from '@/utils'; | ||
import { MetamaskActions, MetaMaskContext } from '@/hooks/MetamaskContext'; | ||
|
||
export function useInstallSnap() { | ||
const [, dispatch] = useContext(MetaMaskContext); | ||
|
||
return React.useCallback(async () => { | ||
try { | ||
await connectSnap(); | ||
const installedSnap = await getSnap(); | ||
|
||
dispatch({ | ||
type: MetamaskActions.SetInstalled, | ||
payload: installedSnap, | ||
}); | ||
} catch (e) { | ||
console.error(e); | ||
dispatch({ type: MetamaskActions.SetError, payload: e }); | ||
} | ||
}, []); | ||
} |
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,8 @@ | ||
import React from 'react'; | ||
|
||
import { MetaMaskContext } from '@/hooks/MetamaskContext'; | ||
|
||
export function useInstalledSnapVersion() { | ||
const [state] = React.useContext(MetaMaskContext); | ||
return state.installedSnap?.version ?? null; | ||
} |
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,24 @@ | ||
import React from 'react'; | ||
import { compareVersions } from 'compare-versions'; | ||
|
||
import { useIsMetaMaskReady } from '@/hooks/use-is-meta-mask-ready'; | ||
import { type FeatureToCheck, isFeatureSupported } from '@/utils'; | ||
import { useInstalledSnapVersion } from '@/hooks/use-installed-snap-version'; | ||
|
||
export function useIsFeatureSupported(feature: FeatureToCheck) { | ||
const isMetaMaskReady = useIsMetaMaskReady(); | ||
const [isSupported, setIsSupported] = React.useState<boolean>(false); | ||
const installedSnapVersion = useInstalledSnapVersion(); | ||
|
||
React.useEffect(() => { | ||
if ( | ||
isMetaMaskReady && | ||
installedSnapVersion && | ||
compareVersions(installedSnapVersion, '0.1.13') > 0 | ||
) { | ||
isFeatureSupported(feature).then(setIsSupported); | ||
} | ||
}, [isMetaMaskReady, feature, installedSnapVersion]); | ||
|
||
return isSupported; | ||
} |
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