这是indexloc提供的服务,不要输入任何密码
Skip to content

Issue 407: Display user image #411

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

Merged
merged 10 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions hosting/next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {fileURLToPath} from "url";
import path from "path";
import {fileURLToPath} from "url";

/** @type {import('next').NextConfig} */
const nextConfig = {
images: {
domains: ["lh3.googleusercontent.com"],
domains: ["lh3.googleusercontent.com", "firebasestorage.googleapis.com"],
},
redirects() {
return [
Expand Down
20 changes: 6 additions & 14 deletions hosting/src/components/Header/DropdownUser.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import PlaceholderAvatar from "@/components/UserPicture/PlaceholderAvatar";
import UserAvatar from "@/components/UserPicture/UserAvatar";
import UserAvatar from "@/components/UserAvatar";
import {useAuthentication} from "@/hooks/useAuthentication";
import {clsx} from "clsx";
import Link from "next/link";
import {Suspense, useEffect, useRef, useState} from "react";

interface DropdownUserProps {
displayName: string;
avatar: string;
}
import {useEffect, useRef, useState} from "react";

interface DropdownItemProps {
href: string;
Expand All @@ -30,11 +24,11 @@ function DropdownItem({href, icon, label}: DropdownItemProps) {
);
}

export default function DropdownUser({displayName, avatar}: DropdownUserProps) {
export default function DropdownUser() {
const [dropdownOpen, setDropdownOpen] = useState(false);
const trigger = useRef<HTMLAnchorElement>(null);
const dropdown = useRef<HTMLDivElement>(null);
const {signout} = useAuthentication();
const {authUser, signout} = useAuthentication();

// close on click outside
useEffect(() => {
Expand Down Expand Up @@ -63,13 +57,11 @@ export default function DropdownUser({displayName, avatar}: DropdownUserProps) {
<div className="relative">
<Link ref={trigger} onClick={() => setDropdownOpen(!dropdownOpen)} className="flex items-center gap-4" href="#">
<span className="hidden text-right lg:block">
<span className="block text-sm font-medium text-black dark:text-white">{displayName}</span>
<span className="block text-sm font-medium text-black dark:text-white">{authUser?.displayName}</span>
</span>

<span className="rounded-full">
<Suspense fallback={<PlaceholderAvatar size={40} />}>
<UserAvatar src={avatar} size={40} />
</Suspense>
<UserAvatar uid={authUser?.uid} size={40} />
</span>
<span className="i-ic-round-keyboard-arrow-down w-[24px] h-[24px]" />
</Link>
Expand Down
7 changes: 1 addition & 6 deletions hosting/src/components/Header/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import DarkModeSwitcher from "@/components/Header/DarkModeSwitcher";
import DropdownUser from "@/components/Header/DropdownUser";
import {useAuthentication} from "@/hooks/useAuthentication";
import {useTanamUser} from "@/hooks/useTanamUser";
import Image from "next/image";
import Link from "next/link";

const Header = (props: {sidebarOpen: string | boolean | undefined; setSidebarOpen: (arg0: boolean) => void}) => {
const {authUser} = useAuthentication();
const {data: tanamUser} = useTanamUser(authUser?.uid);

return (
<header className="sticky top-0 z-999 flex w-full bg-white drop-shadow-1 dark:bg-boxdark dark:drop-shadow-none">
<div className="flex flex-grow items-center justify-between px-4 py-4 shadow-2 md:px-6 2xl:px-11">
Expand Down Expand Up @@ -79,7 +74,7 @@ const Header = (props: {sidebarOpen: string | boolean | undefined; setSidebarOpe
<ul className="flex items-center gap-2 2xsm:gap-4">
<DarkModeSwitcher />
</ul>
{tanamUser ? <DropdownUser displayName={tanamUser.name ?? ""} avatar={authUser?.photoURL ?? ""} /> : <></>}
<DropdownUser />
</div>
</div>
</header>
Expand Down
37 changes: 37 additions & 0 deletions hosting/src/components/UserAvatar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {useTanamUserImage} from "@/hooks/useTanamUser";
import Image from "next/image";
import {Suspense} from "react";

interface UserImageProps {
uid?: string;
size?: number;
}

export default function UserAvatar({uid, size = 112}: UserImageProps) {
const {imageUrl} = useTanamUserImage(uid);

return (
<Suspense fallback={<PlaceholderAvatar size={size} />}>
{imageUrl ? (
<Image
id={`UserAvatar-${uid}`}
width={size}
height={size}
src={imageUrl}
style={{width: "auto", height: "auto"}}
alt="User Profile picture"
/>
) : (
<PlaceholderAvatar size={size} />
)}
</Suspense>
);
}

function PlaceholderAvatar({size}: {size: number}) {
return (
<div className="flex items-center justify-center">
<span className="i-ic-baseline-person" style={{fontSize: size}} />
</div>
);
}
11 changes: 0 additions & 11 deletions hosting/src/components/UserPicture/PlaceholderAvatar.tsx

This file was deleted.

15 changes: 0 additions & 15 deletions hosting/src/components/UserPicture/UserAvatar.tsx

This file was deleted.

39 changes: 38 additions & 1 deletion hosting/src/hooks/useTanamUser.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {TanamUserClient} from "@/models/TanamUserClient";
import {UserNotification} from "@/models/UserNotification";
import {firestore} from "@/plugins/firebase";
import {firestore, storage} from "@/plugins/firebase";
import {getDownloadURL, ref} from "@firebase/storage";
import {doc, onSnapshot} from "firebase/firestore";
import {useEffect, useState} from "react";

Expand Down Expand Up @@ -47,3 +48,39 @@ export function useTanamUser(uid?: string): UseTanamDocumentsResult {

return {data, error};
}

interface UseProfileImageResult {
imageUrl: string | null;
error: UserNotification | null;
}

/**
* Hook to get a profile image URL from Firebase Cloud Storage
*
* @param {string?} uid User ID
* @return {UseProfileImageResult} Hook for profile image URL
*/
export function useTanamUserImage(uid?: string): UseProfileImageResult {
const [imageUrl, setImageUrl] = useState<string | null>(null);
const [error, setError] = useState<UserNotification | null>(null);

useEffect(() => {
if (!uid) {
setImageUrl(null);
return;
}

const imageRef = ref(storage, `tanam-users/${uid}/profile.png`);
console.log(`Fetching profile image for user ${uid}: ${imageRef}`);

getDownloadURL(imageRef)
.then((url) => {
setImageUrl(url);
})
.catch((err) => {
setError(new UserNotification("error", "Error fetching profile image", err.message));
});
}, [uid]);

return {imageUrl, error};
}
2 changes: 2 additions & 0 deletions hosting/src/plugins/firebase.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {initializeApp} from "firebase/app";
import {getAuth} from "firebase/auth";
import {getFirestore} from "firebase/firestore";
import {getStorage} from "firebase/storage";

const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
Expand All @@ -15,3 +16,4 @@ const firebaseConfig = {
export const firebaseApp = initializeApp(firebaseConfig);
export const firebaseAuth = getAuth(firebaseApp);
export const firestore = getFirestore(firebaseApp);
export const storage = getStorage(firebaseApp);
18 changes: 12 additions & 6 deletions storage.rules
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
rules_version = '2';

// Craft rules based on data in your Firestore database
// allow write: if firestore.get(
// /databases/(default)/documents/users/$(request.auth.uid)).data.isAdmin;
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if false;
match /tanam-users/{uid} {
match /{allPaths=**} {
allow read: if request.auth != null && request.auth.uid == uid;
}

match /profile.png {
allow read: if request.auth != null;
}

match /profile-picture-new {
allow create: if request.auth != null && request.auth.uid == uid;
}
}
}
}