Created
November 21, 2024 11:31
-
-
Save ak274/08c05290705136ef303b42cea6970ae6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { data, isLoading } = useOrganizations(); | |
const [columnVisibility, setColumnVisibility] = useState<VisibilityState>({ | |
createdOn: false, | |
updatedOn: false, | |
}); | |
const navigate = useNavigate(); | |
const { id } = useParams(); | |
const rowSelection = useRowSelection(); | |
const statusFilter = useStatus(); | |
const { | |
updateRowSelection, | |
addOrganization, | |
} = useOrganizationActions(); | |
const { pathname } = useLocation(); | |
const selectedRow = Object.keys(rowSelection).at(0); | |
const showDetail = useCallback( | |
(id: string) => { | |
addOrganization(id); | |
navigate(`detail/${id}/${currentTab}`, { replace: true }); | |
updateRowSelection({ [id]: true }); | |
}, | |
[addOrganization, currentTab, navigate, updateRowSelection], | |
); | |
useEffect(() => { | |
if (id) { | |
addOrganization(id); | |
updateRowSelection({ [id]: true }); | |
} | |
if (!isLoading && data?.length) { | |
const firstRow = data.at(0); | |
if (!id && !selectedRow && firstRow) { | |
showDetail(firstRow.id); | |
} | |
if (!id && selectedRow && pathname === '/organizations') { | |
showDetail(selectedRow); | |
} | |
if (!id && !selectedRow) { | |
const firstRow = data.sort((a, b) => b.createdOn - a.createdOn).at(0); | |
showDetail(firstRow?.id ?? ''); | |
} | |
} | |
}, [ | |
isLoading, | |
data, | |
addOrganization, | |
updateRowSelection, | |
showDetail, | |
id, | |
selectedRow, | |
pathname, | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment