All files / shared/utils/src extension-messages.ts

0% Statements 0/93
0% Branches 0/21
0% Functions 0/36
0% Lines 0/78

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164                                                                                                                                                                                                                                                                                                                                       
// Global declaration for Chrome extension API
declare const chrome: {
  runtime: {
    sendMessage: (message: unknown, callback?: (response?: unknown) => void) => void;
    lastError?: { message: string };
  };
};
 
import type { 
  NavigateToUrlMessage, 
  OpenSidePanelMessage, 
  SetActiveContentTabMessage, 
  DevicePrefsUpdatedMessage, 
  CameraPermissionGrantedMessage, 
  CameraPermissionDeniedMessage, 
  GetStorageManagerMessage, 
  SaveSiteConfigMessage, 
  SaveConversationMessage, 
  SendMessageMessage,
  SidePanelPreferences,
  SiteConfiguration,
  ConversationMessage
} from '@buzztrail/shared-types';
 
export async function sendNavigateToUrl(url: string): Promise<void> {
  const msg: NavigateToUrlMessage = { type: 'NAVIGATE_TO_URL', url };
  await new Promise<void>((resolve) => {
    try {
      chrome.runtime.sendMessage(msg, () => {
        // Surface message errors (optional)
        Iif (chrome.runtime.lastError) {
          // eslint-disable-next-line no-console
          console.error('[SidePanel][Nav] Message error:', chrome.runtime.lastError.message);
        }
        resolve();
      });
    } catch {
      resolve();
    }
  });
}
 
export async function sendOpenSidePanel(): Promise<boolean> {
  const msg: OpenSidePanelMessage = { type: 'OPEN_SIDE_PANEL' };
  try {
    const resp = await new Promise<{ success?: boolean }>((resolve) => {
      chrome.runtime.sendMessage(msg, (response) => resolve(response ?? {}));
    });
    return !!resp?.success;
  } catch {
    return false;
  }
}
 
export async function sendSetActiveContentTab(tabId: number): Promise<void> {
  const msg: SetActiveContentTabMessage = { type: 'SET_ACTIVE_CONTENT_TAB', tabId };
  await new Promise<void>((resolve) => {
    try {
      chrome.runtime.sendMessage(msg, () => resolve());
    } catch {
      resolve();
    }
  });
}
 
export async function sendDevicePrefsUpdated(payload: DevicePrefsUpdatedMessage['payload']): Promise<void> {
  const msg: DevicePrefsUpdatedMessage = { type: 'DEVICE_PREFS_UPDATED', payload };
  await new Promise<void>((resolve) => {
    try {
      chrome.runtime.sendMessage(msg, () => resolve());
    } catch {
      resolve();
    }
  });
}
 
export async function requestStorageManager(): Promise<unknown> {
  const msg: GetStorageManagerMessage = { type: 'GET_STORAGE_MANAGER' };
  try {
    const resp = await new Promise<{ success?: boolean; data?: unknown }>((resolve) => {
      chrome.runtime.sendMessage(msg, (response) => resolve(response ?? {}));
    });
    return resp?.success ? resp.data ?? null : null;
  } catch {
    return null;
  }
}
 
export async function sendCameraPermissionGranted(): Promise<void> {
  const msg: CameraPermissionGrantedMessage = { type: 'CAMERA_PERMISSION_GRANTED' };
  await new Promise<void>((resolve) => {
    try { chrome.runtime.sendMessage(msg, () => resolve()); } catch { resolve(); }
  });
}
 
export async function sendCameraPermissionDenied(): Promise<void> {
  const msg: CameraPermissionDeniedMessage = { type: 'CAMERA_PERMISSION_DENIED' };
  await new Promise<void>((resolve) => {
    try { chrome.runtime.sendMessage(msg, () => resolve()); } catch { resolve(); }
  });
}
 
export async function saveSiteConfig(domain: string, config: unknown): Promise<boolean> {
  const msg: SaveSiteConfigMessage = { type: 'SAVE_SITE_CONFIG', domain, config: config as SiteConfiguration };
  try {
    const resp = await new Promise<{ success?: boolean }>((resolve) => {
      chrome.runtime.sendMessage(msg, (response) => resolve(response ?? {}));
    });
    return !!resp?.success;
  } catch {
    return false;
  }
}
 
export async function saveConversation(domain: string, conversation: Partial<unknown>): Promise<boolean> {
  const msg: SaveConversationMessage = { type: 'SAVE_CONVERSATION', domain, conversation: conversation as Partial<ConversationMessage> };
  try {
    const resp = await new Promise<{ success?: boolean }>((resolve) => {
      chrome.runtime.sendMessage(msg, (response) => resolve(response ?? {}));
    });
    return !!resp?.success;
  } catch {
    return false;
  }
}
 
export async function sendUserMessage(payload: SendMessageMessage['payload']): Promise<{ success: boolean; data?: unknown }>{
  const msg: SendMessageMessage = { type: 'SEND_MESSAGE', payload };
  try {
    const resp = await new Promise<{ success?: boolean; data?: unknown }>((resolve) => {
      chrome.runtime.sendMessage(msg, (response) => resolve(response ?? {}));
    });
    return { success: !!resp?.success, data: resp?.data };
  } catch {
    return { success: false };
  }
}
 
export async function getSidePanelPreferences(): Promise<SidePanelPreferences | null> {
  const msg = { type: 'GET_SIDE_PANEL_PREFERENCES' } as const;
  try {
    const resp = await new Promise<{ success?: boolean; data?: unknown }>((resolve) => {
      chrome.runtime.sendMessage(msg, (response) => resolve(response ?? {}));
    });
    return resp?.success ? (resp.data as SidePanelPreferences) : null;
  } catch {
    return null;
  }
}
 
export async function saveSidePanelPreferences(prefs: SidePanelPreferences): Promise<boolean> {
  const msg = { type: 'SAVE_SIDE_PANEL_PREFERENCES', prefs } as const;
  try {
    const resp = await new Promise<{ success?: boolean }>((resolve) => {
      chrome.runtime.sendMessage(msg, (response) => resolve(response ?? {}));
    });
    return !!resp?.success;
  } catch {
    return false;
  }
}