Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ function EntityProject(props: {
entity: ProjectContainedEntity;
onClick?: EntityClickHandler<ProjectEntity>;
}) {
const projectQuery = createProjectQuery(props.entity);
const projectQuery = createProjectQuery(props.entity.projectId);
let projectIconRef!: HTMLDivElement;

createEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion js/app/packages/macro-entity/src/queries/dss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ const selectData: (
ownerId: item.data.ownerId,
frecencyScore: item.frecency_score,
viewedAt: item.data.viewedAt ?? undefined,
parentId: item.data.parentId ?? undefined,
projectId: item.data.parentId ?? undefined,
type: item.tag,
name: item.data.name || 'New Project',
};
Expand Down
35 changes: 21 additions & 14 deletions js/app/packages/macro-entity/src/queries/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,25 @@ import type {
ProjectPreviewData,
} from '@service-storage/generated/schemas';
import { useQuery } from '@tanstack/solid-query';
import type { ChatEntity, DocumentEntity, EntityData } from '../types/entity';
import {
type ChatEntity,
type DocumentEntity,
type EntityData,
getEntityProjectId,
type ProjectEntity,
} from '../types/entity';
import { createApiTokenQuery } from './auth';
import { queryKeys } from './key';

export type ProjectContainedEntity = WithRequired<
Extract<EntityData, DocumentEntity | ChatEntity>,
Extract<EntityData, DocumentEntity | ChatEntity | ProjectEntity>,
'projectId'
>;

export const isProjectContainedEntity = (
entity: EntityData
): entity is ProjectContainedEntity => {
if (entity.type !== 'chat' && entity.type !== 'document') return false;
return !!entity.projectId;
return getEntityProjectId(entity) !== false;
};

const fetchProjectData = async (
Expand Down Expand Up @@ -103,18 +108,20 @@ const fetchProjectData = async (
}
};

export function createProjectQuery<T extends ProjectContainedEntity>(
entity: T
) {
export function createProjectQuery(projectId: string) {
const authQuery = createApiTokenQuery();

const projectQuery = useQuery(() => ({
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
}));
const projectQuery = useQuery(() => {
return {
queryKey: queryKeys.project({
projectId,
}),
queryFn: () => fetchProjectData(projectId, authQuery.data),
enabled: authQuery.isSuccess && !!projectId,
gcTime: 1000 * 60 * 10, // 10 minutes
staleTime: 1000 * 60 * 5, // 5 minutes
};
});

return projectQuery;
}
2 changes: 1 addition & 1 deletion js/app/packages/macro-entity/src/queries/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ const useMapSearchResponseItem = () => {
ownerId: result.owner_id,
createdAt: result.created_at,
updatedAt: result.updated_at,
parentId: result.metadata?.parent_project_id ?? undefined,
projectId: result.metadata?.parent_project_id ?? undefined,
search,
};
}
Expand Down
5 changes: 1 addition & 4 deletions js/app/packages/macro-entity/src/types/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ export type DocumentEntity = BaseDocumentEntity | MarkdownEntity;
export type NamedSubType = 'task';

export const getEntityProjectId = (e: EntityData): string | false => {
if (e.type === 'project') {
return 'parentId' in e ? (e.parentId ?? false) : false;
}
return 'projectId' in e ? (e.projectId ?? false) : false;
};

Expand All @@ -84,7 +81,7 @@ export type EmailEntity = EntityBase & {

export type ProjectEntity = EntityBase & {
type: 'project';
parentId?: string;
projectId?: string;
};

// Create new entity types above this comment
Expand Down