Skip to content

Commit ef224fa

Browse files
committed
fix(google-sheets): restore ignored columns after validation issues
Validation issues caused the plugin not to load anymore. Fixed it by introducing a rescue step and `salvageIgnoredColumns` and store the result afterwards
1 parent b79afcc commit ef224fa

1 file changed

Lines changed: 40 additions & 1 deletion

File tree

plugins/google-sheets/src/sheets.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,28 @@ export async function getPluginContext(): Promise<PluginContext> {
549549
id: uniqueHeaderRowNames.find(name => name === field.name) ?? uniqueHeaderRowNames[index] ?? field.name,
550550
}))
551551
} else {
552-
ignoredColumns = v.parse(v.array(v.string()), JSON.parse(rawIgnoredColumns))
553552
slugColumn = storedSlugColumn
553+
const result = v.safeParse(v.pipe(v.string(), v.parseJson(), v.array(v.string())), rawIgnoredColumns)
554+
555+
if (result.success) {
556+
ignoredColumns = result.output
557+
} else if (framer.isAllowedTo("ManagedCollection.setPluginData")) {
558+
ignoredColumns = await salvageIgnoredColumns(rawIgnoredColumns, ignoredColumns =>
559+
collection.setPluginData(PLUGIN_IGNORED_COLUMNS_KEY, JSON.stringify(ignoredColumns))
560+
)
561+
562+
// Notify the user to be sure they're aware of the issue, to be sure they acknowledge it make it persistent
563+
framer.notify("Some columns couldn’t be restored. Please review your ignored columns in Manage.", {
564+
variant: "warning",
565+
durationMs: Infinity,
566+
})
567+
} else {
568+
ignoredColumns = []
569+
570+
framer.notify("Configuration of ignored columns is invalid. You need write permissions to fix it.", {
571+
variant: "error",
572+
})
573+
}
554574
}
555575

556576
// We should not hash ignored fields since they are not synced to Framer,
@@ -611,3 +631,22 @@ export const useSyncSheetMutation = ({
611631
onError,
612632
})
613633
}
634+
635+
async function salvageIgnoredColumns(
636+
rawIgnoredColumns: unknown,
637+
setIgnoredColumns: (ignoredColumns: string[]) => Promise<void>
638+
): Promise<string[]> {
639+
let result: string[]
640+
641+
const atLeastAnArrayResult = v.safeParse(v.pipe(v.string(), v.parseJson(), v.array(v.unknown())), rawIgnoredColumns)
642+
643+
if (atLeastAnArrayResult.success) {
644+
result = atLeastAnArrayResult.output.filter(i => typeof i === "string")
645+
} else {
646+
result = []
647+
}
648+
649+
await setIgnoredColumns(result)
650+
651+
return result
652+
}

0 commit comments

Comments
 (0)