Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add context to store Intent data
Like that, Harvest knows if it was called from an intent or not. And can use the data to do what it wants. I don't think this is a good idea to have this knowledge or having to check if Harvest is called inside an intent or not, we should not have to check that at this level. But ATM, I don't know yet how to handle the opening of Drive when called from an intent. If we just use a link then this is the intent that changes the URL, not the caller. And just changing this intent url, then we just close the intent and thats it. One solution can have to call intent.redirect() for instance, but we need to check if we are in an intent or not. So I don't like it neither. For now, I just target blank because it fixes my issue. BREAKING CHANGE: If you import Harvest's component without using the HarvestWrapper or the Route Component, you have to wrap manually the component in the IntentProvider.
- Loading branch information
Showing
5 changed files
with
65 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/cozy-harvest-lib/src/components/Providers/IntentProvider.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React, { createContext } from 'react' | ||
|
||
const IntentProviderContext = createContext() | ||
|
||
/** | ||
* @param {object} param | ||
* @param {JSX.Element} param.children | ||
* @param {object=} param.intentData | ||
* @param {string=} param.intentId | ||
* @returns | ||
*/ | ||
export const IntentProvider = ({ children, intentData, intentId }) => { | ||
return ( | ||
<IntentProviderContext.Provider value={{ intentData, intentId }}> | ||
{children} | ||
</IntentProviderContext.Provider> | ||
) | ||
} | ||
|
||
export const useIntentProviderData = () => { | ||
const intentProviderData = React.useContext(IntentProviderContext) | ||
if (!intentProviderData) { | ||
throw new Error( | ||
'useIntentProviderData must be used within a IntentProvider' | ||
) | ||
} | ||
|
||
return intentProviderData | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters