Skip to content

Commit 79f0e50

Browse files
authored
Reimplement localStorage using plain JS. (#823)
* Reimplement localStorage using plain JS. * Clean up unused type parameter.
1 parent 0294ed5 commit 79f0e50

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/components/Home/LocalWarningCallout.tsx

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import { Button } from '@rmwc/button';
1818
import { GridCell } from '@rmwc/grid';
19-
import React, { useState } from 'react';
19+
import React, { useCallback, useEffect, useState } from 'react';
2020

2121
import { Callout } from '../common/Callout';
2222
import { CONSOLE_ROOT } from '../common/constants';
@@ -28,10 +28,22 @@ export const LocalWarningCallout: React.FC<
2828
projectId: string;
2929
}>
3030
> = ({ projectId }) => {
31-
// TODO: investigate local-storage
32-
const [isDismissed, setIsDismissed] = useState(false);
31+
// Default to false to reduce layout flashes if already dismissed.
32+
const [showWarning, setShowWarning] = useState(false);
33+
useEffect(() => {
34+
// Set to true only if localStorage is accessible and not dismissed before.
35+
const val = localStorage.getItem(DISMISS_KEY);
36+
if (val !== 'true') {
37+
setShowWarning(true);
38+
}
39+
}, []);
3340

34-
if (isDismissed) {
41+
const dismiss = useCallback(() => {
42+
setShowWarning(false);
43+
localStorage.setItem(DISMISS_KEY, 'true');
44+
}, [setShowWarning]);
45+
46+
if (!showWarning) {
3547
return null;
3648
}
3749

@@ -47,12 +59,7 @@ export const LocalWarningCallout: React.FC<
4759
target="_blank"
4860
label="View project"
4961
/>
50-
<Button
51-
label="Dismiss"
52-
onClick={() => {
53-
setIsDismissed(true);
54-
}}
55-
/>
62+
<Button label="Dismiss" onClick={dismiss} />
5663
</>
5764
}
5865
>

0 commit comments

Comments
 (0)