import React, { useCallback, useEffect, useState } from "react"; import { isActionOf } from "typesafe-actions"; import { FrameActions, FrameConnector, HostActionsHandler, updateHeight, updateTemplates, timeout } from "../../src"; import { css } from "@emotion/react"; import styled from "@emotion/styled"; type Document = { name: string; document: any; frameSource: string; }; interface AppProps { documents: Document[]; } interface ViewerProps { document: Document; } const TemplatesContainer = styled.div``; const ActionsContainer = styled.div` display: flex; justify-content: center; margin-top: 0.5rem; margin-bottom: 0.5rem; button { color: #fff; padding-left: 1rem; padding-right: 1rem; padding-top: 0.5rem; padding-bottom: 0.5rem; font-weight: 700; border-radius: 0.25rem; background-color: #4299e1; cursor: pointer; border: 0; } button:hover { background-color: #2b6cb0; } `; const DocumentsContainer = styled.div` width: 300px; padding: 0.5rem; .document { cursor: pointer; padding: 0.5rem; background-color: #ebf8ff; border-top: 4px solid #4299e2; margin-bottom: 0.5rem; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06); } .document.active { border-top-color: #38b2ac; background-color: #e6fffa; } `; const Viewer: React.FunctionComponent = ({ document }): React.ReactElement => { const [toFrame, setToFrame] = useState(); const [height, setHeight] = useState(250); const [templates, setTemplates] = useState<{ id: string; label: string }[]>([]); const [selectedTemplate, setSelectedTemplate] = useState(""); const fn = useCallback((toFrame: HostActionsHandler) => { // wrap into a function otherwise toFrame function will be executed setToFrame(() => toFrame); }, []); const fromFrame = (action: FrameActions): void => { if (isActionOf(updateHeight, action)) { setHeight(action.payload); } if (isActionOf(updateTemplates, action)) { setTemplates(action.payload); setSelectedTemplate(action.payload[0].id); } if (isActionOf(timeout, action)) { alert(`Connection timeout on renderer.\nPlease contact the administrator of ${document.frameSource}.`); } }; // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore window.renderDocument = (document) => { if (toFrame && document) { toFrame({ type: "RENDER_DOCUMENT", payload: { document, }, }); } }; useEffect(() => { if (toFrame && document) { toFrame({ type: "RENDER_DOCUMENT", payload: { document: document.document, }, }); } }, [toFrame, document]); useEffect(() => { if (toFrame && selectedTemplate) { toFrame({ type: "SELECT_TEMPLATE", payload: selectedTemplate, }); } }, [selectedTemplate, toFrame]); return (
{!document && (
Please select a document on the left bar
)}
    {templates.map((template) => (
  • setSelectedTemplate(template.id)} > {template.label}
  • ))}
); }; export const AppContainer: React.FunctionComponent = ({ documents }): React.ReactElement => { const [document, setDocument] = useState(); return (

Documents

{documents.length === 0 &&
Please configure the application and provide at least one document
} {documents.map((d, index) => { return (
setDocument(d)}> {d.name}
); })}
{document && }
); };