-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2010 from rachel-lawrie/lawrie_policy2code_9_5
Add info button next to each benefit
- Loading branch information
Showing
6 changed files
with
242 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import React, { useCallback, useEffect, useRef, useState } from "react"; | ||
import { Button, Modal } from "antd"; | ||
import { countryApiCall } from "../api/call"; | ||
import useCountryId from "../hooks/useCountryId"; | ||
import { MarkdownFormatter } from "../layout/MarkdownFormatter"; | ||
import { useSearchParams } from "react-router-dom"; | ||
import { COUNTRY_BASELINE_POLICIES } from "../data/countries"; | ||
|
||
/** | ||
* Modal for displaying AI output | ||
* @param {Object} props | ||
* @param {Object} props.variableName The name of the variable | ||
* @param {String} props.value The value of the variable | ||
* @param {Boolean} props.isModalVisible Whether the modal is visible | ||
* @param {Function} props.setIsModalVisible Function to set the visibility of the modal | ||
* @returns {React.Component} The modal component | ||
*/ | ||
export default function HouseholdAIModal(props) { | ||
const { isModalVisible, setIsModalVisible, variableName } = props; | ||
|
||
const [analysis, setAnalysis] = useState(""); | ||
const countryId = useCountryId(); | ||
|
||
// Check if variable has changed by its name, not the | ||
// object itself; React will treat two objects with same keys | ||
// and values as different if rendered separately | ||
const prevVariableName = useRef(null); | ||
|
||
const [searchParams] = useSearchParams(); | ||
const householdId = searchParams.get("household"); | ||
// Currently not implemented for baseline/reform comparison pairs | ||
const policyId = COUNTRY_BASELINE_POLICIES[countryId]; | ||
|
||
// Function to hide modal | ||
const handleCancel = () => { | ||
setIsModalVisible(false); | ||
}; | ||
|
||
// Convert this and fetchTracer to async/await | ||
const fetchAnalysis = useCallback(async () => { | ||
const jsonObject = { | ||
household_id: householdId, | ||
policy_id: policyId, | ||
variable: variableName, | ||
}; | ||
|
||
const res = await countryApiCall( | ||
countryId, | ||
`/tracer-analysis`, | ||
jsonObject, | ||
"POST", | ||
); | ||
|
||
const reader = res.body.getReader(); | ||
const decoder = new TextDecoder(); | ||
|
||
let isComplete = false; | ||
while (!isComplete) { | ||
const { done, value } = await reader.read().catch((error) => { | ||
console.error("Error reading response stream:", error); | ||
}); | ||
if (done) { | ||
isComplete = true; | ||
} | ||
const chunks = decoder.decode(value, { stream: true }).split("\n"); | ||
for (const chunk of chunks) { | ||
if (chunk) { | ||
const data = JSON.parse(chunk); | ||
if (data.stream) { | ||
setAnalysis((prevAnalysis) => prevAnalysis + data.stream); | ||
} | ||
} | ||
} | ||
} | ||
}, [countryId, householdId, policyId, variableName]); | ||
|
||
useEffect(() => { | ||
function resetModalData() { | ||
prevVariableName.current = variableName; | ||
} | ||
|
||
// If modal isn't shown, don't do anything | ||
if (!isModalVisible) { | ||
return; | ||
} | ||
|
||
// If variable hasn't changed and we generated analysis, | ||
// don't do anything (e.g., user clicked on same variable) | ||
if (variableName === prevVariableName.current) { | ||
return; | ||
} | ||
|
||
fetchAnalysis(); | ||
resetModalData(); | ||
}, [isModalVisible, variableName, fetchAnalysis]); | ||
|
||
return ( | ||
<Modal | ||
title="Explanation" | ||
open={isModalVisible} | ||
onCancel={handleCancel} // Hide the modal on cancel/close | ||
footer={[ | ||
<Button key="back" onClick={handleCancel}> | ||
Close | ||
</Button>, | ||
]} | ||
width="50%" | ||
> | ||
<MarkdownFormatter markdown={analysis} pSize={14} /> | ||
</Modal> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.