2023-04-05 14:16:28 +09:00
|
|
|
import Link from 'next/link';
|
2023-02-15 22:00:06 +01:00
|
|
|
import {
|
|
|
|
|
ActionIcon,
|
|
|
|
|
Badge,
|
|
|
|
|
Card,
|
|
|
|
|
Center,
|
|
|
|
|
Flex,
|
|
|
|
|
Group,
|
|
|
|
|
Image,
|
|
|
|
|
Loader,
|
|
|
|
|
MediaQuery,
|
|
|
|
|
ScrollArea,
|
|
|
|
|
Stack,
|
|
|
|
|
Text,
|
|
|
|
|
Title,
|
2023-04-05 14:16:28 +09:00
|
|
|
createStyles,
|
2023-02-15 22:00:06 +01:00
|
|
|
} from '@mantine/core';
|
2023-05-15 17:40:59 +09:00
|
|
|
import { IconClock, IconRefresh, IconRss } from '@tabler/icons-react';
|
2023-03-23 01:28:17 +08:00
|
|
|
import { useQuery } from '@tanstack/react-query';
|
|
|
|
|
import dayjs from 'dayjs';
|
2023-02-15 22:00:06 +01:00
|
|
|
import { useTranslation } from 'next-i18next';
|
2023-04-05 14:16:28 +09:00
|
|
|
|
2023-04-03 15:45:47 +09:00
|
|
|
import { defineWidget } from '../helper';
|
2023-04-05 14:16:28 +09:00
|
|
|
import { IWidget } from '../widgets';
|
2023-02-15 22:00:06 +01:00
|
|
|
|
|
|
|
|
const definition = defineWidget({
|
|
|
|
|
id: 'rss',
|
|
|
|
|
icon: IconRss,
|
|
|
|
|
options: {
|
|
|
|
|
rssFeedUrl: {
|
2023-04-05 14:16:28 +09:00
|
|
|
type: 'multiple-text',
|
2023-04-16 17:51:07 +09:00
|
|
|
defaultValue: [],
|
2023-02-15 22:00:06 +01:00
|
|
|
},
|
2023-04-05 14:53:18 +09:00
|
|
|
refreshInterval: {
|
|
|
|
|
type: 'slider',
|
2023-04-10 23:29:00 +09:00
|
|
|
defaultValue: 30,
|
|
|
|
|
min: 15,
|
2023-04-05 14:53:18 +09:00
|
|
|
max: 300,
|
2023-04-10 23:29:00 +09:00
|
|
|
step: 15,
|
2023-04-05 14:53:18 +09:00
|
|
|
},
|
2023-02-15 22:00:06 +01:00
|
|
|
},
|
|
|
|
|
gridstack: {
|
|
|
|
|
minWidth: 2,
|
|
|
|
|
minHeight: 2,
|
|
|
|
|
maxWidth: 12,
|
|
|
|
|
maxHeight: 12,
|
|
|
|
|
},
|
|
|
|
|
component: RssTile,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export type IRssWidget = IWidget<(typeof definition)['id'], typeof definition>;
|
|
|
|
|
|
|
|
|
|
interface RssTileProps {
|
|
|
|
|
widget: IRssWidget;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 14:53:18 +09:00
|
|
|
export const useGetRssFeeds = (feedUrls: string[], refreshInterval: number, widgetId: string) =>
|
2023-03-23 01:28:17 +08:00
|
|
|
useQuery({
|
2023-04-05 14:16:28 +09:00
|
|
|
queryKey: ['rss-feeds', feedUrls],
|
2023-04-05 14:42:22 +09:00
|
|
|
// Cache the results for 24 hours
|
|
|
|
|
cacheTime: 1000 * 60 * 60 * 24,
|
2023-04-10 23:29:00 +09:00
|
|
|
staleTime: 1000 * 60 * refreshInterval,
|
2023-03-23 01:28:17 +08:00
|
|
|
queryFn: async () => {
|
2023-04-05 14:16:28 +09:00
|
|
|
const responses = await Promise.all(
|
|
|
|
|
feedUrls.map((feedUrl) =>
|
|
|
|
|
fetch(
|
|
|
|
|
`/api/modules/rss?widgetId=${widgetId}&feedUrl=${encodeURIComponent(feedUrl)}`
|
|
|
|
|
).then((response) => response.json())
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
return responses;
|
2023-03-23 01:28:17 +08:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2023-02-15 22:00:06 +01:00
|
|
|
function RssTile({ widget }: RssTileProps) {
|
|
|
|
|
const { t } = useTranslation('modules/rss');
|
2023-04-05 14:16:28 +09:00
|
|
|
const { data, isLoading, isFetching, isError, refetch } = useGetRssFeeds(
|
2023-03-30 23:15:08 +02:00
|
|
|
widget.properties.rssFeedUrl,
|
2023-04-05 14:53:18 +09:00
|
|
|
widget.properties.refreshInterval,
|
2023-03-30 23:15:08 +02:00
|
|
|
widget.id
|
2023-02-15 22:00:06 +01:00
|
|
|
);
|
|
|
|
|
const { classes } = useStyles();
|
|
|
|
|
|
2023-03-23 01:28:17 +08:00
|
|
|
function formatDate(input: string): string {
|
|
|
|
|
// Parse the input date as a local date
|
2023-04-05 14:16:28 +09:00
|
|
|
try {
|
|
|
|
|
const inputDate = dayjs(new Date(input));
|
|
|
|
|
const now = dayjs(); // Current date and time
|
|
|
|
|
const difference = now.diff(inputDate, 'ms');
|
|
|
|
|
const duration = dayjs.duration(difference, 'ms');
|
|
|
|
|
const humanizedDuration = duration.humanize();
|
|
|
|
|
return `${humanizedDuration} ago`;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return 'Error';
|
|
|
|
|
}
|
2023-03-23 01:28:17 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-15 22:00:06 +01:00
|
|
|
if (!data || isLoading) {
|
|
|
|
|
return (
|
2023-03-23 01:28:17 +08:00
|
|
|
<Center h="100%">
|
2023-02-15 22:00:06 +01:00
|
|
|
<Loader />
|
|
|
|
|
</Center>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-16 17:51:07 +09:00
|
|
|
if (data.length < 1 || !data[0].feed || isError) {
|
2023-02-15 22:00:06 +01:00
|
|
|
return (
|
|
|
|
|
<Center h="100%">
|
|
|
|
|
<Stack align="center">
|
|
|
|
|
<IconRss size={40} strokeWidth={1} />
|
2023-04-16 17:51:07 +09:00
|
|
|
<Title order={6}>{t('descriptor.card.errors.general.title')}</Title>
|
|
|
|
|
<Text align="center">{t('descriptor.card.errors.general.text')}</Text>
|
2023-02-15 22:00:06 +01:00
|
|
|
</Stack>
|
|
|
|
|
</Center>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2023-02-28 20:35:57 +01:00
|
|
|
<Stack h="100%">
|
2023-04-05 14:16:28 +09:00
|
|
|
<ScrollArea className="scroll-area-w100" w="100%" mt="sm" mb="sm">
|
2023-04-05 14:38:38 +09:00
|
|
|
{data.map((feed, index) => (
|
2023-04-05 14:16:28 +09:00
|
|
|
<Stack w="100%" spacing="xs">
|
2023-04-23 22:09:29 +02:00
|
|
|
{feed.feed &&
|
|
|
|
|
feed.feed.items.map((item: any, index: number) => (
|
|
|
|
|
<Card
|
|
|
|
|
key={index}
|
|
|
|
|
withBorder
|
|
|
|
|
component={Link ?? 'div'}
|
|
|
|
|
href={item.link}
|
|
|
|
|
radius="md"
|
|
|
|
|
target="_blank"
|
|
|
|
|
w="100%"
|
|
|
|
|
>
|
2023-04-05 14:16:28 +09:00
|
|
|
{item.enclosure && (
|
2023-04-23 22:09:29 +02:00
|
|
|
// eslint-disable-next-line @next/next/no-img-element
|
|
|
|
|
<img
|
|
|
|
|
className={classes.backgroundImage}
|
|
|
|
|
src={item.enclosure.url ?? undefined}
|
|
|
|
|
alt="backdrop"
|
|
|
|
|
/>
|
2023-04-05 14:16:28 +09:00
|
|
|
)}
|
2023-04-23 22:09:29 +02:00
|
|
|
|
|
|
|
|
<Flex gap="xs">
|
|
|
|
|
{item.enclosure && (
|
|
|
|
|
<MediaQuery query="(max-width: 1200px)" styles={{ display: 'none' }}>
|
|
|
|
|
<Image
|
|
|
|
|
src={item.enclosure?.url ?? undefined}
|
|
|
|
|
width={140}
|
|
|
|
|
height={140}
|
|
|
|
|
radius="md"
|
|
|
|
|
withPlaceholder
|
|
|
|
|
/>
|
|
|
|
|
</MediaQuery>
|
2023-04-05 14:16:28 +09:00
|
|
|
)}
|
2023-04-23 22:09:29 +02:00
|
|
|
<Flex gap={2} direction="column" w="100%">
|
|
|
|
|
{item.categories && (
|
|
|
|
|
<Flex gap="xs" wrap="wrap" h={20} style={{ overflow: 'hidden' }}>
|
|
|
|
|
{item.categories.map((category: any, categoryIndex: number) => (
|
|
|
|
|
<Badge key={categoryIndex}>{category}</Badge>
|
|
|
|
|
))}
|
|
|
|
|
</Flex>
|
|
|
|
|
)}
|
2023-04-05 14:16:28 +09:00
|
|
|
|
2023-04-23 22:09:29 +02:00
|
|
|
<Text lineClamp={2}>{item.title}</Text>
|
|
|
|
|
<Text color="dimmed" size="xs" lineClamp={3}>
|
|
|
|
|
{item.content}
|
|
|
|
|
</Text>
|
2023-04-05 14:16:28 +09:00
|
|
|
|
2023-04-23 22:09:29 +02:00
|
|
|
{item.pubDate && (
|
|
|
|
|
<InfoDisplay title={feed.feed.title} date={formatDate(item.pubDate)} />
|
|
|
|
|
)}
|
|
|
|
|
</Flex>
|
2023-04-05 14:16:28 +09:00
|
|
|
</Flex>
|
2023-04-23 22:09:29 +02:00
|
|
|
</Card>
|
|
|
|
|
))}
|
2023-04-05 14:16:28 +09:00
|
|
|
</Stack>
|
|
|
|
|
))}
|
2023-02-15 22:00:06 +01:00
|
|
|
</ScrollArea>
|
|
|
|
|
|
2023-04-05 14:16:28 +09:00
|
|
|
<ActionIcon
|
|
|
|
|
size="sm"
|
|
|
|
|
radius="xl"
|
|
|
|
|
pos="absolute"
|
|
|
|
|
right={10}
|
|
|
|
|
onClick={() => refetch()}
|
|
|
|
|
bottom={10}
|
|
|
|
|
styles={{
|
|
|
|
|
root: {
|
|
|
|
|
borderColor: 'red',
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
>
|
2023-04-05 14:46:03 +09:00
|
|
|
{isFetching ? <Loader /> : <IconRefresh />}
|
2023-04-05 14:16:28 +09:00
|
|
|
</ActionIcon>
|
2023-02-15 22:00:06 +01:00
|
|
|
</Stack>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 14:38:38 +09:00
|
|
|
const InfoDisplay = ({ date, title }: { date: string; title: string | undefined }) => (
|
2023-02-15 22:00:06 +01:00
|
|
|
<Group mt="auto" spacing="xs">
|
|
|
|
|
<IconClock size={14} />
|
|
|
|
|
<Text size="xs" color="dimmed">
|
|
|
|
|
{date}
|
|
|
|
|
</Text>
|
2023-04-05 14:38:38 +09:00
|
|
|
{title && (
|
|
|
|
|
<Badge variant="outline" size="xs">
|
|
|
|
|
{title}
|
|
|
|
|
</Badge>
|
|
|
|
|
)}
|
2023-02-15 22:00:06 +01:00
|
|
|
</Group>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const useStyles = createStyles(({ colorScheme }) => ({
|
|
|
|
|
backgroundImage: {
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
width: '100%',
|
|
|
|
|
height: '100%',
|
|
|
|
|
filter: colorScheme === 'dark' ? 'blur(30px)' : 'blur(15px)',
|
|
|
|
|
transform: 'scaleX(-1)',
|
|
|
|
|
opacity: colorScheme === 'dark' ? 0.3 : 0.2,
|
|
|
|
|
transition: 'ease-in-out 0.2s',
|
|
|
|
|
|
|
|
|
|
'&:hover': {
|
|
|
|
|
opacity: colorScheme === 'dark' ? 0.4 : 0.3,
|
|
|
|
|
filter: 'blur(40px) brightness(0.7)',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
export default definition;
|