@oxyhq/services 0.0.74 → 0.0.76
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets/dot-icon.tsx +11 -0
- package/dist/assets/verified-icon.tsx +19 -0
- package/dist/components/assets/oxy-logo.tsx +97 -0
- package/dist/components/auth/AccountSwitcherModal.tsx +139 -0
- package/dist/components/auth/SessionOwnerButton.tsx +67 -0
- package/dist/components/auth/SignInButton.tsx +31 -0
- package/dist/components/auth/styles/oavatar.module.scss +36 -0
- package/dist/components/auth/styles/sign-in-button.module.scss +32 -0
- package/dist/components/elements/button/components/button.tsx +35 -0
- package/dist/components/elements/button/components/styles/button.module.scss +27 -0
- package/dist/components/elements/button/index.ts +1 -0
- package/dist/components/elements/ellipsis-wrapper/components/EllipsisWrapper.tsx +18 -0
- package/dist/components/elements/ellipsis-wrapper/components/styles/ellipses-wrapper.module.scss +10 -0
- package/dist/components/elements/ellipsis-wrapper/index.ts +1 -0
- package/dist/components/elements/modal/components/confirmation-modal.tsx +62 -0
- package/dist/components/elements/modal/components/modal.tsx +182 -0
- package/dist/components/elements/modal/components/styles/confirmation-modal.module.scss +125 -0
- package/dist/components/elements/modal/components/styles/modal.module.scss +8 -0
- package/dist/components/elements/modal/hooks/use-track-position.ts +49 -0
- package/dist/components/elements/modal/index.ts +3 -0
- package/dist/config/index.ts +17 -0
- package/dist/features/profile/components/avatar.tsx +30 -0
- package/dist/features/profile/components/styles/avatar.module.scss +14 -0
- package/dist/features/profile/components/styles/user-name.module.scss +19 -0
- package/dist/features/profile/components/styles/user-username.module.scss +4 -0
- package/dist/features/profile/components/user-name.tsx +21 -0
- package/dist/features/profile/components/user-username.tsx +10 -0
- package/dist/features/profile/index.ts +3 -0
- package/dist/hooks/get-user.ts +30 -0
- package/dist/hooks/getUserById.ts +43 -0
- package/dist/hooks/use-user.ts +25 -0
- package/dist/hooks/useOxySession.ts +104 -0
- package/dist/index.css +0 -2
- package/dist/index.ts +13 -0
- package/dist/interfaces/index.tsx +22 -0
- package/dist/types/index.ts +1 -0
- package/dist/utils/cn.ts +6 -0
- package/package.json +6 -5
- package/dist/components/auth/styles/account-switcher-modal.module.css +0 -35
- package/dist/components/auth/styles/account-switcher-modal.module.css.map +0 -1
- package/dist/components/auth/styles/session-owner-modal.module.css +0 -35
- package/dist/components/auth/styles/session-owner-modal.module.css.map +0 -1
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/* eslint-disable jsx-a11y/no-static-element-interactions */
|
|
2
|
+
/* eslint-disable jsx-a11y/click-events-have-key-events */
|
|
3
|
+
/* eslint-disable jsx-a11y/no-noninteractive-element-interactions */
|
|
4
|
+
"use client";
|
|
5
|
+
import { motion } from "framer-motion";
|
|
6
|
+
import React, { useCallback, useLayoutEffect, useRef } from "react";
|
|
7
|
+
import { createPortal } from "react-dom";
|
|
8
|
+
|
|
9
|
+
import styles from "./styles/modal.module.scss";
|
|
10
|
+
|
|
11
|
+
export const Modal = ({
|
|
12
|
+
children,
|
|
13
|
+
onClose,
|
|
14
|
+
closeOnBackdropClick = true,
|
|
15
|
+
background,
|
|
16
|
+
minViewportWidth = null,
|
|
17
|
+
maxViewportWidth = null,
|
|
18
|
+
disableScroll = false,
|
|
19
|
+
focusOnElement = null,
|
|
20
|
+
focusAfterClose = null,
|
|
21
|
+
}: {
|
|
22
|
+
children: React.ReactNode;
|
|
23
|
+
onClose: () => void;
|
|
24
|
+
closeOnBackdropClick?: boolean;
|
|
25
|
+
background?: string;
|
|
26
|
+
minViewportWidth?: number | null;
|
|
27
|
+
maxViewportWidth?: number | null;
|
|
28
|
+
disableScroll?: boolean;
|
|
29
|
+
focusOnElement?: string | null;
|
|
30
|
+
focusAfterClose?: string | null;
|
|
31
|
+
}) => {
|
|
32
|
+
const modalRef = useRef<HTMLDivElement>(null);
|
|
33
|
+
const previouslyFocusedElementRef = useRef<HTMLElement | null>(null);
|
|
34
|
+
|
|
35
|
+
const handleKeyDown = useCallback(
|
|
36
|
+
(e: KeyboardEvent) => {
|
|
37
|
+
if (e.key === "Escape") {
|
|
38
|
+
onClose();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (e.key === "Tab") {
|
|
42
|
+
const modal = modalRef.current;
|
|
43
|
+
if (!modal) return;
|
|
44
|
+
|
|
45
|
+
const focusableElements = Array.from(
|
|
46
|
+
modal.querySelectorAll(
|
|
47
|
+
'a[href]:not([disabled]), button:not([disabled]), textarea:not([disabled]), input[type="text"]:not([disabled]), input[type="radio"]:not([disabled]), input[type="checkbox"]:not([disabled]), select:not([disabled])'
|
|
48
|
+
)
|
|
49
|
+
);
|
|
50
|
+
const firstFocusableElement = focusableElements[0] as HTMLElement;
|
|
51
|
+
const lastFocusableElement = focusableElements[
|
|
52
|
+
focusableElements.length - 1
|
|
53
|
+
] as HTMLElement;
|
|
54
|
+
|
|
55
|
+
if (e.shiftKey) {
|
|
56
|
+
if (document.activeElement === firstFocusableElement) {
|
|
57
|
+
lastFocusableElement.focus();
|
|
58
|
+
e.preventDefault();
|
|
59
|
+
}
|
|
60
|
+
} else {
|
|
61
|
+
if (document.activeElement === lastFocusableElement) {
|
|
62
|
+
firstFocusableElement.focus();
|
|
63
|
+
e.preventDefault();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
[onClose]
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
const handleScroll = useCallback(
|
|
72
|
+
(e: Event) => {
|
|
73
|
+
if (disableScroll) {
|
|
74
|
+
e.preventDefault();
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
[disableScroll]
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
const handleDisplay = useCallback(() => {
|
|
81
|
+
const modal = modalRef.current;
|
|
82
|
+
if (!modal) return;
|
|
83
|
+
|
|
84
|
+
const viewportWidth = window.innerWidth;
|
|
85
|
+
|
|
86
|
+
if (
|
|
87
|
+
(minViewportWidth && viewportWidth < minViewportWidth) ||
|
|
88
|
+
(maxViewportWidth && viewportWidth > maxViewportWidth)
|
|
89
|
+
) {
|
|
90
|
+
onClose();
|
|
91
|
+
}
|
|
92
|
+
}, [onClose, minViewportWidth, maxViewportWidth]);
|
|
93
|
+
|
|
94
|
+
useLayoutEffect(() => {
|
|
95
|
+
const modal = modalRef.current;
|
|
96
|
+
if (!modal) return;
|
|
97
|
+
|
|
98
|
+
const focusableElements = Array.from(
|
|
99
|
+
modal.querySelectorAll(
|
|
100
|
+
'a[href]:not([disabled]), button:not([disabled]), textarea:not([disabled]), input[type="text"]:not([disabled]), input[type="radio"]:not([disabled]), input[type="checkbox"]:not([disabled]), select:not([disabled])'
|
|
101
|
+
)
|
|
102
|
+
);
|
|
103
|
+
const firstFocusableElement = focusableElements?.[0] as HTMLElement;
|
|
104
|
+
|
|
105
|
+
previouslyFocusedElementRef.current = document.activeElement as HTMLElement;
|
|
106
|
+
|
|
107
|
+
modal?.addEventListener("keydown", handleKeyDown);
|
|
108
|
+
if (focusOnElement) {
|
|
109
|
+
const elementToFocus = modal.querySelector(focusOnElement) as HTMLElement;
|
|
110
|
+
elementToFocus?.focus();
|
|
111
|
+
} else {
|
|
112
|
+
firstFocusableElement?.focus();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (disableScroll) {
|
|
116
|
+
document.body.style.overflow = "hidden";
|
|
117
|
+
document.body.style.paddingRight = "11px";
|
|
118
|
+
window.addEventListener("scroll", handleScroll);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (minViewportWidth || maxViewportWidth) {
|
|
122
|
+
handleDisplay();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return () => {
|
|
126
|
+
modal?.removeEventListener("keydown", handleKeyDown);
|
|
127
|
+
if (focusAfterClose) {
|
|
128
|
+
const elementToFocus = document.querySelector(
|
|
129
|
+
focusAfterClose
|
|
130
|
+
) as HTMLElement;
|
|
131
|
+
elementToFocus?.focus();
|
|
132
|
+
} else {
|
|
133
|
+
previouslyFocusedElementRef.current?.focus();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (disableScroll) {
|
|
137
|
+
document.body.style.overflow = "";
|
|
138
|
+
document.body.style.paddingRight = "";
|
|
139
|
+
window.removeEventListener("scroll", handleScroll);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
}, [
|
|
143
|
+
onClose,
|
|
144
|
+
disableScroll,
|
|
145
|
+
handleKeyDown,
|
|
146
|
+
handleScroll,
|
|
147
|
+
handleDisplay,
|
|
148
|
+
minViewportWidth,
|
|
149
|
+
maxViewportWidth,
|
|
150
|
+
focusOnElement,
|
|
151
|
+
focusAfterClose,
|
|
152
|
+
]);
|
|
153
|
+
|
|
154
|
+
const backdropStyle: React.CSSProperties = {
|
|
155
|
+
backgroundColor:
|
|
156
|
+
background === "none" ? "transparent" : "var(--clr-modal-background)",
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
return createPortal(
|
|
160
|
+
<motion.div
|
|
161
|
+
initial={{ opacity: 0 }}
|
|
162
|
+
animate={{ opacity: 1 }}
|
|
163
|
+
exit={{ opacity: 0 }}
|
|
164
|
+
onClick={(e) => {
|
|
165
|
+
e.stopPropagation();
|
|
166
|
+
if (e.currentTarget === e.target && closeOnBackdropClick) onClose();
|
|
167
|
+
}}
|
|
168
|
+
onKeyDown={(e) => {
|
|
169
|
+
e.stopPropagation();
|
|
170
|
+
}}
|
|
171
|
+
ref={modalRef}
|
|
172
|
+
className={`${styles.container}`}
|
|
173
|
+
style={backdropStyle}
|
|
174
|
+
id="dialog"
|
|
175
|
+
role="dialog"
|
|
176
|
+
aria-modal="true"
|
|
177
|
+
>
|
|
178
|
+
{children}
|
|
179
|
+
</motion.div>,
|
|
180
|
+
document.body
|
|
181
|
+
);
|
|
182
|
+
};
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
.container {
|
|
2
|
+
position: fixed;
|
|
3
|
+
top: 0;
|
|
4
|
+
left: 0;
|
|
5
|
+
bottom: 0;
|
|
6
|
+
right: 0;
|
|
7
|
+
width: fit-content;
|
|
8
|
+
max-width: 320px;
|
|
9
|
+
height: fit-content;
|
|
10
|
+
margin: auto;
|
|
11
|
+
background-color: var(--clr-background);
|
|
12
|
+
padding: 2em;
|
|
13
|
+
border-radius: 1rem;
|
|
14
|
+
|
|
15
|
+
.logo {
|
|
16
|
+
display: grid;
|
|
17
|
+
place-items: center;
|
|
18
|
+
margin-bottom: 1em;
|
|
19
|
+
|
|
20
|
+
svg {
|
|
21
|
+
width: var(--fs-kilo);
|
|
22
|
+
height: var(--fs-kilo);
|
|
23
|
+
fill: var(--clr-primary);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
h1 {
|
|
28
|
+
font-size: var(--fs-h2);
|
|
29
|
+
font-weight: var(--fw-700);
|
|
30
|
+
margin-bottom: 0.5em;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
p {
|
|
34
|
+
font-size: var(--fs-milli);
|
|
35
|
+
color: var(--clr-tertiary);
|
|
36
|
+
margin-bottom: 2rem;
|
|
37
|
+
line-height: 1.4;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
.buttons {
|
|
41
|
+
display: grid;
|
|
42
|
+
gap: 0.8rem;
|
|
43
|
+
|
|
44
|
+
button {
|
|
45
|
+
display: grid;
|
|
46
|
+
place-items: center;
|
|
47
|
+
padding: 0.9em;
|
|
48
|
+
border-radius: 100vmax;
|
|
49
|
+
font-size: var(--fs-milli);
|
|
50
|
+
font-weight: var(--fw-700);
|
|
51
|
+
cursor: pointer;
|
|
52
|
+
transition: background-color 0.2s ease;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
.delete {
|
|
56
|
+
background-color: var(--clr-red);
|
|
57
|
+
|
|
58
|
+
&:hover {
|
|
59
|
+
background-color: var(--clr-red-hover);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
&:active {
|
|
63
|
+
background-color: var(--clr-red-active);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
&:focus-visible {
|
|
67
|
+
outline: 2px solid var(--clr-secondary);
|
|
68
|
+
background-color: var(--clr-red-hover);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
.unfollow {
|
|
73
|
+
background-color: var(--clr-secondary);
|
|
74
|
+
color: var(--clr-background);
|
|
75
|
+
|
|
76
|
+
&:hover {
|
|
77
|
+
background-color: var(--clr-secondary-hover);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
&:active {
|
|
81
|
+
background-color: var(--clr-secondary-active);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
&:focus-visible {
|
|
85
|
+
outline: 2px solid var(--clr-secondary);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
.signout {
|
|
90
|
+
background-color: var(--clr-secondary);
|
|
91
|
+
color: var(--clr-background);
|
|
92
|
+
|
|
93
|
+
&:hover {
|
|
94
|
+
background-color: var(--clr-signout-logout-hover);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
&:active {
|
|
98
|
+
background-color: var(--clr-signout-logout-active);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
&:focus-visible {
|
|
102
|
+
outline: 2px solid var(--clr-secondary);
|
|
103
|
+
background-color: var(--clr-signout-logout-hover);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
.cancel {
|
|
108
|
+
color: var(--clr-secondary);
|
|
109
|
+
border: 1px solid var(--clr-tertiary);
|
|
110
|
+
|
|
111
|
+
&:hover {
|
|
112
|
+
background-color: var(--clr-trends-hover);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
&:active {
|
|
116
|
+
background-color: var(--clr-trends-active);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
&:focus-visible {
|
|
120
|
+
outline: 2px solid var(--clr-secondary);
|
|
121
|
+
background-color: var(--clr-trends-hover);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useState, useLayoutEffect } from "react";
|
|
3
|
+
|
|
4
|
+
export const useTrackPosition = ({
|
|
5
|
+
buttonRef,
|
|
6
|
+
trackScroll = false,
|
|
7
|
+
}: {
|
|
8
|
+
buttonRef: React.RefObject<HTMLButtonElement>;
|
|
9
|
+
trackScroll?: boolean;
|
|
10
|
+
}) => {
|
|
11
|
+
const [buttonBoundaries, setButtonBoundaries] = useState<DOMRect | null>(
|
|
12
|
+
buttonRef?.current?.getBoundingClientRect() ?? null,
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
useLayoutEffect(() => {
|
|
16
|
+
const handleResize = () => {
|
|
17
|
+
if (buttonRef?.current) {
|
|
18
|
+
setButtonBoundaries(buttonRef.current.getBoundingClientRect());
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
window.addEventListener("resize", handleResize);
|
|
23
|
+
if (trackScroll) {
|
|
24
|
+
window.addEventListener("scroll", handleResize);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return () => {
|
|
28
|
+
window.removeEventListener("resize", handleResize);
|
|
29
|
+
if (trackScroll) {
|
|
30
|
+
window.removeEventListener("scroll", handleResize);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}, [
|
|
34
|
+
buttonRef,
|
|
35
|
+
buttonRef.current?.getBoundingClientRect,
|
|
36
|
+
trackScroll,
|
|
37
|
+
setButtonBoundaries,
|
|
38
|
+
]);
|
|
39
|
+
|
|
40
|
+
useLayoutEffect(() => {
|
|
41
|
+
setButtonBoundaries(buttonRef.current?.getBoundingClientRect() ?? null);
|
|
42
|
+
}, [
|
|
43
|
+
buttonRef,
|
|
44
|
+
buttonRef.current?.getBoundingClientRect,
|
|
45
|
+
setButtonBoundaries,
|
|
46
|
+
]);
|
|
47
|
+
|
|
48
|
+
return buttonBoundaries;
|
|
49
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
const envSchema = z.object({
|
|
4
|
+
NODE_ENV: z.enum(["development", "production"]),
|
|
5
|
+
OXY_AUTH_URL: z.string().url(),
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
declare global {
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-namespace
|
|
10
|
+
namespace NodeJS {
|
|
11
|
+
interface ProcessEnv extends z.infer<typeof envSchema> {}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const NODE_ENV = process.env.NODE_ENV;
|
|
16
|
+
export const OXY_AUTH_URL =
|
|
17
|
+
NODE_ENV === "production" ? "https://auth.oxy.so" : "http://localhost:3001";
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import Image from "next/image";
|
|
2
|
+
import React from "react";
|
|
3
|
+
|
|
4
|
+
import { cn } from "../../../utils/cn";
|
|
5
|
+
|
|
6
|
+
interface IAvatar extends React.ImgHTMLAttributes<HTMLImageElement> {
|
|
7
|
+
userImage: string | null | undefined;
|
|
8
|
+
className?: string;
|
|
9
|
+
width?: number;
|
|
10
|
+
height?: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const Avatar = ({ userImage, className, ...props }: IAvatar) => {
|
|
14
|
+
return (
|
|
15
|
+
<div
|
|
16
|
+
className={cn(
|
|
17
|
+
"relative aspect-square w-[calc(var(--tw-fs-kilo)+9px)] overflow-hidden rounded-full",
|
|
18
|
+
className
|
|
19
|
+
)}
|
|
20
|
+
>
|
|
21
|
+
<Image
|
|
22
|
+
{...props}
|
|
23
|
+
src={userImage || `/user_placeholder.png`}
|
|
24
|
+
alt="profile picture"
|
|
25
|
+
fill={true}
|
|
26
|
+
className="block size-full object-cover"
|
|
27
|
+
/>
|
|
28
|
+
</div>
|
|
29
|
+
);
|
|
30
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
.container {
|
|
2
|
+
font-size: var(--fs-milli);
|
|
3
|
+
font-weight: var(--fw-500);
|
|
4
|
+
color: var(--clr-secondary);
|
|
5
|
+
display: grid;
|
|
6
|
+
grid-auto-flow: column;
|
|
7
|
+
|
|
8
|
+
span {
|
|
9
|
+
white-space: nowrap;
|
|
10
|
+
overflow: hidden;
|
|
11
|
+
text-overflow: ellipsis;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
.hover {
|
|
16
|
+
&:hover {
|
|
17
|
+
text-decoration: underline;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { VerifiedIcon } from "../../../assets/verified-icon";
|
|
3
|
+
|
|
4
|
+
import styles from "./styles/user-name.module.scss";
|
|
5
|
+
|
|
6
|
+
export const UserName = ({
|
|
7
|
+
name,
|
|
8
|
+
isVerified = false,
|
|
9
|
+
hover = false,
|
|
10
|
+
}: {
|
|
11
|
+
name: string | undefined;
|
|
12
|
+
isVerified?: boolean | undefined;
|
|
13
|
+
hover?: boolean | undefined;
|
|
14
|
+
}) => {
|
|
15
|
+
return (
|
|
16
|
+
<div className={`${styles.container} ${hover ? styles.hover : ""}`}>
|
|
17
|
+
{name && <span>{name}</span>}
|
|
18
|
+
{isVerified && <VerifiedIcon />}
|
|
19
|
+
</div>
|
|
20
|
+
);
|
|
21
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import { OXY_AUTH_URL } from "../config";
|
|
3
|
+
|
|
4
|
+
export const getUser = async (id: string | undefined) => {
|
|
5
|
+
if (!id) {
|
|
6
|
+
throw new Error("User ID is required");
|
|
7
|
+
}
|
|
8
|
+
try {
|
|
9
|
+
const response = await axios.get(OXY_AUTH_URL + `/api/users/${id}`);
|
|
10
|
+
return response.data;
|
|
11
|
+
} catch (error: any) {
|
|
12
|
+
if (axios.isAxiosError(error)) {
|
|
13
|
+
if (error.response) {
|
|
14
|
+
// Server responded with a status code outside of 2xx
|
|
15
|
+
throw new Error(
|
|
16
|
+
`Server responded with status ${error.response.status}: ${error.response.data}`
|
|
17
|
+
);
|
|
18
|
+
} else if (error.request) {
|
|
19
|
+
// Request was made but no response received
|
|
20
|
+
throw new Error("No response received from server");
|
|
21
|
+
} else {
|
|
22
|
+
// Error setting up the request
|
|
23
|
+
throw new Error(`Axios error: ${error.message}`);
|
|
24
|
+
}
|
|
25
|
+
} else {
|
|
26
|
+
// Non-Axios error
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import { OXY_AUTH_URL } from "../config";
|
|
3
|
+
|
|
4
|
+
interface User {
|
|
5
|
+
id: string;
|
|
6
|
+
username: string;
|
|
7
|
+
name: string;
|
|
8
|
+
email: string;
|
|
9
|
+
role: string;
|
|
10
|
+
avatar: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const getUserById = async (id?: string | string[], fields?: (keyof User)[]) => {
|
|
14
|
+
try {
|
|
15
|
+
const response = await axios.get(OXY_AUTH_URL + `/api/users/${id}`);
|
|
16
|
+
if (response.status !== 200) {
|
|
17
|
+
throw new Error(`Unexpected response status: ${response.status}`);
|
|
18
|
+
}
|
|
19
|
+
const user = fields
|
|
20
|
+
? fields.reduce(
|
|
21
|
+
(obj, key) => ({ ...obj, [key]: response.data[key] }),
|
|
22
|
+
{} as User
|
|
23
|
+
)
|
|
24
|
+
: (response.data as User);
|
|
25
|
+
return user;
|
|
26
|
+
} catch (error) {
|
|
27
|
+
if (axios.isAxiosError(error)) {
|
|
28
|
+
if (error.response) {
|
|
29
|
+
throw new Error(
|
|
30
|
+
`Network error: ${error.message}, Status code: ${error.response.status}`
|
|
31
|
+
);
|
|
32
|
+
} else if (error.request) {
|
|
33
|
+
throw new Error("Network error: No response received from server.");
|
|
34
|
+
} else {
|
|
35
|
+
throw new Error(`Network error: ${error.message}`);
|
|
36
|
+
}
|
|
37
|
+
} else {
|
|
38
|
+
throw new Error("An unknown error occurred while loading the user data.");
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export default getUserById;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { getUser } from "./get-user";
|
|
2
|
+
|
|
3
|
+
export interface IUser {
|
|
4
|
+
id: string;
|
|
5
|
+
username: string;
|
|
6
|
+
name: string;
|
|
7
|
+
email: string;
|
|
8
|
+
role: string;
|
|
9
|
+
avatar: string;
|
|
10
|
+
color: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const useUser = async ({
|
|
14
|
+
id,
|
|
15
|
+
initialData: IUser,
|
|
16
|
+
}: {
|
|
17
|
+
id: string | undefined;
|
|
18
|
+
initialData?: IUser;
|
|
19
|
+
}) => {
|
|
20
|
+
if (!id) {
|
|
21
|
+
// Return a default value or null when id is not provided
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
return getUser(id);
|
|
25
|
+
};
|