import { useEffect, useState } from 'react';
import { Viewer, DocumentGate, useDocument, useDocuments } from '@embedpdf/react/runtime';
import type { OpenInput } from '@embedpdf/react/runtime';
import { Stage, stagePlugin } from '@embedpdf/react/stage';
import { RenderLayer, renderPlugin } from '@embedpdf/react/render';
import { metadataPlugin, useMetadata, useMetadataState } from '@embedpdf/react/metadata';
import { localEngine } from '@embedpdf/engine';
import './layer.css';
const engine = localEngine();
const plugins = [stagePlugin(), renderPlugin(), metadataPlugin()];
// The original stays as it is: the document opens with a layer over it, and the changes go there.
const withLayer = async (layer?: Uint8Array): Promise => {
const response = await fetch('https://www.embedpdf.com/pdfs/welcome.pdf');
return {
kind: 'layerBytes',
baseBytes: new Uint8Array(await response.arrayBuffer()),
layer: layer ? { kind: 'artifact', bytes: layer } : { kind: 'fresh' },
};
};
function LayerBar({ reopened, onReopened }: { reopened: boolean; onReopened: () => void }) {
const documents = useDocuments();
const metadata = useMetadata();
const title = useMetadataState((state) => state.metadata?.title ?? '');
const { id } = useDocument();
const [layer, setLayer] = useState(null);
// A change on load, so the layer has something in it. Opened again, the title comes from it.
useEffect(() => {
if (!reopened) void metadata.update({ title: 'Reviewed by Dana' });
}, [metadata, reopened]);
// Only the changes, as bytes you could store next to the original.
const keepChanges = async () => setLayer(await documents.downloadLayer());
// Later: the original again, with the stored changes on top.
const openAgain = async () => {
if (!layer) return;
await documents.close(id);
onReopened();
await documents.open(() => withLayer(layer), { name: 'welcome.pdf' });
};
return (
<>
{reopened
? 'Opened again: the original, with the title from the stored layer.'
: layer
? `The layer holds the changes in ${layer.byteLength.toLocaleString()} bytes.`
: 'Change the title, then keep the changes.'}