All files / apps/developer-portal/layers/github/app/composables useIssues.ts

100% Statements 23/23
100% Branches 4/4
100% Functions 6/6
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                  32x   32x   32x 15x 15x   15x   7x 7x       6x   1x       32x     14x 14x   15x 28x       14x 14x   15x   14x    
import {
    issuesApiUrl,
    toIssues,
    type GithubIssue,
    type IssuesRead,
} from "#layers/github/app/utils/issues.ts";
import { messageOf } from "#layers/github/app/utils/github.ts";
import type { Translate } from "~/types/Translate";
 
const NO_ISSUES: IssuesRead = { fetchedAt: "", error: null, issues: [] };
 
const PER_PAGE = 100;
 
const read = async (repoUrl: string, t: Translate): Promise<IssuesRead> => {
    const fetchedAt = new Date().toISOString();
    const url = issuesApiUrl(repoUrl);
 
    if (url === null) return { fetchedAt, error: t("issuesRead.noRepo"), issues: [] };
 
    try {
        const payload = await $fetch<GithubIssue[]>(url, {
            query: { state: "open", per_page: PER_PAGE },
        });
 
        return { fetchedAt, error: null, issues: toIssues(payload) };
    } catch (cause) {
        return { fetchedAt, error: messageOf(cause, t("issuesRead.failed")), issues: [] };
    }
};
 
export const useIssues = () => {
    const {
        public: { repoUrl },
    } = useRuntimeConfig();
    const { t } = useI18n();
 
    const state = useAsyncData<IssuesRead>("issues", () => read(repoUrl, t), {
        default: () => NO_ISSUES,
        server: false,
    });
 
    const mounted = ref(false);
    onMounted(() => (mounted.value = true));
    // The server never reads GitHub, so every client render before hydration ends must match it
    const shown = computed(() => (mounted.value ? state.data.value : NO_ISSUES));
 
    return Object.assign(state, { reload: state.refresh, shown });
};