|
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { IDocumentManager } from '@jupyterlab/docmanager'; |
| 3 | +import { Notification } from '@jupyterlab/apputils'; |
| 4 | +import { Container, Group, Stack } from '@mantine/core'; |
| 5 | +import { getProfile } from '../services/leetcode'; |
| 6 | +import { LeetCodeProfile } from '../types/leetcode'; |
| 7 | +import Profile from './Profile'; |
| 8 | +import Statistics from './Statistics'; |
| 9 | +import QuestionList from './QuestionList'; |
| 10 | + |
| 11 | +const LeetCodeMain: React.FC<{ docManager: IDocumentManager }> = ({ |
| 12 | + docManager |
| 13 | +}) => { |
| 14 | + const [profile, setProfile] = useState<LeetCodeProfile | null>(null); |
| 15 | + |
| 16 | + useEffect(() => { |
| 17 | + getProfile() |
| 18 | + .then(profile => { |
| 19 | + if (!profile.isSignedIn) { |
| 20 | + Notification.error('Please sign in to LeetCode.', { |
| 21 | + autoClose: 3000 |
| 22 | + }); |
| 23 | + return; |
| 24 | + } |
| 25 | + setProfile(profile); |
| 26 | + }) |
| 27 | + .catch(e => Notification.error(e.message, { autoClose: 3000 })); |
| 28 | + }, []); |
| 29 | + |
| 30 | + const openNoteBook = (path: string) => { |
| 31 | + docManager.openOrReveal(path); |
| 32 | + }; |
| 33 | + |
| 34 | + const calcHeight = () => { |
| 35 | + const mainEle = document.querySelector('#jll-main'); |
| 36 | + const profileEle = document.querySelector('#jll-profile'); |
| 37 | + if (!mainEle || !profileEle) { |
| 38 | + return '100%'; |
| 39 | + } |
| 40 | + const mainStyle = window.getComputedStyle(mainEle); |
| 41 | + const padding = |
| 42 | + parseFloat(mainStyle.paddingTop) + parseFloat(mainStyle.paddingBottom); |
| 43 | + return mainEle.clientHeight - padding - profileEle.clientHeight; |
| 44 | + }; |
| 45 | + |
| 46 | + return ( |
| 47 | + <Container fluid={true} h={'100%'} p="lg" id="jll-main"> |
| 48 | + <Stack> |
| 49 | + <Group id="jll-profile"> |
| 50 | + {profile && <Profile profile={profile} />} |
| 51 | + {profile && <Statistics username={profile.username} />} |
| 52 | + </Group> |
| 53 | + <QuestionList openNotebook={openNoteBook} height={calcHeight()} /> |
| 54 | + </Stack> |
| 55 | + </Container> |
| 56 | + ); |
| 57 | +}; |
| 58 | + |
| 59 | +export default LeetCodeMain; |
0 commit comments