Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added web-server/public/assets/PAT_permissions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
290 changes: 216 additions & 74 deletions web-server/src/content/Dashboards/useIntegrationHandlers.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { LoadingButton } from '@mui/lab';
import { Link, TextField } from '@mui/material';
import { Divider, Link, SxProps, TextField, alpha } from '@mui/material';
import Image from 'next/image';
import { useSnackbar } from 'notistack';
import { FC, useMemo } from 'react';
import { FC, useCallback, useEffect, useMemo } from 'react';
import { useDispatch } from 'react-redux';

import { FlexBox } from '@/components/FlexBox';
Expand All @@ -16,6 +17,7 @@ import {
checkGitHubValidity,
linkProvider
} from '@/utils/auth';
import { depFn } from '@/utils/fn';

export const useIntegrationHandlers = () => {
const { orgId } = useAuth();
Expand Down Expand Up @@ -67,83 +69,223 @@ const ConfigureGithubModalBody: FC<{
};

return (
<FlexBox gap={2} minWidth={'400px'} col>
<FlexBox>Enter you Github token below.</FlexBox>
<FlexBox fullWidth minHeight={'80px'} col>
<TextField
error={!!showError.value}
sx={{ width: '100%' }}
value={token.value}
onChange={(e) => {
handleChange(e.currentTarget.value);
}}
label="Github Personal Access Token"
/>
<Line error tiny mt={1}>
{showError.value}
</Line>
</FlexBox>

<FlexBox gap={4} alignCenter justifyBetween>
<FlexBox col sx={{ opacity: 0.8 }}>
<Line>Learn more about Github</Line>
<Line>
Personal Access Token (PAT)
<Link
ml={1 / 2}
href="https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens"
target="_blank"
rel="noopener noreferrer"
>
here
</Link>
<FlexBox gap2>
<FlexBox gap={2} minWidth={'400px'} col>
<FlexBox>Enter you Github token below.</FlexBox>
<FlexBox fullWidth minHeight={'80px'} col>
<TextField
error={!!showError.value}
sx={{ width: '100%' }}
value={token.value}
onChange={(e) => {
handleChange(e.currentTarget.value);
}}
label="Github Personal Access Token"
/>
<Line error tiny mt={1}>
{showError.value}
</Line>
</FlexBox>
<FlexBox gap={2} justifyEnd>
<LoadingButton
loading={isLoading.value}
variant="contained"
onClick={() => {
if (!token.value) {
setError('Please enter a valid token');
return;
}
isLoading.true();
checkGitHubValidity(token.value)
.then(async (isValid) => {
if (isValid) {
await linkProvider(token.value, orgId, Integration.GITHUB)
.then(() => {
dispatch(fetchCurrentOrg());
enqueueSnackbar('Github linked successfully', {
variant: 'success',
autoHideDuration: 2000

<FlexBox gap={4} alignCenter justifyBetween>
<FlexBox col sx={{ opacity: 0.8 }}>
<Line>Learn more about Github</Line>
<Line>
Personal Access Token (PAT)
<Link
ml={1 / 2}
href="https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens"
target="_blank"
rel="noopener noreferrer"
>
here
</Link>
</Line>
</FlexBox>
<FlexBox gap={2} justifyEnd>
<LoadingButton
loading={isLoading.value}
variant="contained"
onClick={() => {
if (!token.value) {
setError('Please enter a valid token');
return;
}
isLoading.true();
checkGitHubValidity(token.value)
.then(async (isValid) => {
if (isValid) {
await linkProvider(token.value, orgId, Integration.GITHUB)
.then(() => {
dispatch(fetchCurrentOrg());
enqueueSnackbar('Github linked successfully', {
variant: 'success',
autoHideDuration: 2000
});
onClose();
})
.catch((e) => {
setError(
'Failed to link token, please try again later'
);
console.error('Failed to link token', e);
});
onClose();
})
.catch((e) => {
setError(
'Failed to link token, please try again later'
);
console.error('Failed to link token', e);
});
} else {
throw new Error('Invalid token');
}
})
.catch(() => {
setError('Invalid token, please check and try again');
console.error('Invalid token');
})
.finally(() => {
isLoading.false();
});
}}
>
Confirm
</LoadingButton>
} else {
throw new Error('Invalid token');
}
})
.catch(() => {
setError('Invalid token, please check and try again');
console.error('Invalid token');
})
.finally(() => {
isLoading.false();
});
}}
>
Confirm
</LoadingButton>
</FlexBox>
</FlexBox>
</FlexBox>
<Divider orientation="vertical" flexItem />
<TokenPermissions />
</FlexBox>
);
};

const TokenPermissions = () => {
const positionArray = ['0px', '130px', '340px', '740px'];
const position = useEasyState(0);

const changePosition = useCallback(() => {
position.set((position.value + 1) % positionArray.length);
}, [position, positionArray.length]);

const expand = useBoolState(false);
const isLoading = useBoolState(false);

// change position every second
useEffect(() => {
if (expand.value) return depFn(position.set, 0);
const interval = setInterval(changePosition, 2000);
return () => clearInterval(interval);
}, [changePosition, expand.value, position.set]);

const styles: SxProps[] = useMemo(() => {
const baseStyles = {
border: `2px solid ${alpha('rgb(256,0,0)', 0.6)}`,
transition: 'all 0.8s ease',
borderRadius: '12px',
opacity: expand.value ? 0 : 1,
width: '770px',
position: 'absolute',
left: '24px'
};

return [
{
height: '170px',
top: '58px'
},
{
height: '42px',
top: '98px'
},
{
height: '120px',
top: '38px'
},
{
height: '120px',
top: '66px'
}
].map((item) => ({ ...item, ...baseStyles }));
}, [expand.value]);

const expandedStyles = useMemo(() => {
const baseStyles = {
border: `2px solid ${alpha('rgb(256,0,0)', 0.6)}`,
transition: 'all 0.8s ease',
borderRadius: '12px',
opacity: !expand.value ? 0 : 1,
width: '770px',
position: 'absolute',
left: '24px'
};

return [
{
height: '170px',
top: '58px'
},
{
height: '42px',
top: '230px'
},
{
height: '120px',

top: '378px'
},
{
height: '120px',

top: '806px'
}
].map((item) => ({ ...item, ...baseStyles }));
}, [expand.value]);

return (
<FlexBox col gap1>
<div
onMouseEnter={expand.true}
onMouseLeave={expand.false}
style={{
overflow: 'hidden',
borderRadius: '12px',
height: expand.value ? '1257px' : '240px',
transition: 'all 0.8s ease',
position: 'relative'
}}
>
<Image
onLoadStart={isLoading.true}
onLoad={isLoading.false}
style={{
position: 'relative',
bottom: expand.value ? 0 : positionArray[position.value],
transition: 'all 0.8s ease',
opacity: isLoading.value ? 0 : 1
}}
src="/assets/PAT_permissions.png"
width={816}
height={1257}
alt="PAT_permissions"
/>

<FlexBox sx={styles[position.value]} />

{expandedStyles.map((style, index) => (
<FlexBox key={index} sx={style} />
))}

{isLoading.value && (
<FlexBox
sx={{
position: 'absolute',
left: '50%',
top: '50%',
transform: 'translate(-50%, -50%)'
}}
>
Loading...
</FlexBox>
)}
</div>
<Line tiny secondary>
Hover to expand
</Line>
</FlexBox>
);
};