Files
shakespeare/src/hooks/useProjectLabels.ts
T
MK Fain 63d23f5510 Add project labels feature similar to Gmail labels
Features:
- Create and manage labels with customizable colors (18 color options)
- Assign multiple labels to projects via Project Details dialog
- Display settings in Preferences (only shown when labels exist):
  - Colored folder icons: Show project folder icons in label color
  - Group by label: Organize sidebar projects into collapsible groups
- Labels work alongside existing favorites (starred) system:
  - Favorites still sort to top within label groups
  - Star indicator remains visible in all display modes
- Collapsible label groups in sidebar (state persists)
- Unlabeled projects shown at bottom when grouping is enabled
- No UI changes for users without labels (settings hidden until first label)

Technical:
- useLabels hook: Manage labels CRUD and display settings
- useProjectLabels hook: Manage project-label assignments
- LabelSelector component: Assign labels to projects
- LabelsManageDialog: Create, edit, delete labels
- Full i18n support for EN, PT, ZH, HA, YO, IG
2025-12-03 13:25:35 -06:00

84 lines
2.3 KiB
TypeScript

import { useLocalStorage } from '@/hooks/useLocalStorage';
/** Map of project IDs to their assigned label IDs */
type ProjectLabelsMap = Record<string, string[]>;
/**
* Hook for managing project-label associations
*/
export function useProjectLabels() {
const [projectLabels, setProjectLabels] = useLocalStorage<ProjectLabelsMap>('project-label-assignments', {});
const getProjectLabels = (projectId: string): string[] => {
return projectLabels[projectId] || [];
};
const addLabelToProject = (projectId: string, labelId: string) => {
const currentLabels = projectLabels[projectId] || [];
if (!currentLabels.includes(labelId)) {
setProjectLabels({
...projectLabels,
[projectId]: [...currentLabels, labelId],
});
}
};
const removeLabelFromProject = (projectId: string, labelId: string) => {
const currentLabels = projectLabels[projectId] || [];
const newLabels = currentLabels.filter(id => id !== labelId);
if (newLabels.length === 0) {
const { [projectId]: _, ...rest } = projectLabels;
setProjectLabels(rest);
} else {
setProjectLabels({
...projectLabels,
[projectId]: newLabels,
});
}
};
const setProjectLabelsList = (projectId: string, labelIds: string[]) => {
if (labelIds.length === 0) {
const { [projectId]: _, ...rest } = projectLabels;
setProjectLabels(rest);
} else {
setProjectLabels({
...projectLabels,
[projectId]: labelIds,
});
}
};
const getProjectsByLabel = (labelId: string): string[] => {
return Object.entries(projectLabels)
.filter(([_, labels]) => labels.includes(labelId))
.map(([projectId]) => projectId);
};
const hasAnyLabels = (): boolean => {
return Object.keys(projectLabels).length > 0;
};
const removeLabel = (labelId: string) => {
const newProjectLabels: ProjectLabelsMap = {};
for (const [projectId, labels] of Object.entries(projectLabels)) {
const filtered = labels.filter(id => id !== labelId);
if (filtered.length > 0) {
newProjectLabels[projectId] = filtered;
}
}
setProjectLabels(newProjectLabels);
};
return {
projectLabels,
getProjectLabels,
addLabelToProject,
removeLabelFromProject,
setProjectLabelsList,
getProjectsByLabel,
hasAnyLabels,
removeLabel,
};
}