Skip to content

Commit 9a09262

Browse files
fix(highlight): stale cache & centralize CRUD GraphQL queries
1 parent 7875db7 commit 9a09262

File tree

3 files changed

+57
-52
lines changed

3 files changed

+57
-52
lines changed

client/src/apps/highlighter/PostContentHighlighted.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import { useRef } from "react";
44
import { FaTrashCan } from "react-icons/fa6";
55

66
import { highlighter, isHTMLElement } from "@/apps/highlighter/highlighter";
7+
import { removeHighlight } from "@/apps/highlighter/useHighlighter";
78
import { Prose } from "@/components/ui/prose";
89
import { ids } from "@/e2e/ids";
910
import type { ID } from "@/gql-tada";
10-
import { graphql } from "@/gql-tada";
11-
import { mutateAndRefetchMountedQueries } from "@/graphql/mutateAndRefetchMountedQueries";
1211
import { markedConfigured } from "@/utils/marked-configured";
1312
import { useIsLoading } from "@/utils/useIsLoading";
1413
import { useValtioProxyRef } from "@/utils/useValtioProxyRef";
@@ -60,13 +59,7 @@ export function PostContentHighlighted(props: {
6059
return;
6160
}
6261
await loading.track(async () => {
63-
await mutateAndRefetchMountedQueries(
64-
graphql(`
65-
mutation HighlighterDelete($id: ID!) { post_highlight_delete(data: { id: $id }) }
66-
`),
67-
// @ts-expect-error #bad-infer
68-
{ id: state.snap.activeHighlightId },
69-
);
62+
await removeHighlight(state.snap.activeHighlightId!);
7063
});
7164
state.mutable.activeHighlightId = null;
7265
}

client/src/apps/highlighter/highlighter.tsx

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { ActionBar, Button, Kbd } from "@chakra-ui/react";
22
import { useEffect, useRef } from "react";
3+
import { saveHighlight } from "@/apps/highlighter/useHighlighter";
34
import { ids } from "@/e2e/ids";
4-
import { graphql, type ID } from "@/gql-tada";
5-
import { mutateAndRefetchMountedQueries } from "@/graphql/mutateAndRefetchMountedQueries";
5+
import type { ID } from "@/gql-tada";
66
import { toast } from "@/utils/toast";
77
import { useIsLoading } from "@/utils/useIsLoading";
88
import { useValtioProxyRef } from "@/utils/useValtioProxyRef";
@@ -240,39 +240,6 @@ export namespace highlighter {
240240
return textContext;
241241
}
242242

243-
async function saveHighlight(args: {
244-
id: ID;
245-
text: string;
246-
text_prefix: string;
247-
text_postfix: string;
248-
}) {
249-
console.log(attrs.flag, args);
250-
251-
await mutateAndRefetchMountedQueries(
252-
graphql(`
253-
mutation HighlighterCreate(
254-
$id: ID!,
255-
$text: String!,
256-
$text_prefix: String,
257-
$text_postfix: String,
258-
) {
259-
post_highlight_create(data: {
260-
post: { set: $id }
261-
text: $text
262-
text_postfix: $text_postfix
263-
text_prefix: $text_prefix
264-
})
265-
}
266-
`),
267-
{
268-
id: args.id,
269-
text: args.text,
270-
text_prefix: args.text_prefix,
271-
text_postfix: args.text_postfix,
272-
},
273-
);
274-
}
275-
276243
export type ModelType = "comment" | "post" | "review";
277244

278245
export const attrs = {

client/src/apps/highlighter/useHighlighter.tsx

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useQuery } from "@apollo/client/react";
12
import type { ResultOf } from "gql.tada";
23
import { highlighter } from "@/apps/highlighter/highlighter";
34
import type { PostCommentTree } from "@/components/posts/PostDetail";
@@ -41,15 +42,15 @@ export function useHighlighter(props: UseHighlighterProps) {
4142
},
4243
});
4344

45+
const { data } = useQuery(PostHighlightsQuery, {
46+
variables: { ids: state.snap.postIds },
47+
skip: !state.snap.postIds.length,
48+
});
49+
4450
useInit({
45-
isReady: state.snap.postIds.length,
46-
deps: [state.snap.postIds],
51+
isReady: Boolean(data),
52+
deps: [data],
4753
onInit: async () => {
48-
const res = await client.query({
49-
query: PostHighlightsQuery,
50-
variables: { ids: state.snap.postIds },
51-
});
52-
const data = res.data!;
5354
if (isQueryDataComplete(data) && data.post_highlights) {
5455
const highlights: Record<ID, PostHighlight[]> = {};
5556
for (const highlight of data.post_highlights) {
@@ -109,10 +110,54 @@ export function useHighlighter(props: UseHighlighterProps) {
109110
}
110111

111112
return {
112-
highlight: highlight,
113+
highlight,
113114
};
114115
}
115116

117+
export async function saveHighlight(args: {
118+
id: ID;
119+
text: string;
120+
text_prefix: string;
121+
text_postfix: string;
122+
}) {
123+
await client.mutate({
124+
mutation: graphql(`
125+
mutation HighlighterCreate(
126+
$id: ID!,
127+
$text: String!,
128+
$text_prefix: String,
129+
$text_postfix: String,
130+
) {
131+
post_highlight_create(data: {
132+
post: { set: $id }
133+
text: $text
134+
text_postfix: $text_postfix
135+
text_prefix: $text_prefix
136+
})
137+
}
138+
`),
139+
variables: {
140+
id: args.id,
141+
text: args.text,
142+
text_prefix: args.text_prefix,
143+
text_postfix: args.text_postfix,
144+
},
145+
});
146+
await client.refetchQueries({ include: [PostHighlightsQuery] });
147+
}
148+
149+
export async function removeHighlight(id: ID) {
150+
await client.mutate({
151+
mutation: graphql(`
152+
mutation HighlighterDelete($id: ID!) {
153+
post_highlight_delete(data: { id: $id })
154+
}
155+
`),
156+
variables: { id },
157+
});
158+
await client.refetchQueries({ include: [PostHighlightsQuery] });
159+
}
160+
116161
const PostHighlightsQuery = graphql(
117162
`query GetPostHighlights($ids: [ID!]!) {
118163
post_highlights(post_ids: $ids) {

0 commit comments

Comments
 (0)