v3: added index default tab selection setting, fixed notification pages not loading, reduced invite token length, global player state survives page reloads, many visual fixes
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 44s
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 44s
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useContext } from "react";
|
||||
import { useCallback, useContext } from "react";
|
||||
|
||||
import { AuthContext } from "../contexts/AuthContext.ts";
|
||||
|
||||
@@ -16,17 +16,20 @@ function isTokenExpired(token: string): boolean {
|
||||
export const useAuth = () => {
|
||||
const { authResponse, setAuthResponse } = useContext(AuthContext);
|
||||
|
||||
const login = (response: AuthResponse) => {
|
||||
const login = useCallback((response: AuthResponse) => {
|
||||
setAuthResponse(response);
|
||||
localStorage.setItem("authResponse", JSON.stringify(response));
|
||||
};
|
||||
}, [setAuthResponse]);
|
||||
|
||||
const logout = () => {
|
||||
const logout = useCallback(() => {
|
||||
setAuthResponse(null);
|
||||
localStorage.removeItem("authResponse");
|
||||
};
|
||||
}, [setAuthResponse]);
|
||||
|
||||
const authFetch = async (input: RequestInfo, init: RequestInit = {}) => {
|
||||
const authFetch = useCallback(async (
|
||||
input: RequestInfo,
|
||||
init: RequestInit = {},
|
||||
) => {
|
||||
const token = authResponse?.token;
|
||||
|
||||
if (token && isTokenExpired(token)) {
|
||||
@@ -54,7 +57,7 @@ export const useAuth = () => {
|
||||
}
|
||||
|
||||
return res;
|
||||
};
|
||||
}, [authResponse?.token, logout]);
|
||||
|
||||
return {
|
||||
user: authResponse?.user ?? null,
|
||||
|
||||
28
src/hooks/useDefaultFeedTab.ts
Normal file
28
src/hooks/useDefaultFeedTab.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { useCallback, useState } from "react";
|
||||
import { FEED_TABS, type FeedTab } from "../config/feedTabs.ts";
|
||||
|
||||
const STORAGE_KEY = "default-feed-tab";
|
||||
|
||||
function readStored(): FeedTab {
|
||||
const stored = localStorage.getItem(STORAGE_KEY);
|
||||
return stored && (FEED_TABS as readonly string[]).includes(stored)
|
||||
? stored as FeedTab
|
||||
: "hot";
|
||||
}
|
||||
|
||||
/**
|
||||
* User preference for which feed tab the index opens on, persisted to
|
||||
* localStorage (defaults to "hot"). Returns the stored tab and a setter that
|
||||
* persists the choice. Consumers that only need to honour the preference (e.g.
|
||||
* the index default) can ignore the setter.
|
||||
*/
|
||||
export function useDefaultFeedTab(): [FeedTab, (tab: FeedTab) => void] {
|
||||
const [tab, setTabState] = useState<FeedTab>(readStored);
|
||||
|
||||
const setTab = useCallback((t: FeedTab) => {
|
||||
localStorage.setItem(STORAGE_KEY, t);
|
||||
setTabState(t);
|
||||
}, []);
|
||||
|
||||
return [tab, setTab];
|
||||
}
|
||||
Reference in New Issue
Block a user