Skip to content

Commit

Permalink
fix: Typing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Crash-- committed Nov 14, 2023
1 parent a242ff4 commit 8dd23f3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
26 changes: 19 additions & 7 deletions src/components/KonnectorTile.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const statusMap = {
* @param {boolean} props.isInMaintenance Is in maintenance?
* @param {number} props.accountsCount Number of Accounts
* @param {boolean} props.error isInError
* @param {boolean} props.userError user error
* @param {string?} props.userError user error
* @param {boolean} props.loading Loading status
* @returns {number} The status
*/
Expand Down Expand Up @@ -99,26 +99,36 @@ function getErrorForTriggers(triggers, jobs) {
}
return null
}
/**
*
* @param {import('cozy-client/types/types').IOCozyAccount[]} accounts
* @param {import('cozy-client/types/types').IOCozyTrigger[]} triggers
* @returns
*/
const getAccountsFromTrigger = (accounts, triggers) => {
const triggerAccountIds = triggers.map(trigger => trigger.message.account)
const matchingAccounts = Object.values(accounts).filter(account =>
triggerAccountIds.includes(account.id)
)
return matchingAccounts
}

/**
*
* @param {import('cozy-client/types/types').IOCozyTrigger[]} triggers
* @returns
*/
const getFirstUserError = triggers => {
const triggersInError = Object.values(triggers).filter(
t => t.current_state?.status === 'errored'
)
const firstTriggerHavingUserError = Object.values(triggersInError).find(
trigger => {
const e = new KonnectorJobError(trigger.current_state.last_error)
const e = new KonnectorJobError(trigger.current_state?.last_error)
const isUserError = e.isUserError()
return isUserError
}
)
return firstTriggerHavingUserError
return firstTriggerHavingUserError?.current_state
? firstTriggerHavingUserError.current_state.last_error
: null
}
Expand All @@ -132,11 +142,14 @@ const getFirstUserError = triggers => {
*/
export const KonnectorTile = ({ konnector, isInMaintenance, loading }) => {
const allTriggers =
// @ts-ignore
useSelector(state => state.cozy.documents['io.cozy.triggers']) || {}
const triggers = getTriggersBySlug(allTriggers, konnector.slug)
const userError = getFirstUserError(triggers, konnector.slug)
const userError = getFirstUserError(triggers)
// @ts-ignore
const jobs = useSelector(state => state.cozy.documents['io.cozy.jobs']) || {}
const accounts =
// @ts-ignore
useSelector(state => state.cozy.documents['io.cozy.accounts']) || {}
const accountsForKonnector = getAccountsFromTrigger(accounts, triggers)
const error = getErrorForTriggers(triggers, jobs)
Expand All @@ -155,8 +168,7 @@ export const KonnectorTile = ({ konnector, isInMaintenance, loading }) => {
loading,
userError
})
const errorToDisplay =
userError !== true && userError !== null ? userError : error
const errorToDisplay = !userError && userError !== null ? userError : error
return (
<NavLink
to={konnector.slug}
Expand Down
5 changes: 2 additions & 3 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
declare module 'cozy-harvest-lib' {
export const handleOAuthResponse: () => boolean
}
declare module 'cozy-harvest-lib'
declare module 'cozy-ui/transpiled/react/providers/Breakpoints'
declare module 'cozy-ui/transpiled/react/Spinner'
declare module 'cozy-ui/transpiled/react/SquareAppIcon'

0 comments on commit 8dd23f3

Please sign in to comment.