Factor out DeserializationErrorModal.
rodzic
160749762f
commit
1245cdaceb
|
@ -45,6 +45,27 @@ export function createPageWithStateSearchParams(
|
||||||
return search;
|
return search;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const DeserializationErrorModal: React.FC = () => {
|
||||||
|
const [show, setShow] = useState(true);
|
||||||
|
|
||||||
|
if (!show) return null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="page-error">
|
||||||
|
<div>
|
||||||
|
<p>
|
||||||
|
Sorry, an error occurred when trying to load the composition on this
|
||||||
|
page.
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
Either its data is corrupted, or displaying it is no longer supported.
|
||||||
|
</p>
|
||||||
|
<button onClick={() => setShow(false)}>OK</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a component that represents a page which exposes some
|
* Create a component that represents a page which exposes some
|
||||||
* aspect of its state in the current URL, so that it can be
|
* aspect of its state in the current URL, so that it can be
|
||||||
|
@ -86,6 +107,7 @@ export function createPageWithShareableState<T>({
|
||||||
|
|
||||||
/** The default state from the URL, which we'll pass into our component. */
|
/** The default state from the URL, which we'll pass into our component. */
|
||||||
let defaults: T = defaultValue;
|
let defaults: T = defaultValue;
|
||||||
|
|
||||||
let didDeserializeThrow = false;
|
let didDeserializeThrow = false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -95,8 +117,6 @@ export function createPageWithShareableState<T>({
|
||||||
didDeserializeThrow = true;
|
didDeserializeThrow = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const [showError, setShowError] = useState(didDeserializeThrow);
|
|
||||||
|
|
||||||
const onChange = useCallback(
|
const onChange = useCallback(
|
||||||
(value: T) => {
|
(value: T) => {
|
||||||
const newState = serialize(value);
|
const newState = serialize(value);
|
||||||
|
@ -121,21 +141,7 @@ export function createPageWithShareableState<T>({
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{showError && (
|
{didDeserializeThrow && <DeserializationErrorModal />}
|
||||||
<div className="page-error">
|
|
||||||
<div>
|
|
||||||
<p>
|
|
||||||
Sorry, an error occurred when trying to load the composition on
|
|
||||||
this page.
|
|
||||||
</p>
|
|
||||||
<p>
|
|
||||||
Either its data is corrupted, or displaying it is no longer
|
|
||||||
supported.
|
|
||||||
</p>
|
|
||||||
<button onClick={() => setShowError(false)}>OK</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
<Component key={key} defaults={defaults} onChange={onChange} />
|
<Component key={key} defaults={defaults} onChange={onChange} />
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|
Ładowanie…
Reference in New Issue