a tool for shared writing and social publishing
at feature/recommend 51 lines 1.4 kB view raw
1"use client"; 2import { createContext, useContext } from "react"; 3import type { PostPageData } from "app/lish/[did]/[publication]/[rkey]/getPostPageData"; 4 5// Derive types from PostPageData 6type NonNullPostPageData = NonNullable<PostPageData>; 7export type PublicationContext = NonNullPostPageData["publication"]; 8export type CommentOnDocument = NonNullPostPageData["comments"][number]; 9export type DocumentMention = NonNullPostPageData["mentions"][number]; 10export type QuotesAndMentions = NonNullPostPageData["quotesAndMentions"]; 11 12export type DocumentContextValue = Pick< 13 NonNullPostPageData, 14 | "uri" 15 | "normalizedDocument" 16 | "normalizedPublication" 17 | "theme" 18 | "prevNext" 19 | "quotesAndMentions" 20 | "publication" 21 | "comments" 22 | "mentions" 23 | "leafletId" 24 | "recommendsCount" 25>; 26 27const DocumentContext = createContext<DocumentContextValue | null>(null); 28 29export function useDocument() { 30 const ctx = useContext(DocumentContext); 31 if (!ctx) throw new Error("useDocument must be used within DocumentProvider"); 32 return ctx; 33} 34 35export function useDocumentOptional() { 36 return useContext(DocumentContext); 37} 38 39export function DocumentProvider({ 40 children, 41 value, 42}: { 43 children: React.ReactNode; 44 value: DocumentContextValue; 45}) { 46 return ( 47 <DocumentContext.Provider value={value}> 48 {children} 49 </DocumentContext.Provider> 50 ); 51}