|
1 |
| -import React from 'react'; |
| 1 | +import React, { useEffect } from 'react'; |
2 | 2 | import Input from './Input';
|
3 | 3 | import CopyButton from './CopyButton';
|
| 4 | +import { SyncIcon } from '@primer/octicons-react'; |
4 | 5 |
|
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'); |
6 | 22 | return (
|
7 | 23 | <div className="settings text-sm pb-2">
|
8 | 24 | <div>
|
9 | 25 | <h3 className="text-sm font-bold mb-2">Share</h3>
|
10 | 26 |
|
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} /> |
16 | 37 | </div>
|
17 | 38 | )}
|
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> |
24 | 39 | </div>
|
25 | 40 | </div>
|
26 | 41 | );
|
|
0 commit comments