|
| 1 | +import { useEffect, useState } from 'react' |
| 2 | +import browser from 'webextension-polyfill' |
| 3 | + |
| 4 | +export type FirefoxContainer = { |
| 5 | + cookieStoreId: string |
| 6 | + name: string |
| 7 | + color: string |
| 8 | + icon: string |
| 9 | +} |
| 10 | + |
| 11 | +export const use_firefox_containers = () => { |
| 12 | + const [containers, set_containers] = useState<FirefoxContainer[]>([]) |
| 13 | + const [selected_container_id, set_selected_container_id] = |
| 14 | + useState<string>('') |
| 15 | + const [has_permission, set_has_permission] = useState<boolean>(false) |
| 16 | + const [is_firefox, set_is_firefox] = useState<boolean>(false) |
| 17 | + |
| 18 | + useEffect(() => { |
| 19 | + const is_firefox_detected = navigator.userAgent.includes('Firefox') |
| 20 | + set_is_firefox(is_firefox_detected) |
| 21 | + |
| 22 | + const init = async () => { |
| 23 | + try { |
| 24 | + if (is_firefox_detected) { |
| 25 | + const permissions = await browser.permissions.contains({ |
| 26 | + permissions: ['cookies'] |
| 27 | + }) |
| 28 | + set_has_permission(permissions) |
| 29 | + if (permissions) { |
| 30 | + await load_containers() |
| 31 | + } |
| 32 | + } |
| 33 | + } catch (error) { |
| 34 | + console.error('Error initializing Firefox containers:', error) |
| 35 | + } |
| 36 | + } |
| 37 | + init() |
| 38 | + }, []) |
| 39 | + |
| 40 | + const load_containers = async () => { |
| 41 | + try { |
| 42 | + const identities = await browser.contextualIdentities.query({}) |
| 43 | + set_containers(identities) |
| 44 | + |
| 45 | + const { 'selected-firefox-container': selected_id } = |
| 46 | + await browser.storage.local.get('selected-firefox-container') |
| 47 | + set_selected_container_id((selected_id as string) || '') |
| 48 | + } catch (error) { |
| 49 | + console.error('Error loading containers:', error) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + const request_permissions = async () => { |
| 54 | + try { |
| 55 | + const granted = await browser.permissions.request({ |
| 56 | + permissions: ['cookies'] |
| 57 | + }) |
| 58 | + if (granted) { |
| 59 | + set_has_permission(true) |
| 60 | + await load_containers() |
| 61 | + } |
| 62 | + } catch (error) { |
| 63 | + console.error('Error requesting permissions:', error) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + const handle_container_change = async ( |
| 68 | + e: React.ChangeEvent<HTMLSelectElement> |
| 69 | + ) => { |
| 70 | + const value = e.target.value |
| 71 | + set_selected_container_id(value) |
| 72 | + if (value) { |
| 73 | + await browser.storage.local.set({ 'selected-firefox-container': value }) |
| 74 | + } else { |
| 75 | + await browser.storage.local.remove('selected-firefox-container') |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return { |
| 80 | + is_firefox, |
| 81 | + has_permission, |
| 82 | + containers, |
| 83 | + selected_container_id, |
| 84 | + request_permissions, |
| 85 | + handle_container_change |
| 86 | + } |
| 87 | +} |
0 commit comments