All files / src/components ForkEntityModal.vue

94.91% Statements 56/59
91.22% Branches 52/57
100% Functions 11/11
100% Lines 50/50

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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242    177x               3x 3x 3x 3x                       3x           3x                               3x           2x             3x     6x                                       3x                                                                       3x                                                     82x 82x                           82x   82x         82x 82x   82x           82x 82x           32x     32x 32x 32x 32x 23x   8x 8x   31x       82x 115x   33x 25x 25x 25x 25x         82x 6x       4x 4x           3x 2x     82x 82x 1x 1x   82x 11x              
<template>
  <div
    v-if="isOpen"
    class="modal fade show d-block"
    role="dialog"
    aria-modal="true"
    tabindex="-1"
    style="background-color: rgba(0, 0, 0, 0.5)"
    @click.self="cancel"
  >
    <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content">
        <div class="modal-header bg-primary text-white">
          <h5 class="modal-title">
            <font-awesome-icon icon="fa-solid fa-code-branch" class="me-2" />
            {{ t('entity.forkModalTitle') }}
          </h5>
          <button
            type="button"
            class="btn-close btn-close-white"
            :aria-label="t('common.close')"
            :disabled="loading"
            @click="cancel"
          ></button>
        </div>
        <div class="modal-body">
          <p>{{ t('entity.forkModalMessage').replace('{name}', entityName) }}</p>
          <div v-if="error" class="alert alert-danger" role="alert">
            {{ error }}
          </div>
          <div v-if="isJobInProgress" class="mb-3">
            <div class="d-flex justify-content-between small text-muted mb-1">
              <span>{{ jobProgressLabel }}</span>
              <span v-if="progress">{{ progressPercent }}%</span>
            </div>
            <div class="progress" style="height: 0.5rem">
              <div
                class="progress-bar"
                :class="{ 'progress-bar-striped progress-bar-animated': !progress }"
                role="progressbar"
                :style="{ width: `${progress ? progressPercent : 100}%` }"
                :aria-valuenow="progress?.done ?? 0"
                aria-valuemin="0"
                :aria-valuemax="progress?.total ?? 1"
              ></div>
            </div>
          </div>
          <div class="mb-3">
            <label class="form-label" for="fork-name-input">
              {{ t('entity.forkModalNameLabel') }}
            </label>
            <input
              id="fork-name-input"
              v-model="name"
              class="form-control"
              type="text"
              required
              :disabled="loading"
            />
          </div>
          <div class="form-check mb-2">
            <input
              id="fork-copy-items"
              v-model="copyItems"
              class="form-check-input"
              type="checkbox"
              :disabled="loading"
            />
            <label class="form-check-label" for="fork-copy-items">
              {{ t('entity.forkModalCopyItemsLabel') }}
            </label>
          </div>
          <div
            v-if="!previewLoading && preview?.insufficientCredits"
            class="alert alert-warning py-2 mb-2"
            role="alert"
          >
            {{
              t('entity.forkModalInsufficientCredits')
                .replace('{required}', String(preview.estimatedCredits))
                .replace('{balance}', String(preview.balance))
            }}
          </div>
          <div class="text-muted small">
            <div v-if="previewLoading">
              <font-awesome-icon icon="fa-solid fa-spinner" spin class="me-1" />
              {{ t('common.loading') }}
            </div>
            <template v-else-if="preview">
              <div v-if="copyItems">
                <font-awesome-icon icon="fa-solid fa-list" class="me-1" />
                {{ t('entity.forkModalItemCount').replace('{count}', String(preview.itemCount)) }}
              </div>
              <div>
                <font-awesome-icon icon="fa-solid fa-coins" class="me-1" />
                {{
                  t('entity.forkModalEstimatedCost').replace(
                    '{count}',
                    String(preview.estimatedCredits)
                  )
                }}
              </div>
              <div>
                <font-awesome-icon icon="fa-solid fa-wallet" class="me-1" />
                {{ t('entity.forkModalBalance').replace('{count}', String(preview.balance)) }}
              </div>
            </template>
            <div v-else class="text-danger">
              {{ t('entity.forkModalPreviewError') }}
              <button
                type="button"
                class="btn btn-link btn-sm p-0 align-baseline"
                @click="loadPreview"
              >
                {{ t('entity.forkModalPreviewRetry') }}
              </button>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" :disabled="loading" @click="cancel">
            <font-awesome-icon icon="fa-solid fa-xmark" class="me-2" />
            {{ t('common.cancel') }}
          </button>
          <button
            type="button"
            class="btn btn-primary"
            :disabled="
              !name.trim() || loading || previewLoading || !preview || preview.insufficientCredits
            "
            @click="confirm"
          >
            <font-awesome-icon icon="fa-solid fa-code-branch" class="me-2" />
            {{ t('entity.forkEntity') }}
          </button>
        </div>
      </div>
    </div>
  </div>
</template>
 
<script setup lang="ts">
import { computed, ref, watch } from 'vue'
import { useUILanguage } from '@/composables/useUILanguage'
import { useEntitiesStore } from '@/stores/entities'
 
const { t } = useUILanguage()
const entitiesStore = useEntitiesStore()
 
interface Props {
  isOpen: boolean
  entityId: string
  entityName: string
  defaultName: string
  loading?: boolean
  error?: string
  // Fork job status/progress while the job started by `confirm` is running
  status?: 'idle' | 'pending' | 'processing' | 'completed' | 'failed'
  progress?: { done: number; total: number } | null
}
 
const props = defineProps<Props>()
 
const emit = defineEmits<{
  confirm: [payload: { name: string; copyItems: boolean }]
  cancel: []
}>()
 
const copyItems = ref(false)
const name = ref('')
 
const preview = ref<{
  itemCount: number
  estimatedCredits: number
  balance: number
  insufficientCredits: boolean
} | null>(null)
const previewLoading = ref(false)
let previewRequestId = 0
 
// The fork button stays disabled until this has landed, so the user always
// sees the cost (and whether they can afford it) before confirming; a failed
// fetch offers a retry rather than letting the fork go ahead blind.
async function loadPreview() {
  Iif (!props.entityId) return
  // Toggling the checkbox quickly can leave responses arriving out of order;
  // only the latest request may update the state.
  const requestId = ++previewRequestId
  previewLoading.value = true
  try {
    const result = await entitiesStore.forkPreview(props.entityId, copyItems.value)
    if (requestId === previewRequestId) preview.value = result
  } catch (err) {
    console.error('Failed to load fork preview:', err)
    Eif (requestId === previewRequestId) preview.value = null
  } finally {
    if (requestId === previewRequestId) previewLoading.value = false
  }
}
 
watch(
  () => props.isOpen,
  (open) => {
    if (open) {
      copyItems.value = false
      name.value = props.defaultName
      preview.value = null
      loadPreview()
    }
  }
)
 
watch(copyItems, () => {
  Eif (props.isOpen) loadPreview()
})
 
function confirm() {
  Iif (!name.value.trim() || props.loading) return
  emit('confirm', { name: name.value.trim(), copyItems: copyItems.value })
}
 
// A running fork job can't be cancelled, so closing the dialog mid-way would
// only hide it while the fork carries on (and then navigates away).
function cancel() {
  if (props.loading) return
  emit('cancel')
}
 
const isJobInProgress = computed(() => props.status === 'pending' || props.status === 'processing')
const progressPercent = computed(() => {
  Iif (!props.progress || props.progress.total <= 0) return 0
  return Math.min(100, Math.round((props.progress.done / props.progress.total) * 100))
})
const jobProgressLabel = computed(() =>
  props.progress
    ? t('entity.forkProgress')
        .replace('{done}', String(props.progress.done))
        .replace('{total}', String(props.progress.total))
    : t('entity.forkPreparing')
)
</script>