Skip to content

Commit 429288d

Browse files
drew-harrisdensumesh
authored andcommitted
feat: enrich product ui
1 parent 9f08b3a commit 429288d

2 files changed

Lines changed: 52 additions & 13 deletions

File tree

clients/trieve-shopify-extension/extensions/enrich-content-block/src/ActionExtension.tsx

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import {
55
Text,
66
AdminBlock,
77
useApi,
8+
TextField,
9+
Button,
10+
InlineStack,
11+
Banner,
812
} from "@shopify/ui-extensions-react/admin";
913
import { TrieveProvider, useTrieve } from "./TrieveProvider";
1014
import { useChunkExtraContent } from "./useChunkExtraContent";
@@ -29,15 +33,49 @@ function App() {
2933
const { data } = useApi(TARGET);
3034
const productId = data.selected[0].id;
3135
const simplifiedProductId = extractShopifyProductId(productId);
36+
const [content, setContent] = useState("");
37+
const [isSaving, setIsSaving] = useState(false);
38+
const [showSuccess, setShowSuccess] = useState(false);
3239

3340
const { extraContent, loading, updateContent } =
3441
useChunkExtraContent(simplifiedProductId);
3542

43+
useEffect(() => {
44+
if (extraContent) {
45+
setContent(extraContent);
46+
}
47+
}, [extraContent]);
48+
49+
const handleSave = async () => {
50+
setIsSaving(true);
51+
try {
52+
await updateContent(content);
53+
setShowSuccess(true);
54+
// Hide success message after 3 seconds
55+
setTimeout(() => setShowSuccess(false), 3000);
56+
} finally {
57+
setIsSaving(false);
58+
}
59+
};
60+
3661
return (
3762
<AdminBlock title="Enrich Content">
38-
<BlockStack>
39-
<Text fontWeight="bold">Hi</Text>
40-
<Text>Current product:{simplifiedProductId}</Text>
63+
<BlockStack gap="base">
64+
{showSuccess && (
65+
<Banner tone="success" onDismiss={() => setShowSuccess(false)}>
66+
Content saved successfully
67+
</Banner>
68+
)}
69+
<TextField
70+
label="Product Content"
71+
value={content}
72+
onChange={setContent}
73+
/>
74+
<InlineStack gap="base" inlineAlignment="end">
75+
<Button onPress={handleSave} disabled={isSaving}>
76+
{isSaving ? "Saving..." : "Save Content"}
77+
</Button>
78+
</InlineStack>
4179
</BlockStack>
4280
</AdminBlock>
4381
);

clients/trieve-shopify-extension/extensions/enrich-content-block/src/useChunkExtraContent.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useTrieve } from "./TrieveProvider";
33

44
export const useChunkExtraContent = (productId: string | undefined) => {
55
const trieve = useTrieve();
6-
const [extraContent, setExtraContent] = useState<string | null>(null);
6+
const [extraContent, setExtraContent] = useState<string>("");
77

88
const [loading, setLoading] = useState(true);
99

@@ -16,7 +16,12 @@ export const useChunkExtraContent = (productId: string | undefined) => {
1616
const result = await trieve.getChunkByTrackingId({
1717
trackingId: `${productId}-pdp-content`,
1818
});
19+
if (!result) {
20+
setExtraContent("");
21+
return;
22+
}
1923
if (!result.chunk_html) {
24+
setExtraContent("");
2025
return;
2126
}
2227
setExtraContent(result.chunk_html);
@@ -28,15 +33,11 @@ export const useChunkExtraContent = (productId: string | undefined) => {
2833
return;
2934
}
3035

31-
// const result = await trieve.updateChunkByTrackingId({
32-
// trackingId: `${productId}-pdp-content`,
33-
// chunk_html: content,
34-
// });
35-
//
36-
// if (!result.chunk_html) {
37-
// return;
38-
// }
39-
// setExtraContent(result.chunk_html);
36+
const result = await trieve.createChunk({
37+
chunk_html: content,
38+
tracking_id: productId,
39+
upsert_by_tracking_id: true,
40+
});
4041
};
4142

4243
useEffect(() => {

0 commit comments

Comments
 (0)