Skip to content

Commit

Permalink
fix(frontend): editor data updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ashrafchowdury committed Dec 16, 2024
1 parent 7108890 commit d043d39
Showing 1 changed file with 41 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -406,54 +406,58 @@ const TestsetDrawer = ({onClose, data, ...props}: TestsetDrawerProps) => {
useUpdateEffect(() => {
if (updatedData && updatedData !== formatDataPreview) {
const updatedTraceData = traceData.map((trace) => {
const isMatchingOriginalData =
trace.originalData &&
updatedData === getStringOrJson({data: trace.originalData})
const isMatchingKey = trace.key === rowDataPreview
const isMatchingData = updatedData !== getStringOrJson({data: trace.data})

try {
const parsedUpdatedData = JSON.parse(updatedData)

if (isMatchingOriginalData) {
return isMatchingKey
? {
...trace,
...parsedUpdatedData,
isEdited: false,
originalData: null,
isError: false,
}
: trace
}
const parsedUpdatedData =
typeof updatedData === "string" ? JSON.parse(updatedData) : updatedData

const originalDataString = getStringOrJson({data: trace.originalData})
const updatedDataString = getStringOrJson(parsedUpdatedData)

const isMatchingOriginalData = originalDataString === updatedDataString
const isMatchingData = updatedDataString !== getStringOrJson({data: trace.data})

if (isMatchingKey) {
return {
...trace,
...parsedUpdatedData,
...(isMatchingData && !trace.originalData
? {originalData: trace.data}
: {}),
isEdited: true,
isError: false,
if (isMatchingOriginalData) {
return {
...trace,
...parsedUpdatedData,
isEdited: false,
originalData: null,
isError: false,
}
} else {
return {
...trace,
...parsedUpdatedData,
...(isMatchingData && !trace.originalData
? {originalData: trace.data}
: {}),
isEdited: true,
isError: false,
}
}
}

return trace
} catch (error) {
if (isMatchingKey) {
return {...trace, isError: true}
}
return isMatchingKey ? {...trace, isError: true} : trace
}

return trace
})

setTraceData(updatedTraceData)
} else if (updatedData && updatedData == formatDataPreview && selectedTraceData?.isError) {
setTraceData((prevTraceData) => {
return prevTraceData.map((trace) => {
return trace.key === rowDataPreview ? {...trace, isError: false} : trace
})
})
// Only update if there are actual changes
setTraceData((prevTraceData) =>
JSON.stringify(prevTraceData) !== JSON.stringify(updatedTraceData)
? updatedTraceData
: prevTraceData,
)
} else if (updatedData && updatedData === formatDataPreview && selectedTraceData?.isError) {
setTraceData((prevTraceData) =>
prevTraceData.map((trace) =>
trace.key === rowDataPreview ? {...trace, isError: false} : trace,
),
)
}
}, [updatedData])

Expand Down

0 comments on commit d043d39

Please sign in to comment.