Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
233 changes: 39 additions & 194 deletions src/components/Draft/DraftTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import { useDraftFirstResponse } from "./useDraftFirstResponse";
import { useDraftGeneration } from "./useDraftGeneration";
import { useDraftIntake } from "./useDraftIntake";
import { useDraftPersistence } from "./useDraftPersistence";
import { useGuidedRunbook } from "./useGuidedRunbook";
import { useWorkspaceClipboardPacks } from "./useWorkspaceClipboardPacks";
import { ConversationInput } from "./ConversationInput";
Expand Down Expand Up @@ -47,10 +48,6 @@
compactLines,
parseCaseIntake,
} from "../../features/workspace/workspaceAssistant";
import {
shouldMigrateVisibleRunbookSession,
shouldProceedAfterSaveAttempt,
} from "../../features/workspace/workspaceDraftSession";
import {
calculateEditRatio,
countWords,
Expand Down Expand Up @@ -538,7 +535,7 @@
return next;
});
},
[savedDraftId],

Check warning on line 538 in src/components/Draft/DraftTab.tsx

View workflow job for this annotation

GitHub Actions / quality-gates

React Hook useCallback has a missing dependency: 'setCaseIntake'. Either include it or remove the dependency array
);

const handleRefreshSimilarCases = useCallback(async () => {
Expand Down Expand Up @@ -711,7 +708,7 @@
setPendingSimilarCaseOpen(null);
setWorkspaceRunbookScopeKey(createWorkspaceRunbookScopeKey());
resetGeneration();
}, [workspacePersonalization.preferred_note_audience, resetGeneration]);

Check warning on line 711 in src/components/Draft/DraftTab.tsx

View workflow job for this annotation

GitHub Actions / quality-gates

React Hook useCallback has missing dependencies: 'resetApproval', 'resetChecklist', 'resetFirstResponse', 'setCaseIntake', 'setGuidedRunbookNote', 'setGuidedRunbookSession', 'setPendingSimilarCaseOpen', 'setRunbookSessionSourceScopeKey', and 'setRunbookSessionTouched'. Either include them or remove the dependency array

const handleResponseChange = useCallback(
(text: string) => {
Expand Down Expand Up @@ -796,7 +793,7 @@
setGenerating(false);
}
},
[modelLoaded, responseLength, generateStreaming, clearStreamingText],

Check warning on line 796 in src/components/Draft/DraftTab.tsx

View workflow job for this annotation

GitHub Actions / quality-gates

React Hook useCallback has a missing dependency: 'setGenerating'. Either include it or remove the dependency array
);

const handleCancel = useCallback(async () => {
Expand All @@ -808,7 +805,7 @@
setOriginalResponse(streamingText);
setIsResponseEdited(false);
}
}, [cancelGeneration, streamingText]);

Check warning on line 808 in src/components/Draft/DraftTab.tsx

View workflow job for this annotation

GitHub Actions / quality-gates

React Hook useCallback has a missing dependency: 'setGenerating'. Either include it or remove the dependency array

useEffect(() => {
if (viewMode !== "panels") {
Expand Down Expand Up @@ -1125,7 +1122,7 @@
});
showSuccess(`Applied ${kit.name}`);
},
[input, response, caseIntake, logEvent, currentTicketId, showSuccess],

Check warning on line 1125 in src/components/Draft/DraftTab.tsx

View workflow job for this annotation

GitHub Actions / quality-gates

React Hook useCallback has a missing dependency: 'setCaseIntake'. Either include it or remove the dependency array
);

const handleToggleWorkspaceFavorite = useCallback(
Expand Down Expand Up @@ -1204,7 +1201,7 @@
showError("Failed to open similar case");
}
},
[

Check warning on line 1204 in src/components/Draft/DraftTab.tsx

View workflow job for this annotation

GitHub Actions / quality-gates

React Hook useCallback has a missing dependency: 'setPendingSimilarCaseOpen'. Either include it or remove the dependency array
loadSimilarCaseIntoWorkspace,
logEvent,
currentTicketId,
Expand Down Expand Up @@ -1298,7 +1295,7 @@

void handleCopyKbDraft();
},
[

Check warning on line 1298 in src/components/Draft/DraftTab.tsx

View workflow job for this annotation

GitHub Actions / quality-gates

React Hook useCallback has missing dependencies: 'setApprovalQuery' and 'setCaseIntake'. Either include them or remove the dependency array
logEvent,
currentTicketId,
handleGenerate,
Expand All @@ -1324,202 +1321,50 @@
onCompareLastResolution: handleCompareLastResolution,
});

const handleSaveDraft = useCallback(async () => {
if (!hasSaveableWorkspaceContent) {
showError("Cannot save empty draft");
return null;
}

const diagnosisData = buildDiagnosisJson();
const currentCreatedAt = savedDraftCreatedAt ?? new Date().toISOString();
const draftPayload = {
input_text: input,
summary_text: currentTicket?.summary ?? null,
diagnosis_json: diagnosisData,
response_text: response || null,
ticket_id: currentTicketId,
kb_sources_json: sources.length > 0 ? JSON.stringify(sources) : null,
is_autosave: false,
model_name: loadedModelName,
case_intake_json: serializedCaseIntake,
handoff_summary: handoffPack.summary,
status: "draft" as const,
};

const draftId = savedDraftId
? await updateDraft({
id: savedDraftId,
created_at: currentCreatedAt,
updated_at: activeWorkspaceDraft.updated_at,
finalized_at: null,
finalized_by: null,
...draftPayload,
})
: await saveDraft(draftPayload);

if (draftId) {
const nextScopeKey = `draft:${draftId}`;
let runbookScopeLinked = true;
if (workspaceRunbookScopeKey !== nextScopeKey) {
try {
const shouldMigrateActiveRunbookSession = guidedRunbookSession
? shouldMigrateVisibleRunbookSession({
hasGuidedRunbookSession: true,
runbookSessionTouched,
runbookSessionSourceScopeKey,
workspaceRunbookScopeKey,
})
: false;

const activeRunbookSessionId = guidedRunbookSession?.id ?? null;
if (shouldMigrateActiveRunbookSession && activeRunbookSessionId) {
await reassignRunbookSessionById(
activeRunbookSessionId,
nextScopeKey,
);
} else {
await reassignRunbookSessionScope(
workspaceRunbookScopeKey,
nextScopeKey,
);
}
setWorkspaceRunbookScopeKey(nextScopeKey);
setRunbookSessionSourceScopeKey(nextScopeKey);
} catch {
runbookScopeLinked = false;
}
}
setAutosaveDraftId(null);
setSavedDraftId(draftId);
setSavedDraftCreatedAt(currentCreatedAt);
const responseWordCount = countWords(response);
const editRatio = calculateEditRatio(originalResponse, response);
logEvent("response_saved", {
draft_id: draftId,
word_count: responseWordCount,
is_edited: isResponseEdited,
edit_ratio: Number(editRatio.toFixed(3)),
});
if (runbookScopeLinked) {
showSuccess("Draft saved");
} else {
showError(
"Draft saved, but guided runbook progress stayed attached to the previous workspace state",
);
}
return draftId;
}
return null;
}, [
activeWorkspaceDraft.updated_at,
buildDiagnosisJson,
currentTicket?.summary,
currentTicketId,
guidedRunbookSession,
handoffPack.summary,
hasSaveableWorkspaceContent,
const {
handleSaveDraft,
handleConfirmOpenDraft,
handleConfirmOpenSimilarCase,
} = useDraftPersistence({
input,
isResponseEdited,
loadedModelName,
logEvent,
originalResponse,
reassignRunbookSessionById,
reassignRunbookSessionScope,
response,
sources,
currentTicket,
currentTicketId,
savedDraftId,
savedDraftCreatedAt,
runbookSessionSourceScopeKey,
loadedModelName,
handoffPack,
serializedCaseIntake,
isResponseEdited,
originalResponse,
hasSaveableWorkspaceContent,
activeWorkspaceDraft,
workspaceRunbookScopeKey,
guidedRunbookSession,
runbookSessionTouched,
savedDraftId,
runbookSessionSourceScopeKey,
buildDiagnosisJson,
saveDraft,
serializedCaseIntake,
showError,
showSuccess,
sources,
updateDraft,
workspaceRunbookScopeKey,
]);

const handleConfirmOpenSimilarCase = useCallback(
async (mode: "replace" | "save-and-open" | "compare") => {
if (!pendingSimilarCaseOpen) {
return;
}

if (mode === "compare") {
setCompareCase(pendingSimilarCaseOpen);
setPendingSimilarCaseOpen(null);
return;
}

try {
if (mode === "save-and-open") {
const savedId = await handleSaveDraft();
if (!shouldProceedAfterSaveAttempt(mode, savedId)) {
return;
}
}

await loadSimilarCaseIntoWorkspace(pendingSimilarCaseOpen);
setPendingSimilarCaseOpen(null);
void logEvent("workspace_similar_case_opened", {
ticket_id: currentTicketId,
similar_case_id: pendingSimilarCaseOpen.draft_id,
similar_case_ticket: pendingSimilarCaseOpen.ticket_id,
open_mode: mode,
});
showSuccess(
mode === "save-and-open"
? "Saved the current workspace and opened the saved case"
: "Opened the saved case in the workspace",
);
} catch {
showError("Failed to open the saved case");
}
},
[
currentTicketId,
handleSaveDraft,
loadSimilarCaseIntoWorkspace,
logEvent,
pendingSimilarCaseOpen,
showError,
showSuccess,
],
);

const handleConfirmOpenDraft = useCallback(
async (mode: "replace" | "save-and-open") => {
if (!pendingDraftOpen) {
return;
}

try {
if (mode === "save-and-open") {
const savedId = await handleSaveDraft();
if (!shouldProceedAfterSaveAttempt(mode, savedId)) {
return;
}
}

applyLoadedDraft(pendingDraftOpen);
setPendingDraftOpen(null);
showSuccess(
mode === "save-and-open"
? "Saved the current workspace and opened the selected draft"
: "Opened the selected draft in the workspace",
);
} catch {
showError("Failed to open the selected draft");
}
},
[
applyLoadedDraft,
handleSaveDraft,
pendingDraftOpen,
showError,
showSuccess,
],
);
reassignRunbookSessionById,
reassignRunbookSessionScope,
logEvent,
setWorkspaceRunbookScopeKey,
setRunbookSessionSourceScopeKey,
setAutosaveDraftId,
setSavedDraftId,
setSavedDraftCreatedAt,
pendingDraftOpen,
setPendingDraftOpen,
applyLoadedDraft,
pendingSimilarCaseOpen,
setPendingSimilarCaseOpen,
loadSimilarCaseIntoWorkspace,
setCompareCase,
onShowSuccess: showSuccess,
onShowError: showError,
});

// Load initial draft if provided
useEffect(() => {
Expand Down
Loading
Loading