Skip to content

Commit 26558ff

Browse files
authored
Move clear cache to settings (#579)
1 parent e7425cc commit 26558ff

File tree

3 files changed

+33
-30
lines changed

3 files changed

+33
-30
lines changed

src/components/EditorHeader/ControlPanel.jsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ import { toDBML } from "../../utils/exportAs/dbml";
8282
import { exportSavedData } from "../../utils/exportSavedData";
8383
import { nanoid } from "nanoid";
8484
import { getTableHeight } from "../../utils/utils";
85+
import { deleteFromCache, STORAGE_KEY } from "../../utils/cache";
8586

8687
export default function ControlPanel({
8788
diagramId,
@@ -126,7 +127,7 @@ export default function ControlPanel({
126127
const { selectedElement, setSelectedElement } = useSelect();
127128
const { transform, setTransform } = useTransform();
128129
const { t, i18n } = useTranslation();
129-
const { version, setGistId } = useContext(IdContext);
130+
const { version, gistId, setGistId } = useContext(IdContext);
130131
const navigate = useNavigate();
131132

132133
const invertLayout = (component) =>
@@ -1444,13 +1445,19 @@ export default function ControlPanel({
14441445
export_saved_data: {
14451446
function: exportSavedData,
14461447
},
1448+
clear_cache: {
1449+
function: () => {
1450+
deleteFromCache(gistId);
1451+
Toast.success(t("cache_cleared"));
1452+
},
1453+
},
14471454
flush_storage: {
14481455
warning: {
14491456
title: t("flush_storage"),
14501457
message: t("are_you_sure_flush_storage"),
14511458
},
14521459
function: async () => {
1453-
localStorage.removeItem("versions_cache");
1460+
localStorage.removeItem(STORAGE_KEY);
14541461
db.delete()
14551462
.then(() => {
14561463
Toast.success(t("storage_flushed"));

src/components/EditorHeader/SideSheet/Versions.jsx

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,9 @@ import {
2323
useTypes,
2424
} from "../../../hooks";
2525
import { databases } from "../../../data/databases";
26+
import { loadCache, saveCache } from "../../../utils/cache";
2627

2728
const LIMIT = 10;
28-
const STORAGE_KEY = "versions_cache";
29-
30-
function loadCache() {
31-
try {
32-
const saved = localStorage.getItem(STORAGE_KEY);
33-
return saved ? JSON.parse(saved) : {};
34-
} catch {
35-
return {};
36-
}
37-
}
38-
39-
function saveCache(cache) {
40-
localStorage.setItem(STORAGE_KEY, JSON.stringify(cache));
41-
}
4229

4330
export default function Versions({ open, title, setTitle }) {
4431
const { gistId, setGistId, version, setVersion } = useContext(IdContext);
@@ -236,12 +223,6 @@ export default function Versions({ open, title, setTitle }) {
236223
}
237224
};
238225

239-
const onClearCache = () => {
240-
delete cacheRef[gistId];
241-
saveCache(cacheRef);
242-
Toast.success(t("cache_cleared"));
243-
};
244-
245226
useEffect(() => {
246227
if (gistId && open) {
247228
getRevisions();
@@ -250,22 +231,15 @@ export default function Versions({ open, title, setTitle }) {
250231

251232
return (
252233
<div className="mx-5 relative h-full">
253-
<div className="sticky top-0 z-10 sidesheet-theme pb-2 grid grid-cols-3 gap-2">
234+
<div className="sticky top-0 z-10 sidesheet-theme pb-2">
254235
<Button
255-
className={cacheRef[gistId] ? "col-span-2" : "col-span-3"}
256236
block
257237
icon={isRecording ? <Spin /> : <IconPlus />}
258238
disabled={isLoading || isRecording}
259239
onClick={recordVersion}
260240
>
261241
{t("record_version")}
262242
</Button>
263-
264-
{cacheRef[gistId] && (
265-
<Button block type="danger" onClick={onClearCache}>
266-
{t("clear_cache")}
267-
</Button>
268-
)}
269243
</div>
270244

271245
{(!gistId || !versions.length) && !isLoading && (

src/utils/cache.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export const STORAGE_KEY = "versions_cache";
2+
3+
export function loadCache() {
4+
try {
5+
const saved = localStorage.getItem(STORAGE_KEY);
6+
return saved ? JSON.parse(saved) : {};
7+
} catch {
8+
return {};
9+
}
10+
}
11+
12+
export function saveCache(cache) {
13+
localStorage.setItem(STORAGE_KEY, JSON.stringify(cache));
14+
}
15+
16+
export function deleteFromCache(key) {
17+
const cache = loadCache();
18+
if (cache[key]) {
19+
delete cache[key];
20+
saveCache(cache);
21+
}
22+
}

0 commit comments

Comments
 (0)