|
| 1 | +import getPageUidByPageTitle from "roamjs-components/queries/getPageUidByPageTitle"; |
| 2 | +import getShallowTreeByParentUid from "roamjs-components/queries/getShallowTreeByParentUid"; |
| 3 | +import { createPage, createBlock } from "roamjs-components/writes"; |
| 4 | +import setBlockProps from "~/utils/setBlockProps"; |
| 5 | +import getBlockProps from "~/utils/getBlockProps"; |
| 6 | +import INITIAL_NODE_VALUES from "~/data/defaultDiscourseNodes"; |
| 7 | +import { |
| 8 | + DiscourseNodeSchema, |
| 9 | + getTopLevelBlockPropsConfig, |
| 10 | + getPersonalSettingsKey, |
| 11 | +} from "~/components/settings/utils/zodSchema"; |
| 12 | +import { DG_BLOCK_PROP_SETTINGS_PAGE_TITLE, DISCOURSE_NODE_PAGE_PREFIX } from "./zodSchema"; |
| 13 | + |
| 14 | +const ensurePageExists = async (pageTitle: string): Promise<string> => { |
| 15 | + let pageUid = getPageUidByPageTitle(pageTitle); |
| 16 | + |
| 17 | + if (!pageUid) { |
| 18 | + pageUid = window.roamAlphaAPI.util.generateUID(); |
| 19 | + await createPage({ |
| 20 | + title: pageTitle, |
| 21 | + uid: pageUid, |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + return pageUid; |
| 26 | +}; |
| 27 | + |
| 28 | +const ensureBlocksExist = async ( |
| 29 | + pageUid: string, |
| 30 | + blockTexts: string[], |
| 31 | + existingBlockMap: Record<string, string>, |
| 32 | +): Promise<Record<string, string>> => { |
| 33 | + const missingBlocks = blockTexts.filter( |
| 34 | + (blockText) => !existingBlockMap[blockText], |
| 35 | + ); |
| 36 | + |
| 37 | + if (missingBlocks.length > 0) { |
| 38 | + const createdBlocks = await Promise.all( |
| 39 | + missingBlocks.map(async (blockText) => { |
| 40 | + const uid = await createBlock({ |
| 41 | + parentUid: pageUid, |
| 42 | + node: { text: blockText }, |
| 43 | + }); |
| 44 | + return { text: blockText, uid }; |
| 45 | + }), |
| 46 | + ); |
| 47 | + |
| 48 | + createdBlocks.forEach((block) => { |
| 49 | + existingBlockMap[block.text] = block.uid; |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + return existingBlockMap; |
| 54 | +}; |
| 55 | + |
| 56 | +const buildBlockMap = (pageUid: string): Record<string, string> => { |
| 57 | + const existingChildren = getShallowTreeByParentUid(pageUid); |
| 58 | + const blockMap: Record<string, string> = {}; |
| 59 | + existingChildren.forEach((child) => { |
| 60 | + blockMap[child.text] = child.uid; |
| 61 | + }); |
| 62 | + return blockMap; |
| 63 | +}; |
| 64 | + |
| 65 | +const initializeSettingsBlockProps = ( |
| 66 | + blockMap: Record<string, string>, |
| 67 | +): void => { |
| 68 | + const configs = getTopLevelBlockPropsConfig(); |
| 69 | + |
| 70 | + for (const { key, schema } of configs) { |
| 71 | + const uid = blockMap[key]; |
| 72 | + if (uid) { |
| 73 | + const existingProps = getBlockProps(uid); |
| 74 | + if (!existingProps || Object.keys(existingProps).length === 0) { |
| 75 | + const defaults = schema.parse({}); |
| 76 | + setBlockProps(uid, defaults, false); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +const initSettingsPageBlocks = async (): Promise<Record<string, string>> => { |
| 83 | + const pageUid = await ensurePageExists(DG_BLOCK_PROP_SETTINGS_PAGE_TITLE); |
| 84 | + const blockMap = buildBlockMap(pageUid); |
| 85 | + |
| 86 | + const topLevelBlocks = getTopLevelBlockPropsConfig().map(({ key }) => key); |
| 87 | + await ensureBlocksExist(pageUid, topLevelBlocks, blockMap); |
| 88 | + |
| 89 | + initializeSettingsBlockProps(blockMap); |
| 90 | + |
| 91 | + return blockMap; |
| 92 | +}; |
| 93 | + |
| 94 | +const hasNonDefaultNodes = (): boolean => { |
| 95 | + const results = window.roamAlphaAPI.q(` |
| 96 | + [:find ?uid ?title |
| 97 | + :where |
| 98 | + [?page :node/title ?title] |
| 99 | + [?page :block/uid ?uid] |
| 100 | + [(clojure.string/starts-with? ?title "${DISCOURSE_NODE_PAGE_PREFIX}")]] |
| 101 | + `) as [string, string][]; |
| 102 | + |
| 103 | + for (const [pageUid] of results) { |
| 104 | + const blockProps = getBlockProps(pageUid); |
| 105 | + if (!blockProps) continue; |
| 106 | + |
| 107 | + const parsed = DiscourseNodeSchema.safeParse(blockProps); |
| 108 | + if (!parsed.success) continue; |
| 109 | + |
| 110 | + if (parsed.data.backedBy !== "default") { |
| 111 | + return true; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return false; |
| 116 | +}; |
| 117 | + |
| 118 | +const initSingleDiscourseNode = async ( |
| 119 | + node: (typeof INITIAL_NODE_VALUES)[number], |
| 120 | +): Promise<{ label: string; pageUid: string } | null> => { |
| 121 | + if (!node.text) return null; |
| 122 | + |
| 123 | + const pageUid = await ensurePageExists( |
| 124 | + `${DISCOURSE_NODE_PAGE_PREFIX}${node.text}`, |
| 125 | + ); |
| 126 | + const existingProps = getBlockProps(pageUid); |
| 127 | + |
| 128 | + if (!existingProps || Object.keys(existingProps).length === 0) { |
| 129 | + const nodeData = DiscourseNodeSchema.parse({ |
| 130 | + text: node.text, |
| 131 | + type: node.type, |
| 132 | + format: node.format || "", |
| 133 | + shortcut: node.shortcut || "", |
| 134 | + tag: node.tag || "", |
| 135 | + graphOverview: node.graphOverview ?? false, |
| 136 | + canvasSettings: node.canvasSettings || {}, |
| 137 | + backedBy: "default", |
| 138 | + }); |
| 139 | + |
| 140 | + setBlockProps(pageUid, nodeData, false); |
| 141 | + } |
| 142 | + |
| 143 | + return { label: node.text, pageUid }; |
| 144 | +}; |
| 145 | + |
| 146 | +const initDiscourseNodePages = async (): Promise<Record<string, string>> => { |
| 147 | + if (hasNonDefaultNodes()) { |
| 148 | + return {}; |
| 149 | + } |
| 150 | + |
| 151 | + const results = await Promise.all( |
| 152 | + INITIAL_NODE_VALUES.map((node) => initSingleDiscourseNode(node)), |
| 153 | + ); |
| 154 | + |
| 155 | + const nodePageUids: Record<string, string> = {}; |
| 156 | + for (const result of results) { |
| 157 | + if (result) { |
| 158 | + nodePageUids[result.label] = result.pageUid; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return nodePageUids; |
| 163 | +}; |
| 164 | + |
| 165 | +const printAllSettings = ( |
| 166 | + blockMap: Record<string, string>, |
| 167 | + nodePageUids: Record<string, string>, |
| 168 | +): void => { |
| 169 | + const configs = getTopLevelBlockPropsConfig(); |
| 170 | + const featureFlagsUid = blockMap[configs.find(({ key }) => key === "Feature Flags")?.key ?? ""]; |
| 171 | + const globalUid = blockMap[configs.find(({ key }) => key === "Global")?.key ?? ""]; |
| 172 | + const personalKey = getPersonalSettingsKey(); |
| 173 | + const personalUid = blockMap[personalKey]; |
| 174 | + |
| 175 | + const featureFlags = featureFlagsUid ? getBlockProps(featureFlagsUid) : null; |
| 176 | + const globalSettings = globalUid ? getBlockProps(globalUid) : null; |
| 177 | + const personalSettings = personalUid ? getBlockProps(personalUid) : null; |
| 178 | + |
| 179 | + console.group("🔧 Discourse Graph Settings Initialized (RAW DATA)"); |
| 180 | + |
| 181 | + console.group(`🚩 Feature Flags (uid: ${featureFlagsUid})`); |
| 182 | + console.log("Raw block props:", JSON.stringify(featureFlags, null, 2)); |
| 183 | + console.groupEnd(); |
| 184 | + |
| 185 | + console.group(`🌍 Global Settings (uid: ${globalUid})`); |
| 186 | + console.log("Raw block props:", JSON.stringify(globalSettings, null, 2)); |
| 187 | + console.groupEnd(); |
| 188 | + |
| 189 | + console.group(`👤 Personal Settings (uid: ${personalUid})`); |
| 190 | + console.log("Raw block props:", JSON.stringify(personalSettings, null, 2)); |
| 191 | + console.groupEnd(); |
| 192 | + |
| 193 | + console.group("📝 Discourse Nodes"); |
| 194 | + for (const [nodeLabel, pageUid] of Object.entries(nodePageUids)) { |
| 195 | + const nodeProps = getBlockProps(pageUid); |
| 196 | + console.group(`${nodeLabel} (uid: ${pageUid})`); |
| 197 | + console.log("Raw block props:", JSON.stringify(nodeProps, null, 2)); |
| 198 | + console.groupEnd(); |
| 199 | + } |
| 200 | + console.groupEnd(); |
| 201 | + |
| 202 | + const relations = (globalSettings as Record<string, unknown>)?.Relations; |
| 203 | + console.group("🔗 Discourse Relations"); |
| 204 | + console.log("Relations:", JSON.stringify(relations, null, 2)); |
| 205 | + console.groupEnd(); |
| 206 | + |
| 207 | + console.groupEnd(); |
| 208 | +}; |
| 209 | + |
| 210 | +export type InitSchemaResult = { |
| 211 | + blockUids: Record<string, string>; |
| 212 | + nodePageUids: Record<string, string>; |
| 213 | +}; |
| 214 | + |
| 215 | +export const initSchema = async (): Promise<InitSchemaResult> => { |
| 216 | + const blockUids = await initSettingsPageBlocks(); |
| 217 | + const nodePageUids = await initDiscourseNodePages(); |
| 218 | + |
| 219 | + setTimeout(() => { |
| 220 | + printAllSettings(blockUids, nodePageUids); |
| 221 | + }, 2000); |
| 222 | + |
| 223 | + return { blockUids, nodePageUids }; |
| 224 | +}; |
0 commit comments