🏷️ Search for custom tailnet name offers with keywords.
at master 68 lines 1.7 kB view raw
1import { useCallback, useEffect, useState } from 'react'; 2import { getTailnetNames } from '$helpers/tailnet'; 3 4const isCombo = (val: string, tails: string[], scales: string[]) => { 5 const parts = val.split('-'); 6 return ( 7 parts.length === 2 && tails.includes(parts[0]) && scales.includes(parts[1]) 8 ); 9}; 10 11export const useTailnetNames = ( 12 tails: string[], 13 scales: string[], 14 setStatus: (status: 'Running' | 'Stopped') => void, 15 inputValue: string, 16 setInputValue: (v: string) => void, 17) => { 18 const [tailnetNames, setTailnetNames] = useState<string[]>([]); 19 20 useEffect(() => { 21 getTailnetNames().then(setTailnetNames); 22 }, []); 23 24 const isValid = useCallback( 25 (val: string) => { 26 return ( 27 (val && tails.includes(val)) || 28 (val && scales.includes(val)) || 29 isCombo(val, tails, scales) 30 ); 31 }, 32 [tails, scales], 33 ); 34 35 const handleAddTailnet = useCallback(() => { 36 if ( 37 inputValue && 38 isValid(inputValue) && 39 !tailnetNames.includes(inputValue) 40 ) { 41 const updated = [...tailnetNames, inputValue]; 42 setTailnetNames(updated); 43 browser.storage.local.set({ tailnetNames: updated }); 44 setInputValue(''); 45 } 46 }, [inputValue, tailnetNames, setInputValue, isValid]); 47 48 const handleRemoveTailnet = useCallback( 49 (name: string) => { 50 const updated = tailnetNames.filter((n) => n !== name); 51 setTailnetNames(updated); 52 browser.storage.local.set({ tailnetNames: updated }); 53 if (updated.length === 0) { 54 browser.runtime.sendMessage({ action: 'stopCheck' }, (response) => { 55 setStatus(response?.status || 'Stopped'); 56 }); 57 } 58 }, 59 [tailnetNames, setStatus], 60 ); 61 62 return { 63 tailnetNames, 64 setTailnetNames, 65 handleAddTailnet, 66 handleRemoveTailnet, 67 }; 68};