File tree Expand file tree Collapse file tree
clients/trieve-shopify-extension/extensions/enrich-content-block/src Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -7,25 +7,37 @@ import {
77 useApi ,
88} from "@shopify/ui-extensions-react/admin" ;
99import { TrieveProvider , useTrieve } from "./TrieveProvider" ;
10+ import { useChunkExtraContent } from "./useChunkExtraContent" ;
1011
1112const TARGET = "admin.product-details.block.render" ;
1213
14+ function extractShopifyProductId ( gid : string ) : string | undefined {
15+ const parts = gid . substring ( 6 ) . split ( "/" ) ; // Remove "gid://" and then split
16+ if ( parts . length === 3 && parts [ 0 ] === "shopify" && parts [ 1 ] === "Product" ) {
17+ return parts [ 2 ] ;
18+ }
19+ return undefined ;
20+ }
21+
1322export default reactExtension ( TARGET , ( ) => (
1423 < TrieveProvider >
1524 < App />
1625 </ TrieveProvider >
1726) ) ;
1827
1928function App ( ) {
20- const trieve = useTrieve ( ) ;
2129 const { data } = useApi ( TARGET ) ;
2230 const productId = data . selected [ 0 ] . id ;
31+ const simplifiedProductId = extractShopifyProductId ( productId ) ;
32+
33+ const { extraContent, loading, updateContent } =
34+ useChunkExtraContent ( simplifiedProductId ) ;
2335
2436 return (
2537 < AdminBlock title = "Enrich Content" >
2638 < BlockStack >
2739 < Text fontWeight = "bold" > Hi</ Text >
28- < Text > Current product:{ productId } </ Text >
40+ < Text > Current product:{ simplifiedProductId } </ Text >
2941 </ BlockStack >
3042 </ AdminBlock >
3143 ) ;
Original file line number Diff line number Diff line change 1+ import { useEffect , useState } from "react" ;
2+ import { useTrieve } from "./TrieveProvider" ;
3+
4+ export const useChunkExtraContent = ( productId : string | undefined ) => {
5+ const trieve = useTrieve ( ) ;
6+ const [ extraContent , setExtraContent ] = useState < string | null > ( null ) ;
7+
8+ const [ loading , setLoading ] = useState ( true ) ;
9+
10+ // Fetch current product extra content chunk
11+ const getData = async ( ) => {
12+ if ( ! productId ) {
13+ console . info ( "Tried to fetch product content without id" ) ;
14+ return ;
15+ }
16+ const result = await trieve . getChunkByTrackingId ( {
17+ trackingId : `${ productId } -pdp-content` ,
18+ } ) ;
19+ if ( ! result . chunk_html ) {
20+ return ;
21+ }
22+ setExtraContent ( result . chunk_html ) ;
23+ } ;
24+
25+ const updateContent = async ( content : string ) => {
26+ if ( ! productId ) {
27+ console . info ( "Tried to update product content without id" ) ;
28+ return ;
29+ }
30+
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);
40+ } ;
41+
42+ useEffect ( ( ) => {
43+ if ( ! productId ) {
44+ return ;
45+ }
46+ getData ( ) ;
47+ } , [ productId ] ) ;
48+
49+ return {
50+ extraContent,
51+ loading,
52+ updateContent,
53+ } ;
54+ } ;
You can’t perform that action at this time.
0 commit comments