Skip to content

Commit

Permalink
handle legacy sheets service usage (#5446)
Browse files Browse the repository at this point in the history
Co-authored-by: Ben Loe <[email protected]>
  • Loading branch information
BLoe and Ben Loe authored Mar 31, 2023
1 parent bb4ace9 commit aa92de4
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 7 deletions.
52 changes: 52 additions & 0 deletions src/contrib/google/sheets/getSheetServiceOutputKey.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2023 PixieBrix, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { getSheetServiceOutputKey } from "@/contrib/google/sheets/getSheetServiceOutputKey";
import { makeVariableExpression } from "@/runtime/expressionCreators";

describe("getSheetServiceOutputKey", () => {
test("abc", () => {
expect(getSheetServiceOutputKey(makeVariableExpression("abc"))).toEqual(
"abc"
);
});
test("@abc", () => {
expect(getSheetServiceOutputKey(makeVariableExpression("@abc"))).toEqual(
"abc"
);
});
test("@abc.def", () => {
expect(
getSheetServiceOutputKey(makeVariableExpression("@abc.def"))
).toBeUndefined();
});
test("@abc.spreadsheetId", () => {
expect(
getSheetServiceOutputKey(makeVariableExpression("@abc.spreadsheetId"))
).toEqual("abc");
});
test("@abc.spreadsheetId.def", () => {
expect(
getSheetServiceOutputKey(makeVariableExpression("@abc.spreadsheetId.def"))
).toBeUndefined();
});
test("@abc.def.spreadsheetId", () => {
expect(
getSheetServiceOutputKey(makeVariableExpression("@abc.def.spreadsheetId"))
).toBeUndefined();
});
});
41 changes: 41 additions & 0 deletions src/contrib/google/sheets/getSheetServiceOutputKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2023 PixieBrix, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { type Expression } from "@/core";

export function getSheetServiceOutputKey(
fieldValue: Expression
): string | undefined {
const parts = fieldValue.__value__
// Remove the leading @
.replace(/^@/, "")
.split(".");

switch (parts.length) {
case 1: {
return parts[0];
}

case 2: {
return parts[1] === "spreadsheetId" ? parts[0] : undefined;
}

default: {
return undefined;
}
}
}
19 changes: 19 additions & 0 deletions src/contrib/google/sheets/useSpreadsheetId.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,25 @@ describe("useSpreadsheetId", () => {
expect(result.current).toBe(TEST_SPREADSHEET_ID);
});

test("works with legacy service usage", async () => {
const { result, waitForEffect } = renderHook(() => useSpreadsheetId(""), {
initialValues: {
spreadsheetId: makeVariableExpression("@sheet.spreadsheetId"),
services: [
{
id: GOOGLE_SHEET_SERVICE_ID,
outputKey: "sheet",
config: uuidSequence(2),
},
],
},
});

await waitForEffect();

expect(result.current).toBe(TEST_SPREADSHEET_ID);
});

test("works with mod input", async () => {
const { result, waitForEffect } = renderHook(() => useSpreadsheetId(""), {
initialValues: {
Expand Down
14 changes: 7 additions & 7 deletions src/contrib/google/sheets/useSpreadsheetId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,16 @@ import { useField, useFormikContext } from "formik";
import { type Expression, type UserOptions } from "@/core";
import { joinName } from "@/utils";
import { useReducer } from "react";
import {
keyToFieldValue,
type ServiceSlice,
} from "@/components/fields/schemaFields/serviceFieldUtils";
import { type ServiceSlice } from "@/components/fields/schemaFields/serviceFieldUtils";
import { isServiceValueFormat } from "@/components/fields/schemaFields/fieldTypeCheckers";
import { isEmpty, isEqual } from "lodash";
import { isEmpty } from "lodash";
import { pickDependency } from "@/services/useDependency";
import { createSlice, type PayloadAction } from "@reduxjs/toolkit";
import { useAsyncEffect } from "use-async-effect";
import { services } from "@/background/messenger/api";
import { getErrorMessage } from "@/errors/errorHelpers";
import { getOptionsArgForFieldValue } from "@/utils/getOptionsArgForFieldValue";
import { getSheetServiceOutputKey } from "@/contrib/google/sheets/getSheetServiceOutputKey";

type SpreadsheetState = {
spreadsheetId: string | null;
Expand Down Expand Up @@ -115,9 +113,11 @@ function useSpreadsheetId(basePath: string): string | null {
}

try {
const sheetsService = servicesValue.find((service) =>
isEqual(keyToFieldValue(service.outputKey), fieldValue)
const serviceOutputKey = getSheetServiceOutputKey(fieldValue);
const sheetsService = servicesValue.find(
(service) => service.outputKey === serviceOutputKey
);

if (!sheetsService) {
throw new Error(
"Could not find service for spreadsheetId field value: " +
Expand Down

0 comments on commit aa92de4

Please sign in to comment.