v3: follows, notifications, invite-only registration, unread markers

This commit is contained in:
khannurien
2026-03-21 18:42:47 +00:00
parent 7c098e7c4c
commit 608c6bc6a8
55 changed files with 4743 additions and 884 deletions

View File

@@ -0,0 +1,125 @@
import { type ReactNode, useCallback, useEffect, useState } from "react";
import { FollowContext, type FollowContextValue } from "./FollowContext.ts";
import { API_URL } from "../config/api.ts";
import { useAuth } from "../hooks/useAuth.ts";
import type { FollowStatus } from "../model.ts";
export function FollowProvider({ children }: { children: ReactNode }) {
const { token, authFetch } = useAuth();
const [followedUserIds, setFollowedUserIds] = useState<Set<string>>(
new Set(),
);
const [followedPlaylistIds, setFollowedPlaylistIds] = useState<Set<string>>(
new Set(),
);
const [isLoaded, setIsLoaded] = useState(false);
useEffect(() => {
if (!token) {
setFollowedUserIds(new Set());
setFollowedPlaylistIds(new Set());
setIsLoaded(false);
return;
}
let cancelled = false;
fetch(`${API_URL}/api/follows/status`, {
headers: { Authorization: `Bearer ${token}` },
})
.then((r) => r.json())
.then((body) => {
if (cancelled || !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);
});
return () => {
cancelled = true;
};
}, [token]);
const followUser = useCallback(async (userId: string) => {
setFollowedUserIds((prev) => new Set([...prev, userId]));
try {
const res = await authFetch(`${API_URL}/api/follows/users/${userId}`, {
method: "POST",
});
if (!res.ok) throw new Error(`HTTP ${res.status}`);
} catch {
setFollowedUserIds((prev) => {
const n = new Set(prev);
n.delete(userId);
return n;
});
}
}, [authFetch]);
const unfollowUser = useCallback(async (userId: string) => {
setFollowedUserIds((prev) => {
const n = new Set(prev);
n.delete(userId);
return n;
});
try {
const res = await authFetch(`${API_URL}/api/follows/users/${userId}`, {
method: "DELETE",
});
if (!res.ok) throw new Error(`HTTP ${res.status}`);
} catch {
setFollowedUserIds((prev) => new Set([...prev, userId]));
}
}, [authFetch]);
const followPlaylist = useCallback(async (playlistId: string) => {
setFollowedPlaylistIds((prev) => new Set([...prev, playlistId]));
try {
const res = await authFetch(
`${API_URL}/api/follows/playlists/${playlistId}`,
{ method: "POST" },
);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
} catch {
setFollowedPlaylistIds((prev) => {
const n = new Set(prev);
n.delete(playlistId);
return n;
});
}
}, [authFetch]);
const unfollowPlaylist = useCallback(async (playlistId: string) => {
setFollowedPlaylistIds((prev) => {
const n = new Set(prev);
n.delete(playlistId);
return n;
});
try {
const res = await authFetch(
`${API_URL}/api/follows/playlists/${playlistId}`,
{ method: "DELETE" },
);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
} catch {
setFollowedPlaylistIds((prev) => new Set([...prev, playlistId]));
}
}, [authFetch]);
const value: FollowContextValue = {
followedUserIds,
followedPlaylistIds,
followUser,
unfollowUser,
followPlaylist,
unfollowPlaylist,
isLoaded,
};
return (
<FollowContext.Provider value={value}>
{children}
</FollowContext.Provider>
);
}