-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Timo Glastra <[email protected]>
- Loading branch information
1 parent
21c144a
commit 359d058
Showing
19 changed files
with
1,242 additions
and
971 deletions.
There are no files selected for viewing
3 changes: 2 additions & 1 deletion
3
apps/easypid/src/app/(app)/credentials/requestedAttributes.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
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
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
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
58 changes: 43 additions & 15 deletions
58
apps/easypid/src/hooks/useCredentialsWithCustomDisplay.ts
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,25 +1,53 @@ | ||
import { type CredentialForDisplay, useCredentialsForDisplay } from 'packages/agent/src' | ||
import { type CredentialForDisplay, type CredentialForDisplayId, useCredentialsForDisplay } from '@package/agent' | ||
import { useMemo } from 'react' | ||
import { type PidSdJwtVcAttributes, usePidCredential } from './usePidCredential' | ||
|
||
type CustomCredentialForDisplay = CredentialForDisplay & { | ||
attributesForDisplay: PidSdJwtVcAttributes | ||
metadataForDisplay: Record<string, unknown> | ||
attributesForDisplay?: PidSdJwtVcAttributes | ||
metadataForDisplay?: Record<string, unknown> | ||
} | ||
|
||
export const useCredentialsWithCustomDisplay = () => { | ||
const credentials = useCredentialsForDisplay() | ||
const pidCredential = usePidCredential() | ||
export const useCredentialsWithCustomDisplayById = (id: CredentialForDisplayId) => { | ||
const { credentials, isLoading } = useCredentialsWithCustomDisplay(true) | ||
|
||
// replace PID credential with custom one | ||
const index = credentials.credentials.findIndex( | ||
(credential) => credential.metadata.type === pidCredential.credential?.type | ||
) | ||
if (index !== -1 && pidCredential.credential) { | ||
credentials.credentials[index] = pidCredential.credential as CredentialForDisplay | ||
return { | ||
// NOTE: we support both the prefixed id and non-prefixed. We should fix the input id | ||
// but for now this is the easiest approach | ||
credential: useMemo(() => credentials.find((c) => c.id === id || c.id.endsWith(id)), [id, credentials]), | ||
isLoading, | ||
} | ||
} | ||
|
||
export const useCredentialsWithCustomDisplay = (includeHiddenCredentials = false) => { | ||
const { credentials, isLoading: isLoadingCredentialsForDisplay } = useCredentialsForDisplay() | ||
const { | ||
pidCredentialForDisplay: pidCredential, | ||
credentialIds: pidCredentialIds, | ||
credentials: pidCredentials, | ||
isLoading: isLoadingPidCredential, | ||
} = usePidCredential() | ||
|
||
const filteredCredentials = useMemo(() => { | ||
const pidIndex = credentials.findIndex((c) => c.id === pidCredential?.id) | ||
const withoutPids = credentials | ||
.filter((c) => !pidCredentialIds?.includes(c.id)) | ||
.map((c) => ({ ...c, isPid: false })) | ||
|
||
// No pids, just return | ||
if (pidIndex === -1 || !pidCredential) return withoutPids | ||
|
||
// Insert only one of the pids | ||
return [ | ||
...withoutPids.slice(0, pidIndex), | ||
...(includeHiddenCredentials | ||
? pidCredentials.map((p) => ({ ...p, isPid: true })) | ||
: [{ ...pidCredential, isPid: true }]), | ||
...withoutPids.slice(pidIndex), | ||
] | ||
}, [credentials, pidCredential, pidCredentials, pidCredentialIds, includeHiddenCredentials]) | ||
|
||
return credentials as { | ||
credentials: CustomCredentialForDisplay[] | ||
isLoading: boolean | ||
return { | ||
credentials: filteredCredentials as CustomCredentialForDisplay[], | ||
isLoading: isLoadingCredentialsForDisplay || isLoadingPidCredential, | ||
} | ||
} |
Oops, something went wrong.