-
Notifications
You must be signed in to change notification settings - Fork 909
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1470 from oodamien
* pr/1470: Add history feature Closes gh-1470
- Loading branch information
Showing
16 changed files
with
554 additions
and
8 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import '../../../styles/history.scss' | ||
|
||
import PropTypes from 'prop-types' | ||
import React from 'react' | ||
|
||
import Modal from './Modal' | ||
import { Overlay } from '../form' | ||
|
||
function History({ open, onClose }) { | ||
return ( | ||
<> | ||
<Modal open={open || false} onClose={onClose} /> | ||
<Overlay open={open || false} /> | ||
</> | ||
) | ||
} | ||
|
||
History.propTypes = { | ||
open: PropTypes.bool.isRequired, | ||
onClose: PropTypes.func.isRequired, | ||
} | ||
|
||
export default History |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
import PropTypes from 'prop-types' | ||
import get from 'lodash/get' | ||
import React, { useEffect, useRef, useContext, useMemo } from 'react' | ||
import { CSSTransition, TransitionGroup } from 'react-transition-group' | ||
import { clearAllBodyScrollLocks, disableBodyScroll } from 'body-scroll-lock' | ||
import { AppContext } from '../../reducer/App' | ||
import { Transform } from './Utils' | ||
import queryString from 'query-string' | ||
|
||
function HistoryDate({ label, items, onClose }) { | ||
return ( | ||
<> | ||
<div className='date'>{label}</div> | ||
<ul> | ||
{items.map(item => ( | ||
<HistoryItem | ||
key={item.value} | ||
time={item.time} | ||
value={item.value} | ||
onClose={onClose} | ||
/> | ||
))} | ||
</ul> | ||
</> | ||
) | ||
} | ||
|
||
HistoryDate.propTypes = { | ||
label: PropTypes.string.isRequired, | ||
items: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
time: PropTypes.string, | ||
value: PropTypes.string, | ||
}) | ||
), | ||
onClose: PropTypes.func.isRequired, | ||
} | ||
|
||
HistoryDate.defaultProps = { | ||
items: [], | ||
} | ||
|
||
function getLabelFromList(list, key) { | ||
return list.find(item => item.key === key)?.text || key | ||
} | ||
|
||
function getLabelFromDepsList(list, key) { | ||
return list.find(item => item.id === key)?.name || key | ||
} | ||
|
||
function HistoryItem({ time, value, onClose }) { | ||
const { config } = useContext(AppContext) | ||
const params = queryString.parse(value) | ||
const deps = get(params, 'dependencies', '') | ||
.split(',') | ||
.filter(dep => !!dep) | ||
return ( | ||
<li> | ||
<a | ||
className='item' | ||
href={`/#${value}`} | ||
onClick={() => { | ||
onClose() | ||
}} | ||
> | ||
<span className='time'>{time}</span> | ||
<span className='desc'> | ||
<span className='main'> | ||
Project{' '} | ||
<strong> | ||
{getLabelFromList( | ||
get(config, 'lists.project'), | ||
get(params, 'type') | ||
)} | ||
</strong> | ||
{`, `} | ||
Language{' '} | ||
<strong> | ||
{getLabelFromList( | ||
get(config, 'lists.language'), | ||
get(params, 'language') | ||
)} | ||
</strong> | ||
{`, `} | ||
Spring Boot{' '} | ||
<strong> | ||
{getLabelFromList( | ||
get(config, 'lists.boot'), | ||
get(params, 'platformVersion') | ||
)} | ||
</strong> | ||
</span> | ||
<span className='deps'> | ||
{deps.length === 0 && 'No dependencies'} | ||
{deps.length > 0 && ( | ||
<> | ||
Dependencies:{' '} | ||
<strong> | ||
{deps | ||
.map(dep => | ||
getLabelFromDepsList( | ||
get(config, 'lists.dependencies'), | ||
dep | ||
) | ||
) | ||
.join(', ')} | ||
</strong> | ||
</> | ||
)} | ||
</span> | ||
</span> | ||
</a> | ||
</li> | ||
) | ||
} | ||
|
||
HistoryItem.propTypes = { | ||
time: PropTypes.string.isRequired, | ||
value: PropTypes.string.isRequired, | ||
onClose: PropTypes.func.isRequired, | ||
} | ||
|
||
function Modal({ open, onClose }) { | ||
const wrapper = useRef(null) | ||
const { histories, dispatch } = useContext(AppContext) | ||
|
||
const historiesTransform = useMemo(() => Transform(histories), [histories]) | ||
|
||
useEffect(() => { | ||
const clickOutside = event => { | ||
const children = get(wrapper, 'current') | ||
if (children && !children.contains(event.target)) { | ||
onClose() | ||
} | ||
} | ||
document.addEventListener('mousedown', clickOutside) | ||
return () => { | ||
document.removeEventListener('mousedown', clickOutside) | ||
} | ||
}, [onClose]) | ||
|
||
useEffect(() => { | ||
if (get(wrapper, 'current') && open) { | ||
disableBodyScroll(get(wrapper, 'current')) | ||
} | ||
return () => { | ||
clearAllBodyScrollLocks() | ||
} | ||
}, [wrapper, open]) | ||
|
||
return ( | ||
<TransitionGroup component={null}> | ||
{open && ( | ||
<CSSTransition classNames='popup' timeout={300}> | ||
<div className='modal-share'> | ||
<div className='modal-history-container' ref={wrapper}> | ||
<div className='modal-header'> | ||
<h1>History</h1> | ||
<a | ||
href='/#' | ||
onClick={e => { | ||
e.preventDefault() | ||
dispatch({ type: 'CLEAR_HISTORY' }) | ||
onClose() | ||
}} | ||
className='button' | ||
> | ||
<span className='button-content' tabIndex='-1'> | ||
<span>Clear</span> | ||
</span> | ||
</a> | ||
</div> | ||
<div className='modal-content'> | ||
<div className='list'> | ||
{historiesTransform.map(history => ( | ||
<HistoryDate | ||
key={history.label} | ||
label={history.label} | ||
items={history.histories} | ||
onClose={onClose} | ||
/> | ||
))} | ||
</div> | ||
</div> | ||
<div className='modal-action'> | ||
<a | ||
href='/#' | ||
onClick={e => { | ||
e.preventDefault() | ||
onClose() | ||
}} | ||
className='button' | ||
> | ||
<span className='button-content' tabIndex='-1'> | ||
<span>Close</span> | ||
<span className='secondary desktop-only'>ESC</span> | ||
</span> | ||
</a> | ||
</div> | ||
</div> | ||
</div> | ||
</CSSTransition> | ||
)} | ||
</TransitionGroup> | ||
) | ||
} | ||
|
||
Modal.propTypes = { | ||
open: PropTypes.bool.isRequired, | ||
onClose: PropTypes.func.isRequired, | ||
} | ||
|
||
export default Modal |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { DateTime } from 'luxon' | ||
|
||
function isToday(date) { | ||
return date.hasSame(DateTime.now(), 'day') | ||
} | ||
|
||
function isYesterday(date) { | ||
return date.hasSame(DateTime.now().minus({ days: 1 }), 'day') | ||
} | ||
|
||
export function Transform(histories) { | ||
if (histories.length === 0) { | ||
return [] | ||
} | ||
const parsed = histories.map(history => { | ||
const dateLuxon = DateTime.fromISO(history.date) | ||
let label = '' | ||
if (isToday(dateLuxon)) { | ||
label = 'Today, ' | ||
} else if (isYesterday(dateLuxon)) { | ||
label = 'Yesterday, ' | ||
} | ||
return { | ||
date: dateLuxon, | ||
time: `${dateLuxon.toFormat('HH:mm')}`, | ||
label: `${label}${dateLuxon.toFormat('cccc, d LLLL yyyy')}`, | ||
value: history.value, | ||
} | ||
}) | ||
return parsed.reduce((acc, history) => { | ||
if (acc.length === 0) { | ||
acc.push({ | ||
label: history.label, | ||
histories: [history], | ||
}) | ||
} else if (acc[acc.length - 1].label === history.label) { | ||
acc[acc.length - 1].histories.push(history) | ||
} else { | ||
acc.push({ | ||
label: history.label, | ||
histories: [history], | ||
}) | ||
} | ||
return acc | ||
}, []) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as History } from './History' |
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
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
Oops, something went wrong.