Skip to content

Commit dc7e717

Browse files
authored
Merge pull request framer#468 from framer/sheets-fixes
Google Sheets bug fixes
2 parents 3729b9b + 8a4ecee commit dc7e717

4 files changed

Lines changed: 211 additions & 90 deletions

File tree

plugins/google-sheets/src/App.tsx

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -198,22 +198,30 @@ export function App({ pluginContext }: AppProps) {
198198
const [headerRow] = sheet.values
199199

200200
const task = async () => {
201-
await syncSheet({
202-
ignoredColumns,
203-
slugColumn,
204-
fetchedSheet: sheet,
205-
lastSyncedTime,
206-
spreadsheetId,
207-
sheetTitle,
208-
fields,
209-
// Determine if the field type is already configured, otherwise default to "string"
210-
colFieldTypes: headerRow.map(colName => {
211-
const field = fields.find(field => field.name === colName)
212-
return field?.type ?? "string"
213-
}),
214-
})
215-
216-
framer.closePlugin()
201+
try {
202+
await syncSheet({
203+
ignoredColumns,
204+
slugColumn,
205+
fetchedSheet: sheet,
206+
lastSyncedTime,
207+
spreadsheetId,
208+
sheetTitle,
209+
fields,
210+
// Determine if the field type is already configured, otherwise default to "string"
211+
colFieldTypes: headerRow.map(colName => {
212+
const field = fields.find(field => field.name === colName)
213+
return field?.type ?? "string"
214+
}),
215+
})
216+
217+
framer.closePlugin("Synchronization successful", { variant: "success" })
218+
} catch (error) {
219+
console.error(error)
220+
framer.closePlugin(
221+
error instanceof Error ? error.message : "An error occurred while syncing the sheet",
222+
{ variant: "error" }
223+
)
224+
}
217225
}
218226

219227
void task()

plugins/google-sheets/src/pages/MapSheetFields.tsx

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Button } from "../components/Button"
66
import { CheckboxTextfield } from "../components/CheckboxTextField"
77
import { IconChevron } from "../components/Icons"
88
import type { CellValue, CollectionFieldType, HeaderRow, PluginContext, Row, SyncMutationOptions } from "../sheets"
9-
import { assert, generateUniqueNames, syncMethods } from "../utils"
9+
import { generateUniqueNames, isDefined, syncMethods } from "../utils"
1010

1111
interface FieldTypeOption {
1212
type: CollectionFieldType
@@ -104,16 +104,18 @@ const createFieldConfig = (
104104
context: PluginContext,
105105
row?: Row
106106
): ManagedCollectionFieldInput[] => {
107-
return headerRow.map((_, columnIndex) => {
108-
const sanitizedName = uniqueColumnNames[columnIndex]
109-
assert(sanitizedName, "Sanitized name is undefined")
110-
111-
return {
112-
id: sanitizedName,
113-
name: sanitizedName,
114-
type: getFieldType(context, sanitizedName, row?.[columnIndex]),
115-
} as ManagedCollectionFieldInput
116-
})
107+
return headerRow
108+
.map((_, columnIndex) => {
109+
const sanitizedName = uniqueColumnNames[columnIndex]
110+
if (!sanitizedName) return null
111+
112+
return {
113+
id: sanitizedName,
114+
name: sanitizedName,
115+
type: getFieldType(context, sanitizedName, row?.[columnIndex]),
116+
} as ManagedCollectionFieldInput
117+
})
118+
.filter(isDefined)
117119
}
118120

119121
const getFieldNameOverrides = (context: PluginContext): Record<string, string> => {
@@ -159,10 +161,7 @@ export function MapSheetFieldsPage({
159161
const [disabledColumns, setDisabledColumns] = useState(
160162
() => new Set<string>(pluginContext.type === "update" ? pluginContext.ignoredColumns : [])
161163
)
162-
const slugFields = useMemo(
163-
() => getPossibleSlugFields(fieldConfig).filter(fieldConfig => !disabledColumns.has(fieldConfig.id)),
164-
[fieldConfig, disabledColumns]
165-
)
164+
const slugFields = useMemo(() => getPossibleSlugFields(fieldConfig), [fieldConfig])
166165
const [slugColumn, setSlugColumn] = useState<string>(() => getInitialSlugColumn(pluginContext, slugFields))
167166
const [fieldNameOverrides, setFieldNameOverrides] = useState<Record<string, string>>(() =>
168167
getFieldNameOverrides(pluginContext)
@@ -173,29 +172,8 @@ export function MapSheetFieldsPage({
173172
const nextSet = new Set(current)
174173
if (nextSet.has(id)) {
175174
nextSet.delete(id)
176-
177-
// If we're re-enabling a string field and there's currently no valid slug column,
178-
// set this field as the slug column
179-
const field = fieldConfig.find(config => config.id === id)
180-
if (field?.type === "string") {
181-
const currentSlugField = fieldConfig.find(config => config.id === slugColumn)
182-
if (!currentSlugField || nextSet.has(slugColumn)) {
183-
setSlugColumn(id)
184-
}
185-
}
186175
} else {
187176
nextSet.add(id)
188-
189-
// If the disabled column is the slug column, we need to update it to the next
190-
// possible slug field
191-
if (id === slugColumn) {
192-
const nextSlugField = getPossibleSlugFields(fieldConfig).find(
193-
field => field.id !== id && !nextSet.has(field.id)
194-
)
195-
if (nextSlugField) {
196-
setSlugColumn(nextSlugField.id)
197-
}
198-
}
199177
}
200178
return nextSet
201179
})

0 commit comments

Comments
 (0)