Skip to content

Commit 4797e03

Browse files
authored
feat: auto save on share (#278)
1 parent a0bdd0d commit 4797e03

File tree

3 files changed

+45
-19
lines changed

3 files changed

+45
-19
lines changed

src/components/Header.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,18 @@ function Header({
9292

9393
<Modal>
9494
<ModalOpenButton>
95-
<MenuLink as="button" disabled={!gistId}>
95+
<MenuLink as="button">
9696
<ShareAndroidIcon size={12} />
9797
<span>Share</span>
9898
</MenuLink>
9999
</ModalOpenButton>
100100
<ModalContents>
101-
<Share dirty={dirty} />
101+
<Share
102+
dirty={dirty}
103+
dispatch={dispatch}
104+
gistId={gistId}
105+
gistVersion={gistVersion}
106+
/>
102107
</ModalContents>
103108
</Modal>
104109

src/components/Share.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,41 @@
1-
import React from 'react';
1+
import React, { useEffect } from 'react';
22
import Input from './Input';
33
import CopyButton from './CopyButton';
4+
import { SyncIcon } from '@primer/octicons-react';
45

5-
function Share({ dirty }) {
6+
function Share({ dirty, dispatch, gistId, gistVersion }) {
7+
useEffect(() => {
8+
if (!dirty) {
9+
return;
10+
}
11+
12+
dispatch({ type: 'SAVE' });
13+
}, [dirty, gistId, dispatch]);
14+
15+
// it is possible to have a clean state, and still no gistId. This happens
16+
// on either empty playgrounds, or when using statefull urls.
17+
const shareUrl = gistId
18+
? [location.origin, 'gist', gistId, gistVersion].filter(Boolean).join('/')
19+
: location.href;
20+
21+
console.log('share url');
622
return (
723
<div className="settings text-sm pb-2">
824
<div>
925
<h3 className="text-sm font-bold mb-2">Share</h3>
1026

11-
{dirty && (
12-
<div className="bg-blue-100 p-2 text-xs rounded my-2 text-blue-800">
13-
Please note that this playground has
14-
<strong> unsaved changes </strong>. The link below
15-
<strong> will not include </strong> your latest changes!
27+
<label className="text-xs">playground link:</label>
28+
{dirty ? (
29+
<div className="flex space-x-4 items-center border rounded w-full py-2 px-3 bg-white text-gray-800 leading-tight">
30+
<SyncIcon size={12} className="spinner" />
31+
<span>one sec...</span>
32+
</div>
33+
) : (
34+
<div className="flex space-x-4">
35+
<Input key={shareUrl} defaultValue={shareUrl} readOnly name="url" />
36+
<CopyButton text={shareUrl} />
1637
</div>
1738
)}
18-
19-
<label className="text-xs">playground link:</label>
20-
<div className="flex space-x-4">
21-
<Input defaultValue={window.location.href} name="url" />
22-
<CopyButton text={window.location.href} />
23-
</div>
2439
</div>
2540
</div>
2641
);

src/hooks/usePlayground.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ function reducer(state, action, exec) {
122122

123123
return {
124124
...state,
125-
dirty: false,
126125
status: 'saving',
127126
};
128127
}
@@ -132,11 +131,18 @@ function reducer(state, action, exec) {
132131

133132
return {
134133
...state,
135-
dirty: false,
136134
status: 'saving',
137135
};
138136
}
139137

138+
case 'SAVED': {
139+
return {
140+
...state,
141+
dirty: false,
142+
status: 'idle',
143+
};
144+
}
145+
140146
default: {
141147
throw new Error('Unknown action type: ' + action.type);
142148
}
@@ -230,7 +236,7 @@ const effectMap = {
230236
});
231237

232238
history.push(`/gist/${id}/${version}`);
233-
dispatch({ type: 'SET_STATUS', status: 'idle' });
239+
dispatch({ type: 'SAVED' });
234240
},
235241

236242
FORK: async (state, effect, dispatch) => {
@@ -244,7 +250,7 @@ const effectMap = {
244250
});
245251

246252
history.push(`/gist/${id}/${version}`);
247-
dispatch({ type: 'SET_STATUS', status: 'idle' });
253+
dispatch({ type: 'SAVED' });
248254
},
249255

250256
REDIRECT: (state, effect) => {

0 commit comments

Comments
 (0)