Skip to content

Commit

Permalink
Merge pull request #57 from githubnext/aw/onFetchInternalEndpoint
Browse files Browse the repository at this point in the history
Implement onFetchInternalEndpoint
  • Loading branch information
Wattenberger authored Jun 30, 2022
2 parents b8f396e + f8d708f commit 14c6e10
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
15 changes: 12 additions & 3 deletions blocks/file-blocks/edit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { Button, FormControl, TextInput } from "@primer/react";

export default function (props: FileBlockProps) {
const { content, context, onUpdateContent } = props;
const onFetchInternalEndpoint =
props.private__onFetchInternalEndpoint || onFetchInternalEndpointPolyfill;

const [instruction, setInstruction] = useState<string>("");
const [newContent, setNewContent] = useState<string>("");
Expand Down Expand Up @@ -46,9 +48,12 @@ export default function (props: FileBlockProps) {
onSubmit={async (e) => {
e.preventDefault();
setIsLoading(true);
const res = await axios.post("/api/openai-edit", {
instruction: instruction,
input: content,
const res = await onFetchInternalEndpoint("/api/openai-edit", {
method: "POST",
data: {
instruction: instruction,
input: content,
},
});
setNewContent(res.data);
setIsLoading(false);
Expand Down Expand Up @@ -196,3 +201,7 @@ const Change = ({ change, language }: { change: Hunk; language: string }) => {
</div>
);
};

const onFetchInternalEndpointPolyfill = async (url: string, params: any) => {
return await axios(url, params);
};
29 changes: 24 additions & 5 deletions blocks/file-blocks/explain/explanation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ import axios from "axios";
import { useQuery } from "react-query";
import type { Explanation } from ".";

const fetchExplanation = async (code: string, language: string) => {
const res = await axios.post(`/api/explain`, {
code,
language,
const fetchExplanation = async (
code: string,
language: string,
onFetchInternalEndpoint: (url: string, params: any) => Promise<any>
): Promise<Explanation> => {
const res = await onFetchInternalEndpoint("/api/explain", {
method: "POST",
data: {
code,
language,
},
});
return res.data;
};
Expand All @@ -16,9 +23,17 @@ export function ExplanationComponent(props: {
language: string;
}) {
const { explanation } = props;
const onFetchInternalEndpoint =
props.private__onFetchInternalEndpoint || onFetchInternalEndpointPolyfill;

const { data, status } = useQuery(
["explanation", props.explanation.code],
() => fetchExplanation(props.explanation.code, props.language),
() =>
fetchExplanation(
props.explanation.code,
props.language,
onFetchInternalEndpoint
),
{ refetchOnWindowFocus: false }
);

Expand Down Expand Up @@ -49,3 +64,7 @@ export function ExplanationComponent(props: {
</>
);
}

const onFetchInternalEndpointPolyfill = async (url: string, params: any) => {
return await axios(url, params);
};
16 changes: 13 additions & 3 deletions blocks/file-blocks/summarize/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import "./index.css";

export default function (props: FileBlockProps) {
const { content, context } = props;
const onFetchInternalEndpoint =
props.private__onFetchInternalEndpoint || onFetchInternalEndpointPolyfill;
const [sections, setSections] = useState<CodeSection[]>([]);
const [sectionExplanations, setSectionExplanations] = useState<string[]>([]);
const [isCollapsed, setIsCollapsed] = useState<boolean>(false);
Expand Down Expand Up @@ -39,7 +41,7 @@ export default function (props: FileBlockProps) {
type === "function" &&
// don't hammer the endpoint
index < 30
? await fetchCodeSummary(text, language)
? await fetchCodeSummary(text, language, onFetchInternalEndpoint)
: "";
setSectionExplanations((sectionExplanations) => {
let newSectionExplanations = [...sectionExplanations];
Expand Down Expand Up @@ -191,9 +193,13 @@ const Section = ({
);
};

const fetchCodeSummary = async (code: string, language: string) => {
const fetchCodeSummary = async (
code: string,
language: string,
onFetchInternalEndpoint: (url: string, params: any) => Promise<any>
) => {
// this is an endpoint on the main prototype
const response = await axios("/api/explain", {
const response = await onFetchInternalEndpoint("/api/explain", {
method: "POST",
data: {
language,
Expand Down Expand Up @@ -282,3 +288,7 @@ const breakCodeIntoSections = async (
});
return sections.filter((d) => !!d.text.trim());
};

const onFetchInternalEndpointPolyfill = async (url: string, params: any) => {
return await axios(url, params);
};

0 comments on commit 14c6e10

Please sign in to comment.