1
0
Fork 0
mirror of https://github.com/pawelmalak/flame.git synced 2025-07-19 19:49:37 +02:00
flame/client/src/components/Apps/AppTable/AppTable.tsx

225 lines
6.7 KiB
TypeScript
Raw Normal View History

import { Fragment, KeyboardEvent, useState, useEffect } from 'react';
2021-10-22 13:31:02 +02:00
import {
DragDropContext,
Droppable,
Draggable,
DropResult,
} from 'react-beautiful-dnd';
import { Link } from 'react-router-dom';
// Redux
import { connect } from 'react-redux';
2021-10-22 13:31:02 +02:00
import {
pinApp,
deleteApp,
reorderApps,
updateConfig,
createNotification,
} from '../../../store/actions';
// Typescript
2021-10-22 13:31:02 +02:00
import { App, Config, GlobalState, NewNotification } from '../../../interfaces';
// CSS
import classes from './AppTable.module.css';
// UI
import Icon from '../../UI/Icons/Icon/Icon';
2021-05-25 11:44:11 +02:00
import Table from '../../UI/Table/Table';
interface ComponentProps {
apps: App[];
2021-10-22 13:31:02 +02:00
config: Config;
pinApp: (app: App) => void;
deleteApp: (id: number) => void;
2021-05-22 17:03:32 +02:00
updateAppHandler: (app: App) => void;
reorderApps: (apps: App[]) => void;
updateConfig: (formData: any) => void;
createNotification: (notification: NewNotification) => void;
}
const AppTable = (props: ComponentProps): JSX.Element => {
const [localApps, setLocalApps] = useState<App[]>([]);
const [isCustomOrder, setIsCustomOrder] = useState<boolean>(false);
// Copy apps array
useEffect(() => {
setLocalApps([...props.apps]);
2021-10-22 13:31:02 +02:00
}, [props.apps]);
// Check ordering
useEffect(() => {
2021-10-22 13:31:02 +02:00
const order = props.config.useOrdering;
if (order === 'orderId') {
setIsCustomOrder(true);
}
2021-10-22 13:31:02 +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} ?`
);
if (proceed) {
props.deleteApp(app.id);
}
2021-10-22 13:31:02 +02:00
};
// Support keyboard navigation for actions
2021-10-22 13:31:02 +02:00
const keyboardActionHandler = (
e: KeyboardEvent,
app: App,
handler: Function
) => {
if (e.key === 'Enter') {
handler(app);
}
2021-10-22 13:31:02 +02:00
};
const dragEndHanlder = (result: DropResult): void => {
if (!isCustomOrder) {
props.createNotification({
title: 'Error',
2021-10-22 13:31:02 +02:00
message: 'Custom order is disabled',
});
return;
}
if (!result.destination) {
return;
}
const tmpApps = [...localApps];
const [movedApp] = tmpApps.splice(result.source.index, 1);
tmpApps.splice(result.destination.index, 0, movedApp);
setLocalApps(tmpApps);
props.reorderApps(tmpApps);
2021-10-22 13:31:02 +02:00
};
return (
<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>
)}
</div>
<DragDropContext onDragEnd={dragEndHanlder}>
2021-10-22 13:31:02 +02:00
<Droppable droppableId="apps">
{(provided) => (
2021-10-22 13:31:02 +02:00
<Table
headers={['Name', 'URL', 'Icon', 'Actions']}
innerRef={provided.innerRef}
>
{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}
>
{(provided, snapshot) => {
const style = {
2021-10-22 13:31:02 +02:00
border: snapshot.isDragging
? '1px solid var(--color-accent)'
: 'none',
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>
{!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" />
</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" />
</div>
<div
className={classes.TableAction}
onClick={() => props.pinApp(app)}
2021-10-22 13:31:02 +02:00
onKeyDown={(e) =>
keyboardActionHandler(e, app, props.pinApp)
}
2021-10-22 13:31:02 +02:00
tabIndex={0}
>
{app.isPinned ? (
<Icon
icon="mdiPinOff"
color="var(--color-accent)"
/>
) : (
<Icon icon="mdiPin" />
)}
</div>
</td>
)}
</tr>
2021-10-22 13:31:02 +02:00
);
}}
</Draggable>
2021-10-22 13:31:02 +02:00
);
})}
</Table>
)}
</Droppable>
</DragDropContext>
</Fragment>
2021-10-22 13:31:02 +02:00
);
};
const mapStateToProps = (state: GlobalState) => {
return {
2021-10-22 13:31:02 +02:00
apps: state.app.apps,
config: state.config.config,
};
};
const actions = {
pinApp,
deleteApp,
reorderApps,
updateConfig,
2021-10-22 13:31:02 +02:00
createNotification,
};
2021-10-22 13:31:02 +02:00
export default connect(mapStateToProps, actions)(AppTable);