From 020d8e3631796318e3b659f0288d7c74ef206031 Mon Sep 17 00:00:00 2001
From: Lim Chee Aun <cheeaun@gmail.com>
Date: Fri, 15 Sep 2023 00:28:20 +0800
Subject: [PATCH] Allow settings for unauthenticated sessions

---
 src/components/nav-menu.jsx |   7 +++
 src/pages/settings.jsx      | 122 +++++++++++++++++++-----------------
 2 files changed, 72 insertions(+), 57 deletions(-)

diff --git a/src/components/nav-menu.jsx b/src/components/nav-menu.jsx
index e92e96c1..f11f6aa7 100644
--- a/src/components/nav-menu.jsx
+++ b/src/components/nav-menu.jsx
@@ -234,6 +234,13 @@ function NavMenu(props) {
               <MenuLink to="/login">
                 <Icon icon="user" size="l" /> <span>Log in</span>
               </MenuLink>
+              <MenuItem
+                onClick={() => {
+                  states.showSettings = true;
+                }}
+              >
+                <Icon icon="gear" size="l" /> <span>Settings&hellip;</span>
+              </MenuItem>
             </>
           )}
         </section>
diff --git a/src/pages/settings.jsx b/src/pages/settings.jsx
index a1834a4c..ef4682ef 100644
--- a/src/pages/settings.jsx
+++ b/src/pages/settings.jsx
@@ -34,6 +34,7 @@ function Settings({ onClose }) {
   const currentTextSize = store.local.get('textSize') || DEFAULT_TEXT_SIZE;
 
   const [prefs, setPrefs] = useState(store.account.get('preferences') || {});
+  const { masto, authenticated } = api();
   // Get preferences every time Settings is opened
   // NOTE: Disabled for now because I don't expect this to change often. Also for some reason, the /api/v1/preferences endpoint is cached for a while and return old prefs if refresh immediately after changing them.
   // useEffect(() => {
@@ -169,50 +170,55 @@ function Settings({ onClose }) {
             </li>
           </ul>
         </section>
-        <h3>Posting</h3>
-        <section>
-          <ul>
-            <li>
-              <div>
-                <label for="posting-privacy-field">Default visibility</label>
-              </div>
-              <div>
-                <select
-                  id="posting-privacy-field"
-                  value={prefs['posting:default:visibility'] || 'public'}
-                  onChange={(e) => {
-                    const { value } = e.target;
-                    const { masto } = api();
-                    (async () => {
-                      try {
-                        await masto.v1.accounts.updateCredentials({
-                          source: {
-                            privacy: value,
-                          },
-                        });
-                        setPrefs({
-                          ...prefs,
-                          'posting:default:visibility': value,
-                        });
-                        store.account.set('preferences', {
-                          ...prefs,
-                          'posting:default:visibility': value,
-                        });
-                      } catch (e) {
-                        alert('Failed to update posting privacy');
-                        console.error(e);
-                      }
-                    })();
-                  }}
-                >
-                  <option value="public">Public</option>
-                  <option value="unlisted">Unlisted</option>
-                  <option value="private">Followers only</option>
-                </select>
-              </div>
-            </li>
-          </ul>
-        </section>
+        {authenticated && (
+          <>
+            <h3>Posting</h3>
+            <section>
+              <ul>
+                <li>
+                  <div>
+                    <label for="posting-privacy-field">
+                      Default visibility
+                    </label>
+                  </div>
+                  <div>
+                    <select
+                      id="posting-privacy-field"
+                      value={prefs['posting:default:visibility'] || 'public'}
+                      onChange={(e) => {
+                        const { value } = e.target;
+                        (async () => {
+                          try {
+                            await masto.v1.accounts.updateCredentials({
+                              source: {
+                                privacy: value,
+                              },
+                            });
+                            setPrefs({
+                              ...prefs,
+                              'posting:default:visibility': value,
+                            });
+                            store.account.set('preferences', {
+                              ...prefs,
+                              'posting:default:visibility': value,
+                            });
+                          } catch (e) {
+                            alert('Failed to update posting privacy');
+                            console.error(e);
+                          }
+                        })();
+                      }}
+                    >
+                      <option value="public">Public</option>
+                      <option value="unlisted">Unlisted</option>
+                      <option value="private">Followers only</option>
+                    </select>
+                  </div>
+                </li>
+              </ul>
+            </section>
+          </>
+        )}
         <h3>Experiments</h3>
         <section>
           <ul>
@@ -384,21 +390,23 @@ function Settings({ onClose }) {
                 </small>
               </div>
             </li>
-            <li>
-              <button
-                type="button"
-                class="light"
-                onClick={() => {
-                  states.showDrafts = true;
-                  states.showSettings = false;
-                }}
-              >
-                Unsent drafts
-              </button>
-            </li>
+            {authenticated && (
+              <li>
+                <button
+                  type="button"
+                  class="light"
+                  onClick={() => {
+                    states.showDrafts = true;
+                    states.showSettings = false;
+                  }}
+                >
+                  Unsent drafts
+                </button>
+              </li>
+            )}
           </ul>
         </section>
-        <PushNotificationsSection onClose={onClose} />
+        {authenticated && <PushNotificationsSection onClose={onClose} />}
         <h3>About</h3>
         <section>
           <div