Skip to content

Commit e48ee9c

Browse files
committed
feat: Allow service worker / offline mode to be toggled via web component attri
1 parent fa58dcb commit e48ee9c

4 files changed

Lines changed: 15 additions & 1 deletion

File tree

src/components/SaveButton/SaveButton.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const SaveButton = ({ className, type, fill = false }) => {
2121
const webComponent = useSelector((state) => state.editor.webComponent);
2222
const user = useSelector((state) => state.auth.user);
2323
const project = useSelector((state) => state.editor.project);
24+
const offlineEnabled = useSelector((state) => state.editor.offlineEnabled);
2425
const isOnline = useIsOnline();
2526

2627
useEffect(() => {
@@ -41,7 +42,7 @@ const SaveButton = ({ className, type, fill = false }) => {
4142

4243
if (loading !== "success" || projectOwner || !buttonType) return null;
4344

44-
if (!isOnline && !user) {
45+
if (offlineEnabled && !isOnline && !user) {
4546
return (
4647
<div className={classNames(className, "offline-badge")}>
4748
<OfflineIcon />

src/containers/WebComponentLoader.jsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useSelector, useDispatch } from "react-redux";
33
import {
44
disableTheming,
55
setSenseHatAlwaysEnabled,
6+
setOfflineEnabled,
67
setLoadRemixDisabled,
78
setReactAppApiEndpoint,
89
setScratchApiEndpoint,
@@ -75,6 +76,7 @@ const WebComponentLoader = (props) => {
7576
withSidebar = false,
7677
loadCache = true, // Always use cache unless explicitly disabled
7778
initialProject = null,
79+
offlineEnabled = false,
7880
} = props;
7981
const dispatch = useDispatch();
8082

@@ -199,6 +201,10 @@ const WebComponentLoader = (props) => {
199201
dispatch(setReadOnly(readOnly));
200202
}, [readOnly, dispatch]);
201203

204+
useEffect(() => {
205+
dispatch(setOfflineEnabled(offlineEnabled));
206+
}, [offlineEnabled, dispatch]);
207+
202208
useEffect(() => {
203209
// Create a script element to save the existing Prism object if there is one
204210
const script = document.createElement("script");

src/redux/EditorSlice.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export const editorInitialState = {
112112
lastSaveAutosave: false,
113113
lastSavedTime: null,
114114
senseHatAlwaysEnabled: false,
115+
offlineEnabled: false,
115116
senseHatEnabled: false,
116117
loadRemixDisabled: false,
117118
betaModalShowing: false,
@@ -282,6 +283,9 @@ export const EditorSlice = createSlice({
282283
setSenseHatAlwaysEnabled: (state, action) => {
283284
state.senseHatAlwaysEnabled = action.payload;
284285
},
286+
setOfflineEnabled: (state, action) => {
287+
state.offlineEnabled = action.payload;
288+
},
285289
setSenseHatEnabled: (state, action) => {
286290
state.senseHatEnabled = action.payload;
287291
},
@@ -515,6 +519,7 @@ export const {
515519
setReadOnly,
516520
setInstructionsEditable,
517521
setSenseHatAlwaysEnabled,
522+
setOfflineEnabled,
518523
setSenseHatEnabled,
519524
setLoadRemixDisabled,
520525
setReactAppApiEndpoint,

src/web-component.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class WebComponent extends HTMLElement {
7979
"with_projectbar",
8080
"with_sidebar",
8181
"load_cache",
82+
"offline_enabled",
8283
];
8384
}
8485

@@ -97,6 +98,7 @@ class WebComponent extends HTMLElement {
9798
"with_projectbar",
9899
"with_sidebar",
99100
"load_cache",
101+
"offline_enabled",
100102
];
101103
const jsonAttrs = [
102104
"instructions",

0 commit comments

Comments
 (0)