Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added formatting for the credentials and added the schema name in the credential table #45

Merged
merged 3 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/toolbox-core/src/utils/formatSchemaName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const formatSchemaName = (schemaId?: string) =>
schemaId
? schemaId
.split(':')[2]
.replace(/([a-z])([A-Z])/g, '$1 $2')
.split(/-|_| /g)
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ')
: 'Unknown credential'
1 change: 1 addition & 0 deletions packages/toolbox-core/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './uuid'
export * from './formatSchemaName'
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { ConnectionRecord, CredentialExchangeRecord } from '@aries-framework/core'

import { formatSchemaName } from '@animo/toolbox-core/src/utils'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you import everything from the root, export everything from the main index.ts.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created a issue to improve this #47

import { CredentialsUtil } from '@animo/toolbox-core/src/utils/records/CredentialsUtil'
import { Badge, Group, ScrollArea, Table, Text, useMantineTheme } from '@mantine/core'
import React from 'react'

import { useCredentialsFormatData } from '../../contexts/CredentialFormatDataProvider'
import { RecordActions } from '../RecordActions'
import { SmartAvatar } from '../SmartAvatar'

Expand All @@ -17,13 +19,14 @@ interface CredentialsTableProps {

export const CredentialsTable = ({ records, connections, onDelete, onAccept, onDecline }: CredentialsTableProps) => {
const theme = useMantineTheme()
const { formattedData } = useCredentialsFormatData()

return (
<ScrollArea>
<Table verticalSpacing="sm">
<thead>
<tr>
<th>Issuer</th>
<th>Credential</th>
<th>Credential Id</th>
<th>State</th>
<th />
Expand All @@ -32,6 +35,7 @@ export const CredentialsTable = ({ records, connections, onDelete, onAccept, onD
<tbody>
{records.map((record) => {
const connection = connections.find((connection) => connection.id == record.connectionId)
const formattedCredential = formattedData.find((data) => data.id === record.id)
const isLoading = CredentialsUtil.isCredentialWaitingForResponse(record)
const isWaitingForAccept = CredentialsUtil.isCredentialWaitingForAcceptInput(record)
const isWaitingForDecline = CredentialsUtil.isCredentialWaitingForDeclineInput(record)
Expand All @@ -44,7 +48,7 @@ export const CredentialsTable = ({ records, connections, onDelete, onAccept, onD
{connection?.theirLabel}
</SmartAvatar>
<Text size="sm" weight={500}>
{connection?.theirLabel}
{formatSchemaName(formattedCredential?.offer?.indy?.schema_id)}
</Text>
</Group>
</td>
Expand Down
4 changes: 3 additions & 1 deletion packages/toolbox-ui/src/contexts/AgentContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { agentInitializer } from '@animo/toolbox-core/src/agent/AgentInitializer
import AgentProvider from '@aries-framework/react-hooks'
import React, { useEffect, useState } from 'react'

import CredentialFormatDataProvider from './CredentialFormatDataProvider'

interface AgentContextProps {
agentRecord?: IAgentRecord
agentDependenciesProvider: IAgentDependenciesProvider
Expand Down Expand Up @@ -42,7 +44,7 @@ export const AgentContext: React.FunctionComponent<PropsWithChildren & AgentCont

return (
<AgentProvider key={activeAgent?.id} agent={activeAgent?.agent}>
{children}
<CredentialFormatDataProvider agent={activeAgent?.agent}>{children}</CredentialFormatDataProvider>
</AgentProvider>
)
}
122 changes: 122 additions & 0 deletions packages/toolbox-ui/src/contexts/CredentialFormatDataProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import type { Agent, GetFormatDataReturn, IndyCredentialFormat } from '@aries-framework/core'
import type { PropsWithChildren } from 'react'

import { CredentialExchangeRecord } from '@aries-framework/core'
import {
recordsAddedByType,
recordsRemovedByType,
recordsUpdatedByType,
} from '@aries-framework/react-hooks/build/recordUtils'
import { useState, createContext, useContext, useEffect } from 'react'
import * as React from 'react'

type FormattedData = GetFormatDataReturn<[IndyCredentialFormat]> & {
id: string
}

type FormattedDataState = {
formattedData: Array<FormattedData>
loading: boolean
}

const addRecord = (record: FormattedData, state: FormattedDataState): FormattedDataState => {
const newRecordsState = [...state.formattedData]
newRecordsState.unshift(record)
return {
loading: state.loading,
formattedData: newRecordsState,
}
}

const updateRecord = (record: FormattedData, state: FormattedDataState): FormattedDataState => {
const newRecordsState = [...state.formattedData]
const index = newRecordsState.findIndex((r) => r.id === record.id)
if (index > -1) {
newRecordsState[index] = record
}
return {
loading: state.loading,
formattedData: newRecordsState,
}
}

const removeRecord = (record: FormattedData, state: FormattedDataState): FormattedDataState => {
const newRecordsState = state.formattedData.filter((r) => r.id !== record.id)
return {
loading: state.loading,
formattedData: newRecordsState,
}
}

const CredentialFormatDataContext = createContext<FormattedDataState | undefined>(undefined)

export const useCredentialsFormatData = () => {
const credentialFormatDataContext = useContext(CredentialFormatDataContext)
if (!credentialFormatDataContext) {
throw new Error('useCredentialFormatData must be used within a CredentialFormatDataContextProvider')
}
return credentialFormatDataContext
}

export const useCredentialFormatDataById = (id: string): FormattedData | undefined => {
const { formattedData } = useCredentialsFormatData()
return formattedData.find((c) => c.id === id)
}

interface Props {
agent?: Agent
}

const CredentialFormatDataProvider: React.FC<PropsWithChildren<Props>> = ({ agent, children }) => {
const [state, setState] = useState<{
formattedData: Array<FormattedData>
loading: boolean
}>({
formattedData: [],
loading: true,
})

const setInitialState = async () => {
if (agent) {
const records = await agent.credentials.getAll()
const formattedData: Array<FormattedData> = []
for (const record of records) {
const formatData = await agent.credentials.getFormatData(record.id)
formattedData.push({ ...formatData, id: record.id })
}
setState({ formattedData, loading: false })
}
}

useEffect(() => {
setInitialState()
}, [agent])

useEffect(() => {
if (!state.loading) {
const credentialAdded$ = recordsAddedByType(agent, CredentialExchangeRecord).subscribe(async (record) => {
const formatData = await agent!.credentials.getFormatData(record.id)
setState(addRecord({ ...formatData, id: record.id }, state))
})

const credentialUpdate$ = recordsUpdatedByType(agent, CredentialExchangeRecord).subscribe(async (record) => {
const formatData = await agent!.credentials.getFormatData(record.id)
setState(updateRecord({ ...formatData, id: record.id }, state))
})

const credentialRemove$ = recordsRemovedByType(agent, CredentialExchangeRecord).subscribe((record) =>
setState(removeRecord(record, state))
)

return () => {
credentialAdded$.unsubscribe()
credentialUpdate$.unsubscribe()
credentialRemove$.unsubscribe()
}
}
}, [state, agent])

return <CredentialFormatDataContext.Provider value={state}>{children}</CredentialFormatDataContext.Provider>
}

export default CredentialFormatDataProvider