Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Global Session Context #143

Merged
merged 39 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
c3e778a
Feat((admin)/admin): Use UseSession Hook
WillKirkmanM Oct 7, 2024
43a57c5
Feat((app)/layout): Add AuthProvider Globally
WillKirkmanM Oct 7, 2024
a746d35
Feat(ArtistComponent): Use UseSession Hook
WillKirkmanM Oct 7, 2024
3d24707
Feat(history): Use useSession hook
WillKirkmanM Oct 7, 2024
8189ed6
Feat(UsernameComponent): Use useSession hook
WillKirkmanM Oct 7, 2024
cee1c8f
Feat(social): Use useSession hook
WillKirkmanM Oct 7, 2024
addbf3d
Feat((dashboard)/layout): Use useSession hook
WillKirkmanM Oct 7, 2024
b3e76a5
Feat(settings)
WillKirkmanM Oct 7, 2024
f86c43b
Feat((unprotected)/layout): Add AuthProvider to Unproteected Routes
WillKirkmanM Oct 7, 2024
7e68861
Feat((unprotected): Add useSession hook
WillKirkmanM Oct 7, 2024
a9437e9
Feat(SongsInLibrary): Add useSession hook
WillKirkmanM Oct 7, 2024
953a9fb
Feat(FollowButton): Use useSession Hook
WillKirkmanM Oct 7, 2024
9e4ae41
Feat(FriendActivity): Use useSession Hook
WillKirkmanM Oct 7, 2024
0d64490
Feat(FromYourLibrary): Use useSession Hook
WillKirkmanM Oct 7, 2024
e180088
Fix(ListenAgain): Use Null Checking Operator in UseEffect
WillKirkmanM Oct 7, 2024
cb82724
RF(RandomSongs): Remove getSession invocation
WillKirkmanM Oct 7, 2024
8f0dfb0
Feat(RecommendedAlbums): Use useSession Hook
WillKirkmanM Oct 7, 2024
9bc6025
Feat(ScrollButtons): Use useSession Hook
WillKirkmanM Oct 7, 2024
2038a34
Feat(SimilarTo): Use useSession Hook
WillKirkmanM Oct 7, 2024
ee1c122
Feat(LibraryButtons): Use useSession Hook
WillKirkmanM Oct 7, 2024
2b2eefc
Feat(Playlists): Use useSession Hook
WillKirkmanM Oct 7, 2024
22a4a34
Feat(SplashScreen): Use useSession Hook
WillKirkmanM Oct 7, 2024
b0bdd0c
Feat(Sidebar): Use useSession Hook
WillKirkmanM Oct 7, 2024
4071cc1
Feat(Player): Use useSession Hook
WillKirkmanM Oct 7, 2024
5c5e7aa
Feat(SongContextMenu): Use useSession Hook
WillKirkmanM Oct 7, 2024
21e92ab
Feat(AlbumTable): Use useSession Hook
WillKirkmanM Oct 7, 2024
63d62b9
Feat(HorizontalCard): Use useSession Hook
WillKirkmanM Oct 7, 2024
b4d4e12
Feat(SongCard): Use useSession Hook
WillKirkmanM Oct 7, 2024
d3cf9e7
Feat(AddToPlaylistDropdown
WillKirkmanM Oct 7, 2024
1407f27
Feat(usePlayer): Use useSession Hook
WillKirkmanM Oct 7, 2024
307718c
Feat(CreatePlaylistDialog): Use useSession Hook
WillKirkmanM Oct 7, 2024
0079159
Feat(PlaylistTablee): Use useSession Hook
WillKirkmanM Oct 7, 2024
79479a5
Feat(AuthProvider): Context to Globally Access Sessions
WillKirkmanM Oct 7, 2024
a55a442
Fix(SidebarProvider):
WillKirkmanM Oct 7, 2024
a91b3bb
Feat(SearchBar): Context to Globally Access Sessions
WillKirkmanM Oct 7, 2024
8e93d68
Feat(ChangeBitrate): Context to Globally Access Sessions
WillKirkmanM Oct 7, 2024
d5e21ee
Feat(ChangePassword): Context to Globally Access Sessions
WillKirkmanM Oct 7, 2024
6ca2efe
Feat(NavbarProfilePicture): Use useSession Hook
WillKirkmanM Oct 7, 2024
3be2755
Feat(Username): Use useSession Hook
WillKirkmanM Oct 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions apps/web/app/(admin)/admin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
import { useEffect } from "react";
import getSession from "@/lib/Authentication/JWT/getSession";
import { useRouter } from "next/navigation";
import { useSession } from "@/components/Providers/AuthProvider";

export default function AdminPage() {
const { session } = useSession()
const { push } = useRouter();

useEffect(() => {
const session = getSession();

if (session?.role !== "admin") {
push("/home");
}
}, [push]);
}, [push, session?.role]);

return (
<h1 className="text-white text-3xl flex justify-center min-h-screen">You are admin!</h1>
Expand Down
1 change: 0 additions & 1 deletion apps/web/app/(app)/artist/ArtistComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import ScrollButtons from "@/components/Home/ScrollButtons";
import PageGradient from "@/components/Layout/PageGradient";
import AlbumCard from "@/components/Music/Card/Album/AlbumCard";
import SongCard from "@/components/Music/Card/SongCard";
import getSession from "@/lib/Authentication/JWT/getSession";
import { getArtistInfo } from "@music/sdk";
import { Album, Artist, LibrarySong } from "@music/sdk/types";
import { useSearchParams } from "next/navigation";
Expand Down
6 changes: 4 additions & 2 deletions apps/web/app/(app)/history/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import SongCard from "@/components/Music/Card/SongCard";
import { useSession } from "@/components/Providers/AuthProvider";
import getSession from "@/lib/Authentication/JWT/getSession";
import { getListenHistory, getSongInfo } from "@music/sdk";
import { LibrarySong } from "@music/sdk/types";
Expand All @@ -11,9 +12,10 @@ import { useEffect, useState } from "react";
export default function HistoryPage() {
const [listenHistorySongs, setListenHistorySongs] = useState<LibrarySong[]>([]);

const { session } = useSession()

useEffect(() => {
const fetchListenHistory = async () => {
const session = getSession()

if (session) {
const listenHistoryItems = await getListenHistory(Number(session.sub));
Expand All @@ -25,7 +27,7 @@ export default function HistoryPage() {
};

fetchListenHistory();
}, []);
}, [session]);

return (
<>
Expand Down
11 changes: 8 additions & 3 deletions apps/web/app/(app)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Metadata, Viewport } from "next";
import MainLayout from "./main-layout";
import SplashScreen from "@/components/Layout/SplashScreen";
import { cn } from "@music/ui/lib/utils";
import AuthProvider from "@/components/Providers/AuthProvider";

export const metadata: Metadata = {
applicationName: "ParsonLabs Music",
Expand Down Expand Up @@ -48,9 +49,13 @@ export default async function RootLayout({ children }: any) {
"min-h-screen bg-background font-sans antialiased bg-gray-900 texxt-white",
fontSans.variable
)}>
<SplashScreen>
<MainLayout>{children}</MainLayout>
</SplashScreen>
<AuthProvider>
<SplashScreen>
<MainLayout>
{children}
</MainLayout>
</SplashScreen>
</AuthProvider>
</body>
</html>
);
Expand Down
3 changes: 2 additions & 1 deletion apps/web/app/(app)/profile/UsernameComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import PageGradient from "@/components/Layout/PageGradient";
import ArtistCard from "@/components/Music/Artist/ArtistCard";
import AlbumCard from "@/components/Music/Card/Album/AlbumCard";
import SongCard from "@/components/Music/Card/SongCard";
import { useSession } from "@/components/Providers/AuthProvider";
import getSession from "@/lib/Authentication/JWT/getSession";
import { getCache } from "@/lib/Caching/cache";
import { getListenHistory, getProfilePicture, getSongInfo, getUserInfo, LibraryAlbum } from "@music/sdk";
Expand All @@ -23,8 +24,8 @@ export default function UsernameComponent() {
const [topAlbums, setTopAlbums] = useState<(LibraryAlbum & { count: number, artist: Artist })[]>([]);
const [topSongs, setTopSongs] = useState<(LibrarySong & { count: number })[]>([]);
const [profilePicture, setProfilePicture] = useState<string | null>(null);
const { session } = useSession();

const session = getSession();
useEffect(() => {
async function fetchData() {
const userCacheKey = `userInfo_${username}`;
Expand Down
3 changes: 2 additions & 1 deletion apps/web/app/(app)/social/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import FollowButton from "@/components/Friends/FollowButton";
import ArtistCard from "@/components/Music/Artist/ArtistCard";
import { User } from "@music/sdk/types";
import UserCard from "@/components/Music/Card/User/UserCard";
import { useSession } from "@/components/Providers/AuthProvider";

export default function SocialPage() {
const session = getSession();
const { session } = useSession()
const id = session?.sub;

const [followers, setFollowers] = useState<User[]>([]);
Expand Down
3 changes: 2 additions & 1 deletion apps/web/app/(dashboard)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Image from "next/image"
import { usePathname, useRouter } from "next/navigation"
import { useEffect } from "react"
import getSession from "@/lib/Authentication/JWT/getSession"
import { useSession } from "@/components/Providers/AuthProvider"

const fontSans = FontSans({
subsets: ["latin"],
Expand All @@ -22,7 +23,7 @@ type RootLayoutProps = {
}

export default function RootLayout({ children }: RootLayoutProps) {
const session = getSession()
const { session } = useSession()
const isAdmin = session?.role === "admin"
const pathname = usePathname()
const router = useRouter()
Expand Down
6 changes: 3 additions & 3 deletions apps/web/app/(dashboard)/settings/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Avatar, AvatarImage, AvatarFallback } from "@radix-ui/react-avatar"
import { useState, useEffect } from "react"
import { useForm } from "react-hook-form"
import { z } from "zod"
import { useSession } from "@/components/Providers/AuthProvider"

const FormSchema = z.object({
picture: z.instanceof(File).optional(),
Expand All @@ -36,10 +37,10 @@ export default function SettingsPage() {
const [message, setMessage] = useState<string | null>(null)
const [profilePicture, setProfilePicture] = useState<string | null>(null)
const [username, setUsername] = useState<string>("")
const { session } = useSession()

useEffect(() => {
const fetchSessionAndProfilePicture = async () => {
const session = getSession()
if (session) {
setUsername(session.username)
const profilePic = await getProfilePicture(Number(session.sub))
Expand All @@ -51,10 +52,9 @@ export default function SettingsPage() {
}
}
fetchSessionAndProfilePicture()
}, [])
}, [session])

async function onSubmit(data: z.infer<typeof FormSchema>) {
const session = getSession()
const userId = Number(session?.sub)

if (file) {
Expand Down
6 changes: 5 additions & 1 deletion apps/web/app/(unprotected)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import pl from "@/assets/pl-tp.png"
import { cn } from "@music/ui/lib/utils"
import { Metadata } from "next"
import Image from "next/image"
import AuthProvider from "@/components/Providers/AuthProvider"

const fontSans = FontSans({
subsets: ["latin"],
Expand Down Expand Up @@ -34,7 +35,10 @@ export default function RootLayout({ children }: RootLayoutProps) {
ParsonLabs Music
</div>
</div>
{children}

<AuthProvider>
{children}
</AuthProvider>
</body>
</html>
)
Expand Down
8 changes: 4 additions & 4 deletions apps/web/app/(unprotected)/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use client";

import pl from "@/assets/pl-tp.png";
import { useSession } from "@/components/Providers/AuthProvider";
import ServerSelectIcon from "@/components/Setup/Server/ServerSelectIcon";
import getSession from "@/lib/Authentication/JWT/getSession";
import { zodResolver } from '@hookform/resolvers/zod';
Expand All @@ -18,7 +19,7 @@ import {
import { Input } from '@music/ui/components/input';
import { Loader2Icon } from "lucide-react";
import Image from "next/image";
import { useRouter } from "next/navigation";
import { useRouter, useSelectedLayoutSegment } from "next/navigation";
import { useEffect, useState } from "react";
import { SubmitHandler, useForm } from 'react-hook-form';
import * as z from 'zod';
Expand All @@ -34,6 +35,7 @@ export default function MainPage() {
const [showServerURLInput, setShowServerURLInput] = useState(false);
const [showServerSelect, setShowServerSelect] = useState(false);
const { push } = useRouter();
const { session } = useSession()

useEffect(() => {
const checkServerUrl = async () => {
Expand All @@ -46,7 +48,6 @@ export default function MainPage() {
);
let serverInfo: ServerInfo = await response.json();

const session = getSession();
if (serverInfo.product_name && serverInfo.startup_wizard_completed) {
localStorage.setItem("server", JSON.stringify(serverInfo));

Expand Down Expand Up @@ -76,7 +77,7 @@ export default function MainPage() {
};

checkServerUrl();
}, [push]);
}, [push, session]);

const form = useForm<FormData>({
resolver: zodResolver(schema),
Expand All @@ -91,7 +92,6 @@ export default function MainPage() {
localStorage.setItem("server", JSON.stringify({ local_address: data.serverUrl }));
let serverInfo = await getServerInfo();

const session = getSession();
if (serverInfo.product_name && serverInfo.startup_wizard_completed) {
localStorage.setItem("server", JSON.stringify(serverInfo));

Expand Down
5 changes: 3 additions & 2 deletions apps/web/components/Artist/SongsInLibrary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useSearchParams } from "next/navigation";
import { useEffect, useState } from "react";
import ScrollButtons from "../Home/ScrollButtons";
import SongCard from "../Music/Card/SongCard";
import { useSession } from "../Providers/AuthProvider";

async function getSongsFromYourLibrary(user_id: number, artist_id: string) {
const playlists = await getPlaylists(user_id);
Expand Down Expand Up @@ -32,9 +33,9 @@ export default function FromYourLibrary() {

const searchParams = useSearchParams();
const id = searchParams?.get("id");
const { session } = useSession()

useEffect(() => {
const session = getSession();

async function fetchSongs() {
if (session && id) {
Expand All @@ -45,7 +46,7 @@ export default function FromYourLibrary() {
}

fetchSongs();
}, [id]);
}, [id, session]);

return librarySongs.length > 0 && (
<ScrollButtons heading="In your Library" id="InYourLibrary">
Expand Down
3 changes: 2 additions & 1 deletion apps/web/components/Friends/FollowButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import getSession from "@/lib/Authentication/JWT/getSession"
import { follow } from "@music/sdk"
import { Button } from "@music/ui/components/button"
import { useSession } from "../Providers/AuthProvider"

type FollowButtonProps = {
userIDToFollow: number
}

export default function FollowButton({ userIDToFollow }: FollowButtonProps) {
const session = getSession()
const { session } = useSession()

return session && <Button onClick={() => follow(Number(session.sub), userIDToFollow)}>Follow</Button>
}
5 changes: 3 additions & 2 deletions apps/web/components/Friends/FriendActivity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Disc3Icon } from 'lucide-react';
import Link from "next/link";
import { useEffect, useState } from "react";
import { getProfilePicture } from "@music/sdk";
import { useSession } from "../Providers/AuthProvider";

type Friend = User & {
nowPlaying: {
Expand All @@ -20,9 +21,9 @@ type Friend = User & {

export default function FriendActivity() {
const [friends, setFriends] = useState<Friend[]>([])
const { session } = useSession()

useEffect(() => {
const session = getSession();
async function getActivity() {
if (session) {
const followingIDs = await getFollowing(Number(session?.sub) ?? "");
Expand Down Expand Up @@ -63,7 +64,7 @@ export default function FriendActivity() {
return () => {
clearInterval(intervalId);
};
}, []);
}, [session]);

return friends && (
<>
Expand Down
7 changes: 4 additions & 3 deletions apps/web/components/Home/FromYourLibrary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { LibrarySong } from "@music/sdk/types";
import { useEffect, useState } from "react";
import SongCard from "../Music/Card/SongCard";
import ScrollButtons from "./ScrollButtons";
import { useSession } from "../Providers/AuthProvider";

async function getSongsFromYourLibrary(user_id: number, genre?: string) {
const playlists = await getPlaylists(user_id);
Expand Down Expand Up @@ -41,9 +42,9 @@ interface FromYourLibraryProps {
export default function FromYourLibrary({ genre }: FromYourLibraryProps) {
const [librarySongs, setLibrarySongs] = useState<LibrarySong[]>([]);
const [loading, setLoading] = useState(true);
const { session } = useSession()

useEffect(() => {
const session = getSession();

async function fetchSongs() {
const cacheKey = "fromYourLibrary";
Expand All @@ -65,11 +66,11 @@ export default function FromYourLibrary({ genre }: FromYourLibraryProps) {
}

fetchSongs();
}, [genre]);
}, [genre, session]);

if (loading) return null;
if (!librarySongs || librarySongs.length === 0) return null;

``
return (
<ScrollButtons heading="From Your Library" id="FromYourLibrary">
<div className="flex flex-row pb-28">
Expand Down
7 changes: 4 additions & 3 deletions apps/web/components/Home/ListenAgain.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,24 @@ import PageGradient from "../Layout/PageGradient";
import AlbumCard from "../Music/Card/Album/AlbumCard";
import SongCard from "../Music/Card/SongCard";
import ScrollButtons from "./ScrollButtons";
import { useSession } from "../Providers/AuthProvider";

interface ListenAgainProps {
genre?: string;
}

export default function ListenAgain({ genre }: ListenAgainProps) {
const [listenHistorySongs, setListenHistorySongs] = useState<SongInfo[]>([]);

const { session } = useSession()

useEffect(() => {
const fetchListenHistory = async () => {
const session = getSession();
const listenHistory = await getListenAgain(Number(session?.sub));
setListenHistorySongs(listenHistory);
};

fetchListenHistory();
}, [genre]);
}, [genre, session?.sub]);

if (!(listenHistorySongs[0]) || listenHistorySongs.length === 0) return null;

Expand Down
1 change: 0 additions & 1 deletion apps/web/components/Home/RandomSongs.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use client"

import getSession from "@/lib/Authentication/JWT/getSession";
import setCache, { getCache } from "@/lib/Caching/cache";
import { getRandomSong } from "@music/sdk";
import { LibrarySong } from "@music/sdk/types";
Expand Down
Loading
Loading