-
Notifications
You must be signed in to change notification settings - Fork 3
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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' |
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 +1,2 @@ | ||
export * from './uuid' | ||
export * from './formatSchemaName' |
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
122 changes: 122 additions & 0 deletions
122
packages/toolbox-ui/src/contexts/CredentialFormatDataProvider.tsx
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,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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
.There was a problem hiding this comment.
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