Skip to content

Commit 77e0e51

Browse files
authored
chore: UTC-324: [FE] Refactor the CurrentUser provider (#8616)
Co-authored-by: nass600 <nass600@users.noreply.github.com>
1 parent 0e3d756 commit 77e0e51

File tree

14 files changed

+98
-150
lines changed

14 files changed

+98
-150
lines changed

poetry.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/apps/labelstudio/src/app/App.jsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { FF_UNSAVED_CHANGES, isFF } from "../utils/feature-flags";
1818
import { TourProvider } from "@humansignal/core";
1919
import { ToastProvider, ToastViewport } from "@humansignal/ui";
2020
import { JotaiProvider, JotaiStore } from "../utils/jotai-store";
21-
import { CurrentUserProvider } from "../providers/CurrentUser";
2221
import { QueryClientProvider } from "@tanstack/react-query";
2322
import { queryClient } from "@humansignal/core/lib/utils/query-client";
2423
import { RootPage } from "./RootPage";
@@ -72,7 +71,6 @@ const App = ({ content }) => {
7271
<ConfigProvider key="config" />,
7372
<RoutesProvider key="rotes" />,
7473
<ProjectProvider key="project" />,
75-
<CurrentUserProvider key="current-user" />,
7674
ff.isActive(ff.FF_PRODUCT_TOUR) && <TourProvider useAPI={useAPI} />,
7775
].filter(Boolean)}
7876
>

web/apps/labelstudio/src/components/Menubar/Menubar.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { LSLogo } from "../../assets/images";
1717
import { Button, Userpic, ThemeToggle } from "@humansignal/ui";
1818
import { useConfig } from "../../providers/ConfigProvider";
1919
import { useContextComponent, useFixedLocation } from "../../providers/RoutesProvider";
20-
import { useCurrentUser } from "../../providers/CurrentUser";
20+
import { useAuth } from "@humansignal/core/providers/AuthProvider";
2121
import { cn } from "../../utils/bem";
2222
import { absoluteURL, isDefined } from "../../utils/helpers";
2323
import { Breadcrumbs } from "../Breadcrumbs/Breadcrumbs";
@@ -57,7 +57,7 @@ const RightContextMenu = ({ className, ...props }) => {
5757
export const Menubar = ({ enabled, defaultOpened, defaultPinned, children, onSidebarToggle, onSidebarPin }) => {
5858
const menuDropdownRef = useRef();
5959
const useMenuRef = useRef();
60-
const { user, fetch, isInProgress } = useCurrentUser();
60+
const { user, isLoading } = useAuth();
6161
const location = useFixedLocation();
6262

6363
const config = useConfig();
@@ -201,7 +201,7 @@ export const Menubar = ({ enabled, defaultOpened, defaultPinned, children, onSid
201201
}
202202
>
203203
<div title={user?.email} className={menubarClass.elem("user")}>
204-
<Userpic user={user} isInProgress={isInProgress} />
204+
<Userpic user={user} isInProgress={isLoading} />
205205
{showNewsletterDot && <div className={menubarClass.elem("userpic-badge")} />}
206206
</div>
207207
</Dropdown.Trigger>

web/apps/labelstudio/src/components/Modal/Modal.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createRef } from "react";
22
import { render, unmountComponentAtNode } from "react-dom";
33
import { ApiProvider } from "../../providers/ApiProvider";
44
import { ConfigProvider } from "../../providers/ConfigProvider";
5-
import { CurrentUserProvider } from "../../providers/CurrentUser";
5+
import { AuthProvider } from "@humansignal/core/providers/AuthProvider";
66
import { MultiProvider } from "../../providers/MultiProvider";
77
import { cn } from "../../utils/bem";
88
import { Button } from "@humansignal/ui";
@@ -34,7 +34,7 @@ const standaloneModal = (props) => {
3434
<ConfigProvider key="config" />,
3535
<ToastProvider key="toast" />,
3636
<ApiProvider key="api" />,
37-
<CurrentUserProvider key="current-user" />,
37+
<AuthProvider key="auth" />,
3838
<QueryClientProvider client={queryClient} />,
3939
]
4040
}

web/apps/labelstudio/src/providers/CurrentUser.d.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

web/apps/labelstudio/src/providers/CurrentUser.jsx

Lines changed: 0 additions & 26 deletions
This file was deleted.

web/libs/app-common/src/pages/AccountSettings/sections/EmailPreferences.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { useCallback, useMemo, useRef, useState } from "react";
22
import { Checkbox, Spinner } from "@humansignal/ui";
3+
import { useAuth } from "@humansignal/core/providers/AuthProvider";
34

45
/**
56
* FIXME: This is legacy imports. We're not supposed to use such statements
67
* each one of these eventually has to be migrated to core/ui
78
*/
89
import { useAPI } from "apps/labelstudio/src/providers/ApiProvider";
910
import { useConfig } from "apps/labelstudio/src/providers/ConfigProvider";
10-
import { useCurrentUser } from "apps/labelstudio/src/providers/CurrentUser";
1111
import { ff } from "@humansignal/core";
1212

1313
type NotificationCheckboxProps = {
@@ -34,7 +34,7 @@ export const EmailPreferences = () => {
3434
const isEnterpriseEmailNotificationsEnabled =
3535
ff.isActive(ff.FF_ENTERPRISE_EMAIL_NOTIFICATIONS) && window.APP_SETTINGS?.billing?.enterprise;
3636
const config = useConfig();
37-
const { user, setQueryData } = useCurrentUser();
37+
const { user, refetch } = useAuth();
3838
const api = useAPI();
3939
const [isAllowNewsLetter, setIsAllowNewsLetter] = useState(config.user.allow_newsletters);
4040
const [emailNotificationSettings, setEmailNotificationSettings] = useState(
@@ -60,14 +60,11 @@ export const EmailPreferences = () => {
6060
setEmailNotificationSettings(response.lse_fields.email_notification_settings);
6161
// @ts-ignore
6262
emailNotificationSettingsRef.current = response.lse_fields.email_notification_settings;
63-
setQueryData?.({
64-
// @ts-ignore
65-
lse_fields: { email_notification_settings: response.lse_fields.email_notification_settings },
66-
});
63+
refetch();
6764
}
6865
setIsLoading(false);
6966
},
70-
[user?.id, setQueryData],
67+
[user],
7168
);
7269

7370
const message = useMemo(() => {

web/libs/app-common/src/pages/AccountSettings/sections/MembershipInfo.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import { useQuery } from "@tanstack/react-query";
44
import { API } from "apps/labelstudio/src/providers/ApiProvider";
55
import { useMemo } from "react";
66
import type { WrappedResponse } from "@humansignal/core/lib/api-proxy/types";
7-
import { useCurrentUserAtom } from "@humansignal/core/lib/hooks/useCurrentUser";
7+
import { useAuth } from "@humansignal/core/providers/AuthProvider";
88

99
function formatDate(date?: string) {
1010
return format(new Date(date ?? ""), "dd MMM yyyy, KK:mm a");
1111
}
1212

1313
export const MembershipInfo = () => {
14-
const { user } = useCurrentUserAtom();
14+
const { user } = useAuth();
1515
const dateJoined = useMemo(() => {
1616
if (!user?.date_joined) return null;
1717
return formatDate(user?.date_joined);

web/libs/app-common/src/pages/AccountSettings/sections/PersonalAccessToken.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export const PersonalAccessToken = () => {
5353
<div>
5454
<Label text="Access Token" className={styles.label} />
5555
<div className="flex gap-2 w-full justify-between">
56-
<Input name="token" className={styles.input} readOnly value={token} />
56+
<Input name="token" className={styles.input} readOnly value={token ?? ""} />
5757
<Button
5858
leading={<IconFileCopy />}
5959
onClick={() => copyToken()}
@@ -77,7 +77,7 @@ export const PersonalAccessToken = () => {
7777
readOnly
7878
className={styles.textarea}
7979
rawClassName={styles.textarea}
80-
value={curl}
80+
value={curl ?? ""}
8181
/>
8282
<Button
8383
leading={<IconFileCopy />}

web/libs/app-common/src/pages/AccountSettings/sections/PersonalInfo.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Button, InputFile, ToastType, useToast, Userpic } from "@humansignal/ui
44
// @todo we should not use anything from `apps` in `libs`
55
import { API } from "apps/labelstudio/src/providers/ApiProvider";
66
import styles from "../AccountSettings.module.scss";
7-
import { useCurrentUserAtom } from "@humansignal/core/lib/hooks/useCurrentUser";
7+
import { useAuth } from "@humansignal/core/providers/AuthProvider";
88
import { atomWithMutation } from "jotai-tanstack-query";
99
import { useAtomValue } from "jotai";
1010

@@ -41,12 +41,12 @@ const updateUserAvatarAtom = atomWithMutation(() => ({
4141

4242
export const PersonalInfo = () => {
4343
const toast = useToast();
44-
const { user, fetch: refetchUser, isInProgress: userInProgress, updateAsync: updateUser } = useCurrentUserAtom();
44+
const { user, refetch: refetchUser, isLoading: userInProgress, update: updateUser } = useAuth();
4545
const updateUserAvatar = useAtomValue(updateUserAvatarAtom);
4646
const [isInProgress, setIsInProgress] = useState(false);
47-
const [fname, setFname] = useState(user?.first_name);
48-
const [lname, setLname] = useState(user?.last_name);
49-
const [phone, setPhone] = useState(user?.phone);
47+
const [fname, setFname] = useState(user?.first_name ?? "");
48+
const [lname, setLname] = useState(user?.last_name ?? "");
49+
const [phone, setPhone] = useState(user?.phone ?? "");
5050
const avatarRef = useRef<HTMLInputElement>();
5151
const fileChangeHandler: FormEventHandler<HTMLInputElement> = useCallback(
5252
async (e) => {
@@ -61,7 +61,7 @@ export const PersonalInfo = () => {
6161
});
6262

6363
if (!response.$meta.ok) {
64-
toast.show({ message: response?.response?.detail ?? "Error updating avatar", type: ToastType.error });
64+
toast?.show({ message: response?.response?.detail ?? "Error updating avatar", type: ToastType.error });
6565
} else {
6666
refetchUser();
6767
}
@@ -86,7 +86,7 @@ export const PersonalInfo = () => {
8686

8787
refetchUser();
8888
if (!response?.$meta.ok) {
89-
toast.show({ message: response?.response?.detail ?? "Error updating user", type: ToastType.error });
89+
toast?.show({ message: response?.response?.detail ?? "Error updating user", type: ToastType.error });
9090
}
9191
},
9292
[user?.id],
@@ -97,9 +97,9 @@ export const PersonalInfo = () => {
9797
}, [userInProgress]);
9898

9999
useEffect(() => {
100-
setFname(user?.first_name);
101-
setLname(user?.last_name);
102-
setPhone(user?.phone);
100+
setFname(user?.first_name ?? "");
101+
setLname(user?.last_name ?? "");
102+
setPhone(user?.phone ?? "");
103103
}, [user]);
104104

105105
return (
@@ -142,7 +142,7 @@ export const PersonalInfo = () => {
142142
</div>
143143
<div className={styles.flexRow}>
144144
<div className={styles.flex1}>
145-
<Input label="E-mail" type="email" readOnly={true} value={user?.email} />
145+
<Input label="E-mail" type="email" readOnly={true} value={user?.email ?? ""} />
146146
</div>
147147
<div className={styles.flex1}>
148148
<Input
@@ -155,7 +155,7 @@ export const PersonalInfo = () => {
155155
</div>
156156
</div>
157157
<div className={clsx(styles.flexRow, styles.flexEnd)}>
158-
<Button look="primary" style={{ width: 125 }} waiting={isInProgress}>
158+
<Button style={{ width: 125 }} waiting={isInProgress}>
159159
Save
160160
</Button>
161161
</div>

0 commit comments

Comments
 (0)