Skip to content

Commit

Permalink
Merge pull request #296 from us3r-network/F-composites-bufan
Browse files Browse the repository at this point in the history
start index while add composite
  • Loading branch information
sin-bufan authored Jan 29, 2024
2 parents 81f027c + 0ce70c1 commit 17e14c6
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 34 deletions.
2 changes: 1 addition & 1 deletion packages/client/dashboard/src/api/composite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function getComposites({
pageSize?: number
pageNumber?: number
network?: Network
published?:boolean
published?: boolean
}): AxiosPromise<ApiResp<DappCompositeDto[]>> {
let host = APP_API_URL

Expand Down
19 changes: 19 additions & 0 deletions packages/client/dashboard/src/api/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,25 @@ export function startIndexModel({
})
}

export function startIndexModels({
modelIds,
network,
didSession,
}:{
didSession: string,
modelIds: string[],
network: Network }) {
if (!modelIds || modelIds.length===0) return Promise.reject()
const resp = Promise.all(modelIds.map((modelId: string) => {
return startIndexModel({
modelId,
network,
didSession
})
}))
return resp
}

export function getStreamsCountWithModels({
network,
modelStreamIds,
Expand Down
63 changes: 35 additions & 28 deletions packages/client/dashboard/src/components/model/CompositeEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,53 @@
import { Composite } from '@composedb/devtools'
import { RuntimeCompositeDefinition } from '@composedb/types'
import { GraphQLEditor, PassedSchema } from 'graphql-editor'
import { useEffect, useMemo, useState } from 'react'
import { useEffect, useState } from 'react'
import styled from 'styled-components'
import { useCeramicNodeCtx } from '../../context/CeramicNodeCtx'
import { getRuntimeDefinitionFromEncodedComposite } from '../../utils/composeDBUtils'
import { schemas } from '../../utils/composedb-types/schemas'
import CodeDownload from './CodeDownload'

export default function CompositeEditor({
export default function CompositeEditor ({
schema,
encodedDefinition,
encodedDefinition
}: {
schema?: string
encodedDefinition: string
}) {
const { currCeramicNode } = useCeramicNodeCtx()
const [gqlSchema, setGqlSchema] = useState<PassedSchema>({
code: schema || '',
libraries: schemas.library,
libraries: schemas.library
})

const runtimeDefinition = useMemo(() => {
if (!encodedDefinition) {
return null
}
try {
const composite = Composite.from(JSON.parse(encodedDefinition))
return composite.toRuntime()
} catch (error) {
return null
}
}, [encodedDefinition])
const [runtimeDefinition, setRuntimeDefinition] = useState<RuntimeCompositeDefinition|null>(null)

useEffect(() => {
if (!encodedDefinition) return
if (!currCeramicNode?.serviceUrl) return
getRuntimeDefinitionFromEncodedComposite(
encodedDefinition,
currCeramicNode?.serviceUrl
).then(
(result)=>{
setRuntimeDefinition(result)
}
)
}, [currCeramicNode?.serviceUrl, encodedDefinition])


useEffect(() => {
setGqlSchema({
code: schema || '',
libraries: schemas.library,
libraries: schemas.library
})
}, [schema])

return (
<Box>
<EditorBox>
<GraphQLEditor
setSchema={(props) => {
setSchema={props => {
setGqlSchema(props)
}}
schema={gqlSchema}
Expand All @@ -49,19 +56,19 @@ export default function CompositeEditor({
<ResultBox>
{encodedDefinition && (
<CodeDownload
title="Composite"
downloadContent={encodedDefinition}
downloadFileName="composite.json"
content={encodedDefinition}
title='Composite Encoded Definition'
downloadContent={JSON.stringify(encodedDefinition)}
downloadFileName='composite.json'
content={JSON.stringify(encodedDefinition,null,2)}
/>
)}
{runtimeDefinition && (
<CodeDownload
title="Runtime Definition"
title='Composite Runtime Definition'
downloadContent={`// This is an auto-generated file, do not edit manually
export const definition = ${runtimeDefinition}`}
downloadFileName="runtime-composite.js"
content={JSON.stringify(runtimeDefinition)}
export const definition = ${JSON.stringify(runtimeDefinition)}`}
downloadFileName='runtime-composite.js'
content={JSON.stringify(runtimeDefinition,null,2)}
/>
)}
</ResultBox>
Expand Down Expand Up @@ -106,8 +113,8 @@ const EditorBox = styled.div`
`

const ResultBox = styled.div`
display: grid;
grid-template-columns: 1fr 1fr;
display: flex;
flex-direction: column;
gap: 20px;
margin-top: 20px;
> div {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { useCallback, useState } from 'react'
import styled from 'styled-components'
import { createDappComposites } from '../../api/composite'
import useSelectedDapp from '../../hooks/useSelectedDapp'
import { ModelStream } from '../../types.d'
import { schemas } from '../../utils/composedb-types/schemas'
import CloseIcon from '../icons/CloseIcon'
import { createCompositeFromBrowser } from '../../utils/createCompositeFromBrowser'
import { createCompositeFromBrowser } from '../../utils/composeDBUtils'
import { useCeramicNodeCtx } from '../../context/CeramicNodeCtx'
import { startIndexModels } from '../../api/model'

export default function CreateCompositeModal ({
closeModal,
Expand Down Expand Up @@ -65,6 +65,13 @@ export default function CreateCompositeModal ({
if (resp.data.code !== 0) {
throw new Error(resp.data.msg)
}
const modelIds = composite.modelIDs
await startIndexModels({
modelIds,
network: selectedDapp.network,
didSession: session.serialize()
}).catch(console.error)

if (loadDappComposites) await loadDappComposites()
closeModal()
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useAppCtx } from '../../context/AppCtx'
import { useCeramicNodeCtx } from '../../context/CeramicNodeCtx'
import useSelectedDapp from '../../hooks/useSelectedDapp'
import { schemas } from '../../utils/composedb-types/schemas'
import { createCompositeFromBrowser } from '../../utils/createCompositeFromBrowser'
import { createCompositeFromBrowser } from '../../utils/composeDBUtils'
import CloseIcon from '../icons/CloseIcon'
import { createDappModels } from '../../api/model'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default function DappModelEditor () {
</div>
<CompositeEditor
schema={selectComposite.graphql}
encodedDefinition={selectComposite.composite}
encodedDefinition={JSON.parse(selectComposite.composite)}
/>
</Box>
)
Expand Down
19 changes: 18 additions & 1 deletion packages/client/dashboard/src/container/ExploreComposite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { bindingDappComposites, getComposites } from '../api/composite'
import { Dapps } from './ExploreModel'
import CreateCompositeModal from '../components/model/CreateCompositeModal'
import LayoutIcon from '../components/icons/LayoutIcon'
import { startIndexModels } from '../api/model'

export default function ExploreComposite () {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down Expand Up @@ -274,7 +275,23 @@ function Actions ({
compositeId: composite.id,
dapp: selectedDapp,
did: session.serialize()
}).catch(console.error)
})
.then(
() => {
const models = JSON.parse(composite.composite).models
console.log(Object.keys(models))
const modelIds = Object.keys(models)
startIndexModels({
modelIds,
network: selectedDapp.network,
didSession: session.serialize()
}).catch(console.error)
},
err => {
console.error(err)
}
)
.catch(console.error)
}
try {
setAdding(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,29 @@ export async function createCompositeFromBrowser(
}

return { composite: myComposite, runtimeDefinition: myRuntimeDefinition }
}


export async function getRuntimeDefinitionFromEncodedComposite(
encodedDefinition: string,
myCeramicNode: string = '',
) {
if (!encodedDefinition) {
return null
}
try {
const ceramic = new CeramicClient(myCeramicNode)
const composite = await Composite.fromJSON(
{
definition: JSON.parse(JSON.stringify(encodedDefinition)),
ceramic,
})
// console.log('encoded definition: ', encodedDefinition)
// console.log('composite: ', composite)
// console.log('runtime definition:', composite.toRuntime())
return composite.toRuntime()
} catch (error) {
console.log('get runtime error', error)
return null
}
}

0 comments on commit 17e14c6

Please sign in to comment.