Skip to content
Snippets Groups Projects
WidgetRss.vue 3.25 KiB
Newer Older
syuilo's avatar
syuilo committed
<template>
<MkContainer :show-header="widgetProps.showHeader" class="mkw-rss">
syuilo's avatar
syuilo committed
	<template #header><i class="ti ti-rss"></i>RSS</template>
	<template #func><button class="_button" @click="configure"><i class="ti ti-settings"></i></button></template>
syuilo's avatar
syuilo committed

syuilo's avatar
syuilo committed
	<div class="ekmkgxbj">
syuilo's avatar
syuilo committed
		<MkLoading v-if="fetching"/>
		<div class="_fullinfo" v-else-if="(!items || items.length === 0) && widgetProps.showHeader">
			<img src="https://xn--931a.moe/assets/info.jpg" class="_ghost"/>
			<div>{{ i18n.ts.nothing }}</div>
		</div>
		<div v-else :class="$style.feed">
			<a v-for="item in items" :class="$style.item" :href="item.link" :key="item.link" rel="nofollow noopener" target="_blank" :title="item.title">{{ item.title }}</a>
syuilo's avatar
syuilo committed
		</div>
syuilo's avatar
syuilo committed
	</div>
syuilo's avatar
syuilo committed
</MkContainer>
syuilo's avatar
syuilo committed
</template>

<script lang="ts" setup>
import { useWidgetPropsManager, Widget, WidgetComponentEmits, WidgetComponentExpose, WidgetComponentProps } from './widget';
import { GetFormResultType } from '@/scripts/form';
import MkContainer from '@/components/MkContainer.vue';
import { url as base } from '@/config';
import { i18n } from '@/i18n';
import { useInterval } from '@/scripts/use-interval';
syuilo's avatar
syuilo committed

const name = 'rss';
syuilo's avatar
syuilo committed

const widgetPropsDef = {
	url: {
		type: 'string' as const,
		default: 'http://feeds.afpbb.com/rss/afpbb/afpbbnews',
syuilo's avatar
syuilo committed
	},
	refreshIntervalSec: {
		type: 'number' as const,
		default: 60,
	},
	maxEntries: {
		type: 'number' as const,
		default: 15,
	},
	showHeader: {
		type: 'boolean' as const,
		default: true,
	},
};

type WidgetProps = GetFormResultType<typeof widgetPropsDef>;

// 現時点ではvueの制限によりimportしたtypeをジェネリックに渡せない
//const props = defineProps<WidgetComponentProps<WidgetProps>>();
//const emit = defineEmits<WidgetComponentEmits<WidgetProps>>();
const props = defineProps<{ widget?: Widget<WidgetProps>; }>();
const emit = defineEmits<{ (ev: 'updateProps', props: WidgetProps); }>();

const { widgetProps, configure } = useWidgetPropsManager(name,
	widgetPropsDef,
	props,
	emit,
);

const rawItems = ref([]);
const items = computed(() => rawItems.value.slice(0, widgetProps.maxEntries));
const fetching = ref(true);
const fetchEndpoint = computed(() => {
	const url = new URL('/api/fetch-rss', base);
	url.searchParams.set('url', widgetProps.url);
	return url;
});
let intervalClear = $ref<(() => void) | undefined>();

const tick = () => {
	if (document.visibilityState === 'hidden' && rawItems.value.length !== 0) return;

	window.fetch(fetchEndpoint.value, {})
	.then(res => res.json())
	.then(feed => {
		rawItems.value = feed.items ?? [];
		fetching.value = false;
watch(() => fetchEndpoint, tick);
watch(() => widgetProps.refreshIntervalSec, () => {
	if (intervalClear) {
		intervalClear();
	}
	intervalClear = useInterval(tick, Math.max(10000, widgetProps.refreshIntervalSec * 1000), {
		immediate: true,
		afterMounted: true,
	});
}, { immediate: true });

defineExpose<WidgetComponentExpose>({
	name,
	configure,
	id: props.widget ? props.widget.id : null,
syuilo's avatar
syuilo committed
});
</script>

syuilo's avatar
syuilo committed

.item {
	display: block;
	padding: 8px 16px;
	color: var(--fg);
	white-space: nowrap;
	text-overflow: ellipsis;
	overflow: hidden;
syuilo's avatar
syuilo committed

syuilo's avatar
syuilo committed
	}
}
</style>