mirror of
https://github.com/pawelmalak/flame.git
synced 2025-08-04 02:15:18 +02:00
automatically create new category when category in integrations is not found (#11)
automatically create new category when category in docker or kubernetes labels is not found
This commit is contained in:
parent
4280511372
commit
349d63c3e3
4 changed files with 52 additions and 31 deletions
|
@ -52,13 +52,6 @@ const Home = (props: ComponentProps): JSX.Element => {
|
||||||
// Local search query
|
// Local search query
|
||||||
const [localSearch, setLocalSearch] = useState<null | string>(null);
|
const [localSearch, setLocalSearch] = useState<null | string>(null);
|
||||||
|
|
||||||
// Load app categories
|
|
||||||
useEffect(() => {
|
|
||||||
if (appCategories.length === 0) {
|
|
||||||
getAppCategories();
|
|
||||||
}
|
|
||||||
}, [getAppCategories]);
|
|
||||||
|
|
||||||
// Load apps
|
// Load apps
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (apps.length === 0) {
|
if (apps.length === 0) {
|
||||||
|
@ -66,13 +59,6 @@ const Home = (props: ComponentProps): JSX.Element => {
|
||||||
}
|
}
|
||||||
}, [getApps]);
|
}, [getApps]);
|
||||||
|
|
||||||
// Load bookmark categories
|
|
||||||
useEffect(() => {
|
|
||||||
if (bookmarkCategories.length === 0) {
|
|
||||||
getBookmarkCategories();
|
|
||||||
}
|
|
||||||
}, [getBookmarkCategories]);
|
|
||||||
|
|
||||||
// Load bookmarks
|
// Load bookmarks
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (bookmarks.length === 0) {
|
if (bookmarks.length === 0) {
|
||||||
|
@ -120,10 +106,11 @@ const Home = (props: ComponentProps): JSX.Element => {
|
||||||
return [category];
|
return [category];
|
||||||
};
|
};
|
||||||
|
|
||||||
const categoryContainsItems = (category: Category, allItems: App[] | Bookmark[]): boolean => {
|
const categoryContainsPinnedItems = (category: Category, allItems: App[] | Bookmark[]): boolean => {
|
||||||
if (category.apps?.length > 0) return true;
|
if (category.apps?.filter((app: App) => app.isPinned).length > 0) return true;
|
||||||
|
if (category.bookmarks?.filter((bookmark: Bookmark) => bookmark.isPinned).length > 0) return true;
|
||||||
if (category.id < 0) { // Is a default category
|
if (category.id < 0) { // Is a default category
|
||||||
return allItems.findIndex((item: App | Bookmark) => item.categoryId === category.id) >= 0;
|
return allItems.findIndex((item: App | Bookmark) => item.categoryId === category.id && item.isPinned) >= 0;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
@ -160,7 +147,7 @@ const Home = (props: ComponentProps): JSX.Element => {
|
||||||
<AppGrid
|
<AppGrid
|
||||||
categories={
|
categories={
|
||||||
!localSearch
|
!localSearch
|
||||||
? appCategories.filter((category: Category) => category.isPinned && categoryContainsItems(category, apps))
|
? appCategories.filter((category: Category) => category.isPinned && categoryContainsPinnedItems(category, apps))
|
||||||
: searchInCategories(localSearch, appCategories)
|
: searchInCategories(localSearch, appCategories)
|
||||||
}
|
}
|
||||||
apps={
|
apps={
|
||||||
|
@ -189,7 +176,7 @@ const Home = (props: ComponentProps): JSX.Element => {
|
||||||
<BookmarkGrid
|
<BookmarkGrid
|
||||||
categories={
|
categories={
|
||||||
!localSearch
|
!localSearch
|
||||||
? bookmarkCategories.filter((category: Category) => category.isPinned && categoryContainsItems(category, bookmarks))
|
? bookmarkCategories.filter((category: Category) => category.isPinned && categoryContainsPinnedItems(category, bookmarks))
|
||||||
: searchInCategories(localSearch, bookmarkCategories)
|
: searchInCategories(localSearch, bookmarkCategories)
|
||||||
}
|
}
|
||||||
bookmarks={
|
bookmarks={
|
||||||
|
|
|
@ -26,6 +26,8 @@ export const getApps = () => async (dispatch: Dispatch) => {
|
||||||
type: ActionTypes.getAppsSuccess,
|
type: ActionTypes.getAppsSuccess,
|
||||||
payload: res.data.data,
|
payload: res.data.data,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await getCategories(dispatch);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
}
|
}
|
||||||
|
@ -49,16 +51,20 @@ export const getAppCategories = () => async (dispatch: Dispatch) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
await getCategories(dispatch);
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
async function getCategories(dispatch: Dispatch) {
|
||||||
const res = await axios.get<ApiResponse<Category[]>>("/api/categories/apps");
|
const res = await axios.get<ApiResponse<Category[]>>("/api/categories/apps");
|
||||||
|
|
||||||
dispatch<GetAppCategoriesAction<Category[]>>({
|
dispatch<GetAppCategoriesAction<Category[]>>({
|
||||||
type: ActionTypes.getAppCategoriesSuccess,
|
type: ActionTypes.getAppCategoriesSuccess,
|
||||||
payload: res.data.data
|
payload: res.data.data
|
||||||
});
|
});
|
||||||
} catch (err) {
|
}
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ADD CATEGORY
|
* ADD CATEGORY
|
||||||
|
|
|
@ -26,6 +26,8 @@ export const getBookmarks = () => async (dispatch: Dispatch) => {
|
||||||
type: ActionTypes.getBookmarksSuccess,
|
type: ActionTypes.getBookmarksSuccess,
|
||||||
payload: res.data.data,
|
payload: res.data.data,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await getCategories(dispatch);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
}
|
}
|
||||||
|
@ -49,16 +51,20 @@ export const getBookmarkCategories = () => async (dispatch: Dispatch) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
await getCategories(dispatch);
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
async function getCategories(dispatch: Dispatch) {
|
||||||
const res = await axios.get<ApiResponse<Category[]>>("/api/categories/bookmarks");
|
const res = await axios.get<ApiResponse<Category[]>>("/api/categories/bookmarks");
|
||||||
|
|
||||||
dispatch<GetBookmarkCategoriesAction<Category[]>>({
|
dispatch<GetBookmarkCategoriesAction<Category[]>>({
|
||||||
type: ActionTypes.getBookmarkCategoriesSuccess,
|
type: ActionTypes.getBookmarkCategoriesSuccess,
|
||||||
payload: res.data.data
|
payload: res.data.data
|
||||||
});
|
});
|
||||||
} catch (err) {
|
}
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ADD CATEGORY
|
* ADD CATEGORY
|
||||||
|
@ -473,3 +479,4 @@ export const reorderBookmarkCategories =
|
||||||
console.log(err);
|
console.log(err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -217,6 +217,12 @@ async function retrieveDockerApps(apps, orderType, unpinStoppedApps) {
|
||||||
|
|
||||||
for (let i = 0; i < names.length; i++) {
|
for (let i = 0; i < names.length; i++) {
|
||||||
const category = categoriesLabels[i] ? categories.find(category => category.name.toUpperCase() === categoriesLabels[i].toUpperCase()) : dockerDefaultCategory;
|
const category = categoriesLabels[i] ? categories.find(category => category.name.toUpperCase() === categoriesLabels[i].toUpperCase()) : dockerDefaultCategory;
|
||||||
|
if (!category) {
|
||||||
|
category = await createNewCategory(categoriesLabels[i]);
|
||||||
|
if (category) {
|
||||||
|
categories.push(category);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
dockerApps.push({
|
dockerApps.push({
|
||||||
name: names[i] || names[0],
|
name: names[i] || names[0],
|
||||||
|
@ -250,6 +256,15 @@ async function retrieveDockerApps(apps, orderType, unpinStoppedApps) {
|
||||||
return apps;
|
return apps;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function createNewCategory(newCategoryName) {
|
||||||
|
return await Category.create({
|
||||||
|
name: newCategoryName,
|
||||||
|
type: 'apps',
|
||||||
|
isPinned: true,
|
||||||
|
orderId: Number.MAX_SAFE_INTEGER //New category will always be last and can then be re-ordered manually by user
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async function retrieveKubernetesApps(apps, orderType, unpinStoppedApps) {
|
async function retrieveKubernetesApps(apps, orderType, unpinStoppedApps) {
|
||||||
let ingresses = null;
|
let ingresses = null;
|
||||||
|
|
||||||
|
@ -295,6 +310,12 @@ async function retrieveKubernetesApps(apps, orderType, unpinStoppedApps) {
|
||||||
|
|
||||||
for (let i = 0; i < names.length; i++) {
|
for (let i = 0; i < names.length; i++) {
|
||||||
const category = categoriesLabels[i] ? categories.find(category => category.name.toUpperCase() === categoriesLabels[i].toUpperCase()) : kubernetesDefaultCategory;
|
const category = categoriesLabels[i] ? categories.find(category => category.name.toUpperCase() === categoriesLabels[i].toUpperCase()) : kubernetesDefaultCategory;
|
||||||
|
if (!category) {
|
||||||
|
category = await createNewCategory(categoriesLabels[i]);
|
||||||
|
if (category) {
|
||||||
|
categories.push(category);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
kubernetesApps.push({
|
kubernetesApps.push({
|
||||||
name: names[i] || names[0],
|
name: names[i] || names[0],
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue