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

67 lines
1.3 KiB
Vue
Raw Normal View History

2021-11-28 20:07:37 +09:00
<template>
2023-05-27 11:44:04 +09:00
<div v-if="pending">
<MkLoading/>
</div>
<div v-else-if="resolved">
<slot :result="result"></slot>
</div>
<div v-else>
<div :class="$style.error">
<div><i class="ti ti-alert-triangle"></i> {{ i18n.ts.somethingHappened }}</div>
<MkButton inline style="margin-top: 16px;" @click="retry"><i class="ti ti-reload"></i> {{ i18n.ts.retry }}</MkButton>
2021-11-28 20:07:37 +09:00
</div>
2023-05-27 11:44:04 +09:00
</div>
2021-11-28 20:07:37 +09:00
</template>
<script lang="ts" setup>
import { ref, watch } from 'vue';
import MkButton from '@/components/MkButton.vue';
2023-04-01 13:42:40 +09:00
import { defaultStore } from '@/store';
2023-04-01 14:01:57 +09:00
import { i18n } from '@/i18n';
2021-11-28 20:07:37 +09:00
const props = defineProps<{
p: () => Promise<any>;
}>();
2021-11-28 20:07:37 +09:00
const pending = ref(true);
const resolved = ref(false);
const rejected = ref(false);
const result = ref(null);
2021-11-28 20:07:37 +09: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 20:07:37 +09:00
watch(() => props.p, () => {
process();
}, {
immediate: true,
2021-11-28 20:07:37 +09:00
});
const retry = () => {
process();
};
2021-11-28 20:07:37 +09:00
</script>
2023-05-27 11:44:04 +09:00
<style lang="scss" module>
.error {
2021-11-28 20:07:37 +09:00
padding: 16px;
text-align: center;
}
</style>