diff --git a/packages/chakra-components/src/components/Election/SpreadsheetAccess.tsx b/packages/chakra-components/src/components/Election/SpreadsheetAccess.tsx index 72c63776..e688d963 100644 --- a/packages/chakra-components/src/components/Election/SpreadsheetAccess.tsx +++ b/packages/chakra-components/src/components/Election/SpreadsheetAccess.tsx @@ -13,9 +13,10 @@ import { import { useDisclosure } from '@chakra-ui/react-use-disclosure' import { ChakraProps, useMultiStyleConfig } from '@chakra-ui/system' import { useToast } from '@chakra-ui/toast' +import { Wallet } from '@ethersproject/wallet' import { errorToString, useClient, useElection, walletFromRow } from '@vocdoni/react-providers' import { AnonymousService, ArchivedElection, dotobject, VocdoniSDKClient } from '@vocdoni/sdk' -import { useState } from 'react' +import { useEffect, useState } from 'react' import { useForm } from 'react-hook-form' export const SpreadsheetAccess = (rest: ChakraProps) => { @@ -41,6 +42,31 @@ export const SpreadsheetAccess = (rest: ChakraProps) => { formState: { errors }, } = useForm() + const shouldRender = election?.get('census.type') === 'spreadsheet' && !(election instanceof ArchivedElection) + const privkey = window.location.hash ? window.location.hash.split('#')[1] : '' + + // In case of spreadsheet census and a private provided through the URI, do intent to login automatically + // Example url with private key: + // https://app.vocdoni.io/processes/0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef#0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef + useEffect(() => { + ;(async () => { + try { + if (!shouldRender || !privkey) return + const privKeyWallet = new Wallet(privkey) + let client = new VocdoniSDKClient({ + env, + wallet: privKeyWallet, + electionId: election?.id, + }) + setClient(client) + } catch (error) { + console.warn('Error trying to login with private key ', error) + setClient(cl) + } + })() + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [election, env, shouldRender, privkey]) + const onSubmit = async (vals: any) => { try { setLoading(true) @@ -115,7 +141,7 @@ export const SpreadsheetAccess = (rest: ChakraProps) => { message: localize('validation.min_length', { min: 8 }), } - if (election?.get('census.type') !== 'spreadsheet' || election instanceof ArchivedElection) return null + if (!shouldRender) return null if (connected) { return ( @@ -144,7 +170,7 @@ export const SpreadsheetAccess = (rest: ChakraProps) => { {errors[key]?.message?.toString()} ))} - {election.electionType.anonymous && ( + {election?.electionType.anonymous && ( {localize('spreadsheet.anon_sik_label')}