|
| 1 | +<template> |
| 2 | + <div class="h-full overflow-y-auto pb-md"> |
| 3 | + <header |
| 4 | + :class=" |
| 5 | + clsx( |
| 6 | + 'flex flex-row items-center gap-xs px-sm py-xs border-t border-b border-neutral-600', |
| 7 | + themeClasses('bg-neutral-200', 'bg-neutral-800'), |
| 8 | + ) |
| 9 | + " |
| 10 | + > |
| 11 | + <NewButton |
| 12 | + tooltip="Close (Esc)" |
| 13 | + tooltipPlacement="top" |
| 14 | + icon="x" |
| 15 | + tone="empty" |
| 16 | + :class=" |
| 17 | + clsx( |
| 18 | + 'active:bg-white active:text-black', |
| 19 | + themeClasses('hover:bg-neutral-200', 'hover:bg-neutral-600'), |
| 20 | + ) |
| 21 | + " |
| 22 | + @click="navigateBack" |
| 23 | + /> |
| 24 | + <template v-if="policy"> |
| 25 | + <div |
| 26 | + class="shrink ml-auto mr-auto flex flex-row gap-sm items-center justify-center min-w-[32vw]" |
| 27 | + > |
| 28 | + <Icon |
| 29 | + class="shrink" |
| 30 | + size="xs" |
| 31 | + :name="policy.result === 'Fail' ? 'triangle' : 'check-square'" |
| 32 | + :tone="policy.result === 'Fail' ? 'destructive' : 'success'" |
| 33 | + /> |
| 34 | + <span class="grow" |
| 35 | + ><TruncateWithTooltip class="py-2xs">{{ |
| 36 | + policy.name |
| 37 | + }}</TruncateWithTooltip></span |
| 38 | + > |
| 39 | + <span class="shrink"> |
| 40 | + <Timestamp |
| 41 | + refresh |
| 42 | + size="normal" |
| 43 | + relative="standard" |
| 44 | + showTimeIfToday |
| 45 | + :date="policy.createdAt" |
| 46 | + /> |
| 47 | + </span> |
| 48 | + </div> |
| 49 | + </template> |
| 50 | + </header> |
| 51 | + <div class="w-[70vw] ml-auto mr-auto flex flex-row"> |
| 52 | + <div |
| 53 | + :class=" |
| 54 | + clsx( |
| 55 | + 'w-[25vw] pt-md pr-md border-r-[1px]', |
| 56 | + themeClasses('border-neutral-200', 'border-neutral-600'), |
| 57 | + ) |
| 58 | + " |
| 59 | + > |
| 60 | + <h5 class="mb-sm text-sm ml-xs">Policy history</h5> |
| 61 | + <PolicyList |
| 62 | + :policies="policyReports" |
| 63 | + :page="page" |
| 64 | + :maxPages="maxPages" |
| 65 | + @pageBack="pageBack" |
| 66 | + @pageForward="pageForward" |
| 67 | + @select="(p) => navigateToPolicy(p)" |
| 68 | + /> |
| 69 | + </div> |
| 70 | + <div v-if="!policy" class="w-full"> |
| 71 | + <EmptyStateCard |
| 72 | + iconName="no-changes" |
| 73 | + primaryText="Could not find this Policy Report" |
| 74 | + secondaryText="Please select another from the list of policy reports." |
| 75 | + /> |
| 76 | + </div> |
| 77 | + <section v-else class="w-full p-md"> |
| 78 | + <div class="mb-lg"> |
| 79 | + <MarkdownRender :source="policy.policy" /> |
| 80 | + </div> |
| 81 | + <div |
| 82 | + :class=" |
| 83 | + clsx( |
| 84 | + 'border-t-[1px] pt-lg', |
| 85 | + themeClasses('border-neutral-200', 'border-neutral-600'), |
| 86 | + ) |
| 87 | + " |
| 88 | + > |
| 89 | + <MarkdownRender :source="policy.report" /> |
| 90 | + </div> |
| 91 | + </section> |
| 92 | + </div> |
| 93 | + </div> |
| 94 | +</template> |
| 95 | + |
| 96 | +<script setup lang="ts"> |
| 97 | +import { clsx } from "clsx"; |
| 98 | +import { useRoute, useRouter } from "vue-router"; |
| 99 | +import { computed, watch } from "vue"; |
| 100 | +import { |
| 101 | + themeClasses, |
| 102 | + NewButton, |
| 103 | + Icon, |
| 104 | + TruncateWithTooltip, |
| 105 | + Timestamp, |
| 106 | +} from "@si/vue-lib/design-system"; |
| 107 | +import { useQuery } from "@tanstack/vue-query"; |
| 108 | +import EmptyStateCard from "@/components/EmptyStateCard.vue"; |
| 109 | +import { Policy, usePolicy } from "./logic_composables/policy"; |
| 110 | +import PolicyList from "./layout_components/PolicyList.vue"; |
| 111 | +import MarkdownRender from "./MarkdownRender.vue"; |
| 112 | +import { routes, useApi } from "./api_composables"; |
| 113 | +import { useContext } from "./logic_composables/context"; |
| 114 | +import { prevPage } from "./logic_composables/navigation_stack"; |
| 115 | +
|
| 116 | +const router = useRouter(); |
| 117 | +const route = useRoute(); |
| 118 | +
|
| 119 | +// Navigate back to explore_grid view |
| 120 | +const navigateBack = () => { |
| 121 | + const lastPage = prevPage(); |
| 122 | + // if we aren't coming from new-hotness, go to where we came from |
| 123 | + // usually component details |
| 124 | + if (lastPage && lastPage.name !== "new-hotness") { |
| 125 | + router.push({ |
| 126 | + name: lastPage.name, |
| 127 | + params: lastPage.params, |
| 128 | + }); |
| 129 | + } else { |
| 130 | + router.push({ |
| 131 | + name: "new-hotness", |
| 132 | + params: { |
| 133 | + workspacePk: route.params.workspacePk, |
| 134 | + changeSetId: route.params.changeSetId, |
| 135 | + }, |
| 136 | + query: { retainSessionState: 1 }, |
| 137 | + }); |
| 138 | + } |
| 139 | +}; |
| 140 | +
|
| 141 | +const props = defineProps<{ |
| 142 | + policyId: string; |
| 143 | +}>(); |
| 144 | +
|
| 145 | +const { policyReports, page, maxPages } = usePolicy(); |
| 146 | +
|
| 147 | +watch( |
| 148 | + route.query, |
| 149 | + () => { |
| 150 | + if (!route.query.page) return; |
| 151 | +
|
| 152 | + const urlPage = parseInt(route.query.page?.toString() || "1"); |
| 153 | + if (urlPage && urlPage !== page.value) page.value = urlPage; |
| 154 | + }, |
| 155 | + { immediate: true }, |
| 156 | +); |
| 157 | +
|
| 158 | +watch( |
| 159 | + page, |
| 160 | + () => { |
| 161 | + const urlPage = parseInt(route.query.page?.toString() || "1"); |
| 162 | + if (urlPage !== page.value) { |
| 163 | + router.push({ |
| 164 | + ...route.params, |
| 165 | + query: { page: page.value }, |
| 166 | + }); |
| 167 | + } |
| 168 | + }, |
| 169 | + { immediate: true }, |
| 170 | +); |
| 171 | +
|
| 172 | +const pageBack = () => { |
| 173 | + if (page.value === 1) page.value = maxPages.value; |
| 174 | + else page.value -= 1; |
| 175 | +}; |
| 176 | +const pageForward = () => { |
| 177 | + if (page.value === maxPages.value) page.value = 1; |
| 178 | + else page.value += 1; |
| 179 | +}; |
| 180 | +
|
| 181 | +const navigateToPolicy = (policy: Policy) => { |
| 182 | + router.push({ |
| 183 | + name: "new-hotness-policy", |
| 184 | + params: { |
| 185 | + workspacePk: route.params.workspacePk, |
| 186 | + changeSetId: route.params.changeSetId, |
| 187 | + policyId: policy.id, |
| 188 | + }, |
| 189 | + query: { |
| 190 | + page: page.value, |
| 191 | + }, |
| 192 | + }); |
| 193 | +}; |
| 194 | +
|
| 195 | +const ctx = useContext(); |
| 196 | +const api = useApi(ctx); |
| 197 | +const queryKey = computed(() => ["policies", props.policyId]); |
| 198 | +const policyQuery = useQuery<Policy | null>({ |
| 199 | + enabled: true, |
| 200 | + queryKey, |
| 201 | + staleTime: 5000, |
| 202 | + queryFn: async () => { |
| 203 | + const call = api.endpoint<{ report: Policy | null }>(routes.PolicyReport, { |
| 204 | + policyId: props.policyId, |
| 205 | + }); |
| 206 | + const response = await call.get(); |
| 207 | + if (api.ok(response)) { |
| 208 | + return response.data.report; |
| 209 | + } |
| 210 | + return null; |
| 211 | + }, |
| 212 | +}); |
| 213 | +
|
| 214 | +const policy = computed<Policy | null>(() => { |
| 215 | + return policyQuery.data.value || null; |
| 216 | +}); |
| 217 | +</script> |
0 commit comments