v3: code quality pass, various bug fixes
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { type ReactNode, useCallback, useEffect, useState } from "react";
|
||||
import { type ReactNode, useCallback, useEffect, useMemo, useState } from "react";
|
||||
import { FollowContext, type FollowContextValue } from "./FollowContext.ts";
|
||||
import { API_URL } from "../config/api.ts";
|
||||
import { useAuth } from "../hooks/useAuth.ts";
|
||||
@@ -21,23 +21,24 @@ export function FollowProvider({ children }: { children: ReactNode }) {
|
||||
setIsLoaded(false);
|
||||
return;
|
||||
}
|
||||
let cancelled = false;
|
||||
const controller = new AbortController();
|
||||
fetch(`${API_URL}/api/follows/status`, {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
signal: controller.signal,
|
||||
})
|
||||
.then((r) => r.json())
|
||||
.then((body) => {
|
||||
if (cancelled || !body.success) return;
|
||||
if (!body.success) return;
|
||||
const status = body.data as FollowStatus;
|
||||
setFollowedUserIds(new Set(status.followedUserIds));
|
||||
setFollowedPlaylistIds(new Set(status.followedPlaylistIds));
|
||||
setIsLoaded(true);
|
||||
})
|
||||
.catch(() => {
|
||||
if (!cancelled) setIsLoaded(true);
|
||||
.catch((err) => {
|
||||
if (err.name !== "AbortError") setIsLoaded(true);
|
||||
});
|
||||
return () => {
|
||||
cancelled = true;
|
||||
controller.abort();
|
||||
};
|
||||
}, [token]);
|
||||
|
||||
@@ -107,7 +108,7 @@ export function FollowProvider({ children }: { children: ReactNode }) {
|
||||
}
|
||||
}, [authFetch]);
|
||||
|
||||
const value: FollowContextValue = {
|
||||
const value: FollowContextValue = useMemo(() => ({
|
||||
followedUserIds,
|
||||
followedPlaylistIds,
|
||||
followUser,
|
||||
@@ -115,7 +116,15 @@ export function FollowProvider({ children }: { children: ReactNode }) {
|
||||
followPlaylist,
|
||||
unfollowPlaylist,
|
||||
isLoaded,
|
||||
};
|
||||
}), [
|
||||
followedUserIds,
|
||||
followedPlaylistIds,
|
||||
followUser,
|
||||
unfollowUser,
|
||||
followPlaylist,
|
||||
unfollowPlaylist,
|
||||
isLoaded,
|
||||
]);
|
||||
|
||||
return (
|
||||
<FollowContext.Provider value={value}>
|
||||
|
||||
Reference in New Issue
Block a user