2020 <NextDiff :click-handler =" goToNextDiff" />
2121 <PrevDiff :click-handler =" goToPreviousDiff" />
2222 </div >
23- <div class =" flex items-center gap-4" >
23+ <div class =" flex gap-4 items-center " >
2424 <DiffStyle :click-handler =" toggleDiffFashion" />
2525 <CopyLink :click-handler =" copyUrlToClipboard" :copied =" copied" ></CopyLink >
2626 </div >
2929
3030<script lang="ts">
3131import Vue from ' vue'
32- import PrevDiff from ' ../buttons/prevDiff.vue'
33- import NextDiff from ' ../buttons/nextDiff.vue'
3432import CopyLink from ' ../buttons/copyLink.vue'
3533import DiffStyle from ' ../buttons/diffStyle.vue'
36- import { putToClipboard } from ' ~/helpers/utils'
34+ import NextDiff from ' ../buttons/nextDiff.vue'
35+ import PrevDiff from ' ../buttons/prevDiff.vue'
36+ import { SIMPLE_DIFF_CHARACTER_LIMIT } from ' ~/constants/constants'
37+ import {
38+ E2E_LINK_GENERATION_ERROR ,
39+ E2E_LINK_GENERATION_SUCCESS ,
40+ LINK_COPY_SUCCESS ,
41+ } from ' ~/constants/messages'
42+ import {
43+ getEncryptedData ,
44+ getEncryptionKey ,
45+ getExtractedEncryptionKey ,
46+ } from ' ~/helpers/encrypt'
3747import { DiffActionBarData } from ' ~/helpers/types'
48+ import { getRandomDiffId , putToClipboard } from ' ~/helpers/utils'
3849export default Vue .extend ({
3950 components: {
4051 PrevDiff ,
@@ -58,6 +69,7 @@ export default Vue.extend({
5869 comparator: null ,
5970 comparer: null ,
6071 treeWalker: null ,
72+ e2eLink: null ,
6173 }
6274 },
6375 mounted() {
@@ -81,15 +93,67 @@ export default Vue.extend({
8193 }
8294 },
8395 copyUrlToClipboard() {
84- putToClipboard (
85- window .location .href ,
86- ' Link copied to your clipboard' ,
87- this .$store
88- )
89- this .copied = true
90- setTimeout (() => {
91- this .copied = false
92- }, 5000 )
96+ const isCurrentUrlExceedsCharacterLimit =
97+ window .location .href .length > SIMPLE_DIFF_CHARACTER_LIMIT
98+ if (isCurrentUrlExceedsCharacterLimit && this .e2eLink ) {
99+ putToClipboard (this .e2eLink , LINK_COPY_SUCCESS , this .$store )
100+ } else if (isCurrentUrlExceedsCharacterLimit ) {
101+ this .copyE2eUrlToClipboard ()
102+ } else {
103+ putToClipboard (window .location .href , LINK_COPY_SUCCESS , this .$store )
104+ this .copied = true
105+ setTimeout (() => {
106+ this .copied = false
107+ }, 5000 )
108+ }
109+ },
110+ async copyE2eUrlToClipboard() {
111+ try {
112+ this .copied = null
113+ const id = getRandomDiffId ()
114+ const keyBuffer = await getEncryptionKey ()
115+ const encryptedDataText = await getEncryptedData (
116+ window .location .hash .replace (/ ^ #/ , ' ' ),
117+ keyBuffer
118+ )
119+ const extractedEncryptionKey = await getExtractedEncryptionKey (
120+ keyBuffer
121+ )
122+ const response = await fetch (' /api/createLink' , {
123+ method: ' POST' ,
124+ headers: {
125+ ' Content-Type' : ' application/json' ,
126+ },
127+ body: JSON .stringify ({
128+ data: encryptedDataText ,
129+ id ,
130+ }),
131+ })
132+ const data = await response .json ()
133+ if (! data .success ) {
134+ throw new Error (E2E_LINK_GENERATION_ERROR )
135+ }
136+ const newUrl = new URL (window .location .origin )
137+ newUrl .pathname = ' /v2/diff'
138+ newUrl .hash = ` #${extractedEncryptionKey } `
139+ newUrl .searchParams .set (' id' , id )
140+ putToClipboard (
141+ newUrl .toString (),
142+ E2E_LINK_GENERATION_SUCCESS ,
143+ this .$store
144+ )
145+ this .e2eLink = newUrl .toString ()
146+ this .copied = true
147+ setTimeout (() => {
148+ this .copied = false
149+ }, 5000 )
150+ } catch (error : any ) {
151+ this .showErrorToast (E2E_LINK_GENERATION_ERROR )
152+ } finally {
153+ setTimeout (() => {
154+ this .copied = false
155+ }, 5000 )
156+ }
93157 },
94158 goToNextDiff() {
95159 this .diffNavigator .next ()
@@ -100,6 +164,29 @@ export default Vue.extend({
100164 toggleDiffFashion(value : boolean ) {
101165 this .onDiffFashion (value )
102166 },
167+ showErrorToast(content : string ) {
168+ this .$store .commit (' toast/show' , {
169+ show: true ,
170+ content ,
171+ iconHTML: `
172+ <svg
173+ class="w-6 h-6"
174+ fill="none"
175+ stroke="currentColor"
176+ viewBox="0 0 24 24"
177+ xmlns="http://www.w3.org/2000/svg"
178+ >
179+ <path
180+ stroke-linecap="round"
181+ stroke-linejoin="round"
182+ stroke-width="2"
183+ d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"
184+ ></path>
185+ </svg>
186+ ` ,
187+ theme: ' error' ,
188+ })
189+ },
103190 },
104191})
105192 </script >
0 commit comments