Skip to content

Commit 31badf1

Browse files
committed
feat: saving interactions for generate and copy locators
1 parent 15e1c08 commit 31badf1

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

src/components/LightLocatorsGeneratorTab.tsx

+32-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import {
88
Textarea,
99
} from "@mantine/core";
1010
import { useDocumentTitle, useInputState } from "@mantine/hooks";
11+
import { NetlifyFunctions } from "../constants";
1112
import { makeLocators } from "../utils/factories";
13+
import { netlifyFunctionInvoke } from "../utils/general";
1214

1315
const gridColums = 12;
1416

@@ -18,9 +20,37 @@ export default function LightLocatorsGeneratorTab() {
1820
const [generatedLocators, setGeneratedLocators] = useInputState("");
1921
useDocumentTitle("RMG Utils for Stellaris - Light Locators Generator");
2022

21-
const onGenerateClick = () => {
23+
const onGenerateClick = async () => {
2224
const result = makeLocators(locators, stateTime);
2325
setGeneratedLocators(result.join("\n"));
26+
try {
27+
await netlifyFunctionInvoke(
28+
NetlifyFunctions.SAVE_LOCATORS_INTEGRATIONS,
29+
{ "Content-Type": "application/json" },
30+
{ locators, stateTime, type: "generate" }
31+
);
32+
} catch (e) {
33+
// TODO: Report to Sentry
34+
console.error(e);
35+
}
36+
};
37+
38+
const onCopyClick = async (copy: Function) => {
39+
copy();
40+
try {
41+
await netlifyFunctionInvoke(
42+
NetlifyFunctions.SAVE_LOCATORS_INTEGRATIONS,
43+
{ "Content-Type": "application/json" },
44+
{
45+
locators: generatedLocators.split("\n").length,
46+
stateTime,
47+
type: "copy",
48+
}
49+
);
50+
} catch (e) {
51+
// TODO: Report to Sentry
52+
console.error(e);
53+
}
2454
};
2555

2656
return (
@@ -73,7 +103,7 @@ export default function LightLocatorsGeneratorTab() {
73103
{({ copied, copy }) => (
74104
<Button
75105
color={copied ? "teal" : "blue"}
76-
onClick={copy}
106+
onClick={() => onCopyClick(copy)}
77107
styles={{ root: { alignSelf: "flex-end" } }}
78108
className="umami--click--copy-light-locators-button"
79109
>

src/constants.ts

+4
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ export const ROUTES = {
1212
name: "Privacy policy",
1313
},
1414
};
15+
16+
export enum NetlifyFunctions {
17+
SAVE_LOCATORS_INTEGRATIONS = "save_locators_interaction",
18+
}

src/utils/general.ts

+37
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,42 @@
1+
import { NetlifyFunctions } from "../constants";
2+
13
export const dateFormatter = new Intl.DateTimeFormat("default", {
24
year: "numeric",
35
month: "long",
46
day: "numeric",
57
});
8+
9+
export const netlifyFunctionInvoke = async (
10+
name: NetlifyFunctions,
11+
headers = {},
12+
body: Object | undefined,
13+
options = { method: "POST" }
14+
) => {
15+
let url = "";
16+
// add host for local dev
17+
if (import.meta.env.DEV) {
18+
url += import.meta.env.VITE_NETLIFY_FUNCTIONS_LOCAL_SERVER;
19+
}
20+
// add netlify function path
21+
url += `/.netlify/functions/${name}`;
22+
23+
const response = await fetch(url, {
24+
...options,
25+
headers,
26+
body: body ? JSON.stringify(body) : undefined,
27+
});
28+
29+
if (!response.ok) {
30+
throw new Error(`${response.status} ${response.statusText}`);
31+
}
32+
33+
let content = await response.text();
34+
35+
try {
36+
content = JSON.parse(content);
37+
} catch (e) {
38+
// content is not JSON
39+
}
40+
41+
return content;
42+
};

src/vite-env.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
/// <reference types="vite/client" />
22
declare const APP_VERSION: string;
3+
4+
interface ImportMetaEnv {
5+
readonly VITE_NETLIFY_FUNCTIONS_LOCAL_SERVER: string;
6+
}
7+
8+
interface ImportMeta {
9+
readonly env: ImportMetaEnv;
10+
}

0 commit comments

Comments
 (0)