Skip to content

Commit ef98b46

Browse files
author
ci-bot
committed
consolidate reducer and state. Add logic for workspace creation.
1 parent b625fcc commit ef98b46

14 files changed

+170
-121
lines changed

libs/remix-ui/template-explorer-modal/context/template-explorer-context.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ import { metadata, templatesRepository } from '../src/utils/helpers'
66
import { AppContext } from '@remix-ui/app'
77
import { TemplateExplorerModalPlugin } from 'apps/remix-ide/src/app/plugins/remix-template-explorer-modal'
88
import { RemixUiTemplateExplorerModal } from '@remix-ui/template-explorer-modal'
9+
import { TemplateExplorerModalFacade } from '../src/utils/workspaceUtils'
10+
import { TemplateCategoryStrategy } from '../stategies/templateCategoryStrategy'
911

1012
export const TemplateExplorerContext = createContext<TemplateExplorerContextType>({} as any)
1113

1214
export const TemplateExplorerProvider = (props: { plugin: TemplateExplorerModalPlugin }) => {
13-
// const [templateRepository, setTemplateRepository] = useState<TemplateCategory[]>([])
14-
// const [metadata, setMetadata] = useState<any[]>([])
15-
// const [selectedTag, setSelectedTag] = useState<string | null>(null)
16-
// const [recentBump, setRecentBump] = useState<number>(0)
1715
const [state, dispatch] = useReducer(templateExplorerReducer, initialState)
1816
const appContext = useContext(AppContext)
1917
const { plugin } = props
18+
const makeWorkspace = new TemplateExplorerModalFacade(plugin)
19+
const templateCategoryStrategy = new TemplateCategoryStrategy()
2020

2121
useEffect(() => {
2222
dispatch({ type: TemplateExplorerWizardAction.SET_TEMPLATE_REPOSITORY, payload: templatesRepository })
@@ -26,10 +26,6 @@ export const TemplateExplorerProvider = (props: { plugin: TemplateExplorerModalP
2626
dispatch({ type: TemplateExplorerWizardAction.SET_METADATA, payload: metadata })
2727
}, [])
2828

29-
useEffect(() => {
30-
console.log('state context', state)
31-
}, [state])
32-
3329
const setSearchTerm = (term: string) => {
3430
dispatch({ type: TemplateExplorerWizardAction.SET_SEARCH_TERM, payload: term })
3531
}
@@ -163,7 +159,7 @@ export const TemplateExplorerProvider = (props: { plugin: TemplateExplorerModalP
163159
}
164160
}
165161

166-
const contextValue = { templateRepository: state.templateRepository, metadata: state.metadata, selectedTag: state.selectedTag, recentTemplates, filteredTemplates, dedupedTemplates, handleTagClick, clearFilter, addRecentTemplate, RECENT_KEY, allTags, plugin, setSearchTerm, dispatch, state }
162+
const contextValue = { templateRepository: state.templateRepository, metadata: state.metadata, selectedTag: state.selectedTag, recentTemplates, filteredTemplates, dedupedTemplates, handleTagClick, clearFilter, addRecentTemplate, RECENT_KEY, allTags, plugin, setSearchTerm, dispatch, state, theme: plugin.theme, makeWorkspace, templateCategoryStrategy }
167163

168164
return (
169165
<TemplateExplorerContext.Provider value={contextValue}>

libs/remix-ui/template-explorer-modal/reducers/contract-wizard-reducer.tsx

Lines changed: 0 additions & 57 deletions
This file was deleted.

libs/remix-ui/template-explorer-modal/reducers/template-explorer-reducer.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const templateExplorerReducer = (state: TemplateExplorerWizardState, acti
4747
return { ...state, workspaceTemplateChosen: action.payload }
4848
}
4949
case TemplateExplorerWizardAction.SET_WORKSPACE_TEMPLATE_GROUP:{
50-
return { ...state, workspaceTemplateGroupChosen: action.payload.templateGroupChosen }
50+
return { ...state, workspaceTemplateGroupChosen: action.payload }
5151
}
5252
case TemplateExplorerWizardAction.SET_WORKSPACE_NAME:{
5353
return { ...state, workspaceName: action.payload }
@@ -103,6 +103,7 @@ export const templateExplorerReducer = (state: TemplateExplorerWizardState, acti
103103
return { ...state, tokenName: action.payload }
104104
}
105105
case ContractWizardAction.CONTRACT_NAME_UPDATE: {
106+
console.log('action.payload contractName', action.payload)
106107
return { ...state, contractName: action.payload }
107108
}
108109
default: {

libs/remix-ui/template-explorer-modal/src/components/contract-wizard.tsx

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import * as erc20 from '../contractCode/erc20'
44
import * as erc721 from '../contractCode/erc721'
55
import * as erc1155 from '../contractCode/erc1155'
66
import { AccessControlType, ContractTypeStrategy, ContractWizardAction } from '../../types/template-explorer-types'
7-
import { contractWizardReducer } from '../../reducers/contract-wizard-reducer'
87
import { getErc1155ContractCode, getErc20ContractCode, getErc721ContractCode } from '../utils/contractWizardUtils'
98
import { TemplateExplorerContext } from '../../context/template-explorer-context'
9+
import { CustomTooltip } from '@remix-ui/helper'
10+
import { OverlayTrigger, Tooltip } from 'react-bootstrap'
11+
import { TemplateExplorerModalFacade } from '../utils/workspaceUtils'
1012

1113
const defaultStrategy: ContractTypeStrategy = {
1214
contractType: 'erc20',
@@ -26,16 +28,8 @@ const defaultStrategy: ContractTypeStrategy = {
2628
}
2729

2830
export function ContractWizard () {
29-
const [tokenName, setTokenName] = useState('MyToken')
30-
31-
const [accessControl, setAccessControl] = useState<'ownable' | 'roles' | 'managed' | ''>('')
32-
const [upgradability, setUpgradability] = useState({
33-
uups: false,
34-
transparent: false
35-
})
36-
const [initGit, setInitGit] = useState(false)
3731
const [showEditModal, setShowEditModal] = useState(false)
38-
const { state, dispatch } = useContext(TemplateExplorerContext)
32+
const { state, dispatch, theme, makeWorkspace } = useContext(TemplateExplorerContext)
3933
const strategy = state
4034

4135
function toggleContractOption(key: keyof typeof strategy.contractOptions) {
@@ -58,10 +52,10 @@ export function ContractWizard () {
5852
}
5953
function updateContractName(contractName: string) {
6054
dispatch({ type: ContractWizardAction.CONTRACT_NAME_UPDATE, payload: contractName })
55+
dispatch({ type: ContractWizardAction.TOKEN_NAME_UPDATE, payload: contractName })
6156
}
6257

6358
useEffect(() => {
64-
// console.log('strategy', strategy)
6559
if (strategy.contractType === 'erc20') {
6660
dispatch({ type: ContractWizardAction.CONTRACT_CODE_UPDATE, payload: getErc20ContractCode(strategy.contractType, strategy) })
6761
} else if (strategy.contractType === 'erc721') {
@@ -76,7 +70,9 @@ export function ContractWizard () {
7670
<div className="row g-3">
7771
<div className="col-12 d-flex align-items-center justify-content-between">
7872
<div className="d-flex align-items-center gap-2">
79-
{showEditModal ? <input className="form-control form-control-sm" value={tokenName} onChange={(e) => setTokenName(e.target.value)} /> : <span className="fw-semibold">{tokenName}</span>}
73+
{showEditModal ? <input className="form-control form-control-sm" value={state.tokenName} onChange={(e) => updateContractName(e.target.value)} /> : <span className={`fw-semibold fs-6 ${theme.currentTheme().name === 'Light' ? 'text-dark' : 'text-light'}`}>
74+
{state.tokenName}
75+
</span>}
8076
<i className="fas fa-edit" onClick={() => setShowEditModal(true)}></i>
8177
</div>
8278
<div className="d-flex align-items-center gap-2">
@@ -98,7 +94,7 @@ export function ContractWizard () {
9894
<div className="mb-3">
9995
<div className="fw-semibold mb-2">Contract settings</div>
10096
<label className="form-label text-uppercase small mb-1">Token name</label>
101-
<input className="form-control form-control-sm" placeholder="My Token" value={tokenName} onChange={(e) => updateTokenName(e.target.value)} />
97+
<input className="form-control form-control-sm" placeholder="My Token" value={state.tokenName} readOnly />
10298
</div>
10399

104100
<div className="mb-3">
@@ -154,16 +150,30 @@ export function ContractWizard () {
154150
<Editor
155151
height="460px"
156152
defaultLanguage="typescript"
157-
options={{ readOnly: true, minimap: { enabled: false }, theme: 'vs-dark' }}
153+
options={{ readOnly: true, minimap: { enabled: false }, theme: theme.currentTheme().name === 'Light' ? 'vs' : 'vs-dark' }}
158154
value={strategy.contractCode as string}
159155
/>
160156
</div>
161157
<div className="d-flex justify-content-between align-items-center gap-3 mt-3">
162158
<div className="form-check m-0">
163-
<input className="form-check-input" type="checkbox" id="initGit" checked={initGit} onChange={(e) => setInitGit(e.target.checked)} />
164-
<label className="form-check-label" htmlFor="initGit">Initialize as a Git repository</label>
159+
<>
160+
<input className="form-check-input" type="checkbox" id="initGit" checked={state.initializeAsGitRepo}
161+
onChange={(e) => dispatch({ type: ContractWizardAction.INITIALIZE_AS_GIT_REPO_UPDATE, payload: e.target.checked })} />
162+
<label className="form-check-label" htmlFor="initGit">Initialize as a Git repository</label>
163+
</>
165164
</div>
166-
<button className="btn btn-primary btn-sm">Validate workspace</button>
165+
166+
<button className="btn btn-primary btn-sm" data-id="validateWorkspaceButton" onClick={async () => {
167+
console.log('about to create workspace')
168+
await makeWorkspace.createWorkspace({
169+
workspaceName: state.workspaceTemplateChosen.displayName,
170+
workspaceTemplateName: state.workspaceTemplateChosen.value,
171+
opts: { mintable: state.contractOptions.mintable, burnable: state.contractOptions.burnable, pausable: state.contractOptions.pausable, uups: state.contractUpgradability.uups, transparent: state.contractUpgradability.transparent },
172+
isEmpty: false,
173+
isGitRepo: state.initializeAsGitRepo,
174+
createCommit: true
175+
})
176+
}}>Validate workspace</button>
167177
</div>
168178
</div>
169179
</div>

libs/remix-ui/template-explorer-modal/src/components/generateWorkspaceWithAi.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import { initialState, templateExplorerReducer } from '../../reducers/template-e
55
export function GenerateWorkspaceWithAi() {
66
const [state, dispatch] = useReducer(templateExplorerReducer, initialState)
77
return (
8-
<section>
9-
<div className="d-flex flex-column bg-light" style={{ height: '80%' }}>
10-
<div>
11-
<label className="form-label text-uppercase small mb-1">Workspace description</label>
8+
<section className="mx-3 p-2">
9+
<div className="d-flex flex-column p-3 bg-light" style={{ minHeight: '90%' }}>
10+
<div className="d-flex flex-row justify-content-between align-items-center mb-3 border-bottom border-light">
11+
<label className="form-label text-uppercase mb-2">Write a prompt to generate a workspace</label>
12+
<span className="badge badge-pill text-primary border mb-2 border-primary">Beta</span>
1213
</div>
1314
<div>
1415
<textarea className="form-control text-dark" onChange={(e) => dispatch({ type: TemplateExplorerWizardAction.SET_WORKSPACE_NAME, payload: e.target.value })}
@@ -17,6 +18,14 @@ export function GenerateWorkspaceWithAi() {
1718
/>
1819
</div>
1920
</div>
21+
<div className="d-flex justify-content-end align-items-center mt-3">
22+
<button className="btn btn-primary btn-sm" data-id="validateWorkspaceButton" onClick={async () => {
23+
console.log('about to create workspace with ai')
24+
}}>
25+
<i className="fa-solid fa-magic me-2"></i>
26+
Generate my Workspace
27+
</button>
28+
</div>
2029
</section>
2130
)
2231
}

libs/remix-ui/template-explorer-modal/src/components/genericWorkspaceTemplate.tsx

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,43 @@
1-
import React, { useReducer } from 'react'
1+
import React, { useContext, useReducer } from 'react'
22
import { initialState, templateExplorerReducer } from '../../reducers/template-explorer-reducer'
3-
import { TemplateExplorerWizardAction } from '../../types/template-explorer-types'
3+
import { ContractWizardAction, TemplateExplorerWizardAction } from '../../types/template-explorer-types'
4+
import { TemplateExplorerContext } from '../../context/template-explorer-context'
45

56
export function GenericWorkspaceTemplate() {
6-
const [state, dispatch] = useReducer(templateExplorerReducer, initialState)
7+
8+
const { state, dispatch, makeWorkspace } = useContext(TemplateExplorerContext)
9+
710
return (
8-
<section>
9-
<div className="d-flex flex-column gap-3 bg-light" style={{ height: '80%' }}>
11+
<section className="mx-3 p-2">
12+
<div className="d-flex flex-column p-3 bg-light" style={{ minHeight: '80%' }}>
1013
<div>
1114
<label className="form-label text-uppercase small mb-1">Workspace name</label>
1215
</div>
1316
<div>
1417
<input type="text" className="form-control text-dark" value={state.workspaceName} onChange={(e) => dispatch({ type: TemplateExplorerWizardAction.SET_WORKSPACE_NAME, payload: e.target.value })} />
1518
</div>
19+
20+
<div className="d-flex justify-content-between align-items-center gap-3 mt-3">
21+
<div className="form-check m-0">
22+
<>
23+
<input className="form-check-input" type="checkbox" id="initGit" checked={state.initializeAsGitRepo}
24+
onChange={(e) => dispatch({ type: ContractWizardAction.INITIALIZE_AS_GIT_REPO_UPDATE, payload: e.target.checked })} />
25+
<label className="form-check-label" htmlFor="initGit">Initialize as a Git repository</label>
26+
</>
27+
</div>
28+
29+
<button className="btn btn-primary btn-sm" data-id="validateWorkspaceButton" onClick={async () => {
30+
console.log('about to create workspace generic')
31+
await makeWorkspace.createWorkspace({
32+
workspaceName: state.workspaceTemplateChosen.displayName,
33+
workspaceTemplateName: state.workspaceTemplateChosen.value,
34+
opts: { },
35+
isEmpty: false,
36+
isGitRepo: state.initializeAsGitRepo,
37+
createCommit: true
38+
})
39+
}}>Create workspace</button>
40+
</div>
1641
</div>
1742
</section>
1843
)

libs/remix-ui/template-explorer-modal/src/components/template-explorer-body.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ export function TemplateExplorerBody({ plugin }: TemplateExplorerBodyProps) {
1717
console.log('state template explorer body', state)
1818
}, [state])
1919
return (
20-
<section>
20+
<section className="mx-4">
2121
<TopCards />
2222
{
2323
(dedupedTemplates.length === 0) ? <NotFound /> : (
24-
<div className="body overflow-y-hidden">
24+
<div className="body overflow-y-hidden pt-3">
2525
<>
2626
<label className="text-dark fs-5">Workspace Templates</label>
2727
<div className="">

libs/remix-ui/template-explorer-modal/src/components/template-explorer.tsx

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import isElectron from 'is-electron'
22
import React, { useContext, useEffect } from 'react'
33
import { TemplateCategory, TemplateExplorerProps, TemplateExplorerWizardAction, TemplateItem } from '../../types/template-explorer-types'
44
import { TemplateExplorerContext } from '../../context/template-explorer-context'
5-
import { CookbookStrategy, GenAiStrategy, GenericStrategy, RemixDefaultStrategy, TemplateCateogryStrategy, WizardStrategy } from '../../stategies/templateCategoryStrategy'
5+
import { CookbookStrategy, GenAiStrategy, GenericStrategy, RemixDefaultStrategy, WizardStrategy } from '../../stategies/templateCategoryStrategy'
66

77
export function TemplateExplorer() {
88

9-
const { metadata, dedupedTemplates, addRecentTemplate, plugin, dispatch, state } = useContext(TemplateExplorerContext)
9+
const { metadata, dedupedTemplates, addRecentTemplate, plugin, dispatch, state, templateCategoryStrategy } = useContext(TemplateExplorerContext)
1010

1111
return (
1212
<div className="template-explorer-container overflow-y-auto" style={{ height: '350px', padding: '1rem' }}>
@@ -55,26 +55,28 @@ export function TemplateExplorer() {
5555
flexDirection: 'column'
5656
}}
5757
onClick={() => {
58-
const strategy = new TemplateCateogryStrategy()
59-
dispatch({ type: TemplateExplorerWizardAction.SELECT_TEMPLATE, payload: item.value })
58+
dispatch({ type: TemplateExplorerWizardAction.SELECT_TEMPLATE, payload: item })
6059
dispatch({ type: TemplateExplorerWizardAction.SET_WORKSPACE_TEMPLATE_GROUP, payload: template.name })
6160
dispatch({ type: TemplateExplorerWizardAction.SET_WORKSPACE_NAME, payload: item.value })
6261
if (template.name.toLowerCase() === 'generic' && !item.value.toLowerCase().includes('remixaitemplate') && item.value !== 'remixDefault') {
63-
strategy.setStrategy(new GenericStrategy())
64-
strategy.switchScreen(dispatch)
62+
templateCategoryStrategy.setStrategy(new GenericStrategy())
63+
templateCategoryStrategy.switchScreen(dispatch)
6564
} else if (template.name.toLowerCase() === 'generic' && item.value.toLowerCase().includes('remixaitemplate')) {
66-
strategy.setStrategy(new GenAiStrategy())
67-
strategy.switchScreen(dispatch)
65+
templateCategoryStrategy.setStrategy(new GenAiStrategy())
66+
templateCategoryStrategy.switchScreen(dispatch)
6867
} else if (template.name.toLowerCase() === 'generic' && item.value === 'remixDefault') {
6968
console.log('remixdefault')
70-
strategy.setStrategy(new RemixDefaultStrategy())
71-
strategy.switchScreen(dispatch)
69+
templateCategoryStrategy.setStrategy(new RemixDefaultStrategy())
70+
templateCategoryStrategy.switchScreen(dispatch)
7271
} else if (template.name.toLowerCase().includes('zeppelin')) {
73-
strategy.setStrategy(new WizardStrategy())
74-
strategy.switchScreen(dispatch)
72+
templateCategoryStrategy.setStrategy(new WizardStrategy())
73+
templateCategoryStrategy.switchScreen(dispatch)
7574
} else if (template.name.toLowerCase().includes('cookbook')) {
76-
strategy.setStrategy(new CookbookStrategy())
77-
strategy.switchScreen(dispatch)
75+
templateCategoryStrategy.setStrategy(new CookbookStrategy())
76+
templateCategoryStrategy.switchScreen(dispatch)
77+
} else if (template.name.toLowerCase() !== 'generic' && template.name.toLowerCase() !== 'zeppelin' && template.name.toLowerCase() !== 'cookbook') {
78+
templateCategoryStrategy.setStrategy(new GenericStrategy())
79+
templateCategoryStrategy.switchScreen(dispatch)
7880
}
7981
}}
8082
onMouseEnter={(e) => {

0 commit comments

Comments
 (0)