@@ -2,3 +2,105 @@ SERVER_URL = 'https://minesweeper-leaderboard.tuggi.dev';
22// SERVER_URL = 'http://localhost:3001';
33LOCAL_STORAGE_USERNAME_KEY = "ms_lb_username" ;
44LOCAL_STORAGE_COLLAPSE_KEY = "ms_lb_collapsed" ;
5+ LOCAL_STORAGE_SHORTCUTS_KEY = "ms_shortcuts_enabled" ;
6+ LOCAL_STORAGE_COLOR_KEY = "ms_color_enabled" ;
7+ LOCAL_STORAGE_MARKS_KEY = "ms_marks_enabled" ;
8+
9+ // name
10+ // onChange ==> (state) => ...
11+ // local storage key
12+ // value
13+
14+ const setPreferenceValue = ( key , value ) => {
15+ const pref = preferences . find ( ( p ) => p . name === key ) ;
16+
17+ if ( ! pref ) {
18+ return ;
19+ }
20+
21+ pref . value = value ;
22+
23+ this . localStorage . setItem ( pref . lsKey , value ) ;
24+ pref . onChange ( value ) ;
25+ } ;
26+
27+ const getPreferenceValue = ( key ) => {
28+ const pref = preferences . find ( ( p ) => p . name === key ) ;
29+
30+ return pref ?. value ?? undefined ;
31+ } ;
32+
33+ const initPrefValuesFromLs = ( ) => {
34+ preferences . forEach ( ( p ) => {
35+ const lsValue = this . localStorage . getItem ( p . lsKey ) ;
36+
37+ if ( lsValue ) {
38+ p . value = p . onLsLoad ( lsValue ) ;
39+ } else {
40+ this . localStorage . setItem ( p . lsKey , p . value ) ;
41+ }
42+
43+ p . onChange ( p . value ) ;
44+ } ) ;
45+ } ;
46+
47+ const handleRadioUpdate = ( id , state ) => {
48+ const radioEl = this . document . getElementById ( id ) ;
49+ if ( state ) {
50+ radioEl . classList . add ( "selected" ) ;
51+ } else {
52+ radioEl . classList . remove ( "selected" ) ;
53+ }
54+ } ;
55+
56+ const preferences = [
57+ {
58+ name : "collapsed" ,
59+ onChange : toggleCollapsed ,
60+ onLsLoad : ( value ) => {
61+ return value === "true" ;
62+ } ,
63+ lsKey : LOCAL_STORAGE_COLLAPSE_KEY ,
64+ value : false ,
65+ } ,
66+ {
67+ name : "shortcuts" ,
68+ onChange : ( state ) => {
69+ handleRadioUpdate ( "shortcuts" , state ) ;
70+ } ,
71+ onLsLoad : ( value ) => {
72+ return value === "true" ;
73+ } ,
74+ lsKey : LOCAL_STORAGE_SHORTCUTS_KEY ,
75+ value : false ,
76+ } ,
77+ {
78+ name : "marks" ,
79+ onChange : ( state ) => {
80+ handleRadioUpdate ( "marks" , state ) ;
81+ } ,
82+ onLsLoad : ( value ) => {
83+ return value === "true" ;
84+ } ,
85+ lsKey : LOCAL_STORAGE_MARKS_KEY ,
86+ value : false ,
87+ } ,
88+ {
89+ name : "color" ,
90+ onChange : ( state ) => {
91+ handleRadioUpdate ( "saturation" , state ) ;
92+
93+ document . body . style . filter = `saturate(${ state ? 1 : 0 } )` ;
94+ document . body . style . backdropFilter = `saturate(${ state ? 1 : 0 } )` ;
95+ } ,
96+ onLsLoad : ( value ) => {
97+ return value === "true" ;
98+ } ,
99+ lsKey : LOCAL_STORAGE_COLOR_KEY ,
100+ value : false ,
101+ } ,
102+ ] ;
103+
104+ document . addEventListener ( "DOMContentLoaded" , ( ) => {
105+ initPrefValuesFromLs ( ) ;
106+ } ) ;
0 commit comments