Sharkey/packages/frontend/src/components/form/suspense.vue

72 lines
1.4 KiB
Vue
Raw Normal View History

<!--
SPDX-FileCopyrightText: syuilo and other misskey contributors
SPDX-License-Identifier: AGPL-3.0-only
-->
2021-11-28 13:07:37 +02:00
<template>
2023-05-27 05:44:04 +03:00
<div v-if="pending">
<MkLoading/>
</div>
<div v-else-if="resolved">
<slot :result="result"></slot>
</div>
<div v-else>
<div :class="$style.error">
2023-09-30 22:53:52 +03:00
<div><i class="ph-warning ph-bold ph-lg"></i> {{ i18n.ts.somethingHappened }}</div>
<MkButton inline style="margin-top: 16px;" @click="retry"><i class="ph-arrow-clockwise ph-bold ph-lg"></i> {{ i18n.ts.retry }}</MkButton>
2021-11-28 13:07:37 +02:00
</div>
2023-05-27 05:44:04 +03:00
</div>
2021-11-28 13:07:37 +02:00
</template>
<script lang="ts" setup>
import { ref, watch } from 'vue';
import MkButton from '@/components/MkButton.vue';
2023-09-19 10:37:43 +03:00
import { defaultStore } from '@/store.js';
import { i18n } from '@/i18n.js';
2021-11-28 13:07:37 +02:00
const props = defineProps<{
p: () => Promise<any>;
}>();
2021-11-28 13:07:37 +02:00
const pending = ref(true);
const resolved = ref(false);
const rejected = ref(false);
const result = ref(null);
2021-11-28 13:07:37 +02:00
const process = () => {
if (props.p == null) {
return;
}
const promise = props.p();
pending.value = true;
resolved.value = false;
rejected.value = false;
promise.then((_result) => {
pending.value = false;
resolved.value = true;
result.value = _result;
});
promise.catch(() => {
pending.value = false;
rejected.value = true;
});
};
2021-11-28 13:07:37 +02:00
watch(() => props.p, () => {
process();
}, {
immediate: true,
2021-11-28 13:07:37 +02:00
});
const retry = () => {
process();
};
2021-11-28 13:07:37 +02:00
</script>
2023-05-27 05:44:04 +03:00
<style lang="scss" module>
.error {
2021-11-28 13:07:37 +02:00
padding: 16px;
text-align: center;
}
</style>