-
-
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
base: next
Are you sure you want to change the base?
Changes from 7 commits
aaf7860
2aa2cba
a1b2157
9d66e05
fe15863
1abc446
25ff38b
9f83cab
eae5aa6
87e537a
b0bc9a0
cf79b3b
78262e9
6287a1c
7d3d2e2
fd94c6a
b87aed7
39ce253
2737f75
07121fd
233ce57
f88c509
8f3096b
9f418bf
6778fd1
934fff4
32af617
02a957a
4c71375
47cdf92
466aeb1
72ff0b1
6d3daeb
c7b22d6
1be8e73
01a1a94
10ae938
9849a2c
4a3cb09
84207e0
2e2e10c
8452c4f
a55d5ef
8384ca3
02dbe24
0ee8837
b648d08
929d606
1154f17
5b7f83a
d114f96
30a5ec0
68d00a7
ed0f0df
d0586a1
b03dfb4
dbd8dd1
b8b13c5
a3b1da8
4413807
3fda18b
66416c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); | ||
slax57 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return ( | ||
<AppBar> | ||
<TitlePortal /> | ||
<Stack direction="row" spacing={1}> | ||
<Tooltip title={isOnline ? 'Online' : 'Offline'}> | ||
<CircleIcon | ||
slax57 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 ( | ||
slax57 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<> | ||
<Layout appBar={MyAppBar}> | ||
{children} | ||
<NotificationsFromQueryClient /> | ||
</Layout> | ||
<ReactQueryDevtools | ||
initialIsOpen={false} | ||
buttonPosition="bottom-left" | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
const useIsOnline = () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in my tests with Firefox, the app bar never shows the offline icon when setting the network dev tools to offline. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reproduced. However, I simply use react-query There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After further tests, it appears toggling network in the firefox devtool does not trigger the |
||
const [isOnline, setIsOnline] = React.useState(onlineManager.isOnline()); | ||
React.useEffect(() => { | ||
const handleChange = isOnline => { | ||
setIsOnline(isOnline); | ||
}; | ||
return onlineManager.subscribe(handleChange); | ||
}); | ||
slax57 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return isOnline; | ||
}; | ||
|
||
/** | ||
* When react-query resume persisted mutations through their default functions (provided in the getOfflineFirstQueryClient file) after the browser tab | ||
djhi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* 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([], { | ||
slax57 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
onSettled(data, error) { | ||
if (error) { | ||
notify(error.message, { type: 'error' }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will result in a doubled mutation with e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you actually try it? This effect only runs when there are persisted mutations to resume. For instance, create a new post with title |
||
} | ||
}, | ||
}); | ||
return null; | ||
}; |
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