Skip to content

Commit

Permalink
Merge pull request #301 from us3r-network/F-quickStartGuide-bufan
Browse files Browse the repository at this point in the history
F-quickStartGuide-bufan
  • Loading branch information
sin-bufan authored Feb 4, 2024
2 parents 26dc58f + 1f31207 commit 0aec8e8
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ import {
import InfiniteScroll from 'react-infinite-scroll-component'
import styled from 'styled-components'
import { bindingDappComposites, getComposites } from '../../api/composite'
import { updateDapp } from '../../api/dapp'
import { PAGE_SIZE, S3_SCAN_URL } from '../../constants'
import { useAppCtx } from '../../context/AppCtx'
import { useCeramicNodeCtx } from '../../context/CeramicNodeCtx'
import useSelectedDapp from '../../hooks/useSelectedDapp'
import { CeramicStatus, DappCompositeDto, Network } from '../../types.d'
import { DappCompositeDto, Network } from '../../types.d'
import { shortPubKey } from '../../utils/shortPubKey'
import { TableBox, TableContainer } from '../common/TableBox'
import CheckCircleIcon from '../icons/CheckCircleIcon'
Expand All @@ -26,14 +25,11 @@ import CreateCompositeModal from './CreateNewComposite'
import { Dapps } from '../model/ExploreModelList'
import NoCeramicNodeModal from '../node/NoCeramicNodeModal'
import { startIndexModelsFromBrowser } from '../../utils/composeDBUtils'
import { updateDapp } from '../../api/dapp'
import { difference, uniq } from 'lodash'

export function CompositeList ({
searchText
}: {
searchText?: string
}) {
export function CompositeList ({ searchText }: { searchText?: string }) {
const { selectedDapp } = useSelectedDapp()
const { currCeramicNode } = useCeramicNodeCtx()
const [composites, setComposites] = useState<Array<DappCompositeDto>>([])
const [hasMore, setHasMore] = useState(true)
const pageNum = useRef(1)
Expand Down Expand Up @@ -130,20 +126,7 @@ export function CompositeList ({
</td>
<td>
{/* <OpsBtns modelId={item.stream_id} /> */}
<Actions
composite={item}
hasIndexed={
item.dapps?.findIndex(
dapp => dapp.id === selectedDapp?.id
) !== -1
}
ceramicNodeId={
currCeramicNode &&
currCeramicNode.status === CeramicStatus.RUNNING
? currCeramicNode?.id
: undefined
}
/>
<Actions composite={item} />
</td>
</tr>
)
Expand All @@ -157,62 +140,66 @@ export function CompositeList ({
)
}

function Actions ({
composite,
hasIndexed,
ceramicNodeId
}: {
composite: DappCompositeDto
hasIndexed: boolean
ceramicNodeId?: number
}) {
function Actions ({ composite }: { composite: DappCompositeDto }) {
const session = useSession()
const { currCeramicNode } = useCeramicNodeCtx()
const { loadDapps } = useAppCtx()
const { loadDapps, loadCurrDapp } = useAppCtx()
const { selectedDapp } = useSelectedDapp()
const [adding, setAdding] = useState(false)

const bindComposite = useCallback(async () => {
if (!session || !selectedDapp) return
if (!currCeramicNode) return
if (!hasIndexed) {
bindingDappComposites({
try {
setAdding(true)
const compsiteModels = JSON.parse(composite.composite).models
const modelIds = Object.keys(compsiteModels)
const dappModels = selectedDapp.models || []
const newModels = difference(modelIds,dappModels)
if (newModels.length > 0) {
console.log('start indexing new models in composite on private node')
startIndexModelsFromBrowser(
modelIds,
selectedDapp.network,
currCeramicNode.serviceUrl + '/',
currCeramicNode.privateKey
).then((result) => {
console.log('indexd models on private node:', result)
})
.catch(err => {
console.error(err)
})
console.log('store new models in composite of dapp to server')
const models = uniq([...dappModels, ...newModels])
await updateDapp(
{ ...selectedDapp, models },
session.serialize()
// ceramicNodeId
)
}
console.log('store composite of dapp to server')
await bindingDappComposites({
compositeId: composite.id,
dapp: selectedDapp,
did: session.serialize()
})
.then(
() => {
const models = JSON.parse(composite.composite).models
const modelIds = Object.keys(models)
startIndexModelsFromBrowser(
modelIds,
selectedDapp.network,
currCeramicNode.serviceUrl + '/',
currCeramicNode.privateKey
)
},
err => {
console.error(err)
}
)
.catch(console.error)
}
try {
setAdding(true)
const composites = selectedDapp.composites || []
composites.push(composite)
await updateDapp(
{ ...selectedDapp, composites },
session.serialize(),
)
console.log('reload dapps')
await loadDapps()
await loadCurrDapp()
} catch (err) {
console.error(err)
} finally {
setAdding(false)
}
}, [session, selectedDapp, currCeramicNode, hasIndexed, composite, loadDapps])
}, [
session,
selectedDapp,
currCeramicNode,
composite.id,
composite.composite,
loadDapps,
loadCurrDapp
])
return (
<OpsBox>
<DialogTrigger>
Expand Down Expand Up @@ -240,18 +227,14 @@ function Actions ({
</button>
) : (
<>
{hasIndexed ? (
<button disabled>
{selectedDapp?.composites?.map(c=>c.id).includes(composite.id) ? (
<button disabled title='This composite has been added to Dapp'>
<CheckCircleIcon />
</button>
) : ceramicNodeId ? (
) : currCeramicNode ? (
<button
disabled={!ceramicNodeId}
title={
ceramicNodeId
? 'Add this model to Dapp'
: 'There is no available node now, Deploy a private node first!'
}
disabled={!currCeramicNode}
title='Add this model to Dapp'
onClick={() => {
bindComposite()
}}
Expand Down
68 changes: 34 additions & 34 deletions packages/client/dashboard/src/components/model/ExploreModelList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import {
import InfiniteScroll from 'react-infinite-scroll-component'
import styled from 'styled-components'
import { updateDapp } from '../../api/dapp'
import { CERAMIC_MAINNET_HOST, CERAMIC_TESTNET_HOST, PAGE_SIZE } from '../../constants'
import {
getModelStreamList,
getModelsInfoByIds,
} from '../../api/model'
CERAMIC_MAINNET_HOST,
CERAMIC_TESTNET_HOST,
PAGE_SIZE
} from '../../constants'
import { getModelStreamList, getModelsInfoByIds } from '../../api/model'
import { ImgOrName } from '../common/ImgOrName'
import { TableBox, TableContainer } from '../common/TableBox'
import CheckCircleIcon from '../icons/CheckCircleIcon'
Expand Down Expand Up @@ -56,7 +57,7 @@ export default function ModelList ({
}
return new S3ModelCollectionModel(CERAMIC_TESTNET_HOST, 'testnet')
}, [selectedDapp])

const fetchPersonalCollections = useCallback(async () => {
if (!session || !s3ModelCollection) return
try {
Expand Down Expand Up @@ -263,7 +264,6 @@ export default function ModelList ({
{/* <OpsBtns modelId={item.stream_id} /> */}
<Actions
stream_id={item.stream_id}
hasIndexed={false}
hasStarItem={hasStarItem}
fetchPersonal={fetchPersonalCollections}
/>
Expand Down Expand Up @@ -361,12 +361,10 @@ const DappBox = styled.div`
`

function Actions ({
stream_id,
hasStarItem,
fetchPersonal,
stream_id,
hasIndexed,
}: {
hasIndexed: boolean
stream_id: string
hasStarItem:
| {
Expand Down Expand Up @@ -396,29 +394,30 @@ function Actions ({
async (modelId: string) => {
if (!session || !selectedDapp) return
if (!currCeramicNode) return
console.log('currCeramicNode', currCeramicNode, hasIndexed)
if (!hasIndexed) {
// await startIndexModel({
// modelId,
// network: selectedDapp.network as Network,
// didSession: session.serialize()
// }).catch(console.error)
await startIndexModelsFromBrowser(
[modelId],
selectedDapp.network as Network,
currCeramicNode.serviceUrl + '/',
currCeramicNode.privateKey,
)
}
const models = selectedDapp.models || []
if (models.includes(modelId)) return
setAdding(true)
try {
setAdding(true)
const models = selectedDapp.models || []
console.log('start indexing on private node')
startIndexModelsFromBrowser(
[modelId],
selectedDapp.network as Network,
currCeramicNode.serviceUrl + '/',
currCeramicNode.privateKey
).then((result) => {
console.log('indexd models on private node:', result)
})
.catch(err => {
console.error(err)
})
console.log('store models of dapp to server')
models.push(modelId)
await updateDapp(
{ ...selectedDapp, models },
session.serialize(),
session.serialize()
// ceramicNodeId
)
console.log('reload dapps')
await loadDapps()
await loadCurrDapp()
} catch (err) {
Expand All @@ -427,7 +426,13 @@ function Actions ({
setAdding(false)
}
},
[session, selectedDapp, hasIndexed, loadDapps, loadCurrDapp,currCeramicNode]
[
session,
selectedDapp,
loadDapps,
loadCurrDapp,
currCeramicNode
]
)

const collectModel = useCallback(
Expand Down Expand Up @@ -496,17 +501,12 @@ function Actions ({
) : (
<>
{selectedDapp?.models?.includes(modelId) ? (
<button disabled>
<button disabled title='This model has been added to Dapp'>
<CheckCircleIcon />
</button>
) : currCeramicNode ? (
<button
disabled={!currCeramicNode}
title={
currCeramicNode
? 'Add this model to Dapp'
: 'There is no available node now, Deploy a private node first!'
}
title='Add this model to Dapp'
onClick={() => {
addModelToDapp(modelId)
}}
Expand Down
47 changes: 15 additions & 32 deletions packages/client/dashboard/src/utils/composeDBUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Ed25519Provider } from 'key-did-provider-ed25519'
import { getResolver } from 'key-did-resolver'
import { fromString } from 'uint8arrays/from-string'
import { Network } from '../types'
import { StreamID } from '@ceramicnetwork/streamid'

export async function createCompositeFromBrowser(
graphql: string,
Expand Down Expand Up @@ -130,7 +131,7 @@ export async function startIndexModelsFromBrowser(
console.error('Please specify a CeramicNodeAdminPrivateKey')
return
}
console.log('Start index models on private node: ', models, network, myCeramicNode)

// 0 Login
console.log('Connecting to the ceramic node: ', myCeramicNode)
const ceramic = new CeramicClient(myCeramicNode)
Expand Down Expand Up @@ -166,41 +167,23 @@ export async function startIndexModelsFromBrowser(
return
}

//1 Create My Composite
let myComposite: Composite
try {
console.log('Creating the composite...')
myComposite = await Composite.fromModels({
ceramic: ceramic as unknown as CeramicApi,
models,
index: false,
})
console.log(`Creating the composite... Done! The encoded representation:`)
console.log(myComposite)
} catch (e) {
console.error((e as Error).message)
return
}

//2 Deploy My Composite
if (myCeramicNodeAdminPrivateKey)
// 1 Start indexing models on private node
console.log('Start index models on private node: ', models, network, myCeramicNode)
const indexedModels = []
for await (const m of models) {
const modelStreamId = StreamID.fromString(m)
try {
console.log('Deploying the composite...')
// Notify the Ceramic node to index the models present in the composite
await myComposite.startIndexingOn(ceramic as unknown as CeramicApi)
// Logging the model stream IDs to stdout, so that they can be piped using standard I/O or redirected to a file
console.log(
JSON.stringify(Object.keys(myComposite.toParams().definition.models))
)
console.log(`Deploying the composite... Done!`)
} catch (e) {
console.error((e as Error).message)
return
console.log(`Index models, stream id:${m}`);
const res = await ceramic.admin.startIndexingModels([modelStreamId]);
console.log(`Indexed model: ${m}.`, res);
indexedModels.push(m)
} catch (error) {
console.error(`Add model ${m} index err: ${error}`);
}
return myComposite
}
return indexedModels
}


export async function getRuntimeDefinitionFromEncodedComposite(
encodedDefinition: string,
myCeramicNode: string = '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export const extraScalars: Record<string, ScalarWithTitle> = {
...getGraphQLScalarSchema<JSONSchema.String>('timezone'),
maxLength: 100,
},
URI: { ...getGraphQLScalarSchema<JSONSchema.String>('uri'), maxLength: 100 },
// URI: { ...getGraphQLScalarSchema<JSONSchema.String>('uri'), maxLength: 100 },
UTCOffset: {
...getGraphQLScalarSchema<JSONSchema.String>('utcoffset'),
maxLength: 100,
Expand Down

0 comments on commit 0aec8e8

Please sign in to comment.