Skip to content

Commit d543919

Browse files
drew-harrisdensumesh
authored andcommitted
fix: fix cors issues and tracking id
revert thing jlsdkf
1 parent 429288d commit d543919

4 files changed

Lines changed: 105 additions & 27 deletions

File tree

clients/trieve-shopify-extension/extensions/enrich-content-block/shopify.extension.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type = "ui_extension"
99

1010
# Only 1 target can be specified for each Admin action extension
1111
[[extensions.targeting]]
12-
module = "./src/ActionExtension.tsx"
12+
module = "./src/BlockExtension.tsx"
1313
target = "admin.product-details.block.render"
1414

1515

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import { useEffect, useState } from "react";
22
import {
33
reactExtension,
44
BlockStack,
5-
Text,
65
AdminBlock,
76
useApi,
87
TextField,
98
Button,
109
InlineStack,
1110
Banner,
11+
TextArea,
1212
} from "@shopify/ui-extensions-react/admin";
13-
import { TrieveProvider, useTrieve } from "./TrieveProvider";
13+
import { TrieveProvider } from "./TrieveProvider";
1414
import { useChunkExtraContent } from "./useChunkExtraContent";
1515

1616
const TARGET = "admin.product-details.block.render";
@@ -37,7 +37,7 @@ function App() {
3737
const [isSaving, setIsSaving] = useState(false);
3838
const [showSuccess, setShowSuccess] = useState(false);
3939

40-
const { extraContent, loading, updateContent } =
40+
const { extraContent, updateContent } =
4141
useChunkExtraContent(simplifiedProductId);
4242

4343
useEffect(() => {
@@ -51,8 +51,6 @@ function App() {
5151
try {
5252
await updateContent(content);
5353
setShowSuccess(true);
54-
// Hide success message after 3 seconds
55-
setTimeout(() => setShowSuccess(false), 3000);
5654
} finally {
5755
setIsSaving(false);
5856
}
@@ -66,7 +64,7 @@ function App() {
6664
Content saved successfully
6765
</Banner>
6866
)}
69-
<TextField
67+
<TextArea
7068
label="Product Content"
7169
value={content}
7270
onChange={setContent}

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

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import { createContext, useContext, useEffect, useState } from "react";
22
import { TrieveSDK } from "trieve-ts-sdk";
3-
import {
4-
getAppId,
5-
getTrieveApiKeyDatasetId,
6-
sdkFromKey,
7-
TrieveKey,
8-
} from "extensions/admin-block-pdp-questions/src/BlockExtension";
93
import { InlineStack } from "@shopify/ui-extensions-react/admin";
104
import { Text } from "@shopify/ui-extensions-react/admin";
115
import { ProgressIndicator } from "@shopify/ui-extensions-react/admin";
@@ -15,6 +9,88 @@ type TrieveContextShape = {
159
trieveSdk: TrieveSDK;
1610
} | null;
1711

12+
export const sdkFromKey = (key: TrieveKey): TrieveSDK => {
13+
const trieve = new TrieveSDK({
14+
baseUrl: "https://api.trieve.ai",
15+
apiKey: key.key,
16+
datasetId: key.currentDatasetId ? key.currentDatasetId : undefined,
17+
organizationId: key.organizationId,
18+
omitCredentials: true,
19+
});
20+
21+
return trieve;
22+
};
23+
24+
export type TrieveKey = {
25+
id?: string;
26+
userId?: string;
27+
organizationId?: string;
28+
currentDatasetId: string | null;
29+
key: string;
30+
};
31+
async function makeGraphQLQuery(
32+
query: string,
33+
variables: {
34+
ownerId?: any;
35+
namespace?: string;
36+
key?: string;
37+
type?: string;
38+
value?: string;
39+
id?: any;
40+
appId?: string;
41+
metafieldsSetInput?: any;
42+
},
43+
) {
44+
const graphQLQuery = {
45+
query,
46+
variables,
47+
};
48+
49+
const res = await fetch("shopify:admin/api/graphql.json", {
50+
method: "POST",
51+
body: JSON.stringify(graphQLQuery),
52+
});
53+
54+
if (!res.ok) {
55+
console.error("Network error");
56+
}
57+
58+
return await res.json();
59+
}
60+
export async function getAppId() {
61+
return await makeGraphQLQuery(
62+
`query {
63+
currentAppInstallation {
64+
id
65+
}
66+
}
67+
`,
68+
{},
69+
);
70+
}
71+
72+
export async function getTrieveApiKeyDatasetId(appId: string) {
73+
return await makeGraphQLQuery(
74+
`query GetAppMetafields($appId: ID!) {
75+
appInstallation(id: $appId) {
76+
id
77+
metafields(first: 10, namespace: "trieve") {
78+
edges {
79+
node {
80+
id
81+
namespace
82+
key
83+
value
84+
type
85+
}
86+
}
87+
}
88+
}
89+
}`,
90+
{ appId },
91+
);
92+
}
93+
1894
const TrieveContext = createContext<TrieveContextShape>(null);
1995

2096
export const useTrieve = () => {

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,29 @@ export const useChunkExtraContent = (productId: string | undefined) => {
55
const trieve = useTrieve();
66
const [extraContent, setExtraContent] = useState<string>("");
77

8-
const [loading, setLoading] = useState(true);
9-
108
// Fetch current product extra content chunk
119
const getData = async () => {
1210
if (!productId) {
1311
console.info("Tried to fetch product content without id");
1412
return;
1513
}
16-
const result = await trieve.getChunkByTrackingId({
17-
trackingId: `${productId}-pdp-content`,
18-
});
19-
if (!result) {
20-
setExtraContent("");
21-
return;
22-
}
23-
if (!result.chunk_html) {
14+
try {
15+
const result = await trieve.getChunkByTrackingId({
16+
trackingId: `${productId}-pdp-content`,
17+
});
18+
19+
if (!result) {
20+
setExtraContent("");
21+
return;
22+
}
23+
if (!result.chunk_html) {
24+
setExtraContent("");
25+
return;
26+
}
27+
setExtraContent(result.chunk_html);
28+
} catch {
2429
setExtraContent("");
25-
return;
2630
}
27-
setExtraContent(result.chunk_html);
2831
};
2932

3033
const updateContent = async (content: string) => {
@@ -35,8 +38,10 @@ export const useChunkExtraContent = (productId: string | undefined) => {
3538

3639
const result = await trieve.createChunk({
3740
chunk_html: content,
38-
tracking_id: productId,
41+
tracking_id: `${productId}-pdp-content`,
3942
upsert_by_tracking_id: true,
43+
weight: 100,
44+
group_tracking_ids: [productId],
4045
});
4146
};
4247

@@ -49,7 +54,6 @@ export const useChunkExtraContent = (productId: string | undefined) => {
4954

5055
return {
5156
extraContent,
52-
loading,
5357
updateContent,
5458
};
5559
};

0 commit comments

Comments
 (0)