Skip to content

Commit

Permalink
fix: Get triggers current_state efficiently
Browse files Browse the repository at this point in the history
We used to query triggers with their `current_state` at Home startup. We
removed it for performances purposes, but now it is only made when the
focus event is fired. This means we can miss this state when entering a
konnector group, which is required to correctly display the icons.
So, we query the triggers when required, and mutualize it with the focus
queries to benefit from the redux store.
  • Loading branch information
paultranvan committed Jan 15, 2025
1 parent c9f8817 commit 80d945e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
9 changes: 9 additions & 0 deletions src/components/Sections/SectionView.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useState, useRef } from 'react'
import cx from 'classnames'

import { useQuery } from 'cozy-client'
import type { IOCozyKonnector } from 'cozy-client/types/types'
import useBreakpoints from 'cozy-ui/transpiled/react/providers/Breakpoints'
import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n'
Expand All @@ -17,6 +18,7 @@ import {
} from 'components/Sections/SectionsTypes'
import { useSections } from './SectionsContext'
import CandidateServiceTile from 'components/CandidateServiceTile'
import { makeTriggersWithJobStatusQuery } from 'queries'

export const SectionBody = ({ section }: SectionViewProps): JSX.Element => {
const { isMobile } = useBreakpoints()
Expand All @@ -26,6 +28,13 @@ export const SectionBody = ({ section }: SectionViewProps): JSX.Element => {
const shouldOpenStoreModal = section.type === 'category' && section.pristine
const { isRunning, isInMaintenance } = useSections()

// This query is useful to fetch the triggers' current_state, used to display
// the konnector icon. The data is fetched from the store in KonnectorTile
useQuery(
makeTriggersWithJobStatusQuery.definition(),
makeTriggersWithJobStatusQuery.options
)

return (
<div
className={cx(
Expand Down
17 changes: 10 additions & 7 deletions src/containers/ReloadFocus.jsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import React from 'react'
import PropTypes from 'prop-types'

import { withClient, Q } from 'cozy-client'
import {
makeTriggersWithJobStatusQuery,
makeAppsQuery,
makeJobsQuery
} from 'queries'
import { withClient } from 'cozy-client'
class RealoadFocus extends React.Component {
static contextTypes = {
store: PropTypes.object
}
componentDidMount() {
const client = this.props.client
window.addEventListener('focus', () => {
client.query(Q('io.cozy.jobs'))
client.query(makeJobsQuery.definition(), makeJobsQuery.options)
client.query(
Q('io.cozy.triggers').where({
worker: { $in: ['client', 'konnector'] }
})
makeTriggersWithJobStatusQuery.definition(),
makeTriggersWithJobStatusQuery.options
)
client.query(Q('io.cozy.apps'))
client.query(makeAppsQuery.definition(), makeAppsQuery.options)
})
}

Expand Down
24 changes: 23 additions & 1 deletion src/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ export const konnectorsConn = {
fetchPolicy: defaultFetchPolicy
}

export const makeJobsQuery = {
definition: () => Q('io.cozy.jobs'),
options: {
as: 'io.cozy.jobs',
fetchPolicy: defaultFetchPolicy
}
}

export const makeAppsQuery = {
definition: () => Q('io.cozy.apps'),
options: {
Expand All @@ -39,7 +47,21 @@ export const makeTriggersQuery = {
.limitBy(1000)
},
options: {
as: 'io.cozy.triggers/worker=konnector',
as: 'io.cozy.triggers/worker=client-or-konnector',
fetchPolicy: defaultFetchPolicy
}
}

export const makeTriggersWithJobStatusQuery = {
definition: () => {
return Q('io.cozy.triggers')
.where({
worker: { $in: ['client', 'konnector'] } // client is for CLISK
})
.limitBy(1000)
},
options: {
as: 'io.cozy.triggers/worker=client-or-konnector/withjobstatus',
fetchPolicy: defaultFetchPolicy
}
}
Expand Down

0 comments on commit 80d945e

Please sign in to comment.