forked from metaplex-foundation/metaplex
-
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.
Improve init store (metaplex-foundation#213)
* chore: missed env CLIENT_ID used NEXT_PUBLIC prefix now * chore: return missed #root * chore: organize styles * feat: storeProvider * feat: STORE_ADDRESS in ENV * feat: improve store initialization
- Loading branch information
Showing
29 changed files
with
692 additions
and
504 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React, { | ||
createContext, | ||
FC, | ||
useState, | ||
useContext, | ||
useEffect, | ||
useMemo, | ||
} from 'react'; | ||
import { getStoreID, setProgramIds, StringPublicKey } from '../utils'; | ||
import { useQuerySearch } from '../hooks'; | ||
|
||
interface StoreConfig { | ||
// Store Address | ||
storeAddress?: StringPublicKey; | ||
// Store was configured via ENV or query params | ||
isConfigured: boolean; | ||
// Initial calculating of store address completed (successfully or not) | ||
isReady: boolean; | ||
// recalculate store address for specified owner address | ||
setStoreForOwner: (ownerAddress?: string) => Promise<string | undefined>; | ||
} | ||
|
||
export const StoreContext = createContext<StoreConfig>(null!); | ||
|
||
export const StoreProvider: FC<{ | ||
ownerAddress?: string; | ||
storeAddress?: string; | ||
}> = ({ children, ownerAddress, storeAddress }) => { | ||
const searchParams = useQuerySearch(); | ||
const ownerAddressFromQuery = searchParams.get('store'); | ||
|
||
const initOwnerAddress = ownerAddressFromQuery || ownerAddress; | ||
const initStoreAddress = !ownerAddressFromQuery ? storeAddress : undefined; | ||
const isConfigured = Boolean(initStoreAddress || initOwnerAddress); | ||
|
||
const [store, setStore] = useState< | ||
Pick<StoreConfig, 'storeAddress' | 'isReady'> | ||
>({ | ||
storeAddress: initStoreAddress, | ||
isReady: Boolean(!initOwnerAddress || initStoreAddress), | ||
}); | ||
|
||
const setStoreForOwner = useMemo( | ||
() => async (ownerAddress?: string) => { | ||
const storeAddress = await getStoreID(ownerAddress); | ||
setProgramIds(storeAddress); // fallback | ||
setStore({ storeAddress, isReady: true }); | ||
console.log(`CUSTOM STORE: ${storeAddress}`); | ||
return storeAddress; | ||
}, | ||
[], | ||
); | ||
|
||
useEffect(() => { | ||
console.log(`STORE_OWNER_ADDRESS: ${initOwnerAddress}`); | ||
if (initOwnerAddress && !initStoreAddress) { | ||
setStoreForOwner(initOwnerAddress); | ||
} else { | ||
setProgramIds(initStoreAddress); // fallback | ||
console.log(`CUSTOM STORE FROM ENV: ${initStoreAddress}`); | ||
} | ||
}, [initOwnerAddress]); | ||
|
||
return ( | ||
<StoreContext.Provider value={{ ...store, setStoreForOwner, isConfigured }}> | ||
{children} | ||
</StoreContext.Provider> | ||
); | ||
}; | ||
|
||
export const useStore = () => { | ||
return useContext(StoreContext); | ||
}; |
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,5 @@ | ||
import { useLocation } from 'react-router-dom'; | ||
|
||
export function useQuerySearch() { | ||
return new URLSearchParams(useLocation().search); | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
REACT_APP_STORE_OWNER_ADDRESS_ADDRESS= | ||
REACT_APP_STORE_ADDRESS= | ||
REACT_APP_BIG_STORE=FALSE |
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
@import '../../_colors.less'; | ||
|
||
.App-Bar { | ||
padding: 0px; | ||
justify-content: space-between !important; | ||
|
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 { CopyOutlined } from '@ant-design/icons'; | ||
import { Button, Card } from 'antd'; | ||
import { FC } from 'react'; | ||
import { useCallback, useRef } from 'react'; | ||
|
||
interface Variables { | ||
storeAddress?: string; | ||
storeOwnerAddress?: string; | ||
} | ||
|
||
export const SetupVariables: FC<Variables> = ({ | ||
storeAddress, | ||
storeOwnerAddress, | ||
}) => { | ||
const ref = useRef<HTMLDivElement>(null); | ||
|
||
const copySettings = useCallback(() => { | ||
const text = ref.current?.innerText; | ||
if (text) { | ||
navigator.clipboard.writeText(text); | ||
} | ||
}, []); | ||
|
||
if (!storeAddress && !storeOwnerAddress) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Card | ||
title="Store configuration" | ||
extra={ | ||
<Button | ||
type="dashed" | ||
onClick={copySettings} | ||
icon={<CopyOutlined />} | ||
></Button> | ||
} | ||
> | ||
<div ref={ref}> | ||
{storeOwnerAddress && <p>STORE_OWNER_ADDRESS={storeOwnerAddress}</p>} | ||
{storeAddress && <p>STORE_ADDRESS={storeAddress}</p>} | ||
</div> | ||
</Card> | ||
); | ||
}; |
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.