2021-06-18 12:09:59 +02:00
|
|
|
import { Fragment, KeyboardEvent, useState, useEffect } from 'react';
|
2021-10-22 13:31:02 +02:00
|
|
|
import {
|
|
|
|
DragDropContext,
|
|
|
|
Droppable,
|
|
|
|
Draggable,
|
|
|
|
DropResult,
|
|
|
|
} from 'react-beautiful-dnd';
|
2021-06-18 12:09:59 +02:00
|
|
|
import { Link } from 'react-router-dom';
|
|
|
|
|
|
|
|
// Redux
|
2021-11-09 14:33:51 +01:00
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
2021-06-18 12:09:59 +02:00
|
|
|
|
|
|
|
// Typescript
|
2021-11-09 14:33:51 +01:00
|
|
|
import { App } from '../../../interfaces';
|
2021-05-13 18:23:12 +02:00
|
|
|
|
2021-06-18 12:09:59 +02:00
|
|
|
// CSS
|
2021-05-13 18:23:12 +02:00
|
|
|
import classes from './AppTable.module.css';
|
2021-06-18 12:09:59 +02:00
|
|
|
|
|
|
|
// UI
|
2021-11-09 14:33:51 +01:00
|
|
|
import { Icon, Table } from '../../UI';
|
|
|
|
import { State } from '../../../store/reducers';
|
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
import { actionCreators } from '../../../store';
|
|
|
|
|
|
|
|
interface Props {
|
2021-05-22 17:03:32 +02:00
|
|
|
updateAppHandler: (app: App) => void;
|
2021-05-13 18:23:12 +02:00
|
|
|
}
|
|
|
|
|
2021-11-09 14:33:51 +01:00
|
|
|
export const AppTable = (props: Props): JSX.Element => {
|
|
|
|
const {
|
|
|
|
apps: { apps },
|
|
|
|
config: { config },
|
|
|
|
} = useSelector((state: State) => state);
|
|
|
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const { pinApp, deleteApp, reorderApps, updateConfig, createNotification } =
|
|
|
|
bindActionCreators(actionCreators, dispatch);
|
|
|
|
|
2021-06-18 12:09:59 +02:00
|
|
|
const [localApps, setLocalApps] = useState<App[]>([]);
|
|
|
|
const [isCustomOrder, setIsCustomOrder] = useState<boolean>(false);
|
|
|
|
|
|
|
|
// Copy apps array
|
|
|
|
useEffect(() => {
|
2021-11-09 14:33:51 +01:00
|
|
|
setLocalApps([...apps]);
|
|
|
|
}, [apps]);
|
2021-06-18 12:09:59 +02:00
|
|
|
|
|
|
|
// Check ordering
|
|
|
|
useEffect(() => {
|
2021-11-09 14:33:51 +01:00
|
|
|
const order = config.useOrdering;
|
2021-06-18 12:09:59 +02:00
|
|
|
|
|
|
|
if (order === 'orderId') {
|
|
|
|
setIsCustomOrder(true);
|
|
|
|
}
|
2021-10-22 13:31:02 +02:00
|
|
|
}, []);
|
2021-06-18 12:09:59 +02:00
|
|
|
|
2021-05-13 18:23:12 +02:00
|
|
|
const deleteAppHandler = (app: App): void => {
|
2021-10-22 13:31:02 +02:00
|
|
|
const proceed = window.confirm(
|
|
|
|
`Are you sure you want to delete ${app.name} at ${app.url} ?`
|
|
|
|
);
|
2021-05-13 18:23:12 +02:00
|
|
|
|
|
|
|
if (proceed) {
|
2021-11-09 14:33:51 +01:00
|
|
|
deleteApp(app.id);
|
2021-05-13 18:23:12 +02:00
|
|
|
}
|
2021-10-22 13:31:02 +02:00
|
|
|
};
|
2021-05-13 18:23:12 +02:00
|
|
|
|
2021-06-18 12:09:59 +02:00
|
|
|
// Support keyboard navigation for actions
|
2021-10-22 13:31:02 +02:00
|
|
|
const keyboardActionHandler = (
|
|
|
|
e: KeyboardEvent,
|
|
|
|
app: App,
|
|
|
|
handler: Function
|
|
|
|
) => {
|
2021-05-22 18:10:12 +02:00
|
|
|
if (e.key === 'Enter') {
|
|
|
|
handler(app);
|
|
|
|
}
|
2021-10-22 13:31:02 +02:00
|
|
|
};
|
2021-05-22 18:10:12 +02:00
|
|
|
|
2021-06-15 16:02:57 +02:00
|
|
|
const dragEndHanlder = (result: DropResult): void => {
|
2021-06-18 12:09:59 +02:00
|
|
|
if (!isCustomOrder) {
|
2021-11-09 14:33:51 +01:00
|
|
|
createNotification({
|
2021-06-18 12:09:59 +02:00
|
|
|
title: 'Error',
|
2021-10-22 13:31:02 +02:00
|
|
|
message: 'Custom order is disabled',
|
|
|
|
});
|
2021-06-18 12:09:59 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-06-15 16:02:57 +02:00
|
|
|
|
|
|
|
if (!result.destination) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-18 12:09:59 +02:00
|
|
|
const tmpApps = [...localApps];
|
2021-06-15 16:02:57 +02:00
|
|
|
const [movedApp] = tmpApps.splice(result.source.index, 1);
|
|
|
|
tmpApps.splice(result.destination.index, 0, movedApp);
|
|
|
|
|
2021-06-18 12:09:59 +02:00
|
|
|
setLocalApps(tmpApps);
|
2021-11-09 14:33:51 +01:00
|
|
|
reorderApps(tmpApps);
|
2021-10-22 13:31:02 +02:00
|
|
|
};
|
2021-06-15 16:02:57 +02:00
|
|
|
|
2021-05-13 18:23:12 +02:00
|
|
|
return (
|
2021-06-18 12:09:59 +02:00
|
|
|
<Fragment>
|
|
|
|
<div className={classes.Message}>
|
2021-10-22 13:31:02 +02:00
|
|
|
{isCustomOrder ? (
|
|
|
|
<p>You can drag and drop single rows to reorder application</p>
|
|
|
|
) : (
|
|
|
|
<p>
|
|
|
|
Custom order is disabled. You can change it in{' '}
|
|
|
|
<Link to="/settings/other">settings</Link>
|
|
|
|
</p>
|
|
|
|
)}
|
2021-06-18 12:09:59 +02:00
|
|
|
</div>
|
|
|
|
<DragDropContext onDragEnd={dragEndHanlder}>
|
2021-10-22 13:31:02 +02:00
|
|
|
<Droppable droppableId="apps">
|
2021-06-18 12:09:59 +02:00
|
|
|
{(provided) => (
|
2021-10-22 13:31:02 +02:00
|
|
|
<Table
|
2021-11-08 23:40:30 +01:00
|
|
|
headers={['Name', 'URL', 'Icon', 'Visibility', 'Actions']}
|
2021-10-22 13:31:02 +02:00
|
|
|
innerRef={provided.innerRef}
|
|
|
|
>
|
2021-06-18 12:09:59 +02:00
|
|
|
{localApps.map((app: App, index): JSX.Element => {
|
|
|
|
return (
|
2021-10-22 13:31:02 +02:00
|
|
|
<Draggable
|
|
|
|
key={app.id}
|
|
|
|
draggableId={app.id.toString()}
|
|
|
|
index={index}
|
|
|
|
>
|
2021-06-18 12:09:59 +02:00
|
|
|
{(provided, snapshot) => {
|
|
|
|
const style = {
|
2021-10-22 13:31:02 +02:00
|
|
|
border: snapshot.isDragging
|
|
|
|
? '1px solid var(--color-accent)'
|
|
|
|
: 'none',
|
2021-06-18 12:09:59 +02:00
|
|
|
borderRadius: '4px',
|
|
|
|
...provided.draggableProps.style,
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<tr
|
|
|
|
{...provided.draggableProps}
|
|
|
|
{...provided.dragHandleProps}
|
|
|
|
ref={provided.innerRef}
|
|
|
|
style={style}
|
|
|
|
>
|
2021-10-22 13:31:02 +02:00
|
|
|
<td style={{ width: '200px' }}>{app.name}</td>
|
|
|
|
<td style={{ width: '200px' }}>{app.url}</td>
|
|
|
|
<td style={{ width: '200px' }}>{app.icon}</td>
|
2021-11-08 23:40:30 +01:00
|
|
|
<td style={{ width: '200px' }}>
|
|
|
|
{app.isPublic ? 'Visible' : 'Hidden'}
|
|
|
|
</td>
|
2021-06-18 12:09:59 +02:00
|
|
|
{!snapshot.isDragging && (
|
|
|
|
<td className={classes.TableActions}>
|
|
|
|
<div
|
|
|
|
className={classes.TableAction}
|
|
|
|
onClick={() => deleteAppHandler(app)}
|
2021-10-22 13:31:02 +02:00
|
|
|
onKeyDown={(e) =>
|
|
|
|
keyboardActionHandler(
|
|
|
|
e,
|
|
|
|
app,
|
|
|
|
deleteAppHandler
|
|
|
|
)
|
|
|
|
}
|
|
|
|
tabIndex={0}
|
|
|
|
>
|
|
|
|
<Icon icon="mdiDelete" />
|
2021-06-18 12:09:59 +02:00
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
className={classes.TableAction}
|
|
|
|
onClick={() => props.updateAppHandler(app)}
|
2021-10-22 13:31:02 +02:00
|
|
|
onKeyDown={(e) =>
|
|
|
|
keyboardActionHandler(
|
|
|
|
e,
|
|
|
|
app,
|
|
|
|
props.updateAppHandler
|
|
|
|
)
|
|
|
|
}
|
|
|
|
tabIndex={0}
|
|
|
|
>
|
|
|
|
<Icon icon="mdiPencil" />
|
2021-06-18 12:09:59 +02:00
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
className={classes.TableAction}
|
2021-11-09 14:33:51 +01:00
|
|
|
onClick={() => pinApp(app)}
|
2021-10-22 13:31:02 +02:00
|
|
|
onKeyDown={(e) =>
|
2021-11-09 14:33:51 +01:00
|
|
|
keyboardActionHandler(e, app, pinApp)
|
2021-06-18 12:09:59 +02:00
|
|
|
}
|
2021-10-22 13:31:02 +02:00
|
|
|
tabIndex={0}
|
|
|
|
>
|
|
|
|
{app.isPinned ? (
|
|
|
|
<Icon
|
|
|
|
icon="mdiPinOff"
|
|
|
|
color="var(--color-accent)"
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<Icon icon="mdiPin" />
|
|
|
|
)}
|
2021-06-18 12:09:59 +02:00
|
|
|
</div>
|
|
|
|
</td>
|
|
|
|
)}
|
|
|
|
</tr>
|
2021-10-22 13:31:02 +02:00
|
|
|
);
|
2021-06-18 12:09:59 +02:00
|
|
|
}}
|
|
|
|
</Draggable>
|
2021-10-22 13:31:02 +02:00
|
|
|
);
|
2021-06-18 12:09:59 +02:00
|
|
|
})}
|
|
|
|
</Table>
|
|
|
|
)}
|
|
|
|
</Droppable>
|
|
|
|
</DragDropContext>
|
|
|
|
</Fragment>
|
2021-10-22 13:31:02 +02:00
|
|
|
);
|
|
|
|
};
|