🏷️ Search for custom tailnet name offers with keywords.

refactor: temporarily use old eligibility logic the new eligibility map is broken atm

Chloe 730ec866 0cedd96d

Changed files
+37 -56
helpers
+37 -56
helpers/eligibility.ts
··· 3 import { fetchTailscaleOffers, getTailcontrolCookie } from '$helpers/offers'; 4 import type { Eligibility, EligibilityApiResponse } from '$types/eligibility'; 5 6 - const eligibilityMap: Record<string, Eligibility> = { 7 - apiError: { 8 - eligible: false, 9 - reason: 'API error occurred.', 10 - id: 'api-error', 11 - error: 'API error', 12 - }, 13 - eligible: { 14 - eligible: true, 15 - reason: 'Eligible for new offers!', 16 - id: 'eligible', 17 - offerType: 'reoffer', 18 - }, 19 - 'forbidden - not admin': { 20 - eligible: false, 21 - reason: 22 - 'User does not have the Admin role in Tailscale, and thus has insufficient privileges.', 23 - id: 'not-admin', 24 - error: 'forbidden - not admin', 25 - }, 26 - 'forbidden - not logged in': { 27 - eligible: false, 28 - reason: 'User is not logged in to Tailscale.', 29 - id: 'not-logged-in', 30 - error: 'forbidden - not logged in', 31 - }, 32 - locked: { 33 - eligible: false, 34 - reason: 'User already used their tailnet name for an HTTPS certificate.', 35 - id: 'custom-tailnet', 36 - offerType: 'locked', 37 - }, 38 - unknown: { 39 - eligible: false, 40 - reason: 'Unknown eligibility state.', 41 - id: 'unknown', 42 - }, 43 - }; 44 - 45 export const parseEligibilityResponse = ( 46 body: EligibilityApiResponse, 47 ): Eligibility => { 48 if (body.error) { 49 return { 50 - ...eligibilityMap.apiError, 51 reason: `API error: ${body.error}`, 52 error: body.error, 53 }; 54 } 55 56 - if (body.error && eligibilityMap[body.error]) 57 - return eligibilityMap[body.error]; 58 - 59 - if (body.data?.offerType === 'locked') return eligibilityMap.locked; 60 - if (body.data?.offerType === 'reoffer') return eligibilityMap.eligible; 61 62 return { 63 - ...eligibilityMap.unknown, 64 offerType: body.data?.offerType, 65 }; 66 }; ··· 76 'INFO', 77 'Eligibility', 78 ); 79 - let eligibility: Eligibility = eligibilityMap.unknown; 80 81 const cacheDurationMs = eligibilityCacheInterval * 1000; 82 const cached = await browser.storage.local.get('eligibility'); ··· 91 try { 92 const cookie = await getTailcontrolCookie(); 93 if (!cookie) { 94 - eligibility = eligibilityMap['forbidden - not logged in']; 95 handleEligibilityResult(eligibility); 96 return eligibility; 97 } ··· 99 const cookieValue = cookie.value; 100 const response = await fetchTailscaleOffers(cookieValue); 101 const body = await response.json(); 102 - console.log(body); 103 eligibility = parseEligibilityResponse(body); 104 handleEligibilityResult(eligibility); 105 106 return eligibility; 107 - } catch (e) { 108 const errorMsg = e instanceof Error ? e.message : 'Unknown error'; 109 110 - eligibility = { 111 - ...eligibilityMap.unknown, 112 - reason: errorMsg, 113 - error: errorMsg, 114 - }; 115 - 116 handleEligibilityResult(eligibility); 117 118 return eligibility;
··· 3 import { fetchTailscaleOffers, getTailcontrolCookie } from '$helpers/offers'; 4 import type { Eligibility, EligibilityApiResponse } from '$types/eligibility'; 5 6 export const parseEligibilityResponse = ( 7 body: EligibilityApiResponse, 8 ): Eligibility => { 9 if (body.error) { 10 + if (body.error === 'forbidden - not admin') { 11 + return { 12 + eligible: false, 13 + reason: 14 + 'User does not have the Admin role in Tailscale, and thus has insufficient privileges.', 15 + id: 'not-admin', 16 + error: body.error, 17 + }; 18 + } 19 + 20 return { 21 + eligible: false, 22 reason: `API error: ${body.error}`, 23 + id: 'api-error', 24 error: body.error, 25 }; 26 } 27 28 + if (body.data && body.data.offerType === 'locked') { 29 + return { 30 + eligible: false, 31 + reason: 'User already used their tailnet name for an HTTPS certificate.', 32 + id: 'custom-tailnet', 33 + offerType: body.data.offerType, 34 + }; 35 + } 36 37 return { 38 + eligible: true, 39 + reason: 'Eligible!', 40 + id: 'eligible', 41 offerType: body.data?.offerType, 42 }; 43 }; ··· 53 'INFO', 54 'Eligibility', 55 ); 56 + let eligibility: Eligibility = { 57 + eligible: false, 58 + reason: 'Unknown', 59 + id: 'unknown', 60 + }; 61 62 const cacheDurationMs = eligibilityCacheInterval * 1000; 63 const cached = await browser.storage.local.get('eligibility'); ··· 72 try { 73 const cookie = await getTailcontrolCookie(); 74 if (!cookie) { 75 + eligibility.reason = 76 + 'Tailcontrol cookie not found. User must log in to Tailscale.'; 77 + eligibility.eligible = false; 78 + eligibility.id = 'not-logged-in'; 79 handleEligibilityResult(eligibility); 80 return eligibility; 81 } ··· 83 const cookieValue = cookie.value; 84 const response = await fetchTailscaleOffers(cookieValue); 85 const body = await response.json(); 86 eligibility = parseEligibilityResponse(body); 87 handleEligibilityResult(eligibility); 88 89 return eligibility; 90 + } catch (e: unknown) { 91 const errorMsg = e instanceof Error ? e.message : 'Unknown error'; 92 93 + eligibility.reason = errorMsg; 94 + eligibility.id = 'unknown-error'; 95 + eligibility.eligible = false; 96 + eligibility.error = errorMsg; 97 handleEligibilityResult(eligibility); 98 99 return eligibility;