Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

chore: fix project entity preview types and reactivity (#154)

gbirmana20802e0278e
diff --git a/js/app/packages/macro-entity/src/components/EntityWithEverything.tsx b/js/app/packages/macro-entity/src/components/EntityWithEverything.tsx
index 704ec94e31..89ba8ce0a5 100644
--- a/js/app/packages/macro-entity/src/components/EntityWithEverything.tsx
+++ b/js/app/packages/macro-entity/src/components/EntityWithEverything.tsx
@@ -1,3 +1,4 @@
+import { matches } from '@core/util/match';
import CheckIcon from '@phosphor-icons/core/assets/regular/check.svg';
import { useEmail } from '@service-gql/client';
import { mergeRefs } from '@solid-primitives/refs';
@@ -23,7 +24,11 @@ import {
import { Dynamic } from 'solid-js/web';
import { ITEM_WRAPPER } from '../constants/classStrings';
import { createProfilePictureQuery } from '../queries/auth';
-import { createProjectQuery } from '../queries/project';
+import {
+ createProjectQuery,
+ isProjectContainedEntity,
+ type ProjectContainedEntity,
+} from '../queries/project';
import type { EntityData, EntityOf } from '../types/entity';
import type { Notification, WithNotification } from '../types/notification';
import type { WithSearch } from '../types/search';
@@ -439,7 +444,9 @@ export function EntityWithEverything(
>
<div class="flex flex-row items-center justify-end gap-4 min-w-0">
<Show when={!showActionList()}>
- <EntityProject entity={props.entity} />
+ <Show when={matches(props.entity, isProjectContainedEntity)}>
+ {(entity) => <EntityProject entity={entity()} />}
+ </Show>
<Show when={props.timestamp ?? props.entity.updatedAt}>
{(date) => {
const formattedDate = createFormattedDate(date());
@@ -672,20 +679,8 @@ function NotificationUserIcon(props: { id: string; name?: string }) {
);
}
-function EntityProject(props: { entity: EntityData }) {
- if (
- !(
- (props.entity.type === 'chat' || props.entity.type === 'document') &&
- props.entity.projectId
- )
- ) {
- return null;
- }
-
+function EntityProject(props: { entity: ProjectContainedEntity }) {
const projectQuery = createProjectQuery(props.entity);
- const Loading = () => (
- <div class="h-3 w-10 bg-ink-placeholder animate-pulse" />
- );
return (
<div class="flex gap-1 items-center text-xs text-ink-extra-muted min-w-0">
<svg
@@ -701,7 +696,9 @@ function EntityProject(props: { entity: EntityData }) {
fill="currentColor"
/>
</svg>
- <Suspense fallback={<Loading />}>
+ <Suspense
+ fallback={<div class="h-3 w-10 bg-ink-placeholder animate-pulse" />}
+ >
<Show when={projectQuery.data?.name}>
{(name) => <div class="truncate">{name()}</div>}
</Show>
diff --git a/js/app/packages/macro-entity/src/queries/project.ts b/js/app/packages/macro-entity/src/queries/project.ts
index dcce655131..bf7805382b 100644
--- a/js/app/packages/macro-entity/src/queries/project.ts
+++ b/js/app/packages/macro-entity/src/queries/project.ts
@@ -1,9 +1,22 @@
import { SERVER_HOSTS } from '@core/constant/servers';
+import type { WithRequired } from '@core/util/withRequired';
import { useQuery } from '@tanstack/solid-query';
import type { ChatEntity, DocumentEntity, EntityData } from '../types/entity';
import { createApiTokenQuery } from './auth';
import { queryKeys } from './key';
+export type ProjectContainedEntity = WithRequired<
+ Extract<EntityData, DocumentEntity | ChatEntity>,
+ 'projectId'
+>;
+
+export const isProjectContainedEntity = (
+ entity: EntityData
+): entity is ProjectContainedEntity => {
+ if (entity.type !== 'chat' && entity.type !== 'document') return false;
+ return !!entity.projectId;
+};
+
const fetchProjectData = async (projectId: string, apiToken?: string) => {
if (!apiToken) throw new Error('No API token provided');
@@ -83,15 +96,15 @@ const fetchProjectData = async (projectId: string, apiToken?: string) => {
}
};
-type DocumentOrChatEntity = Extract<EntityData, DocumentEntity | ChatEntity>;
-
-export function createProjectQuery<T extends DocumentOrChatEntity>(entity: T) {
+export function createProjectQuery<T extends ProjectContainedEntity>(
+ entity: T
+) {
const authQuery = createApiTokenQuery();
const projectQuery = useQuery(() => ({
- queryKey: queryKeys.project({ projectId: entity.projectId! }),
- queryFn: () => fetchProjectData(entity.projectId!, authQuery.data),
- enabled: !!entity.projectId && authQuery.isSuccess,
+ queryKey: queryKeys.project({ projectId: entity.projectId }),
+ queryFn: () => fetchProjectData(entity.projectId, authQuery.data),
+ enabled: authQuery.isSuccess,
gcTime: 1000 * 60 * 10, // 10 minutes
staleTime: 1000 * 60 * 5, // 5 minutes
}));