refactor: extract shared types + safe chrome wrappers into src/lib
- src/lib/types.ts: message contract (both ANALYZE_TEXT shapes preserved), config/response types, storage-key constants. - src/lib/messaging.ts: single safeStorageGet/safeStorageSet/safeSendMessage/ isExtensionValid implementation replacing the three divergent copies in Options, Popup, and content. Content script keeps its refresh-toast behavior via an onContextInvalidated callback. - New '@lib' import alias (wxt force-overwrites '~' and '@' to srcDir, so those cannot point at ./src); wired in wxt.config, tsconfig, vitest. - tests/unit/messaging.test.ts: 14 unit tests over the wrappers. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,41 +1,6 @@
|
||||
import React, { useEffect, useRef, useState } from 'react';
|
||||
import { createRoot } from 'react-dom/client';
|
||||
|
||||
// ─── Safe chrome storage wrapper ─────────────────────────────────────────────
|
||||
|
||||
function safeStorageGet(
|
||||
storage: chrome.storage.StorageArea,
|
||||
keys: string[],
|
||||
callback: (result: Record<string, string>) => void,
|
||||
) {
|
||||
try {
|
||||
if (typeof chrome === 'undefined' || !chrome.runtime?.id) return;
|
||||
storage.get(keys, callback);
|
||||
} catch (err) {
|
||||
console.warn('LexAI: Extension context invalidated', err);
|
||||
}
|
||||
}
|
||||
|
||||
function safeStorageSet(storage: chrome.storage.StorageArea, data: Record<string, string>) {
|
||||
try {
|
||||
if (typeof chrome === 'undefined' || !chrome.runtime?.id) return;
|
||||
storage.set(data);
|
||||
} catch (err) {
|
||||
console.warn('LexAI: Extension context invalidated', err);
|
||||
}
|
||||
}
|
||||
|
||||
async function safeSendMessage(
|
||||
message: Record<string, unknown>,
|
||||
): Promise<Record<string, string> | null> {
|
||||
try {
|
||||
if (typeof chrome === 'undefined' || !chrome.runtime?.id) return null;
|
||||
return await chrome.runtime.sendMessage(message);
|
||||
} catch (err) {
|
||||
console.warn('LexAI: sendMessage failed', err);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
import { safeSendMessage, safeStorageGet, safeStorageSet } from '@lib/messaging';
|
||||
|
||||
// ─── Constants ───────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -222,7 +187,7 @@ function Popup() {
|
||||
|
||||
// Load config + restore session input + load writing style
|
||||
useEffect(() => {
|
||||
safeStorageGet(chrome.storage.local, ['provider', 'apiKey', 'apiKeyEnc', 'model', 'writingStyle'], (result) => {
|
||||
safeStorageGet(['provider', 'apiKey', 'apiKeyEnc', 'model', 'writingStyle'], (result) => {
|
||||
if (result.apiKey || result.apiKeyEnc) {
|
||||
setConfigured(true);
|
||||
setProvider(result.provider || 'openai');
|
||||
@@ -235,11 +200,11 @@ function Popup() {
|
||||
|
||||
if (!sessionRestored.current) {
|
||||
sessionRestored.current = true;
|
||||
safeStorageGet(chrome.storage.session, ['lexai_popup_input'], (res) => {
|
||||
safeStorageGet(['lexai_popup_input'], (res) => {
|
||||
if (res.lexai_popup_input) {
|
||||
setInputText(res.lexai_popup_input);
|
||||
}
|
||||
});
|
||||
}, chrome.storage.session);
|
||||
}
|
||||
}, []);
|
||||
|
||||
@@ -247,14 +212,14 @@ function Popup() {
|
||||
const handleInputChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
||||
const val = e.target.value;
|
||||
setInputText(val);
|
||||
safeStorageSet(chrome.storage.session, { lexai_popup_input: val });
|
||||
safeStorageSet({ lexai_popup_input: val }, undefined, chrome.storage.session);
|
||||
};
|
||||
|
||||
// Save writing style on change
|
||||
const handleStyleChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
|
||||
const val = e.target.value;
|
||||
setWritingStyle(val);
|
||||
safeStorageSet(chrome.storage.local, { writingStyle: val });
|
||||
safeStorageSet({ writingStyle: val });
|
||||
};
|
||||
|
||||
const openSettings = () => {
|
||||
|
||||
Reference in New Issue
Block a user