Skip to content

Commit

Permalink
feat: add display name to user class (not persisted) (Chainlit#1188)
Browse files Browse the repository at this point in the history
* feat: add display name to user class (not persisted)

* fix: copilot test
  • Loading branch information
willydouhard authored Aug 6, 2024
1 parent 6435856 commit c89ea37
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
4 changes: 3 additions & 1 deletion backend/chainlit/user.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict, Literal, TypedDict
from typing import Dict, Literal, Optional, TypedDict

from dataclasses_json import DataClassJsonMixin
from pydantic.dataclasses import Field, dataclass
Expand All @@ -19,13 +19,15 @@
class UserDict(TypedDict):
id: str
identifier: str
display_name: Optional[str]
metadata: Dict


# Used when logging-in a user
@dataclass
class User(DataClassJsonMixin):
identifier: str
display_name: Optional[str] = None
metadata: Dict = Field(default_factory=dict)


Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/atoms/buttons/userButton/avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import UserIcon from 'assets/user';

export default function UserAvatar(props: IconButtonProps) {
const { user } = useAuth();

const displayName = user?.display_name || user?.identifier;
return (
<IconButton {...props}>
<Avatar
Expand All @@ -19,7 +19,7 @@ export default function UserAvatar(props: IconButtonProps) {
src={user?.metadata?.image || undefined}
>
{user ? (
user.identifier?.[0]?.toUpperCase()
displayName?.[0]?.toUpperCase()
) : (
<UserIcon sx={{ height: 20, width: 20 }} />
)}
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/atoms/buttons/userButton/menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default function UserMenu({ anchorEl, open, handleClose }: Props) {
{user.id}
</Typography>
<Typography width="100%" fontSize="13px" fontWeight={400}>
{user.identifier}
{user.display_name || user.identifier}
</Typography>
</ListItem>
);
Expand Down Expand Up @@ -86,7 +86,7 @@ export default function UserMenu({ anchorEl, open, handleClose }: Props) {
<MenuItem
key="logout"
onClick={() => {
logout();
logout(true);
handleClose();
}}
>
Expand Down
5 changes: 4 additions & 1 deletion libs/react-client/src/api/hooks/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ export const useAuth = () => {

const isReady = !!(!isLoading && authConfig);

const logout = async () => {
const logout = async (reload = false) => {
await apiClient.logout();
setUser(null);
removeToken();
setAccessToken('');
setThreadHistory(undefined);
if (reload) {
window.location.reload();
}
};

const saveAndSetToken = (token: string | null | undefined) => {
Expand Down
1 change: 1 addition & 0 deletions libs/react-client/src/types/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ export interface IUserMetadata extends Record<string, any> {
export interface IUser {
id: string;
identifier: string;
display_name?: string;
metadata: IUserMetadata;
}

0 comments on commit c89ea37

Please sign in to comment.