Files
outline/plugins/granola/client/components/RuleDialog.tsx
T
Tom Moor ba2d9888cc feat: Granola integration (#14906)
* wip

* fix: Address review feedback on Granola integration

- Limit the size of Granola API responses
- Load the team when authorizing notes into personal collections
- Add the user's connection from granola.list to the integrations store

* feat: Connect Granola from a dialog

* fix: Accept Granola webhook test events

* Copy pass

* Copy pass
2026-12-05 14:59:12 -05:00

202 lines
5.6 KiB
TypeScript

import { observer } from "mobx-react";
import { useCallback, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import styled from "styled-components";
import { breakpoint } from "@shared/styles/breakpoint";
import type { NavigationNode } from "@shared/types";
import { NavigationNodeType } from "@shared/types";
import Button from "~/components/Button";
import DocumentExplorer from "~/components/DocumentExplorer";
import {
FlexContainer,
Footer,
} from "~/components/DocumentExplorer/Components";
import type { Option } from "~/components/InputSelect";
import { LabelText } from "~/components/Input";
import { InputSelect } from "~/components/InputSelect";
import Text from "~/components/Text";
import useCollectionTrees from "~/hooks/useCollectionTrees";
import useRequest from "~/hooks/useRequest";
import useStores from "~/hooks/useStores";
import { client } from "~/utils/ApiClient";
import type { GranolaRule } from "../../shared/types";
interface Folder {
id: string;
name: string;
}
interface Props {
/** The ID of the Granola integration. */
integrationId: string;
/** The rule to edit, if any. */
rule?: GranolaRule;
/** Folder IDs that the other rules use, null is the rule for all other notes. */
usedFolderIds: (string | null)[];
/** Called with the new rule. */
onSubmit: (rule: GranolaRule) => Promise<void>;
}
const OtherNotes = "*";
/**
* Dialog that chooses a Granola folder and the Outline location that its notes
* go to.
*/
function RuleDialog({ integrationId, rule, usedFolderIds, onSubmit }: Props) {
const { t } = useTranslation();
const { policies } = useStores();
const collectionTrees = useCollectionTrees();
const [folderId, setFolderId] = useState<string | undefined>(
rule ? (rule.folderId ?? OtherNotes) : undefined
);
const [location, setLocation] = useState<NavigationNode | null>(null);
const [saving, setSaving] = useState(false);
const fetchFolders = useCallback(async () => {
const res = await client.post<{ data: Folder[] }>("/granola.folders", {
id: integrationId,
});
return res.data;
}, [integrationId]);
const { data: folders, loading } = useRequest(fetchFolders, true);
const folderOptions = useMemo((): Option[] => {
const isAvailable = (id: string | null) => !usedFolderIds.includes(id);
return [
...(isAvailable(null)
? [
{
type: "item" as const,
label: t("All other notes"),
value: OtherNotes,
},
]
: []),
...(folders ?? [])
.filter((folder) => isAvailable(folder.id))
.map((folder) => ({
type: "item" as const,
label: folder.name,
value: folder.id,
})),
];
}, [folders, usedFolderIds, t]);
// Only offer collections the user can create documents in.
const items = useMemo(
() =>
collectionTrees.filter((node) =>
node.collectionId
? policies.get(node.collectionId)?.abilities.createDocument
: true
),
[policies, collectionTrees]
);
const handleSubmit = async () => {
if (!folderId || !location) {
return;
}
setSaving(true);
try {
await onSubmit({
folderId: folderId === OtherNotes ? null : folderId,
folderName: folders?.find((folder) => folder.id === folderId)?.name,
...(location.type === NavigationNodeType.Collection
? { collectionId: location.id }
: { documentId: location.id }),
locationName: location.title,
});
} finally {
setSaving(false);
}
};
return (
<Layout column>
<FolderSelect>
<InputSelect
label={t("Granola folder")}
options={folderOptions}
value={folderId}
onChange={setFolderId}
// The default placeholder lowercases the label, which would read "granola".
displayValue={(option) => option?.label ?? t("Choose a folder")}
disabled={loading}
/>
</FolderSelect>
<LocationLabel>{t("Location")}</LocationLabel>
<ExplorerFill>
<DocumentExplorer
items={items}
defaultValue={rule?.documentId ?? rule?.collectionId}
onSelect={setLocation}
onSubmit={setLocation}
/>
</ExplorerFill>
<Footer justify="space-between" align="center" gap={8}>
<Text ellipsis type="secondary">
{location
? location.title || t("Untitled")
: t("Choose where these notes are saved")}
</Text>
<Button
disabled={!folderId || !location || saving}
onClick={handleSubmit}
>
{saving ? `${t("Saving")}…` : t("Save")}
</Button>
</Footer>
</Layout>
);
}
// Matches the height of the document move dialog, so the folder select takes
// its space from the explorer list rather than pushing the footer out.
const Layout = styled(FlexContainer)`
height: calc(65vh + 76px);
${breakpoint("tablet")`
height: max(calc(40vh + 76px), 340px);
`}
`;
const FolderSelect = styled.div`
flex-shrink: 0;
padding: 0 24px 12px;
`;
const LocationLabel = styled(LabelText)`
flex-shrink: 0;
padding: 0 24px 4px;
`;
// The explorer sizes its list to the viewport, here it fills the space left.
const ExplorerFill = styled.div`
display: flex;
flex: 1;
min-height: 0;
> div {
display: flex;
flex: 1;
flex-direction: column;
min-width: 0;
}
> div > :first-child {
flex: none;
}
> div > div:last-child {
flex: 1;
height: auto;
min-height: 0;
}
`;
export default observer(RuleDialog);