Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Voting: avoid crashing when external app details are not loadable #1066

Merged
merged 4 commits into from
Jan 18, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/voting/app/src/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect } from 'react'
import React, { useCallback } from 'react'
import {
Button,
Header,
Expand Down
2 changes: 1 addition & 1 deletion apps/voting/app/src/app-logic.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useMemo, useState } from 'react'
import React, { useCallback, useMemo } from 'react'
import { AragonApi, useApi, useAppState, usePath } from '@aragon/api-react'
import appStateReducer from './app-state-reducer'
import { EMPTY_CALLSCRIPT } from './evmscript-utils'
Expand Down
4 changes: 0 additions & 4 deletions apps/voting/app/src/components/NewVotePanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import {
useSidePanelFocusOnReady,
} from '@aragon/ui'

const initialState = {
question: '',
}

const NewVotePanel = React.memo(function NewVotePanel({
panelState,
onCreateVote,
Expand Down
5 changes: 4 additions & 1 deletion apps/voting/app/src/components/VoteCard/VoteOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ class VoteOptions extends React.Component {

const percentages =
votingPower > 0
? percentageList(options.map(o => o.power / votingPower), 2)
? percentageList(
options.map(o => o.power / votingPower),
2
)
: [0, 0]

return (
Expand Down
1 change: 0 additions & 1 deletion apps/voting/app/src/hooks/usePanelState.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useCallback, useMemo, useState } from 'react'
import { noop } from '../utils'

// Handles the state of a panel.
export default function usePanelState() {
Expand Down
19 changes: 10 additions & 9 deletions apps/voting/app/src/hooks/useVotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ function useDecoratedVotes() {
`Voting: vote #${vote.voteId} does not list any execution targets. The app's cache is likely corrupted and needs to be reset.`
)
} else if (!executionTargets.length) {
// If there's no execution target, consider it targetting this Voting app
targetApp = {
...currentApp,
// Don't attach an identifier for this Voting app
identifier: undefined,
}
// If there's no execution target, consider it targeting this Voting app
targetApp = { ...currentApp }
// Don't attach an identifier for this Voting app
delete targetApp.identifier
} else if (executionTargets.length > 1) {
// If there's multiple targets, make a "multiple" version
targetApp = {
Expand All @@ -53,11 +51,12 @@ function useDecoratedVotes() {
if (targetApp) {
const { appAddress, icon, identifier, name } = targetApp
executionTargetData = {
identifier,
address: appAddress,
name,
// If the app name was not loaded, use the app's address
name: name || appAddress,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if we should use the shortened version here?

Copy link
Contributor Author

@sohkai sohkai Jan 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, let's do that! Done in ccb6e2b.

// Only try to get the icon if it's available
iconSrc: typeof icon === 'function' ? icon(24) : null,
identifier,
}
}

Expand All @@ -80,7 +79,9 @@ function useDecoratedVotes() {
identifier,
name,
}))
.sort((a, b) => a.name.localeCompare(b.name))
.sort((a, b) => {
return a.name ? a.name.localeCompare(b.name) : 1
})

return [decoratedVotes, executionTargets]
}, [votes, connectedAccountVotes, currentApp, installedApps])
Expand Down
25 changes: 16 additions & 9 deletions apps/voting/app/src/screens/Votes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { useMemo } from 'react'
import {
Bar,
DropDown,
Expand Down Expand Up @@ -47,10 +47,14 @@ const Votes = React.memo(function Votes({
const { layoutName } = useLayout()
const { openVotes, closedVotes } = useVotes(filteredVotes)

const multipleOfTarget = executionTargets.reduce((map, { name }) => {
map.set(name, map.has(name))
return map
}, new Map())
const multipleOfTarget = useMemo(
() =>
executionTargets.reduce((map, { appAddress }) => {
map.set(appAddress, map.has(appAddress))
return map
}, new Map()),
[executionTargets]
)

return (
<React.Fragment>
Expand Down Expand Up @@ -121,9 +125,9 @@ const Votes = React.memo(function Votes({
'All',
<ThisVoting showTag={multipleOfTarget.get('Voting')} />,
...executionTargets.map(
({ name, identifier }) =>
`${name}${
multipleOfTarget.get(name) && identifier
({ appAddress, name, identifier }) =>
`${name || appAddress}${
multipleOfTarget.get(appAddress) && identifier
? ` (${identifier})`
: ''
}`
Expand Down Expand Up @@ -175,7 +179,10 @@ const ThisVoting = ({ showTag }) => (
)

const VoteGroups = React.memo(({ openVotes, closedVotes, onSelectVote }) => {
const voteGroups = [['Open votes', openVotes], ['Closed votes', closedVotes]]
const voteGroups = [
['Open votes', openVotes],
['Closed votes', closedVotes],
]

return (
<React.Fragment>
Expand Down