fix: hidden post
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
"use client";
|
||||
|
||||
/**
|
||||
* HiddenCountContext — lets a hide/unhide action deep in the tree (the
|
||||
* PostCardMenu) nudge the owner's hidden-post count held by the /u/me layout,
|
||||
* so the "Hidden" tab appears/disappears instantly without a refetch.
|
||||
*
|
||||
* The /u/me layout provides it around its subtree. Anywhere else (home feed,
|
||||
* other users' profiles) there's no provider, so `adjustHiddenCount` defaults
|
||||
* to a no-op — hiding from those surfaces simply doesn't touch a tab that
|
||||
* isn't there.
|
||||
*/
|
||||
|
||||
import { createContext, useContext } from "react";
|
||||
|
||||
type HiddenCountValue = {
|
||||
/** Adjust the owner's hidden-post count by `delta` (the layout clamps at 0). */
|
||||
adjustHiddenCount: (delta: number) => void;
|
||||
};
|
||||
|
||||
const HiddenCountContext = createContext<HiddenCountValue>({
|
||||
adjustHiddenCount: () => {},
|
||||
});
|
||||
|
||||
export const HiddenCountProvider = HiddenCountContext.Provider;
|
||||
|
||||
export function useHiddenCount(): HiddenCountValue {
|
||||
return useContext(HiddenCountContext);
|
||||
}
|
||||
@@ -38,6 +38,7 @@ import {
|
||||
} from "@/lib/social";
|
||||
import { useAuth } from "@/components/auth/auth-provider";
|
||||
import { useAuthGate } from "@/components/auth/auth-gate";
|
||||
import { useHiddenCount } from "@/components/profile/hidden-count-context";
|
||||
import { useMediaQuery } from "@/components/selection-toolbar/shared";
|
||||
import { ConfirmModal } from "@/components/ui/confirm-modal";
|
||||
import { ReportDialog, type ReportOption } from "@/components/social/report-dialog";
|
||||
@@ -78,6 +79,8 @@ export function PostCardMenu({
|
||||
}) {
|
||||
const { user, loading } = useAuth();
|
||||
const { requireAuth } = useAuthGate();
|
||||
// No-op outside the /u/me layout; there, it toggles the "Hidden" tab live.
|
||||
const { adjustHiddenCount } = useHiddenCount();
|
||||
const isMobile = useMediaQuery("(max-width: 767px)");
|
||||
const [open, setOpen] = useState(false);
|
||||
const [blockOpen, setBlockOpen] = useState(false);
|
||||
@@ -219,6 +222,8 @@ export function PostCardMenu({
|
||||
setOpen(false);
|
||||
const next = !hidden;
|
||||
setHidden(next); // optimistic
|
||||
// Optimistically toggle the owner's "Hidden" tab via the layout context.
|
||||
adjustHiddenCount(next ? 1 : -1);
|
||||
try {
|
||||
if (next) await posts.hide(post.id);
|
||||
else await posts.unhide(post.id);
|
||||
@@ -228,6 +233,7 @@ export function PostCardMenu({
|
||||
onHiddenChange?.(post.id, next);
|
||||
} catch {
|
||||
setHidden(!next); // revert
|
||||
adjustHiddenCount(next ? -1 : 1); // revert the tab count too
|
||||
toast.error("Could not update post visibility");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user