Introduce update functions for less error-prone local storage updates

For local storage updates to be persisted it had to be assigned for
example with `storage.value = storage.value`. It was easy to forget this
and it’s not immediately obvious why this is needed. (In fact, eslint
complains.)

We introduce an `update` method for the local storage and an
`updateStoredDraftReview` to encapsulate the necessary logic.
This commit is contained in:
Thomas Scholtes
2025-07-17 11:08:41 +02:00
parent ceca51c1bb
commit 595382b240
2 changed files with 72 additions and 42 deletions
+63 -39
View File
@@ -62,6 +62,8 @@ const draftReviewStoredSchema = z.object({
),
});
type DraftReviewStored = z.infer<typeof draftReviewStoredSchema>;
const storage = useLocalStorage(
"repo.patches.draftReviews",
z.record(z.string(), draftReviewStoredSchema),
@@ -89,19 +91,18 @@ export const draftReviewStorage = {
},
create(rid: string, revisionId: string): string {
const draftReviews = storage.value;
const id = crypto.randomUUID();
draftReviews[id] = {
id,
rid,
revision: revisionId,
summary: "",
labels: [],
comments: [],
};
storage.value = draftReviews;
storage.update(reviews => {
reviews[id] = {
id,
rid,
revision: revisionId,
summary: "",
labels: [],
comments: [],
};
return reviews;
});
return id;
},
@@ -109,20 +110,19 @@ export const draftReviewStorage = {
id: string,
props: { summary: string; verdict: Verdict | undefined; labels: string[] },
) {
const draftPatches = storage.value;
draftPatches[id].summary = props.summary;
draftPatches[id].verdict = props.verdict;
draftPatches[id].labels = props.labels;
storage.value = draftPatches;
updateStoredDraftReview(id, review => {
return Object.assign(review, props);
});
},
deleteComment(id: string, commentId: string) {
const draftReviews = storage.value;
const index = draftReviews[id].comments.findIndex(
comment => comment.id === commentId,
);
draftReviews[id].comments.splice(index, 1);
storage.value = draftReviews;
updateStoredDraftReview(id, review => {
const index = review.comments.findIndex(
comment => comment.id === commentId,
);
review.comments.splice(index, 1);
return review;
});
},
addComment(
@@ -133,14 +133,15 @@ export const draftReviewStorage = {
location: CodeLocation;
},
): string {
const draftPatches = storage.value;
const commentId = crypto.randomUUID();
draftPatches[id].comments.push({
id: crypto.randomUUID(),
body: comment.body,
location: comment.location,
updateStoredDraftReview(id, review => {
review.comments.push({
id: commentId,
body: comment.body,
location: comment.location,
});
return review;
});
storage.value = draftPatches;
return commentId;
},
@@ -152,20 +153,28 @@ export const draftReviewStorage = {
embeds: Embed[];
},
) {
const draftPatches = storage.value;
const storedComment = draftPatches[id].comments.find(
comment => comment.id === commentId,
);
storedComment!.body = comment.body;
storage.value = draftPatches;
updateStoredDraftReview(id, review => {
const storedComment = review.comments.find(
comment => comment.id === commentId,
);
if (!storedComment) {
throw new Error(
`Comment ${commentId} does not exist for draft review ${id}`,
);
}
storedComment!.body = comment.body;
return review;
});
},
async publish(id: string) {
const draftReviewStored = storage.value[id];
delete storage.value[id];
// We need to explicitly persist the storage
// eslint-disable-next-line no-self-assign
storage.value = storage.value;
storage.update(reviews => {
delete reviews[id];
return reviews;
});
await invoke<Patch>("create_patch_review", {
args: {
rid: draftReviewStored.rid,
@@ -182,6 +191,21 @@ export const draftReviewStorage = {
},
};
function updateStoredDraftReview(
id: string,
update: (draftReviewStored: DraftReviewStored) => DraftReviewStored,
): void {
storage.update(reviews => {
const review = reviews[id];
if (!review) {
throw new Error(`Draft review ${id} does not exist`);
}
reviews[id] = update(review);
return reviews;
});
}
function draftReviewFromStored(
draftReviewStored: z.infer<typeof draftReviewStoredSchema>,
author: Author,
+9 -3
View File
@@ -46,14 +46,20 @@ export default function useLocalStorage<
}
}
function set(v: S) {
value = v;
if (!disableLocalStorage) localStorage.setItem(key, JSON.stringify(value));
}
return {
get value() {
return value;
},
set value(v: S) {
value = v;
if (!disableLocalStorage)
localStorage.setItem(key, JSON.stringify(value));
set(v);
},
update(fn: (v: S) => S) {
set(fn(value));
},
clear() {
value = initialValue;