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() {
|
2023-11-02 13:50:33 -07:00
|
|
|
if (currentUserId == null) {
|
2023-08-10 15:56:36 -07:00
|
|
|
return {
|
2023-11-02 13:50:33 -07:00
|
|
|
isLoggedIn: false,
|
|
|
|
id: null,
|
2023-08-10 15:56:36 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-11-02 13:50:33 -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() {
|
|
|
|
try {
|
|
|
|
const element = document.querySelector("meta[name=dti-current-user-id]");
|
2023-11-02 17:12:59 -07:00
|
|
|
const value = element.getAttribute("content");
|
|
|
|
if (value === "null") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return value;
|
2023-11-02 13:50:33 -07:00
|
|
|
} 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;
|