Skip to content

Commit

Permalink
Merge branch 'preview'
Browse files Browse the repository at this point in the history
  • Loading branch information
pdelfan committed Nov 26, 2024
2 parents e8eaac9 + 721eaad commit b03a928
Show file tree
Hide file tree
Showing 46 changed files with 165 additions and 125 deletions.
23 changes: 13 additions & 10 deletions src/app/dashboard/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import TopBar from "@/components/navigational/topBar/TopBar";
import Composer from "@/components/actions/composer/Composer";
import { getProfile } from "@/lib/api/bsky/actor";
import { getSessionFromServer } from "@/lib/api/auth/session";
import { AgentProvider } from "../providers/agent";

export const metadata: Metadata = {
title: { template: "%s — Ouranos", default: "Ouranos" },
Expand All @@ -21,15 +22,17 @@ export default async function DashboardLayout({
const profile = await getProfile(session?.user.bskySession.handle);

return (
<main className="bg-skin-base flex justify-center gap-6 pb-20 md:mt-6 lg:gap-16 animate-fade">
{profile && <Composer author={profile} />}
<SidePanel />
<section className="w-full md:max-w-xl">
{profile && <TopBar profile={profile} />}
{children}
</section>
{profile && <Aside avatar={profile?.avatar} handle={profile?.handle} />}
<AppBar />
</main>
<AgentProvider session={session}>
<main className="bg-skin-base flex justify-center gap-6 pb-20 md:mt-6 lg:gap-16 animate-fade">
{profile && <Composer author={profile} />}
<SidePanel />
<section className="w-full md:max-w-xl">
{profile && <TopBar profile={profile} />}
{children}
</section>
{profile && <Aside avatar={profile?.avatar} handle={profile?.handle} />}
<AppBar />
</main>
</AgentProvider>
);
}
56 changes: 56 additions & 0 deletions src/app/providers/agent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"use client";

import { ReactNode, createContext, useContext, useEffect, useRef } from "react";
import { createAgent } from "@/lib/api/bsky/agent";
import AtpAgent from "@atproto/api";
import type { Session } from "next-auth";
import { isSessionExpired } from "@/lib/utils/session";
import { useSession } from "next-auth/react";

const AgentContext = createContext<AtpAgent | null>(null);

interface AgentProviderProps {
children: ReactNode;
session: Session | null;
}

export const AgentProvider = (props: AgentProviderProps) => {
const { children, session } = props;

if (!session) return;
const agent = createAgent(session.user.service);
agent.sessionManager.session = session.user.bskySession;

return (
<AgentContext.Provider value={agent}>{children}</AgentContext.Provider>
);
};

export const useAgent = () => {
const { data: session, status } = useSession();
const agent = useContext(AgentContext);

useEffect(() => {
if (!session || !agent) return;

const getSession = async () => {
if (isSessionExpired(session.user.bskySession)) {
const result = await agent.resumeSession(session.user.bskySession);

if (!result.success) {
throw new Error("Could not resume session");
}
}

agent.sessionManager.session = session.user.bskySession;
};

getSession();
}, [agent, session]);

if (status !== "authenticated" || !agent || !session?.user) {
throw new Error("AgentProvider must be used inside SessionProvider");
}

return agent;
};
6 changes: 2 additions & 4 deletions src/components/contentDisplay/feedHeader/FeedHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import { BiSolidBookmarkAlt } from "react-icons/bi";
import { BiPlus } from "react-icons/bi";
import { BiSolidHeart } from "react-icons/bi";
import Link from "next/link";
import { getAgentFromClient } from "@/lib/api/bsky/agent";
import { useAgent } from "@/app/providers/agent";

interface Props {
feed: string;
}

export default function FeedHeader(props: Props) {
const { feed } = props;
const agent = useAgent();
const router = useRouter();
const [isSaved, setIsSaved] = useState<boolean | null>(null);
const [isPinned, setIsPinned] = useState<boolean | null>(null);
Expand Down Expand Up @@ -55,7 +56,6 @@ export default function FeedHeader(props: Props) {
const toggleSave = async () => {
setIsSaved((prev) => !prev);
try {
const agent = await getAgentFromClient();
const response = await toggleSaveFeed(agent, feed);
if (!response.success) {
setIsSaved((prev) => !prev);
Expand All @@ -71,7 +71,6 @@ export default function FeedHeader(props: Props) {
const togglePin = async () => {
setIsPinned((prev) => !prev);
try {
const agent = await getAgentFromClient();
const response = await togglePinFeed(agent, feed);
if (!response.success) {
setIsPinned((prev) => !prev);
Expand All @@ -85,7 +84,6 @@ export default function FeedHeader(props: Props) {
};

const toggleLike = async () => {
const agent = await getAgentFromClient();
setIsLiked((prev) => !prev);
if (!likeUri && feedInfo) {
try {
Expand Down
6 changes: 3 additions & 3 deletions src/components/contentDisplay/feedItem/FeedItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { toggleSaveFeed } from "@/lib/api/bsky/feed";
import Link from "next/link";
import { useQueryClient } from "@tanstack/react-query";
import { savedFeedsQueryKey } from "@/containers/settings/myFeedsContainer/MyFeedsContainer";
import { getAgentFromClient } from "@/lib/api/bsky/agent";
import { useAgent } from "@/app/providers/agent";

interface Props {
feedItem: GeneratorView;
Expand All @@ -21,6 +21,7 @@ interface Props {

export default function FeedItem(props: Props) {
const { feedItem, saved, rounded = true } = props;
const agent = useAgent();
const { avatar, displayName, description, likeCount, creator } = feedItem;
const [isSaved, setIsSaved] = useState(saved);
const router = useRouter();
Expand All @@ -29,7 +30,6 @@ export default function FeedItem(props: Props) {
const handleSave = async () => {
setIsSaved((prev) => !prev);
try {
const agent = await getAgentFromClient();
const response = await toggleSaveFeed(agent, feedItem.uri);
if (!response.success) {
setIsSaved((prev) => !prev);
Expand All @@ -46,7 +46,7 @@ export default function FeedItem(props: Props) {
<Link
href={{
pathname: `/dashboard/feeds/${encodeURIComponent(
feedItem.uri.split(":")[3].split("/")[0]
feedItem.uri.split(":")[3].split("/")[0],
)}`,
query: { uri: feedItem.uri },
}}
Expand Down
4 changes: 2 additions & 2 deletions src/components/contentDisplay/lists/Lists.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import FeedAlert from "@/components/feedback/feedAlert/FeedAlert";
import LoadingSpinner from "@/components/status/loadingSpinner/LoadingSpinner";
import ListsSkeleton from "./ListsSkeleton";
import InfiniteScroll from "react-infinite-scroll-component";
import { getAgentFromClient } from "@/lib/api/bsky/agent";
import { useAgent } from "@/app/providers/agent";

export default function Lists() {
const { data: session } = useSession();
const agent = useAgent();

const {
status,
Expand All @@ -27,7 +28,6 @@ export default function Lists() {
queryKey: ["lists"],
queryFn: async ({ pageParam }) => {
if (!session?.user.id) return;
const agent = await getAgentFromClient();
return getLists(session.user.id, pageParam, agent);
},
initialPageParam: "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useEffect, useState } from "react";
import PostHider from "@/components/dataDisplay/postHider/PostHider";
import FeedAlert from "@/components/feedback/feedAlert/FeedAlert";
import NotificationContentSkeleton from "./NotificationContentSkeleton";
import { getAgentFromClient } from "@/lib/api/bsky/agent";
import { useAgent } from "@/app/providers/agent";

interface Props {
uri: string;
Expand All @@ -20,12 +20,12 @@ interface Props {

export default function NotificationContnet(props: Props) {
const { uri, filter } = props;
const agent = useAgent();
const router = useRouter();

const { status, data, error, isLoading, isFetching } = useQuery({
queryKey: ["notificationContent", uri],
queryFn: async () => {
const agent = await getAgentFromClient();
return getPost(agent, uri);
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { AppBskyFeedDefs } from "@atproto/api";
import { ContentFilterResult } from "../../../../types/feed";
import { useQuery } from "@tanstack/react-query";
import NotificationPostSkeleton from "./NotificationPostSkeleton";
import { getAgentFromClient } from "@/lib/api/bsky/agent";
import { useAgent } from "@/app/providers/agent";

interface Props {
uri: string;
Expand All @@ -15,6 +15,7 @@ interface Props {

export default function NotificationPost(props: Props) {
const { uri, filter } = props;
const agent = useAgent();

const {
status,
Expand All @@ -25,7 +26,6 @@ export default function NotificationPost(props: Props) {
} = useQuery({
queryKey: ["notificationPost", uri],
queryFn: async () => {
const agent = await getAgentFromClient();
return getPost(agent, uri);
},
});
Expand Down
5 changes: 3 additions & 2 deletions src/components/navigational/appBar/AppBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ import { PiMagnifyingGlassBold, PiMagnifyingGlassFill } from "react-icons/pi";
import { FaRegBell } from "react-icons/fa6";
import { FaBell } from "react-icons/fa";
import { HiClipboardList, HiOutlineClipboardList } from "react-icons/hi";
import { getAgentFromClient } from "@/lib/api/bsky/agent";
import { useAgent } from "@/app/providers/agent";

export default function AppBar() {
const pathname = usePathname();
const agent = useAgent();

const {
data: notificationsCount,
error,
isFetching,
} = useQuery({
queryKey: ["notificationsCount"],
queryFn: async () => {
const agent = await getAgentFromClient();
return getUnreadNotificationsCount(agent);
},
refetchInterval: 10000,
Expand Down
4 changes: 2 additions & 2 deletions src/components/navigational/feedTabs/FeedTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import FeedTabsSkeleton from "./FeedTabsSkeleton";
import React from "react";
import { useQuery } from "@tanstack/react-query";
import { useScrollContext } from "@/app/providers/scroll";
import { getAgentFromClient } from "@/lib/api/bsky/agent";
import { useAgent } from "@/app/providers/agent";

export default function FeedTabs() {
const agent = useAgent();
const pathname = usePathname();
const searchParams = useSearchParams();
const uri = searchParams.get("uri");
Expand All @@ -26,7 +27,6 @@ export default function FeedTabs() {
} = useQuery({
queryKey: ["savedFeeds"],
queryFn: async () => {
const agent = await getAgentFromClient();
return getSavedFeeds(agent);
},
});
Expand Down
5 changes: 3 additions & 2 deletions src/components/navigational/navbar/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@ import { HiClipboardList, HiOutlineClipboardList } from "react-icons/hi";
import { FaBell, FaRegBell } from "react-icons/fa6";
import { getUnreadNotificationsCount } from "@/lib/api/bsky/notification";
import { useQuery } from "@tanstack/react-query";
import { getAgentFromClient } from "@/lib/api/bsky/agent";
import { useAgent } from "@/app/providers/agent";

export default function Navbar() {
const agent = useAgent();
const pathname = usePathname();

const {
data: notificationsCount,
error,
isFetching,
} = useQuery({
queryKey: ["notificationsCount"],
queryFn: async () => {
const agent = await getAgentFromClient();
return getUnreadNotificationsCount(agent);
},
refetchInterval: 10000,
Expand Down
4 changes: 2 additions & 2 deletions src/containers/lists/ListMembersContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import LoadingSpinner from "@/components/status/loadingSpinner/LoadingSpinner";
import { getListMembers } from "@/lib/api/bsky/list";
import FeedAlert from "@/components/feedback/feedAlert/FeedAlert";
import InfiniteScroll from "react-infinite-scroll-component";
import { getAgentFromClient } from "@/lib/api/bsky/agent";
import { useAgent } from "@/app/providers/agent";

interface Props {
list: string;
}

export default function ListMembersContainer(props: Props) {
const { list } = props;
const agent = useAgent();

const {
status,
Expand All @@ -29,7 +30,6 @@ export default function ListMembersContainer(props: Props) {
} = useInfiniteQuery({
queryKey: ["list members", list],
queryFn: async ({ pageParam }) => {
const agent = await getAgentFromClient();
return getListMembers(agent, list, pageParam);
},
initialPageParam: "",
Expand Down
4 changes: 2 additions & 2 deletions src/containers/lists/ListsContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"use client";

import { useAgent } from "@/app/providers/agent";
import ListItem from "@/components/contentDisplay/listItem/ListItem";
import ListsSkeleton from "@/components/contentDisplay/lists/ListsSkeleton";
import FeedAlert from "@/components/feedback/feedAlert/FeedAlert";
import LoadingSpinner from "@/components/status/loadingSpinner/LoadingSpinner";
import { getProfile } from "@/lib/api/bsky/actor";
import { getAgentFromClient } from "@/lib/api/bsky/agent";
import { getLists } from "@/lib/api/bsky/list";
import { useInfiniteQuery } from "@tanstack/react-query";
import { Fragment } from "react";
Expand All @@ -17,6 +17,7 @@ interface Props {

export default function ListsContainer(props: Props) {
const { handle } = props;
const agent = useAgent();
const {
status,
data: lists,
Expand All @@ -29,7 +30,6 @@ export default function ListsContainer(props: Props) {
} = useInfiniteQuery({
queryKey: ["user lists", handle],
queryFn: async ({ pageParam }) => {
const agent = await getAgentFromClient();
const profile = await getProfile(handle, agent);
if (!profile) throw new Error("Could not get user id to show lists");
return getLists(profile.did, pageParam, agent);
Expand Down
4 changes: 2 additions & 2 deletions src/containers/posts/UserPostsContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import usePreferences from "@/lib/hooks/bsky/actor/usePreferences";
import ComposeButton from "@/components/actions/composeButton/ComposeButton";
import LoadingSpinner from "@/components/status/loadingSpinner/LoadingSpinner";
import InfiniteScroll from "react-infinite-scroll-component";
import { getAgentFromClient } from "@/lib/api/bsky/agent";
import { useAgent } from "@/app/providers/agent";

interface Props {
mode: UserPostMode;
Expand All @@ -20,6 +20,7 @@ interface Props {

export default function UserPostsConatiner(props: Props) {
const { mode, handle } = props;
const agent = useAgent();
const {
data: profile,
isLoading,
Expand All @@ -28,7 +29,6 @@ export default function UserPostsConatiner(props: Props) {
} = useQuery({
queryKey: ["profile", handle],
queryFn: async () => {
const agent = await getAgentFromClient();
return getProfile(handle, agent);
},
});
Expand Down
Loading

0 comments on commit b03a928

Please sign in to comment.