|
| 1 | +import { forceCache } from "../utils/canProcessor"; |
| 2 | + |
| 3 | +interface SettingsModalProps { |
| 4 | + isOpen: boolean; |
| 5 | + onClose: () => void; |
| 6 | + bannerApi: { |
| 7 | + showDefault: () => void; |
| 8 | + showCache: () => void; |
| 9 | + hideDefault: () => void; |
| 10 | + hideCache: () => void; |
| 11 | + toggleDefault: () => void; |
| 12 | + toggleCache: () => void; |
| 13 | + }; |
| 14 | +} |
| 15 | + |
| 16 | +async function uploadFileToCache(file: File) { |
| 17 | + if (!file) return; |
| 18 | + |
| 19 | + const fileContent = await file.text(); |
| 20 | + |
| 21 | + // Try Cache API first (requires secure context) |
| 22 | + try { |
| 23 | + const cache = await caches.open("dbc-files"); |
| 24 | + const cacheKey = "cache.dbc"; |
| 25 | + |
| 26 | + console.log("[uploadFileToCache] Uploading DBC file to cache..."); |
| 27 | + await cache.delete(cacheKey); |
| 28 | + const request = new Request(cacheKey); |
| 29 | + const res = new Response(fileContent, { |
| 30 | + headers: { "Content-Type": "text/plain; charset=utf-8" }, |
| 31 | + }); |
| 32 | + await cache.put(request, res); |
| 33 | + console.log("[uploadFileToCache] Successfully cached DBC file"); |
| 34 | + |
| 35 | + // Verify it was cached |
| 36 | + const verify = await cache.match(cacheKey); |
| 37 | + console.log( |
| 38 | + "[uploadFileToCache] Verification - cached file exists:", |
| 39 | + !!verify, |
| 40 | + ); |
| 41 | + } catch (error) { |
| 42 | + console.warn( |
| 43 | + "[uploadFileToCache] Cache API not available, using localStorage fallback:", |
| 44 | + error instanceof Error ? error.message : String(error), |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + // Always save to localStorage as fallback (works in non-secure contexts) |
| 49 | + try { |
| 50 | + localStorage.setItem("dbc-file-content", fileContent); |
| 51 | + console.log("[uploadFileToCache] Successfully saved DBC to localStorage"); |
| 52 | + } catch (error) { |
| 53 | + console.error("[uploadFileToCache] Error saving to localStorage:", error); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +function SettingsModal({ isOpen, onClose, bannerApi }: Readonly<SettingsModalProps>) { |
| 58 | + if (!isOpen) return null; |
| 59 | + |
| 60 | + const handleChange = async (e: React.ChangeEvent<HTMLInputElement>) => { |
| 61 | + const file = e.target.files?.[0]; |
| 62 | + if (!file) return; |
| 63 | + await uploadFileToCache(file); |
| 64 | + |
| 65 | + // Set localStorage flag to indicate cache is active |
| 66 | + localStorage.setItem("dbc-cache-active", "true"); |
| 67 | + |
| 68 | + bannerApi.showCache(); |
| 69 | + bannerApi.hideDefault(); |
| 70 | + forceCache(true); |
| 71 | + onClose(); |
| 72 | + globalThis.location.reload(); |
| 73 | + }; |
| 74 | + |
| 75 | + const handleBackdropClick = (e: React.MouseEvent<HTMLDivElement>) => { |
| 76 | + if (e.target === e.currentTarget) { |
| 77 | + onClose(); |
| 78 | + } |
| 79 | + }; |
| 80 | + |
| 81 | + return ( |
| 82 | + <div |
| 83 | + className="fixed inset-0 z-[100] flex items-center justify-center bg-black/60 backdrop-blur-sm" |
| 84 | + onClick={handleBackdropClick} |
| 85 | + > |
| 86 | + <div className="relative bg-sidebar rounded-xl shadow-2xl border border-gray-600 w-[66%] h-[80%] p-6 flex flex-col animate-in fade-in zoom-in-95 duration-200"> |
| 87 | + {/* Close button */} |
| 88 | + <button |
| 89 | + onClick={onClose} |
| 90 | + className="absolute top-4 right-4 text-gray-400 hover:text-white transition-colors cursor-pointer" |
| 91 | + aria-label="Close settings" |
| 92 | + > |
| 93 | + <svg |
| 94 | + xmlns="http://www.w3.org/2000/svg" |
| 95 | + className="h-6 w-6" |
| 96 | + fill="none" |
| 97 | + viewBox="0 0 24 24" |
| 98 | + stroke="currentColor" |
| 99 | + > |
| 100 | + <path |
| 101 | + strokeLinecap="round" |
| 102 | + strokeLinejoin="round" |
| 103 | + strokeWidth={2} |
| 104 | + d="M6 18L18 6M6 6l12 12" |
| 105 | + /> |
| 106 | + </svg> |
| 107 | + </button> |
| 108 | + |
| 109 | + {/* Header */} |
| 110 | + <h2 className="text-2xl font-semibold text-white mb-6">Settings</h2> |
| 111 | + |
| 112 | + {/* Settings content area - scrollable for future settings */} |
| 113 | + <div className="flex-1 overflow-y-auto space-y-4"> |
| 114 | + {/* DBC Upload Section - compact single row */} |
| 115 | + <div className="flex flex-row w-full rounded-lg text-white bg-option justify-between items-center px-4 py-3"> |
| 116 | + <span className="text-sm font-medium">Custom DBC File</span> |
| 117 | + <label |
| 118 | + htmlFor="dbc-upload-modal" |
| 119 | + className="bg-banner-button hover:bg-banner-button-hover px-4 py-1.5 cursor-pointer text-center text-sm font-semibold text-white rounded-md transition-colors shadow-sm" |
| 120 | + > |
| 121 | + Upload DBC |
| 122 | + </label> |
| 123 | + <input |
| 124 | + className="sr-only" |
| 125 | + id="dbc-upload-modal" |
| 126 | + type="file" |
| 127 | + accept=".dbc" |
| 128 | + onChange={handleChange} |
| 129 | + /> |
| 130 | + </div> |
| 131 | + |
| 132 | + {/* Future settings will go here */} |
| 133 | + </div> |
| 134 | + </div> |
| 135 | + </div> |
| 136 | + ); |
| 137 | +} |
| 138 | + |
| 139 | +export default SettingsModal; |
0 commit comments