'use client' import { Mutex } from "async-mutex" import { useRef, useState } from "react" import styled from "styled-components" import useAsyncEffect from "use-async-effect" import { useShallow } from "zustand/shallow" import { useCurrentAuthenticatedUserStore } from "../providers" import { useUserStore } from "../providers/UsersProvider" type UserTableProps = { page: number } const UserTableStyle = styled.table` width: 100% ` const UserTableHead = styled.thead` background-color: #34067eff ` const UserTH = styled.th` align: center ` const UserTableItem = styled.td` align: center ` const UserTableBody = styled.tbody` > :nth-child(even) { background-color: #410041ff; } > :hover { background-color: #707070ff; } ` const UserTableRow = styled.tr` ` const userTableMutex = new Mutex() export const UserTable = ({page}: UserTableProps) => { const userStore = useUserStore(useShallow((state) => ({ ...state }))) console.log(page) const [callLock, setCallLock] = useState(false) const authUserStore = useCurrentAuthenticatedUserStore(useShallow((state) => state)) console.log(authUserStore) const callLockRef = useRef(callLock) useAsyncEffect(async () => { if(!callLockRef.current && authUserStore.Admin) { callLockRef.current = true setCallLock(true) await userStore.sync(page) callLockRef.current = false setCallLock(false) } }, [page, authUserStore.Id, authUserStore.Name]) useAsyncEffect(async () => { if(authUserStore.Id === -1) { const release = await userTableMutex.acquire() await authUserStore.sync() console.log(authUserStore) await release() } }, [authUserStore.Id]) console.log(userStore.tableUsers) return authUserStore.Admin && ( id name position active admin {userStore.tableUsers.map((u) => ( {u.Id} {u.Name} {u.JobPosition} { if(u.Active) await userStore.deactivateUser(u.Id) else await userStore.activateUser(u.Id) }} checked={u.Active}/> { if(u.Admin) await userStore.demoteUser(u.Id) else await userStore.promoteUser(u.Id) }} checked={u.Admin}/> ))} ) }