generated from mantinedev/next-pages-template
/
AppShelf.tsx
135 lines (129 loc) · 3.51 KB
/
AppShelf.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import React, { useEffect, useState } from 'react';
import { motion } from 'framer-motion';
import {
Text,
AspectRatio,
createStyles,
SimpleGrid,
Card,
useMantineTheme,
Image,
Group,
Space,
} from '@mantine/core';
import AppShelfMenu from './AppShelfMenu';
import AddItemShelfItem from './AddAppShelfItem';
import { useConfig } from '../../tools/state';
import { pingQbittorrent } from '../../tools/api';
import { serviceItem } from '../../tools/types';
const useStyles = createStyles((theme) => ({
main: {
backgroundColor: theme.colorScheme === 'dark' ? theme.colors.dark[5] : theme.colors.gray[1],
width: 200,
height: 180,
},
}));
const AppShelf = (props: any) => {
const { config, addService, removeService, setConfig } = useConfig();
/* A hook that is used to load the config from local storage. */
useEffect(() => {
const localConfig = localStorage.getItem('config');
if (localConfig) {
setConfig(JSON.parse(localConfig));
}
}, []);
if (config.services && config.services.length === 0) {
config.services.forEach((service) => {
if (service.type === 'qBittorrent') {
pingQbittorrent(service);
}
});
}
return (
<SimpleGrid m="xl" cols={4} spacing="xl">
{config.services.map((service, i) => (
<AppShelfItem service={service} />
))}
<AddItemShelfItem />
</SimpleGrid>
);
};
export function AppShelfItem(props: any) {
const { service }: { service: serviceItem } = props;
const theme = useMantineTheme();
const { removeService } = useConfig();
const { classes } = useStyles();
const [hovering, setHovering] = useState(false);
return (
<motion.div
key={service.name}
onHoverStart={(e) => {
setHovering(true);
}}
onHoverEnd={(e) => {
setHovering(false);
}}
>
<Card
className={classes.main}
style={{
boxShadow: hovering ? '0px 0px 3px rgba(0, 0, 0, 0.5)' : '0px 0px 1px rgba(0, 0, 0, 0.5)',
}}
radius="md"
>
<Card.Section>
<Group position="apart" mx="lg">
<Space />
<Text
// TODO: #1 Remove this hack to get the text to be centered.
ml={15}
style={{
alignSelf: 'center',
alignContent: 'center',
alignItems: 'center',
justifyContent: 'center',
justifyItems: 'center',
}}
mt="sm"
weight={500}
>
{service.name}
</Text>
<motion.div
style={{
alignSelf: 'flex-end',
}}
animate={{
opacity: hovering ? 1 : 0,
}}
>
<AppShelfMenu name={service.name} removeitem={removeService} />
</motion.div>
</Group>
</Card.Section>
<Card.Section>
<AspectRatio ratio={5 / 3} m="xl">
<motion.i
whileHover={{
cursor: 'pointer',
scale: 1.1,
}}
>
<Image
onClick={() => {
window.open(service.url);
}}
style={{
maxWidth: '50%',
marginBottom: 10,
}}
src={service.icon}
/>
</motion.i>
</AspectRatio>
</Card.Section>
</Card>
</motion.div>
);
}
export default AppShelf;