From e05d3e53a2b663973adf2e02296b3a5bbfa8ee78 Mon Sep 17 00:00:00 2001 From: enes Date: Fri, 20 Sep 2024 10:26:32 +0200 Subject: [PATCH] fix: remove duplicate states and fix default signer --- src/components/DrawPDFFields/index.tsx | 107 +++++++------------------ src/pages/create/index.tsx | 64 ++++++++++++--- 2 files changed, 81 insertions(+), 90 deletions(-) diff --git a/src/components/DrawPDFFields/index.tsx b/src/components/DrawPDFFields/index.tsx index bad5d3a..076c6fb 100644 --- a/src/components/DrawPDFFields/index.tsx +++ b/src/components/DrawPDFFields/index.tsx @@ -11,17 +11,16 @@ import styles from './style.module.scss' import React, { useEffect, useState } from 'react' import { ProfileMetadata, User, UserRole } from '../../types' import { MouseState, PdfPage, DrawnField, DrawTool } from '../../types/drawing' -import { truncate } from 'lodash' -import { settleAllFullfilfedPromises, hexToNpub, npubToHex } from '../../utils' -import { getSigitFile, SigitFile } from '../../utils/file' +import { hexToNpub, npubToHex, getProfileUsername } from '../../utils' +import { SigitFile } from '../../utils/file' import { getToolboxLabelByMarkType } from '../../utils/mark' import { FileDivider } from '../FileDivider' import { ExtensionFileBox } from '../ExtensionFileBox' import { FONT_SIZE, FONT_TYPE, inPx } from '../../utils/pdf' import { useScale } from '../../hooks/useScale' import { AvatarIconButton } from '../UserAvatarIconButton' -import { LoadingSpinner } from '../LoadingSpinner' import { UserAvatar } from '../UserAvatar' +import _ from 'lodash' const DEFAULT_START_SIZE = { width: 140, @@ -29,22 +28,32 @@ const DEFAULT_START_SIZE = { } as const interface Props { - selectedFiles: File[] users: User[] metadata: { [key: string]: ProfileMetadata } - onDrawFieldsChange: (sigitFiles: SigitFile[]) => void + sigitFiles: SigitFile[] + setSigitFiles: React.Dispatch> selectedTool?: DrawTool } export const DrawPDFFields = (props: Props) => { - const { selectedFiles, selectedTool, onDrawFieldsChange, users } = props + const { selectedTool, sigitFiles, setSigitFiles, users } = props + const signers = users.filter((u) => u.role === UserRole.signer) const defaultSignerNpub = signers.length ? hexToNpub(signers[0].pubkey) : '' const [lastSigner, setLastSigner] = useState(defaultSignerNpub) - const { to, from } = useScale() + /** + * Return first pubkey that is present in the signers list + * @param pubkeys + * @returns available pubkey or empty string + */ + const getAvailableSigner = (...pubkeys: string[]) => { + const availableSigner: string | undefined = pubkeys.find((pubkey) => + signers.some((s) => s.pubkey === npubToHex(pubkey)) + ) + return availableSigner || '' + } - const [sigitFiles, setSigitFiles] = useState([]) - const [parsingPdf, setIsParsing] = useState(false) + const { to, from } = useScale() const [mouseState, setMouseState] = useState({ clicked: false @@ -64,33 +73,6 @@ export const DrawPDFFields = (props: Props) => { activeDrawnField?.pageIndex === pageIndex && activeDrawnField?.drawnFieldIndex === drawnFieldIndex - useEffect(() => { - if (selectedFiles) { - /** - * Reads the binary files and converts to internal file type - * and sets to a state (adds images if it's a PDF) - */ - const parsePages = async () => { - const files = await settleAllFullfilfedPromises( - selectedFiles, - getSigitFile - ) - - setSigitFiles(files) - } - - setIsParsing(true) - - parsePages().finally(() => { - setIsParsing(false) - }) - } - }, [selectedFiles]) - - useEffect(() => { - if (sigitFiles) onDrawFieldsChange(sigitFiles) - }, [onDrawFieldsChange, sigitFiles]) - /** * Drawing events */ @@ -136,7 +118,7 @@ export const DrawPDFFields = (props: Props) => { top: to(page.width, y), width: event.pointerType === 'mouse' ? 0 : DEFAULT_START_SIZE.width, height: event.pointerType === 'mouse' ? 0 : DEFAULT_START_SIZE.height, - counterpart: lastSigner, + counterpart: getAvailableSigner(lastSigner, defaultSignerNpub), type: selectedTool.identifier } @@ -518,7 +500,7 @@ export const DrawPDFFields = (props: Props) => { Counterpart