Fully autonomous AI Agents system capable of performing complex penetration testing tasks
Go ai-agentsai-security-toolanthropicautonomous-agentsgolanggptgraphqlmulti-agent-systemoffensive-securityopen-sourceopenaipenetration-testingpenetration-testing-toolsreactsecurity-automationsecurity-testingsecurity-toolsself-hosted
perf(frontend): useOptimistic for flow rename and favorite toggle
Sergey Kozyrenko73c6652dbf88
diff --git a/frontend/src/pages/flows/flow.tsx b/frontend/src/pages/flows/flow.tsxindex 3f4b6c9b..d3ead6ec 100644--- a/frontend/src/pages/flows/flow.tsx+++ b/frontend/src/pages/flows/flow.tsx@@ -15,7 +15,7 @@ import {Star,Trash,} from 'lucide-react';-import { useCallback, useEffect, useState } from 'react';+import { startTransition, useCallback, useEffect, useOptimistic, useState } from 'react';import { useNavigate } from 'react-router-dom';import { toast } from 'sonner';@@ -82,7 +82,12 @@ function Flow() {const { isFavoriteFlow, toggleFavoriteFlow } = useFavorites();const flow = flowData?.flow;- const flowTitle = flow?.title ?? '';+ const actualFlowTitle = flow?.title ?? '';+ // Surface an optimistic title while the rename mutation is in flight so the+ // breadcrumb, document title and edit affordance flip immediately on Save.+ // If the mutation rejects, React rolls back to `actualFlowTitle` on its own+ // and we surface the toast below — no manual reconciliation needed.+ const [flowTitle, setOptimisticFlowTitle] = useOptimistic(actualFlowTitle, (_current, next: string) => next);const isFlowRunning = flow ? ![StatusType.Failed, StatusType.Finished].includes(flow.status) : false;// Single controller drives the desktop toolbar AND the mobile dropdown@@ -117,23 +122,33 @@ function Flow() {return;}- try {- const { data } = await renameFlowMutation({- variables: {- flowId,- title: newTitle,- },- });-- if (data?.renameFlow === ResultType.Success) {- toast.success('Flow renamed successfully');- handleFlowRenameCancel();+ // Drop the new title into the optimistic state immediately so the+ // breadcrumb/tab/PageTitle flip before the network round-trip. The+ // optimistic value lives only inside this transition — once the+ // mutation settles, useOptimistic falls back to the Apollo cache+ // (which the mutation response has already updated on success, or+ // left untouched on error).+ startTransition(async () => {+ setOptimisticFlowTitle(newTitle);++ try {+ const { data } = await renameFlowMutation({+ variables: {+ flowId,+ title: newTitle,+ },+ });++ if (data?.renameFlow === ResultType.Success) {+ toast.success('Flow renamed successfully');+ handleFlowRenameCancel();+ }+ } catch (error) {+ const errorMessage = error instanceof Error ? error.message : 'Failed to rename flow';+ toast.error(errorMessage);}- } catch (error) {- const errorMessage = error instanceof Error ? error.message : 'Failed to rename flow';- toast.error(errorMessage);- }- }, [editingInputRef, flowId, handleFlowRenameCancel, renameFlowMutation]);+ });+ }, [editingInputRef, flowId, handleFlowRenameCancel, renameFlowMutation, setOptimisticFlowTitle]);const handleFlowFinish = useCallback(async () => {if (!flow) {diff --git a/frontend/src/providers/favorites-provider.tsx b/frontend/src/providers/favorites-provider.tsxindex b0dd7545..a417dd79 100644--- a/frontend/src/providers/favorites-provider.tsx+++ b/frontend/src/providers/favorites-provider.tsx@@ -1,4 +1,4 @@-import { createContext, type ReactNode, useCallback, useContext, useEffect, useMemo } from 'react';+import { createContext, type ReactNode, startTransition, useCallback, useContext, useEffect, useMemo, useOptimistic } from 'react';import { toast } from 'sonner';import {@@ -51,12 +51,26 @@ export function FavoritesProvider({ children }: FavoritesProviderProps) {});// Get favorite flow IDs from GraphQL as numbers- const favoriteFlowIds = useMemo(() => {+ const actualFavoriteFlowIds = useMemo(() => {const ids = userPreferencesData?.settingsUser?.favoriteFlows ?? [];return ids.map((id) => +id);}, [userPreferencesData?.settingsUser?.favoriteFlows]);+ // Surface the optimistic set so star-clicks flip in the UI before the+ // mutation + subscription round-trip lands. React rolls back to+ // `actualFavoriteFlowIds` automatically if the transition's action throws.+ const [favoriteFlowIds, applyOptimisticFavorite] = useOptimistic(+ actualFavoriteFlowIds,+ (current: number[], action: { id: number; type: 'add' | 'remove' }) => {+ if (action.type === 'add') {+ return current.includes(action.id) ? current : [...current, action.id];+ }++ return current.filter((id) => id !== action.id);+ },+ );+// Migration: sync localStorage favorites to backend on first loaduseEffect(() => {const migrateLocalStorageFavorites = async () => {@@ -131,39 +145,49 @@ export function FavoritesProvider({ children }: FavoritesProviderProps) {const addFavoriteFlow = useCallback(async (flowId: number | string) => {const id = typeof flowId === 'string' ? flowId : flowId.toString();-- try {- await addFavoriteFlowMutation({- variables: { flowId: id },- });- } catch (error) {- const errorMessage = error instanceof Error ? error.message : 'Failed to add favorite';- toast.error('Failed to add to favorites', {- description: errorMessage,- });- Log.error('Error adding favorite flow:', error);- }+ const numericId = typeof flowId === 'string' ? +flowId : flowId;++ startTransition(async () => {+ applyOptimisticFavorite({ id: numericId, type: 'add' });++ try {+ await addFavoriteFlowMutation({+ variables: { flowId: id },+ });+ } catch (error) {+ const errorMessage = error instanceof Error ? error.message : 'Failed to add favorite';+ toast.error('Failed to add to favorites', {+ description: errorMessage,+ });+ Log.error('Error adding favorite flow:', error);+ }+ });},- [addFavoriteFlowMutation],+ [addFavoriteFlowMutation, applyOptimisticFavorite],);const removeFavoriteFlow = useCallback(async (flowId: number | string) => {const id = typeof flowId === 'string' ? flowId : flowId.toString();-- try {- await deleteFavoriteFlowMutation({- variables: { flowId: id },- });- } catch (error) {- const errorMessage = error instanceof Error ? error.message : 'Failed to remove favorite';- toast.error('Failed to remove from favorites', {- description: errorMessage,- });- Log.error('Error removing favorite flow:', error);- }+ const numericId = typeof flowId === 'string' ? +flowId : flowId;++ startTransition(async () => {+ applyOptimisticFavorite({ id: numericId, type: 'remove' });++ try {+ await deleteFavoriteFlowMutation({+ variables: { flowId: id },+ });+ } catch (error) {+ const errorMessage = error instanceof Error ? error.message : 'Failed to remove favorite';+ toast.error('Failed to remove from favorites', {+ description: errorMessage,+ });+ Log.error('Error removing favorite flow:', error);+ }+ });},- [deleteFavoriteFlowMutation],+ [applyOptimisticFavorite, deleteFavoriteFlowMutation],);const toggleFavoriteFlow = useCallback(