order sharing
This commit is contained in:
74
src/share.js
Normal file
74
src/share.js
Normal file
@@ -0,0 +1,74 @@
|
||||
import { compressToEncodedURIComponent, decompressFromEncodedURIComponent } from 'lz-string';
|
||||
import {useChartOrderStore} from "@/orderbuild.js";
|
||||
import {changeIntervalSecs, onChartReady, setSymbol, widget} from "@/charts/chart.js";
|
||||
import {usePrefStore, useStore} from "@/store/store.js";
|
||||
import {lookupSymbol} from "@/charts/datafeed.js";
|
||||
|
||||
export async function getShareUrl() {
|
||||
const co = useChartOrderStore();
|
||||
const s = useStore()
|
||||
const sym = co.selectedSymbol
|
||||
console.log('symbol', sym)
|
||||
const data = {
|
||||
version: 1,
|
||||
chainId: s.chainId,
|
||||
orders: co.orders,
|
||||
symbol: {
|
||||
base: {a: sym.base.a, s: sym.base.s},
|
||||
quote: {a: sym.quote.a, s: sym.quote.s},
|
||||
route: {
|
||||
fee: sym.fee,
|
||||
exchange: sym.exchangeId,
|
||||
}
|
||||
},
|
||||
period: co.intervalSecs,
|
||||
}
|
||||
const json = JSON.stringify(data)
|
||||
console.log('sharing data', json, data)
|
||||
const compressed = compressToEncodedURIComponent(json);
|
||||
const baseUrl = `${window.location.protocol}//${window.location.hostname}:${window.location.port}`;
|
||||
const imageFile = await takeSnapshot()
|
||||
return `${baseUrl}/shared?i=${imageFile}&d=${compressed}`;
|
||||
}
|
||||
|
||||
export function loadShareUrl() {
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const dataStr = urlParams.get('d');
|
||||
if (!dataStr) return
|
||||
const json = decompressFromEncodedURIComponent(dataStr);
|
||||
const data = JSON.parse(json);
|
||||
console.log('loaded shared orders data', data)
|
||||
const co = useChartOrderStore();
|
||||
const s = useStore()
|
||||
const ticker = `${data.chainId}|${data.symbol.route.exchange}|${data.symbol.base.a}|${data.symbol.quote.a}|${data.symbol.route.fee}`;
|
||||
const symbol = lookupSymbol(ticker)
|
||||
if (symbol===null) {
|
||||
console.error('could not find symbol for ticker', ticker)
|
||||
return
|
||||
}
|
||||
s.chainId = data.chainId
|
||||
const prefs = usePrefStore()
|
||||
prefs.selectedSymbol = ticker
|
||||
for (const order of data.orders) {
|
||||
order.amount = 0
|
||||
order.valid = false
|
||||
}
|
||||
co.orders = data.orders
|
||||
changeIntervalSecs(data.period)
|
||||
onChartReady(()=>{
|
||||
setSymbol(symbol)
|
||||
.catch((e)=>console.error('could not set symbol', e))
|
||||
})
|
||||
console.log('loaded orders', s.chainId, co.orders)
|
||||
}
|
||||
|
||||
export async function takeSnapshot() {
|
||||
const screenshotCanvas = await widget.takeClientScreenshot();
|
||||
const image = await new Promise((resolve) => screenshotCanvas.toBlob(resolve));
|
||||
const response = await fetch(import.meta.env.VITE_SNAPSHOT_URL, {
|
||||
method: 'PUT',
|
||||
body: image,
|
||||
credentials: 'same-origin',
|
||||
});
|
||||
return response.text()
|
||||
}
|
||||
Reference in New Issue
Block a user