|
| 1 | +import {provideAppState} from '@codeimage/store/index'; |
1 | 2 | import {withEntityPlugin} from '@codeimage/store/plugins/withEntityPlugin';
|
2 | 3 | import {withIndexedDbPlugin} from '@codeimage/store/plugins/withIndexedDbPlugin';
|
3 | 4 | import {toast} from '@codeimage/ui';
|
4 |
| -import {untrack} from 'solid-js'; |
| 5 | +import {createEffect, on, untrack} from 'solid-js'; |
5 | 6 | import {withAsyncAction} from 'statebuilder/asyncAction';
|
6 |
| -import {provideAppState} from '..'; |
7 | 7 | import * as api from '../../data-access/preset';
|
8 | 8 | import {useIdb} from '../../hooks/use-indexed-db';
|
9 | 9 | import {getAuth0State} from '../auth/auth0';
|
@@ -50,6 +50,42 @@ const PresetStoreDefinition = experimental__defineResource(fetchInitialState)
|
50 | 50 | .extend(withIndexedDbPlugin<PresetsArray>(idbKey, []))
|
51 | 51 | .extend(withPresetBridge(idbKey))
|
52 | 52 | .extend(withAsyncAction())
|
| 53 | + .extend(store => { |
| 54 | + const sharePresetKey = 'share_preset'; |
| 55 | + createEffect( |
| 56 | + on( |
| 57 | + () => store.state === 'ready', |
| 58 | + isReady => { |
| 59 | + if (isReady) { |
| 60 | + const params = new URLSearchParams(window.location.search); |
| 61 | + if (params.has(sharePresetKey)) { |
| 62 | + const sharePreset = params.get(sharePresetKey) as string; |
| 63 | + try { |
| 64 | + const preset = JSON.parse(window.atob(sharePreset)); |
| 65 | + store.bridge |
| 66 | + .addNewPreset(preset.name, preset.data) |
| 67 | + .then(preset => store.set(_ => [preset, ...(_ ?? [])])) |
| 68 | + .then(() => { |
| 69 | + const url = |
| 70 | + window.location.origin + window.location.pathname; |
| 71 | + window.history.replaceState(undefined, '', url); |
| 72 | + }) |
| 73 | + .then(() => |
| 74 | + toast.success('Preset imported successfully', { |
| 75 | + position: 'bottom-center', |
| 76 | + }), |
| 77 | + ); |
| 78 | + } catch (e) { |
| 79 | + toast.error('Invalid preset', { |
| 80 | + position: 'bottom-center', |
| 81 | + }); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + }, |
| 86 | + ), |
| 87 | + ); |
| 88 | + }) |
53 | 89 | .extend(store => {
|
54 | 90 | return {
|
55 | 91 | sortedPresets() {
|
@@ -104,6 +140,11 @@ const PresetStoreDefinition = experimental__defineResource(fetchInitialState)
|
104 | 140 | });
|
105 | 141 | });
|
106 | 142 | }),
|
| 143 | + copyLink: store.asyncAction((payload: Preset) => { |
| 144 | + const data = window.btoa(JSON.stringify(payload)); |
| 145 | + const link = `${window.location.origin}${window.location.pathname}?share_preset=${data}`; |
| 146 | + return navigator.clipboard.writeText(link); |
| 147 | + }), |
107 | 148 | syncPreset: store.asyncAction((payload: Preset) => {
|
108 | 149 | return untrack(() => {
|
109 | 150 | const currentState = store();
|
|
0 commit comments