All files / src/components/views/Schemas SchemaUsage.tsx

0% Statements 0/22
0% Branches 0/12
0% Functions 0/11
0% Lines 0/21

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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215                                                                                                                                                                                                                                                                                                                                                                                                                                             
import React, { useState } from "react";
import styled from "styled-components";
import { Link, Flex, Box, Text, Button } from "rimble-ui";
import { LinkIcon, Send } from "@rimble/icons";
import { SchemaDataResponse, SchemaDataInput } from "./types";
import { H5 } from "../../layouts/LayoutComponents";
import { fonts } from "../../../themes";
import { CopyableTruncatableText } from "../../elements/CopyableTruncatableText/CopyableTruncatableText";
import { HighlightedJson } from "../../elements/HighlightedJson/HighlightedJson";
import { Tabs } from "../../layouts/Tabs/Tabs";
import { ModalContent, ModalWithX } from "../../elements/Modals";
import { PopupGroup } from "../../elements/Popup/Popup";
import { getLdTypesFromSchemaResponse, getSchemaUris } from "./utils";
import { links } from "../../../config";
 
const StyledTabs = styled(Tabs)`
  li:first-child {
    margin-left: 0;
  }
  li:last-child {
    margin-right: 0;
  }
`;
 
const StyledLink: React.FC<any> = (props) => <Link fontSize={2} fontWeight={2} {...props} />;
 
export interface SchemaUsageProps {
  schema: SchemaDataResponse | SchemaDataInput;
  style?: any;
}
 
export const SchemaUsage: React.FunctionComponent<SchemaUsageProps> = (props) => {
  const { schema } = props;
  const [urlTab, setUrlTab] = useState("jsonLd");
  const [issueTab, setIssueTab] = useState("sertoAgent");
  const [infoModalOpen, setInfoModalOpen] = useState(false);
  const [exampleVcModalOpen, setExampleVcModalOpen] = useState(false);
 
  const uris = getSchemaUris(schema);
 
  const { subjectLdType, credLdType } = getLdTypesFromSchemaResponse(schema);
  // @TODO/tobek Need a more elegant way to do this, probably VcSchema library should have a utility that creates an example VC that actually has all the fields filled in based on schema.
  const exampleVc = {
    "@context": ["https://www.w3.org/2018/credentials/v1", ...(uris?.jsonLdContext ? [uris.jsonLdContext] : [])],
    ...(uris?.jsonSchema && {
      credentialSchema: {
        id: uris.jsonSchema,
        type: "JsonSchemaValidator2018",
      },
    }),
    type: ["VerifiableCredential", ...(credLdType ? [credLdType] : [])],
    issuer: "[ISSUER]",
    issuanceDate: "[DATE]",
    credentialSubject: {
      ...(subjectLdType ? { type: subjectLdType } : {}),
      id: "[SUBJECT]",
      "...": "...",
    },
  };
 
  return (
    <Box fontFamily={fonts.sansSerif} style={props.style}>
      <PopupGroup>
        <H5 mt={1} mb={2}>
          <Flex alignItems="center">
            <LinkIcon size="20px" />
            <Box ml={1}>Schema URLs</Box>
          </Flex>
        </H5>
        <StyledTabs
          activeTabName={urlTab}
          onTabClicked={(tabName) => setUrlTab(tabName)}
          titleFontSize={1}
          tabs={[
            {
              tabName: "jsonLd",
              title: "JSON-LD Context",
              content: (
                <Box mt={3} mb={2}>
                  <CopyableTruncatableText
                    fontFamily={fonts.monospace}
                    fontSize={0}
                    size="20px"
                    fontWeight={2}
                    text={uris?.jsonLdContext || "[none]"}
                    linkOutHref={!!uris?.jsonLdContext}
                  />
                  <StyledLink display="inline-block" fontSize={0} mt={2} onClick={() => setInfoModalOpen(true)}>
                    Learn more about JSON-LD Context
                  </StyledLink>
                </Box>
              ),
            },
            {
              tabName: "jsonSchema",
              title: "JSON Schema",
              content: (
                <Box mt={3} mb={2}>
                  <CopyableTruncatableText
                    fontFamily={fonts.monospace}
                    fontSize={0}
                    size="20px"
                    fontWeight={2}
                    text={uris?.jsonSchema || "[none]"}
                    linkOutHref={!!uris?.jsonSchema}
                  />
                  <StyledLink display="inline-block" fontSize={0} mt={2} onClick={() => setInfoModalOpen(true)}>
                    Learn more about JSON Schema
                  </StyledLink>
                </Box>
              ),
            },
          ]}
        />
      </PopupGroup>
 
      <PopupGroup>
        <H5 mt={1} mb={2}>
          <Flex alignItems="center">
            <Send size="18px" />
            <Box ml={1}>Issue Verifiable Credentials</Box>
          </Flex>
        </H5>
 
        <StyledTabs
          activeTabName={issueTab}
          onTabClicked={(tabName) => setIssueTab(tabName)}
          titleFontSize={1}
          tabs={[
            {
              tabName: "sertoAgent",
              title: "Use Serto Agent",
              content: (
                <Box mt={3} mb={2}>
                  <Text fontSize={0} my={2}>
                    Use Serto Agent, our user-friendly admin tool, to issue VCs.
                  </Text>
                  <Button.Outline as="a" size="small" width="100%" href={links.SUITE} target="_blank">
                    <Send size="15px" mr={2} />
                    Issue VC with Serto Agent
                  </Button.Outline>
                </Box>
              ),
            },
            {
              tabName: "manual",
              title: "Manually",
              content: (
                <Box mt={3} mb={2}>
                  <Text fontSize={0} my={2}>
                    Use your own agent, like{" "}
                    <StyledLink fontSize={0} href="https://veramo.io/" target="_blank">
                      Veramo
                    </StyledLink>
                    , to issue VCs manually.
                  </Text>
                  <Button.Outline size="small" width="100%" onClick={() => setExampleVcModalOpen(true)}>
                    View example VC for this schema
                  </Button.Outline>
                </Box>
              ),
            },
          ]}
        />
      </PopupGroup>
 
      <ModalWithX isOpen={infoModalOpen} close={() => setInfoModalOpen(false)} width={9}>
        <ModalContent onClick={(e: Event) => e.stopPropagation()}>
          <Text my={3}>
            <b>JSON-LD contexts</b> (
            <StyledLink href="https://w3c.github.io/json-ld-syntax/#the-context" target="_blank">
              specification
            </StyledLink>
            ) are JSON data that define data relationships and semantics, and can leverage property and entity types on
            schema repositories such as{" "}
            <StyledLink href="https://schema.org" target="_Blank">
              schema.org
            </StyledLink>
            .
          </Text>
          <Text my={3}>
            For more details on using JSON-LD contexts in VCs, see{" "}
            <StyledLink href="https://www.w3.org/TR/vc-data-model/#contexts" target="_blank">
              the contexts section of the VC spec
            </StyledLink>
            .
          </Text>
          <Text my={3}>
            <b>JSON Schema</b> (
            <StyledLink href="https://json-schema.org/" target="_blank">
              specification
            </StyledLink>
            ) schemas are also JSON data, but define data structure, requirements, descriptions and other metadata,
            thereby supporting validation of VCs to ensure conformance.
          </Text>
          <Text my={3}>
            For more details on using JSON Schemas in VCs, see{" "}
            <StyledLink href="https://www.w3.org/TR/vc-data-model/#data-schemas" target="_blank">
              the <code>credentialSchema</code> field in the VC spec
            </StyledLink>
            .
          </Text>
          <Text my={3}></Text>
        </ModalContent>
      </ModalWithX>
 
      <ModalWithX isOpen={exampleVcModalOpen} close={() => setExampleVcModalOpen(false)} width={10}>
        <ModalContent onClick={(e: Event) => e.stopPropagation()}>
          <HighlightedJson json={exampleVc} />
        </ModalContent>
      </ModalWithX>
    </Box>
  );
};