|
| 1 | +import { reactive, computed, unref, MaybeRef } from 'vue'; |
| 2 | +import { defineStore } from 'pinia'; |
| 3 | + |
| 4 | +import { |
| 5 | + DoubleKeyRecord, |
| 6 | + deleteSecondKey, |
| 7 | + getDoubleKeyRecord, |
| 8 | + patchDoubleKeyRecord, |
| 9 | +} from '@/src/utils/doubleKeyRecord'; |
| 10 | +import { Maybe } from '@/src/types'; |
| 11 | + |
| 12 | +import { createViewConfigSerializer } from './common'; |
| 13 | +import { ViewConfig } from '../../io/state-file/schema'; |
| 14 | +import { SegmentGroupConfig } from './types'; |
| 15 | + |
| 16 | +type Config = SegmentGroupConfig; |
| 17 | +const CONFIG_NAME = 'segmentGroup'; |
| 18 | + |
| 19 | +export const defaultConfig = () => ({ |
| 20 | + outlineOpacity: 1.0, |
| 21 | +}); |
| 22 | + |
| 23 | +export const useSegmentGroupConfigStore = defineStore( |
| 24 | + `${CONFIG_NAME}Config`, |
| 25 | + () => { |
| 26 | + const configs = reactive<DoubleKeyRecord<Config>>({}); |
| 27 | + |
| 28 | + const getConfig = (viewID: Maybe<string>, dataID: Maybe<string>) => |
| 29 | + getDoubleKeyRecord(configs, viewID, dataID); |
| 30 | + |
| 31 | + const updateConfig = ( |
| 32 | + viewID: string, |
| 33 | + dataID: string, |
| 34 | + patch: Partial<Config> |
| 35 | + ) => { |
| 36 | + const config = { |
| 37 | + ...defaultConfig(), |
| 38 | + ...getConfig(viewID, dataID), |
| 39 | + ...patch, |
| 40 | + }; |
| 41 | + |
| 42 | + patchDoubleKeyRecord(configs, viewID, dataID, config); |
| 43 | + }; |
| 44 | + |
| 45 | + const initConfig = (viewID: string, dataID: string) => |
| 46 | + updateConfig(viewID, dataID, defaultConfig()); |
| 47 | + |
| 48 | + const removeView = (viewID: string) => { |
| 49 | + delete configs[viewID]; |
| 50 | + }; |
| 51 | + |
| 52 | + const removeData = (dataID: string, viewID?: string) => { |
| 53 | + if (viewID) { |
| 54 | + delete configs[viewID]?.[dataID]; |
| 55 | + } else { |
| 56 | + deleteSecondKey(configs, dataID); |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + const serialize = createViewConfigSerializer(configs, CONFIG_NAME); |
| 61 | + |
| 62 | + const deserialize = ( |
| 63 | + viewID: string, |
| 64 | + config: Record<string, ViewConfig> |
| 65 | + ) => { |
| 66 | + Object.entries(config).forEach(([dataID, viewConfig]) => { |
| 67 | + if (viewConfig.segmentGroup) { |
| 68 | + updateConfig(viewID, dataID, viewConfig.segmentGroup); |
| 69 | + } |
| 70 | + }); |
| 71 | + }; |
| 72 | + |
| 73 | + // For updating all configs together // |
| 74 | + |
| 75 | + const aConfig = computed(() => { |
| 76 | + const viewIDs = Object.keys(configs); |
| 77 | + if (viewIDs.length === 0) return null; |
| 78 | + const firstViewID = viewIDs[0]; |
| 79 | + const dataIDs = Object.keys(configs[firstViewID]); |
| 80 | + if (dataIDs.length === 0) return null; |
| 81 | + const firstDataID = dataIDs[0]; |
| 82 | + return configs[firstViewID][firstDataID]; |
| 83 | + }); |
| 84 | + |
| 85 | + const updateAllConfigs = (dataID: string, patch: Partial<Config>) => { |
| 86 | + Object.keys(configs).forEach((viewID) => { |
| 87 | + updateConfig(viewID, dataID, patch); |
| 88 | + }); |
| 89 | + }; |
| 90 | + |
| 91 | + return { |
| 92 | + configs, |
| 93 | + getConfig, |
| 94 | + initConfig, |
| 95 | + updateConfig, |
| 96 | + removeView, |
| 97 | + removeData, |
| 98 | + serialize, |
| 99 | + deserialize, |
| 100 | + aConfig, |
| 101 | + updateAllConfigs, |
| 102 | + }; |
| 103 | + } |
| 104 | +); |
| 105 | + |
| 106 | +export const useGlobalSegmentGroupConfig = (dataId: MaybeRef<string>) => { |
| 107 | + const store = useSegmentGroupConfigStore(); |
| 108 | + |
| 109 | + const views = computed(() => Object.keys(store.configs)); |
| 110 | + |
| 111 | + const configs = computed(() => |
| 112 | + views.value.map((viewID) => ({ |
| 113 | + config: store.getConfig(viewID, unref(dataId)), |
| 114 | + viewID, |
| 115 | + })) |
| 116 | + ); |
| 117 | + |
| 118 | + // get any one |
| 119 | + const config = computed(() => configs.value.find(({ config: c }) => c)); |
| 120 | + |
| 121 | + // update all configs |
| 122 | + const updateConfig = (patch: Partial<Config>) => { |
| 123 | + configs.value.forEach(({ viewID }) => |
| 124 | + store.updateConfig(viewID, unref(dataId), patch) |
| 125 | + ); |
| 126 | + }; |
| 127 | + |
| 128 | + return { config, updateConfig }; |
| 129 | +}; |
0 commit comments