| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- <template>
- <div>
- <div
- class="solution-toggle"
- @click="toggleSolutions"
- :class="{
- 'solution-toggle-show': isHidingSolutions,
- }"
- >
- <a v-if="isHidingSolutions" class="link-solution" target="_blank"
- ><Icon name="lightbulb" class="text-xs mr-1" /> Show solutions</a
- >
- <a v-else class="link-solution" target="_blank">Hide solutions</a>
- </div>
- <div
- ref="solutionCard"
- class="solution"
- :class="{
- 'solution-hidden': isHidingSolutions,
- }"
- >
- <div class="solution-main">
- <div class="solution-background mx-0">
- <svg
- class="hidden absolute right-0 h-full | md:block"
- x="0px"
- y="0px"
- viewBox="0 0 299 452"
- >
- <g style="opacity: 0.075">
- <polygon
- style="fill:rgb(63,63,63)"
- points="298.1,451.9 150.9,451.9 21,226.9 298.1,227.1"
- />
- <polygon
- style="fill:rgb(151,151,151)"
- points="298.1,227.1 21,226.9 150.9,1.9 298.1,1.9"
- />
- </g>
- </svg>
- </div>
- <div class="solution-content-wrapper scrollbar">
- <div class="solution-content ml-0">
- <h2 v-if="solution.title !== ''" class="solution-title">
- {{ solution.title }}
- </h2>
- <div
- v-if="solution.description"
- v-html="markdown(solution.description)"
- class="solution-description"
- ></div>
- <div v-if="solution.is_runnable">
- <p
- v-html="markdown(solution.action_description)"
- class="solution-description"
- ></p>
- <p v-if="canExecuteSolutions === null" class="py-4 text-sm italic">
- Loading...
- </p>
- <div class="mt-4">
- <button
- v-if="
- solution.is_runnable &&
- canExecuteSolutions === true &&
- executionSuccessful === null
- "
- :disabled="runningSolution"
- @click="execute"
- class="button-secondary button-lg bg-tint-300 hover:bg-tint-400"
- >
- <span v-if="runningSolution">Running...</span>
- <span v-if="!runningSolution">{{
- solution.run_button_text
- }}</span>
- </button>
- <p v-if="executionSuccessful">
- <strong class="font-semibold"
- >The solution was executed successfully.</strong
- >
- <a href="#" @click.prevent="refresh" class="link-solution"
- >Refresh now.</a
- >
- </p>
- <p v-if="executionSuccessful === false">
- Something went wrong when executing the solution. Please try
- refreshing the page and try again.
- </p>
- </div>
- </div>
- <div
- class="mt-8 grid justify-start"
- v-if="Object.entries(solution.links).length > 0"
- >
- <div class="border-t-2 border-gray-700 opacity-25 " />
- <div class="pt-2 grid cols-auto-1fr gapx-4 gapy-2 text-sm">
- <label class="font-semibold uppercase tracking-wider"
- >Read more</label
- >
- <ul>
- <li v-for="(link, label) in solution.links" :key="label">
- <a :href="link" class="link-solution" target="_blank">{{
- label
- }}</a>
- </li>
- </ul>
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
- </template>
- <script>
- const MarkdownIt = require('markdown-it')();
- const cookieName = 'hide_solutions';
- let animationTimeout = null;
- export default {
- inject: ['config'],
- props: {
- solution: { required: true },
- },
- data() {
- return {
- isHidingSolutions: this.hasHideSolutionsCookie(),
- canExecuteSolutions: null,
- runningSolution: false,
- executionSuccessful: null,
- };
- },
- computed: {
- healthCheckEndpoint() {
- return this.solution.execute_endpoint.replace('execute-solution', 'health-check');
- },
- },
- created() {
- this.configureRunnableSolutions();
- },
- mounted() {
- if (this.isHidingSolutions) {
- this.$refs.solutionCard.classList.add('solution-hidden');
- }
- },
- methods: {
- configureRunnableSolutions() {
- if (!this.config.enableRunnableSolutions) {
- this.canExecuteSolutions = false;
- return;
- }
- this.checkExecutionEndpoint();
- },
- markdown(string) {
- return MarkdownIt.render(string);
- },
- async checkExecutionEndpoint() {
- try {
- const healthCheck = await (await fetch(this.healthCheckEndpoint)).json();
- this.canExecuteSolutions = healthCheck.can_execute_commands;
- } catch (error) {
- this.canExecuteSolutions = false;
- }
- },
- async execute() {
- if (this.runningSolution) {
- return;
- }
- try {
- this.runningSolution = true;
- const response = await fetch(this.solution.execute_endpoint, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- Accept: 'application/json',
- },
- body: JSON.stringify({
- solution: this.solution.class,
- parameters: this.solution.run_parameters,
- }),
- });
- this.executionSuccessful = response.status === 200;
- } catch (error) {
- console.error(error);
- this.executionSuccessful = false;
- } finally {
- this.runningSolution = false;
- }
- },
- refresh() {
- location.reload();
- },
- getUrlLabel(url) {
- const tempElement = document.createElement('a');
- tempElement.href = url;
- return tempElement.hostname;
- },
- toggleSolutions() {
- if (!this.isHidingSolutions) {
- this.$refs.solutionCard.classList.add('solution-hiding');
- animationTimeout = window.setTimeout(() => {
- this.$refs.solutionCard.classList.remove('solution-hiding');
- this.toggleHidingSolutions();
- }, 100);
- } else {
- window.clearTimeout(animationTimeout);
- this.toggleHidingSolutions();
- }
- },
- toggleHidingSolutions() {
- if (this.isHidingSolutions) {
- document.cookie = `${cookieName}=;expires=Thu, 01 Jan 1970 00:00:00 UTC;path=/;`;
- this.isHidingSolutions = false;
- return;
- }
- const expires = new Date();
- expires.setTime(expires.getTime() + 365 * 24 * 60 * 60 * 1000);
- document.cookie = `${cookieName}=true;expires=${expires.toUTCString()};path=/;`;
- this.isHidingSolutions = true;
- },
- hasHideSolutionsCookie() {
- return document.cookie.includes(cookieName);
- },
- },
- };
- </script>
|