Skip to content

Commit

Permalink
Merge pull request #294 from cabcookie/create-activity-names
Browse files Browse the repository at this point in the history
fix: refrain from changing selection sets which has unforeseen side effects
  • Loading branch information
cabcookie authored Feb 26, 2025
2 parents 6ff5300 + 4548639 commit de7ed55
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 103 deletions.
4 changes: 0 additions & 4 deletions api/useActivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ export type TempIdMapping = {
type ProjectLinkData = {
id: string;
projectsId: string;
name: string;
accounts: { name: string }[];
};

export type Activity = {
Expand Down Expand Up @@ -59,8 +57,6 @@ const selectionSet = [
"updatedAt",
"forProjects.id",
"forProjects.projectsId",
"forProjects.projects.project",
"forProjects.projects.accounts.account.name",
"noteBlockIds",
"noteBlocks.id",
"noteBlocks.content",
Expand Down
2 changes: 0 additions & 2 deletions api/useMeetings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ export const meetingSelectionSet = [
"activities.updatedAt",
"activities.forProjects.id",
"activities.forProjects.projectsId",
"activities.forProjects.projects.project",
"activities.forProjects.projects.accounts.account.name",
"activities.noteBlockIds",
"activities.noteBlocks.id",
"activities.noteBlocks.content",
Expand Down
118 changes: 118 additions & 0 deletions helpers/activity/namer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { type Schema } from "@/amplify/data/resource";
import { Activity } from "@/api/useActivity";
import { generateClient as generateApiClient } from "aws-amplify/api";
import { generateClient } from "aws-amplify/data";
import { debounce } from "lodash";
import { format, isFuture, isPast } from "date-fns";
import { emptyDocument } from "@/components/ui-elements/editors/helpers/document";
import { generateText } from "@tiptap/core";
import { Extensions } from "@tiptap/core";

const client = generateClient<Schema>();
const apiClient = generateApiClient<Schema>({ authMode: "userPool" });

export const nameActivity = debounce(
async (activity: Activity, extensions: Extensions) => {
if (!activity.meetingId) return;
const meeting = await getMeeting(activity.meetingId);
const projects = await getProjects(activity);

const content = [
[meeting, projects].join("\n\nProject(s):\n"),
getNotes(activity, extensions),
].join("\n\nNotes:\n");

const name = await getNameForActivity(content);
if (!name) return;
await updateActivityName(activity.id, name);
},
30000
);

const updateActivityName = async (activityId: string, name: string) => {
console.log({ activityId, name });
const { data, errors } = await client.models.Activity.update({
id: activityId,
name,
});
if (errors) console.error("Error updating activity name", errors);
if (!data) console.error("No data returned from update activity name");
};

const getMeeting = async (meetingId: string) => {
const { data, errors } = await client.models.Meeting.get(
{ id: meetingId },
{
selectionSet: [
"topic",
"meetingOn",
"createdAt",
"participants.person.name",
"participants.person.accounts.startDate",
"participants.person.accounts.endDate",
"participants.person.accounts.position",
"participants.person.accounts.account.name",
],
}
);
if (errors || !data) return "";
return [
`Meeting: ${data.topic} on ${format(data.meetingOn || data.createdAt, "PPP")}`,
data.participants
.map((p) =>
[
p.person.name,
p.person.accounts
.filter(
(a) =>
!a.startDate ||
(isPast(a.startDate) && (!a.endDate || isFuture(a.endDate)))
)
.map((pos) => {
return `${[pos.position, pos.account.name].join(" at ")}`;
})
.join(", "),
].join(", ")
)
.join("\n"),
].join("\nParticipants:\n");
};

const getProject = async (projectId: string) => {
const { data, errors } = await client.models.Projects.get(
{ id: projectId },
{ selectionSet: ["project", "accounts.account.name"] }
);
if (errors || !data) {
console.error("Error getting project", errors);
return;
}
return data;
};

const getProjects = async (activity: Activity) => {
const projects = await Promise.all(
activity.projects.map((p) => getProject(p.projectsId))
);
return projects
.filter((p) => !!p)
.map((p) =>
[p.project, p.accounts.map((a) => a.account.name).join(", ")].join(
" for account(s): "
)
);
};

const getNotes = (activity: Activity, extensions: Extensions) =>
generateText(activity.notes ?? emptyDocument, extensions);

const getNameForActivity = async (content: string) => {
const { data, errors } = await apiClient.generations.chatNamer({
content,
});
if (errors || !data) {
console.error("Errors or no data", errors);
return;
}
return data.name;
};
100 changes: 3 additions & 97 deletions pages/activities/[id].tsx
Original file line number Diff line number Diff line change
@@ -1,105 +1,11 @@
import { type Schema } from "@/amplify/data/resource";
import useActivity, { Activity } from "@/api/useActivity";
import useActivity from "@/api/useActivity";
import ActivityComponent from "@/components/activities/activity";
import MainLayout from "@/components/layouts/MainLayout";
import { useRouter } from "next/router";
import { generateClient as generateApiClient } from "aws-amplify/api";
import { generateClient } from "aws-amplify/data";
import { debounce } from "lodash";
import { format, isFuture, isPast } from "date-fns";
import { format } from "date-fns";
import { useEffect } from "react";
import { emptyDocument } from "@/components/ui-elements/editors/helpers/document";
import { generateText } from "@tiptap/core";
import useExtensions from "@/components/ui-elements/editors/notes-editor/useExtensions";
import { Extensions } from "@tiptap/core";

const client = generateClient<Schema>();
const apiClient = generateApiClient<Schema>({ authMode: "userPool" });

const getMeeting = async (meetingId: string) => {
const { data, errors } = await client.models.Meeting.get(
{ id: meetingId },
{
selectionSet: [
"topic",
"meetingOn",
"createdAt",
"participants.person.name",
"participants.person.accounts.startDate",
"participants.person.accounts.endDate",
"participants.person.accounts.position",
"participants.person.accounts.account.name",
],
}
);
if (errors || !data) return "";
return [
`Meeting: ${data.topic} on ${format(data.meetingOn || data.createdAt, "PPP")}`,
data.participants
.map((p) =>
[
p.person.name,
p.person.accounts
.filter(
(a) =>
!a.startDate ||
(isPast(a.startDate) && (!a.endDate || isFuture(a.endDate)))
)
.map((pos) => {
return `${[pos.position, pos.account.name].join(" at ")}`;
})
.join(", "),
].join(", ")
)
.join("\n"),
].join("\nParticipants:\n");
};

const getProjects = (activity: Activity) =>
activity.projects?.map((p) =>
[p.name, p.accounts.map((a) => a.name).join(", ")].join(" for account(s): ")
);

const getNotes = (activity: Activity, extensions: Extensions) =>
generateText(activity.notes ?? emptyDocument, extensions);

const getNameForActivity = async (content: string) => {
const { data, errors } = await apiClient.generations.chatNamer({
content,
});
if (errors || !data) {
console.error("Errors or no data", errors);
return;
}
return data.name;
};

const updateActivityName = async (activityId: string, name: string) => {
console.log({ activityId, name });
const { data, errors } = await client.models.Activity.update({
id: activityId,
name,
});
if (errors) console.error("Error updating activity name", errors);
if (!data) console.error("No data returned from update activity name");
};

const nameActivity = debounce(
async (activity: Activity, extensions: Extensions) => {
if (!activity.meetingId) return;
const meeting = await getMeeting(activity.meetingId);

const content = [
[meeting, getProjects(activity)].join("\n\nProject(s):\n"),
getNotes(activity, extensions),
].join("\n\nNotes:\n");

const name = await getNameForActivity(content);
if (!name) return;
await updateActivityName(activity.id, name);
},
30000
);
import { nameActivity } from "@/helpers/activitiy/namer";

const AccountDetailPage = () => {
const router = useRouter();
Expand Down

0 comments on commit de7ed55

Please sign in to comment.