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]");
|
|
|
|
return JSON.parse(element.getAttribute("content"));
|
|
|
|
} 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;
|