All files / src/components/views/Credentials Credential.tsx

0% Statements 0/15
0% Branches 0/29
0% Functions 0/3
0% Lines 0/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155                                                                                                                                                                                                                                                                                                                     
import { useState } from "react";
import { Box, Button, Flex, Table } from "rimble-ui";
import { VC } from "vc-schema-tools";
import {
  CredentialFooter,
  CredentialDetailsTDLeft,
  CredentialDetailsTDRight,
  CredentialTR,
} from "./CredentialComponents";
import { config } from "../../../config";
import { baseColors, colors } from "../../../themes";
import { dateTimeFormat, truncateDid } from "../../../utils";
import { CopyToClipboard } from "../../elements";
import { useVcSchema } from "../../../services/useVcSchema";
import { CredentialHeader } from "./CredentialHeader";
import { CredentialProperty } from "./CredentialProperty";
import { ViewSchemaButton } from "../Schemas/ViewSchemaButton";
import { RENDER_CONTEXT } from "../../../context";
 
export interface CredentialProps {
  vc: VC;
  isOpen?: boolean;
  renderContext?: RENDER_CONTEXT;
}
 
export const Credential: React.FunctionComponent<CredentialProps> = (props) => {
  const { vc } = props;
  const { vcSchema } = useVcSchema(vc);
  const vcType = vc.type[vc.type.length - 1];
  const issuanceDate =
    typeof vc.issuanceDate === "number"
      ? dateTimeFormat(new Date(vc.issuanceDate * 1000))
      : dateTimeFormat(new Date(vc.issuanceDate));
  const issuanceDateFormatted = `${issuanceDate.dateFormatted} at ${issuanceDate.timeFormatted}`;
  const expirationDate = vc.expirationDate && dateTimeFormat(new Date(vc.expirationDate));
  const expirationDateFormatted =
    expirationDate && `${expirationDate.dateFormatted} at ${expirationDate.timeFormatted}`;
  const expired = vc.expirationDate && new Date(vc.expirationDate) < new Date(Date.now());
  const issuer = typeof vc.issuer === "string" ? vc.issuer : vc.issuer.id;
  const vcUrl = vc.proof?.jwt && `${config.SEARCH_UI_URL}/vc-validator?vc=${vc.proof.jwt}`;
  const [isOpen, setIsOpen] = useState<boolean>(props.isOpen || false);
 
  return (
    <Box
      bg={baseColors.white}
      border={2}
      borderRadius={2}
      boxShadow={1}
      maxWidth="480px"
      mb={3}
      overflow="hidden"
      width="100%"
    >
      <CredentialHeader issuer={issuer} vc={vc} vcType={vcType} vcSchema={vcSchema} />
      {isOpen && (
        <>
          <Box borderBottom={2} mb={3} pb={1} pt={2} px={3}>
            <Table border={0} boxShadow={0} mb={2} width="100%" style={{ tableLayout: "fixed" }}>
              <tbody>
                {Object.entries(vc.credentialSubject).map(
                  ([key, value]) =>
                    // "type" is a reserved keyword in JSON-LD, and `credentialSubject.type` is just used to define what semantic type of linked data the VC subject is, and we present that info elsewhere as the schema, so would be redundant to list it. Some issuers include `credentialSubject.type` in every single VC.
                    key !== "type" && (
                      <CredentialProperty
                        key={key}
                        keyName={key}
                        value={value}
                        schema={vcSchema?.parsedJsonSchema.properties?.credentialSubject?.properties?.[key]}
                      />
                    ),
                )}
              </tbody>
            </Table>
          </Box>
          <Box mb={3} px={3}>
            <Table border={0} boxShadow={0} mb={3} width="100%" style={{ tableLayout: "fixed" }}>
              <tbody>
                {vc.credentialSubject.id && (
                  <CredentialTR>
                    <CredentialDetailsTDLeft>Subject</CredentialDetailsTDLeft>
                    <CredentialDetailsTDRight>
                      <Flex alignItem="center" justifyContent="flex-end">
                        {truncateDid(vc.credentialSubject.id)}
                        <Box ml={1}>
                          <CopyToClipboard text={vc.credentialSubject.id} size="16px" />
                        </Box>
                      </Flex>
                    </CredentialDetailsTDRight>
                  </CredentialTR>
                )}
                <CredentialTR>
                  <CredentialDetailsTDLeft>Issuer</CredentialDetailsTDLeft>
                  <CredentialDetailsTDRight>
                    <Flex alignItem="center" justifyContent="flex-end">
                      {truncateDid(issuer)}
                      <Box ml={1}>
                        <CopyToClipboard text={issuer} size="16px" />
                      </Box>
                    </Flex>
                  </CredentialDetailsTDRight>
                </CredentialTR>
                <CredentialTR>
                  <CredentialDetailsTDLeft>Date Issued</CredentialDetailsTDLeft>
                  <CredentialDetailsTDRight>{issuanceDateFormatted}</CredentialDetailsTDRight>
                </CredentialTR>
                {expirationDate && (
                  <CredentialTR>
                    <CredentialDetailsTDLeft color={expired ? colors.danger.base : colors.silver}>
                      Expires
                    </CredentialDetailsTDLeft>
                    <CredentialDetailsTDRight>{expirationDateFormatted}</CredentialDetailsTDRight>
                  </CredentialTR>
                )}
              </tbody>
            </Table>
          </Box>
          {vcSchema?.slug && (
            <Box borderTop={2} mb={3} px={3} pt={3}>
              <Table border={0} boxShadow={0} mb={3} width="100%" style={{ tableLayout: "fixed" }}>
                <tbody>
                  <CredentialTR>
                    <CredentialDetailsTDLeft>Schema</CredentialDetailsTDLeft>
                    <CredentialDetailsTDRight>
                      <ViewSchemaButton schema={vcSchema}>
                        <u>{vcSchema.slug}</u>
                      </ViewSchemaButton>
                    </CredentialDetailsTDRight>
                  </CredentialTR>
                </tbody>
              </Table>
            </Box>
          )}
          {vcUrl && props.renderContext !== RENDER_CONTEXT.SEARCH && (
            <Box my={4} px={3}>
              <Button.Outline as="a" href={vcUrl} size="small" target="_blank" width="100%">
                Verify on Serto Search
              </Button.Outline>
            </Box>
          )}
        </>
      )}
      <Box pb={1} px={2} pt={2}>
        <CredentialFooter
          expired={expired}
          isOpen={isOpen}
          renderContext={props.renderContext}
          setIsOpen={(isOpen) => setIsOpen(isOpen)}
          vc={vc}
          vcUrl={vcUrl}
        />
      </Box>
    </Box>
  );
};