-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Add support for offline/local first applications #10545
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
Open
djhi
wants to merge
12
commits into
next
Choose a base branch
from
support-offline-mode
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
aaf7860
Add support for offline/local first applications
djhi 2aa2cba
Ensure no existing tests are broken
djhi a1b2157
Try stabilizing tests
djhi 9d66e05
Fix e2e tests
djhi fe15863
Introduce `addOfflineSupportToQueryClient`
djhi 1abc446
Add documentation
djhi 25ff38b
Trigger build
djhi 9f83cab
Merge remote-tracking branch 'origin/next' into support-offline-mode
slax57 eae5aa6
fix merge issues
slax57 87e537a
Merge branch 'next' into support-offline-mode
djhi b0bc9a0
Cleanup documentation
djhi cf79b3b
Fix e2e tests
djhi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,21 +1,80 @@ | ||
import * as React from 'react'; | ||
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'; | ||
import { AppBar, Layout, InspectorButton, TitlePortal } from 'react-admin'; | ||
import { | ||
AppBar, | ||
Layout, | ||
InspectorButton, | ||
TitlePortal, | ||
useNotify, | ||
} from 'react-admin'; | ||
import { onlineManager, useQueryClient } from '@tanstack/react-query'; | ||
import { Stack, Tooltip } from '@mui/material'; | ||
import CircleIcon from '@mui/icons-material/Circle'; | ||
import '../assets/app.css'; | ||
|
||
const MyAppBar = () => ( | ||
<AppBar> | ||
<TitlePortal /> | ||
<InspectorButton /> | ||
</AppBar> | ||
); | ||
const MyAppBar = () => { | ||
const isOnline = useIsOnline(); | ||
return ( | ||
<AppBar> | ||
<TitlePortal /> | ||
<Stack direction="row" spacing={1}> | ||
<Tooltip title={isOnline ? 'Online' : 'Offline'}> | ||
<CircleIcon | ||
sx={{ | ||
color: isOnline ? 'success.main' : 'warning.main', | ||
width: 24, | ||
height: 24, | ||
}} | ||
/> | ||
</Tooltip> | ||
</Stack> | ||
<InspectorButton /> | ||
</AppBar> | ||
); | ||
}; | ||
|
||
export default ({ children }) => ( | ||
<> | ||
<Layout appBar={MyAppBar}>{children}</Layout> | ||
<ReactQueryDevtools | ||
initialIsOpen={false} | ||
buttonPosition="bottom-left" | ||
/> | ||
</> | ||
); | ||
export default ({ children }) => { | ||
return ( | ||
<> | ||
<Layout appBar={MyAppBar}> | ||
{children} | ||
<NotificationsFromQueryClient /> | ||
</Layout> | ||
<ReactQueryDevtools | ||
initialIsOpen={false} | ||
buttonPosition="bottom-left" | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
const useIsOnline = () => { | ||
const [isOnline, setIsOnline] = React.useState(onlineManager.isOnline()); | ||
React.useEffect(() => { | ||
const handleChange = isOnline => { | ||
setIsOnline(isOnline); | ||
}; | ||
return onlineManager.subscribe(handleChange); | ||
}); | ||
|
||
return isOnline; | ||
}; | ||
|
||
/** | ||
* When react-query resume persisted mutations through their default functions (provided in the getOfflineFirstQueryClient file) after the browser tab | ||
* has been closed, it cannot handle their side effects unless we set up some defaults. In order to leverage the react-admin notification system | ||
* we add a default onSettled function to the mutation defaults here. | ||
*/ | ||
const NotificationsFromQueryClient = () => { | ||
const queryClient = useQueryClient(); | ||
const notify = useNotify(); | ||
|
||
queryClient.setMutationDefaults([], { | ||
onSettled(data, error) { | ||
if (error) { | ||
notify(error.message, { type: 'error' }); | ||
} | ||
}, | ||
}); | ||
return null; | ||
}; |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because react-query now persist queries and mutations for offline mode, the previous test now leaks into the second (e.g. this post has its title changed to Lorem Ipsum). I tried to configure testIsolation in Cypress but our version is probably too old