2023-11-02 13:50:33 -07:00
|
|
|
// Read the current user ID once from the <meta> tags, and use that forever!
|
|
|
|
const currentUserId = readCurrentUserId();
|
2023-08-10 15:56:36 -07:00
|
|
|
|
|
|
|
function useCurrentUser() {
|
2024-09-09 16:10:45 -07:00
|
|
|
if (currentUserId == null) {
|
|
|
|
return {
|
|
|
|
isLoggedIn: false,
|
|
|
|
id: null,
|
|
|
|
};
|
|
|
|
}
|
2023-08-10 15:56:36 -07:00
|
|
|
|
2024-09-09 16:10:45 -07:00
|
|
|
return {
|
|
|
|
isLoggedIn: true,
|
|
|
|
id: currentUserId,
|
|
|
|
};
|
2023-08-10 15:56:36 -07:00
|
|
|
}
|
|
|
|
|
2023-11-02 13:50:33 -07:00
|
|
|
function readCurrentUserId() {
|
2024-09-09 16:10:45 -07:00
|
|
|
try {
|
|
|
|
const element = document.querySelector("meta[name=dti-current-user-id]");
|
|
|
|
const value = element.getAttribute("content");
|
|
|
|
if (value === "null") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(
|
|
|
|
`[readCurrentUserId] Couldn't read user ID, using null instead`,
|
|
|
|
error,
|
|
|
|
);
|
|
|
|
return null;
|
|
|
|
}
|
2023-08-10 15:56:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export default useCurrentUser;
|