Repository: supabase/supabase
File: apps/studio/components/interfaces/Reports/ReportBlock/ReportBlock.tsx (TypeScript)

Code before:
            setRows(data.result)
            setIsWriteQuery(queryType === 'mutation')
          },
          onError: (mutationError) => {
            const lowerMessage = mutationError.message.toLowerCase()
            const isReadOnlyError =
              lowerMessage.includes('read-only transaction') ||
              lowerMessage.includes('permission denied') ||
              lowerMessage.includes('must be owner')

            if (queryType === 'select' && isReadOnlyError) {
              setIsWriteQuery(true)
            }
          },
        }
      )
      return true
    },
    [projectRef, readOnlyConnectionString, postgresConnectionString, executeSql]
  )

  useEffect(() => {
    if (!isSuccess) return
    if (!isSnippet) return
    const fetchedSql = (data?.content as SqlSnippets.Content | undefined)?.sql
    if (fetchedSql) {
      runQuery('select', fetchedSql)
    }
  }, [data, isSuccess, runQuery, isSnippet])

  const sqlHasChanged = useChangedSync(sql)
  const isRefreshingChanged = useChangedSync(isRefreshing)
  if (sqlHasChanged || (isRefreshingChanged && isRefreshing)) {
    runQuery('select', sql)
  }

  return (
    <>
      {isSnippet ? (
        <QueryBlock
          blockWriteQueries
          id={item.id}
          label={item.label}
          chartConfig={chartConfig}
          sql={sql}
          results={rows}
          initialHideSql={true}
          errorText={snippetMissing ? 'SQL snippet not found' : executeSqlError?.message}
          isExecuting={executeSqlLoading}
          isWriteQuery={isWriteQuery}
          actions={

Review comment (suggestion):
@ivasilov with the refetching, I had to use the parents isRefreshing and then fire. Adding isRefreshing to the query key would fire per change `false` + `true`

Any ideas if this is right? Or anything we can do to make it better

Code after:
    },
    enabled: !isLoadingContent && contentError == null,
    refetchOnWindowFocus: false,
  })

  const rows = queryResult?.result

  useEffect(() => {
    if (executeSqlError) {
      const errorMessage = String(executeSqlError).toLowerCase()
      const isReadOnlyError =
        errorMessage.includes('read-only transaction') ||
        errorMessage.includes('permission denied') ||
        errorMessage.includes('must be owner')

      if (isReadOnlyError) {
        setIsWriteQuery(true)
      }
    }
  }, [executeSqlError])

  useEffect(() => {
    if (isRefreshing) {
      refetch()
    }
  }, [isRefreshing, refetch])

  return (
    <>
      {isSnippet ? (
        <QueryBlock
          blockWriteQueries
          id={item.id}
          label={item.label}
          chartConfig={chartConfig}
          sql={sql}
          results={rows}
          initialHideSql={true}
          errorText={
            snippetMissing
              ? 'SQL snippet not found'
              : executeSqlError
                ? String(executeSqlError)
                : undefined
          }
          isExecuting={executeSqlLoading}
          isWriteQuery={isWriteQuery}
          actions={
            !isLoadingContent && (
              <ButtonTooltip
                type="text"