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

0% Statements 0/29
0% Branches 0/21
0% Functions 0/14
0% Lines 0/29

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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
import { useState } from "react";
import { VC } from "vc-schema-tools";
import { Code, FileDownload, KeyboardArrowDown, KeyboardArrowUp, OpenInNew } from "@rimble/icons";
import { Box, Button, Flex, Link, Text } from "rimble-ui";
import {
  CopyToClipboard,
  ExpiredPill,
  HoverPathStroke,
  HoverSvgFill,
  ModalWithX,
  QrCode,
  UnstyledButton,
} from "../../elements";
import { H3 } from "../../layouts";
import { RENDER_CONTEXT } from "../../../context";
import { baseColors, fonts, colors } from "../../../themes";
import { useVcSchema } from "../../../services/useVcSchema";
import { CredentialShareButton } from "./CredentialShare";
import { SocialMediaVerify } from "./CredentialShare/SocialMediaVerify";
import { ShareViaQr } from "./CredentialShare/ShareViaQr";
 
export interface CredentialFooterProps {
  vc: VC;
  vcUrl?: string;
  expired: any;
  isOpen: boolean;
  renderContext?: RENDER_CONTEXT;
  setIsOpen(isOpen: boolean): void;
}
 
export const CredentialFooter: React.FunctionComponent<CredentialFooterProps> = (props) => {
  return (
    <Flex alignItems="center" justifyContent="space-between">
      <Flex>
        <CredentialJsonDownload vc={props.vc} />
        {props.renderContext !== RENDER_CONTEXT.VC_EMBED && (
          <>
            <Box mr={2}>
              <CredentialShareButton vc={props.vc} />
            </Box>
            {props.vc.proof?.jwt && <CredentialEmbed jwt={props.vc.proof.jwt} />}
          </>
        )}
        {props.vcUrl && props.vc.proof?.jwt && (
          <>
            {props.renderContext !== RENDER_CONTEXT.VC_EMBED && <QRCodeExpand vcUrl={props.vcUrl} />}
            <CopyToClipboard
              color={colors.midGray}
              iconPadding={1}
              hoverTitle="Copy Token"
              size="15px"
              text={props.vc.proof.jwt}
            />
            <Box mt="1px" ml={2} p="3px">
              <SocialMediaVerify vc={props.vc} vcUrl={props.vcUrl} />
            </Box>
          </>
        )}
      </Flex>
      {props.expired && <ExpiredPill />}
      {props.renderContext !== RENDER_CONTEXT.VC_EMBED ? (
        <Flex
          alignItems="center"
          color={colors.silver}
          fontSize={0}
          fontFamily={fonts.sansSerif}
          onClick={() => props.setIsOpen(!props.isOpen)}
          style={{ cursor: "pointer" }}
        >
          {props.isOpen ? (
            <>
              Collapse
              <KeyboardArrowUp color={colors.midGray} ml={1} size="18px" />
            </>
          ) : (
            <>
              Expand
              <KeyboardArrowDown color={colors.midGray} ml={1} size="18px" />
            </>
          )}
        </Flex>
      ) : (
        <>
          {props.vcUrl && (
            <Button.Text as="a" href={props.vcUrl} size="small" target="_blank">
              <Flex alignItems="center">
                Verify on Serto Search <OpenInNew color={colors.primary.base} ml={1} size="18px" />
              </Flex>
            </Button.Text>
          )}
        </>
      )}
    </Flex>
  );
};
 
export interface CredentialEmbedProps {
  jwt: string;
}
 
export const CredentialEmbed: React.FunctionComponent<CredentialEmbedProps> = (props) => {
  const [isEmbedModalOpen, setIsEmbedModalOpen] = useState(false);
  const embedHtml = `<iframe src="https://search.serto.id/vc-embed?vc=${props.jwt}" sandbox="allow-popups allow-same-origin allow-scripts" style="border: none;" width="480px" height="160px"></iframe>`;
 
  return (
    <>
      <UnstyledButton onClick={() => setIsEmbedModalOpen(true)} mr={2}>
        <Box p={1} title="View Embed Code">
          <HoverSvgFill>
            <Code color={colors.midGray} size="18px" />
          </HoverSvgFill>
        </Box>
      </UnstyledButton>
      <ModalWithX borderRadius={2} isOpen={isEmbedModalOpen} close={() => setIsEmbedModalOpen(false)}>
        <Box borderRadius={2} maxWidth="450px" pb={3} px={3}>
          <H3 mt={0}>Embed Credential</H3>
          <Box position="relative">
            <Box position="absolute" right={3} top={3} zIndex={1}>
              <CopyToClipboard text={embedHtml} />
            </Box>
            <Box
              bg={colors.nearWhite}
              borderRadius={1}
              maxHeight="300px"
              p={3}
              style={{ overflow: "scroll", overflowWrap: "break-word" }}
            >
              <code style={{ fontSize: "12px" }}>{embedHtml}</code>
            </Box>
          </Box>
        </Box>
      </ModalWithX>
    </>
  );
};
 
export interface QRCodeExpandProps {
  vcUrl: string;
}
 
export const QRCodeExpand: React.FunctionComponent<QRCodeExpandProps> = (props) => {
  const [isQrModalOpen, setIsQrModalOpen] = useState(false);
  return (
    <>
      <UnstyledButton onClick={() => setIsQrModalOpen(true)} mr={2}>
        <Box p={1} title="View QR Code">
          <HoverPathStroke>
            <QrCode color={colors.midGray} size="18px" />
          </HoverPathStroke>
        </Box>
      </UnstyledButton>
      <ModalWithX borderRadius={2} isOpen={isQrModalOpen} close={() => setIsQrModalOpen(false)}>
        <Box bg={baseColors.white} borderRadius={2} maxWidth="450px" pb={5} px={6}>
          <Text color={colors.midGray} my={3} textAlign="center">
            Scan to Verify on Serto Search
          </Text>
          <ShareViaQr url={props.vcUrl} />
        </Box>
      </ModalWithX>
    </>
  );
};
 
export interface DownloadCredentialJsonProps {
  vc: VC;
}
 
export const CredentialJsonDownload: React.FunctionComponent<DownloadCredentialJsonProps> = (props) => {
  const { vcSchema } = useVcSchema(props.vc);
  const fileName = vcSchema?.slug ? vcSchema.slug + ".json" : "credential.json";
  const credential = "text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(props.vc));
  return (
    <Link href={"data:" + credential} download={fileName} mr={2} p={1} title="Download Credential JSON">
      <HoverSvgFill>
        <FileDownload color={colors.midGray} size="18px" />
      </HoverSvgFill>
    </Link>
  );
};
 
export const CredentialTR: React.FunctionComponent = (props) => {
  return (
    <tr
      style={{
        height: "auto",
      }}
    >
      {props.children}
    </tr>
  );
};
 
export const CredentialTDRight: React.FunctionComponent = (props) => {
  return (
    <td
      style={{
        border: "none",
        padding: "5px 0",
        textAlign: "right",
        verticalAlign: "top",
        wordBreak: "break-word",
      }}
    >
      <Text.span color={baseColors.black} fontFamily={fonts.sansSerif} fontSize={1} lineHeight="copy">
        {props.children}
      </Text.span>
    </td>
  );
};
 
export const CredentialTDLeft: React.FunctionComponent = (props) => {
  return (
    <td
      style={{
        border: "none",
        padding: "5px 0",
        verticalAlign: "top",
      }}
    >
      <Text.span color={colors.silver} fontFamily={fonts.sansSerif} fontSize={1} lineHeight="copy">
        {props.children}
      </Text.span>
    </td>
  );
};
 
export const CredentialDetailsTDRight: React.FunctionComponent = (props) => {
  return (
    <td
      style={{
        border: "none",
        padding: "3px 0",
        textAlign: "right",
        verticalAlign: "top",
        wordBreak: "break-word",
      }}
    >
      <Text.span color={colors.silver} fontFamily={fonts.sansSerif} fontSize={0} lineHeight="copy">
        {props.children}
      </Text.span>
    </td>
  );
};
 
export interface CredentialDetailsTDLeftProps {
  color?: string;
}
 
export const CredentialDetailsTDLeft: React.FunctionComponent<CredentialDetailsTDLeftProps> = (props) => {
  return (
    <td
      style={{
        border: "none",
        padding: "3px 0",
        verticalAlign: "top",
      }}
    >
      <Text.span color={props.color || colors.silver} fontFamily={fonts.sansSerif} fontSize={0} lineHeight="copy">
        {props.children}
      </Text.span>
    </td>
  );
};