Skip to content

Commit 5ab2b5f

Browse files
committed
fix(editor): stabilize async option refetching
1 parent 2bb744a commit 5ab2b5f

File tree

3 files changed

+23
-11
lines changed
  • apps/sim/app
    • api/tools/opencode/prompt
    • workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components

3 files changed

+23
-11
lines changed

apps/sim/app/api/tools/opencode/prompt/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export async function POST(request: NextRequest) {
176176

177177
return buildSuccessResponse(result.threadId, result.content, result.cost)
178178
} catch (error) {
179-
if (reusedStoredThread && threadId && !newThread && shouldRetryWithFreshOpenCodeSession(error)) {
179+
if (reusedStoredThread && threadId && shouldRetryWithFreshOpenCodeSession(error)) {
180180
let freshSessionId = threadId
181181

182182
try {

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/combobox/combobox.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,30 +127,35 @@ export const ComboBox = memo(function ComboBox({
127127
const [hydratedOption, setHydratedOption] = useState<{ label: string; id: string } | null>(null)
128128
const previousDependencyValuesRef = useRef<string>('')
129129
const isOptionsFetchInFlightRef = useRef(false)
130+
const fetchErrorRef = useRef<string | null>(null)
131+
const hasAttemptedOptionsFetchRef = useRef(false)
130132

131133
/**
132134
* Fetches options from the async fetchOptions function if provided
133135
*/
134-
const fetchOptionsIfNeeded = useCallback(async (force = false) => {
136+
const fetchOptionsIfNeeded = useCallback(async (force = fetchErrorRef.current !== null) => {
135137
if (
136138
!fetchOptions ||
137139
isPreview ||
138140
disabled ||
139-
(!force && hasAttemptedOptionsFetch) ||
141+
(!force && hasAttemptedOptionsFetchRef.current) ||
140142
isOptionsFetchInFlightRef.current
141143
) {
142144
return
143145
}
144146

145147
isOptionsFetchInFlightRef.current = true
148+
hasAttemptedOptionsFetchRef.current = true
146149
setHasAttemptedOptionsFetch(true)
150+
fetchErrorRef.current = null
147151
setIsLoadingOptions(true)
148152
setFetchError(null)
149153
try {
150154
const options = await fetchOptions(blockId, subBlockId)
151155
setFetchedOptions(options)
152156
} catch (error) {
153157
const errorMessage = error instanceof Error ? error.message : 'Failed to fetch options'
158+
fetchErrorRef.current = errorMessage
154159
setFetchError(errorMessage)
155160
setFetchedOptions([])
156161
} finally {
@@ -163,7 +168,6 @@ export const ComboBox = memo(function ComboBox({
163168
subBlockId,
164169
isPreview,
165170
disabled,
166-
hasAttemptedOptionsFetch,
167171
])
168172

169173
// Determine the active value based on mode (preview vs. controlled vs. store)
@@ -328,7 +332,9 @@ export const ComboBox = memo(function ComboBox({
328332
currentDependencyValuesStr !== previousDependencyValuesStr
329333
) {
330334
setFetchedOptions([])
335+
fetchErrorRef.current = null
331336
setFetchError(null)
337+
hasAttemptedOptionsFetchRef.current = false
332338
setHasAttemptedOptionsFetch(false)
333339
setHydratedOption(null)
334340
}
@@ -454,10 +460,10 @@ export const ComboBox = memo(function ComboBox({
454460
const handleOpenChange = useCallback(
455461
(open: boolean) => {
456462
if (open) {
457-
void fetchOptionsIfNeeded(fetchError !== null)
463+
void fetchOptionsIfNeeded()
458464
}
459465
},
460-
[fetchError, fetchOptionsIfNeeded]
466+
[fetchOptionsIfNeeded]
461467
)
462468

463469
/**

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/dropdown/dropdown.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ export const Dropdown = memo(function Dropdown({
133133
const previousModeRef = useRef<string | null>(null)
134134
const previousDependencyValuesRef = useRef<string>('')
135135
const isOptionsFetchInFlightRef = useRef(false)
136+
const fetchErrorRef = useRef<string | null>(null)
137+
const hasAttemptedOptionsFetchRef = useRef(false)
136138

137139
const [builderData, setBuilderData] = useSubBlockValue<any[]>(blockId, 'builderData')
138140
const [data, setData] = useSubBlockValue<string>(blockId, 'data')
@@ -156,26 +158,29 @@ export const Dropdown = memo(function Dropdown({
156158
: []
157159
: null
158160

159-
const fetchOptionsIfNeeded = useCallback(async (force = false) => {
161+
const fetchOptionsIfNeeded = useCallback(async (force = fetchErrorRef.current !== null) => {
160162
if (
161163
!fetchOptions ||
162164
isPreview ||
163165
disabled ||
164-
(!force && hasAttemptedOptionsFetch) ||
166+
(!force && hasAttemptedOptionsFetchRef.current) ||
165167
isOptionsFetchInFlightRef.current
166168
) {
167169
return
168170
}
169171

170172
isOptionsFetchInFlightRef.current = true
173+
hasAttemptedOptionsFetchRef.current = true
171174
setHasAttemptedOptionsFetch(true)
175+
fetchErrorRef.current = null
172176
setIsLoadingOptions(true)
173177
setFetchError(null)
174178
try {
175179
const options = await fetchOptions(blockId, subBlockId)
176180
setFetchedOptions(options)
177181
} catch (error) {
178182
const errorMessage = error instanceof Error ? error.message : 'Failed to fetch options'
183+
fetchErrorRef.current = errorMessage
179184
setFetchError(errorMessage)
180185
setFetchedOptions([])
181186
} finally {
@@ -188,7 +193,6 @@ export const Dropdown = memo(function Dropdown({
188193
subBlockId,
189194
isPreview,
190195
disabled,
191-
hasAttemptedOptionsFetch,
192196
])
193197

194198
/**
@@ -197,10 +201,10 @@ export const Dropdown = memo(function Dropdown({
197201
const handleOpenChange = useCallback(
198202
(open: boolean) => {
199203
if (open) {
200-
void fetchOptionsIfNeeded(fetchError !== null)
204+
void fetchOptionsIfNeeded()
201205
}
202206
},
203-
[fetchError, fetchOptionsIfNeeded]
207+
[fetchOptionsIfNeeded]
204208
)
205209

206210
const evaluatedOptions = useMemo(() => {
@@ -391,7 +395,9 @@ export const Dropdown = memo(function Dropdown({
391395
currentDependencyValuesStr !== previousDependencyValuesStr
392396
) {
393397
setFetchedOptions([])
398+
fetchErrorRef.current = null
394399
setFetchError(null)
400+
hasAttemptedOptionsFetchRef.current = false
395401
setHasAttemptedOptionsFetch(false)
396402
setHydratedOption(null)
397403
}

0 commit comments

Comments
 (0)