Skip to content

Commit 3f78192

Browse files
committedOct 22, 2024
Add logout button, Hide follow button if user not logged in
1 parent 96db447 commit 3f78192

File tree

4 files changed

+69
-16
lines changed

4 files changed

+69
-16
lines changed
 

‎app/[id]/page.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export default function ProfilePage() {
9393
<span>{user?.followers?.length} followers</span>
9494
<span>{user?.following?.length} following</span>
9595
</div>
96-
{currentUser?.id !== user?.id && (
96+
{currentUser?.id !== user?.id && currentUser && (
9797
<>
9898
{amIFollowing ? (
9999
<button

‎components/Layout/TwitterLayout.tsx

+51-15
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
"use client";
1+
import { useState, useCallback, useMemo } from "react";
22
import { useCurrentUser } from "@/hooks/user";
33
import { CredentialResponse, GoogleLogin } from "@react-oauth/google";
44
import { BiCheckCircle, BiHash, BiHomeCircle, BiUser } from "react-icons/bi";
55
import { BsBell, BsBookmark, BsEnvelope, BsTwitter } from "react-icons/bs";
66
import { SlOptions } from "react-icons/sl";
77
import { graphqlClient } from "@/clients/api";
88
import { verifyUserGoogleTokenQuery } from "@/graphql/query/user";
9-
import { useCallback, useMemo } from "react";
109
import toast from "react-hot-toast";
1110
import { useQueryClient } from "@tanstack/react-query";
1211
import Link from "next/link";
@@ -24,6 +23,7 @@ interface TwitterSidebarButton {
2423
export default function TwitterLayout({ children }: TwitterLayoutProps) {
2524
const queryClient = useQueryClient();
2625
const { user } = useCurrentUser();
26+
const [isDropdownOpen, setIsDropdownOpen] = useState(false);
2727

2828
const sidebarMenuItems: TwitterSidebarButton[] = useMemo(
2929
() => [
@@ -93,10 +93,17 @@ export default function TwitterLayout({ children }: TwitterLayoutProps) {
9393
[queryClient]
9494
);
9595

96+
const handleLogout = () => {
97+
window.localStorage.removeItem("__twitter_token");
98+
queryClient.invalidateQueries({ queryKey: ["currentUser"] });
99+
setIsDropdownOpen(false);
100+
toast.success("Logged out successfully");
101+
};
102+
96103
return (
97104
<div>
98105
<div className="grid grid-cols-12 h-screen w-screen lg:px-56">
99-
<div className="col-span-2 sm:col-span-3 pt-1 flex relative sm:justify-end pr-4">
106+
<div className="col-span-2 sm:col-span-3 pt-1 relative pr-4">
100107
<div>
101108
<div className="text-2xl h-fit hover:bg-gray-800 rounded-full p-4 cursor-pointer transition-all w-fit">
102109
<Link href={"/"}>
@@ -127,26 +134,55 @@ export default function TwitterLayout({ children }: TwitterLayoutProps) {
127134
</div>
128135
</div>
129136
</div>
137+
130138
{user && (
131-
<div className=" absolute bottom-5 flex gap-2 items-center bg-slate-700 rounded-full px-3 py-2 cursor-pointer">
132-
{user && user.profileImageURL && (
133-
<img
134-
src={user.profileImageURL}
135-
alt="User profile"
136-
height={50}
137-
width={50}
138-
className="rounded-full"
139-
/>
140-
)}
141-
<div className="hidden lg:block">
142-
<h3>{user.firstName}</h3>
139+
<div className="absolute bottom-5">
140+
<div
141+
className="bg-slate-700 rounded-full px-3 py-2 cursor-pointer w-full"
142+
onClick={() => setIsDropdownOpen(!isDropdownOpen)}
143+
>
144+
<div className="flex items-center">
145+
{user.profileImageURL ? (
146+
<img
147+
src={user.profileImageURL}
148+
alt="User profile"
149+
height={50}
150+
width={50}
151+
className="rounded-full"
152+
/>
153+
) : (
154+
<div className="h-10 w-10 bg-green-700 rounded-full flex items-center justify-center">
155+
<span className="text-white font-semibold">
156+
{user.firstName.charAt(0)}
157+
</span>
158+
</div>
159+
)}
160+
<span className="hidden lg:inline ml-2 text-white">
161+
{user.firstName}
162+
</span>
163+
</div>
143164
</div>
165+
166+
{isDropdownOpen && (
167+
<div className="absolute bottom-16 left-0 bg-slate-700 p-3 rounded-lg w-48 shadow-lg">
168+
<ul>
169+
<li
170+
className="cursor-pointer hover:bg-gray-600 p-2 rounded-lg"
171+
onClick={handleLogout}
172+
>
173+
Logout
174+
</li>
175+
</ul>
176+
</div>
177+
)}
144178
</div>
145179
)}
146180
</div>
181+
147182
<div className="col-span-10 sm:col-span-5 border-l-[1px] border-r-[1px] h-screen overflow-scroll scroll border-gray-600 no-scrollbar">
148183
{children}
149184
</div>
185+
150186
<div className="sm:col-span-3 p-5">
151187
{!user && (
152188
<div className="p-5 bg-slate-700 rounded-lg flex flex-col items-center">

‎gql/graphql.ts

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export type User = {
8585
id: Scalars['ID']['output'];
8686
lastName?: Maybe<Scalars['String']['output']>;
8787
profileImageURL?: Maybe<Scalars['String']['output']>;
88+
recommendedUsers?: Maybe<Array<Maybe<User>>>;
8889
tweets?: Maybe<Array<Maybe<Tweet>>>;
8990
};
9091

‎graphql.schema.json

+16
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,22 @@
501501
"isDeprecated": false,
502502
"deprecationReason": null
503503
},
504+
{
505+
"name": "recommendedUsers",
506+
"description": null,
507+
"args": [],
508+
"type": {
509+
"kind": "LIST",
510+
"name": null,
511+
"ofType": {
512+
"kind": "OBJECT",
513+
"name": "User",
514+
"ofType": null
515+
}
516+
},
517+
"isDeprecated": false,
518+
"deprecationReason": null
519+
},
504520
{
505521
"name": "tweets",
506522
"description": null,

0 commit comments

Comments
 (0)
Please sign in to comment.