|
| 1 | +import { type App, Modal, Notice } from 'obsidian'; |
| 2 | + |
| 3 | +import { t } from '../../../i18n/i18n'; |
| 4 | +import type { QoderHostContext } from '../../../qoder/qoder-host-context'; |
| 5 | +import { |
| 6 | + countFeedbackChars, |
| 7 | + FEEDBACK_CONTENT_LIMIT, |
| 8 | + submitUserFeedback, |
| 9 | + type UserFeedbackOutcome, |
| 10 | +} from '../../../qoder/services/submit-user-feedback'; |
| 11 | + |
| 12 | +export interface FeedbackModalContext { |
| 13 | + plugin: QoderHostContext; |
| 14 | + /** Active chat session, attached so the team can correlate the report. */ |
| 15 | + sessionId?: string; |
| 16 | + /** Plugin version reported as `--caller-version`. */ |
| 17 | + callerVersion?: string; |
| 18 | + /** Starts the existing device-flow sign-in; shown when a rejection may be auth-related. */ |
| 19 | + onRequestSignIn?: () => void; |
| 20 | +} |
| 21 | + |
| 22 | +type FeedbackFailure = Extract<UserFeedbackOutcome, { ok: false }>; |
| 23 | +type FeedbackSuccess = Extract<UserFeedbackOutcome, { ok: true }>; |
| 24 | + |
| 25 | +export function openFeedbackModal( |
| 26 | + app: App, |
| 27 | + context: FeedbackModalContext, |
| 28 | +): Promise<UserFeedbackOutcome | null> { |
| 29 | + return new Promise(resolve => { |
| 30 | + new FeedbackModal(app, context, resolve).open(); |
| 31 | + }); |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * qodercli reports a missing session in prose rather than as a status code, so |
| 36 | + * the sign-in affordance keys off its wording. Anything else — an unsupported |
| 37 | + * flag, a network failure — is shown verbatim without suggesting a re-login. |
| 38 | + */ |
| 39 | +const AUTH_FAILURE_HINT = /log ?in|logged in|unauthor|unauthenticat|credential/i; |
| 40 | + |
| 41 | +function isAuthFailure(detail: string | undefined): boolean { |
| 42 | + return !!detail && AUTH_FAILURE_HINT.test(detail); |
| 43 | +} |
| 44 | + |
| 45 | +function describeFailure(failure: FeedbackFailure): string { |
| 46 | + switch (failure.reason) { |
| 47 | + case 'cliUnavailable': |
| 48 | + return t('feedback.errorCliUnavailable'); |
| 49 | + case 'emptyContent': |
| 50 | + return t('feedback.errorEmpty'); |
| 51 | + case 'contentTooLong': |
| 52 | + return t('feedback.errorTooLong', { limit: FEEDBACK_CONTENT_LIMIT }); |
| 53 | + case 'rejected': |
| 54 | + return failure.detail?.trim() || t('feedback.errorRejected'); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +class FeedbackModal extends Modal { |
| 59 | + private readonly context: FeedbackModalContext; |
| 60 | + private readonly resolveOutcome: (outcome: UserFeedbackOutcome | null) => void; |
| 61 | + private resolved = false; |
| 62 | + private closed = false; |
| 63 | + private submitting = false; |
| 64 | + private includeDiagnostics = true; |
| 65 | + |
| 66 | + constructor( |
| 67 | + app: App, |
| 68 | + context: FeedbackModalContext, |
| 69 | + resolve: (outcome: UserFeedbackOutcome | null) => void, |
| 70 | + ) { |
| 71 | + super(app); |
| 72 | + this.context = context; |
| 73 | + this.resolveOutcome = resolve; |
| 74 | + } |
| 75 | + |
| 76 | + onOpen(): void { |
| 77 | + this.setTitle(t('feedback.title')); |
| 78 | + this.modalEl.addClass('qoderian-feedback-modal'); |
| 79 | + |
| 80 | + const cliAvailable = this.context.plugin.getResolvedQoderCliPath() !== null; |
| 81 | + |
| 82 | + if (!cliAvailable) { |
| 83 | + this.contentEl.createDiv({ |
| 84 | + cls: 'qoderian-feedback-banner', |
| 85 | + text: t('feedback.errorCliUnavailable'), |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + const textarea = this.contentEl.createEl('textarea', { |
| 90 | + cls: 'qoderian-feedback-textarea', |
| 91 | + attr: { rows: '6', placeholder: t('feedback.placeholder') }, |
| 92 | + }); |
| 93 | + |
| 94 | + const counter = this.contentEl.createDiv({ cls: 'qoderian-feedback-counter' }); |
| 95 | + |
| 96 | + const emailField = this.contentEl.createDiv({ cls: 'qoderian-feedback-field' }); |
| 97 | + emailField.createEl('label', { |
| 98 | + cls: 'qoderian-feedback-label', |
| 99 | + text: t('feedback.emailLabel'), |
| 100 | + }); |
| 101 | + const email = emailField.createEl('input', { |
| 102 | + cls: 'qoderian-feedback-input', |
| 103 | + attr: { type: 'email', placeholder: t('feedback.emailPlaceholder') }, |
| 104 | + }); |
| 105 | + emailField.createDiv({ cls: 'qoderian-feedback-hint', text: t('feedback.emailHint') }); |
| 106 | + |
| 107 | + const diagnostics = this.contentEl.createDiv({ cls: 'qoderian-feedback-diagnostics' }); |
| 108 | + const diagnosticsToggle = diagnostics.createEl('input', { |
| 109 | + cls: 'qoderian-feedback-checkbox', |
| 110 | + attr: { type: 'checkbox' }, |
| 111 | + }); |
| 112 | + diagnosticsToggle.checked = this.includeDiagnostics; |
| 113 | + const diagnosticsText = diagnostics.createDiv({ cls: 'qoderian-feedback-diagnostics-text' }); |
| 114 | + diagnosticsText.createDiv({ |
| 115 | + cls: 'qoderian-feedback-label', |
| 116 | + text: t('feedback.diagnosticsLabel'), |
| 117 | + }); |
| 118 | + diagnosticsText.createDiv({ |
| 119 | + cls: 'qoderian-feedback-hint', |
| 120 | + text: t('feedback.diagnosticsDesc'), |
| 121 | + }); |
| 122 | + |
| 123 | + if (this.context.sessionId) { |
| 124 | + this.contentEl.createDiv({ |
| 125 | + cls: 'qoderian-feedback-session', |
| 126 | + text: t('feedback.sessionHint', { sessionId: this.context.sessionId.slice(0, 8) }), |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + const error = this.contentEl.createDiv({ cls: 'qoderian-feedback-error qoderian-hidden' }); |
| 131 | + |
| 132 | + const actions = this.contentEl.createDiv({ cls: 'qoderian-feedback-actions' }); |
| 133 | + const cancelButton = actions.createEl('button', { |
| 134 | + cls: 'qoderian-feedback-button', |
| 135 | + text: t('common.cancel'), |
| 136 | + }); |
| 137 | + const submitButton = actions.createEl('button', { |
| 138 | + cls: 'qoderian-feedback-button qoderian-feedback-button--primary', |
| 139 | + text: t('feedback.submit'), |
| 140 | + }); |
| 141 | + |
| 142 | + const hideError = (): void => { |
| 143 | + error.empty(); |
| 144 | + error.addClass('qoderian-hidden'); |
| 145 | + }; |
| 146 | + |
| 147 | + const refresh = (): void => { |
| 148 | + const count = countFeedbackChars(textarea.value ?? ''); |
| 149 | + counter.setText(`${count} / ${FEEDBACK_CONTENT_LIMIT}`); |
| 150 | + if (count > FEEDBACK_CONTENT_LIMIT) { |
| 151 | + counter.addClass('qoderian-feedback-counter--over'); |
| 152 | + } else { |
| 153 | + counter.removeClass('qoderian-feedback-counter--over'); |
| 154 | + } |
| 155 | + |
| 156 | + // Over-limit input is rejected rather than silently truncated, so the |
| 157 | + // user never believes a shortened version was submitted. |
| 158 | + const submittable = |
| 159 | + cliAvailable && !this.submitting && count > 0 && count <= FEEDBACK_CONTENT_LIMIT; |
| 160 | + submitButton.disabled = !submittable; |
| 161 | + }; |
| 162 | + |
| 163 | + const setBusy = (busy: boolean): void => { |
| 164 | + this.submitting = busy; |
| 165 | + submitButton.setText(busy ? t('feedback.submitting') : t('feedback.submit')); |
| 166 | + cancelButton.disabled = busy; |
| 167 | + textarea.disabled = busy; |
| 168 | + email.disabled = busy; |
| 169 | + diagnosticsToggle.disabled = busy; |
| 170 | + refresh(); |
| 171 | + }; |
| 172 | + |
| 173 | + const showError = (message: string, signIn: (() => void) | null): void => { |
| 174 | + error.empty(); |
| 175 | + error.createDiv({ cls: 'qoderian-feedback-error-message', text: message }); |
| 176 | + if (signIn) { |
| 177 | + const signInButton = error.createEl('button', { |
| 178 | + cls: 'qoderian-feedback-signin', |
| 179 | + text: t('feedback.signIn'), |
| 180 | + }); |
| 181 | + signInButton.addEventListener('click', () => { |
| 182 | + signIn(); |
| 183 | + this.close(); |
| 184 | + }); |
| 185 | + } |
| 186 | + error.removeClass('qoderian-hidden'); |
| 187 | + }; |
| 188 | + |
| 189 | + const submit = async (): Promise<void> => { |
| 190 | + if (this.submitting || !cliAvailable) return; |
| 191 | + hideError(); |
| 192 | + setBusy(true); |
| 193 | + |
| 194 | + const outcome = await submitUserFeedback(this.context.plugin, { |
| 195 | + content: textarea.value ?? '', |
| 196 | + email: email.value ?? '', |
| 197 | + sessionId: this.context.sessionId, |
| 198 | + callerVersion: this.context.callerVersion, |
| 199 | + includeWorkspaceDiagnostics: this.includeDiagnostics, |
| 200 | + }); |
| 201 | + |
| 202 | + if (outcome.ok) { |
| 203 | + this.resolved = true; |
| 204 | + this.resolveOutcome(outcome); |
| 205 | + this.close(); |
| 206 | + await this.announceSuccess(outcome); |
| 207 | + return; |
| 208 | + } |
| 209 | + |
| 210 | + // `submitFeedback` cannot be aborted, so the modal may already be gone by |
| 211 | + // the time it settles. Report through a Notice rather than detached DOM. |
| 212 | + if (this.closed) { |
| 213 | + new Notice(t('feedback.noticeFailed')); |
| 214 | + return; |
| 215 | + } |
| 216 | + |
| 217 | + setBusy(false); |
| 218 | + const onRequestSignIn = this.context.onRequestSignIn; |
| 219 | + // A blank email plus auth wording is the only case where signing in is the |
| 220 | + // fix: qodercli requires an address when no session exists. |
| 221 | + const offerSignIn = |
| 222 | + onRequestSignIn && !(email.value ?? '').trim() && isAuthFailure(outcome.detail) |
| 223 | + ? onRequestSignIn |
| 224 | + : null; |
| 225 | + showError(describeFailure(outcome), offerSignIn); |
| 226 | + }; |
| 227 | + |
| 228 | + textarea.addEventListener('input', () => { |
| 229 | + hideError(); |
| 230 | + refresh(); |
| 231 | + }); |
| 232 | + email.addEventListener('input', hideError); |
| 233 | + diagnosticsToggle.addEventListener('change', () => { |
| 234 | + this.includeDiagnostics = diagnosticsToggle.checked; |
| 235 | + }); |
| 236 | + cancelButton.addEventListener('click', () => this.close()); |
| 237 | + submitButton.addEventListener('click', () => { |
| 238 | + void submit(); |
| 239 | + }); |
| 240 | + |
| 241 | + refresh(); |
| 242 | + textarea.focus(); |
| 243 | + } |
| 244 | + |
| 245 | + /** |
| 246 | + * Copies the server request id so the user can quote it in a follow-up. When |
| 247 | + * no clipboard API is available the id is still spelled out in the notice. |
| 248 | + */ |
| 249 | + private async announceSuccess(outcome: FeedbackSuccess): Promise<void> { |
| 250 | + const requestId = outcome.requestId; |
| 251 | + if (!requestId) { |
| 252 | + new Notice(t('feedback.noticeSuccess')); |
| 253 | + return; |
| 254 | + } |
| 255 | + try { |
| 256 | + await navigator.clipboard.writeText(requestId); |
| 257 | + new Notice(t('feedback.noticeSuccessCopied', { requestId })); |
| 258 | + } catch { |
| 259 | + new Notice(t('feedback.noticeSuccessWithId', { requestId })); |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + onClose(): void { |
| 264 | + this.closed = true; |
| 265 | + if (!this.resolved) { |
| 266 | + this.resolved = true; |
| 267 | + this.resolveOutcome(null); |
| 268 | + } |
| 269 | + this.contentEl.empty(); |
| 270 | + } |
| 271 | +} |
0 commit comments