import React from "react";
import {
Braces,
ALargeSmall,
Calendar1,
CalendarClock,
Hash,
Brackets,
ListOrdered,
ToggleRight,
Binary,
LetterText,
Bolt,
Package,
CirclePlus,
CircleMinus,
RefreshCw,
} from "lucide-react";
const Stores = () => {
const COLUMN_TYPES = window.COLUMN_TYPES;
const COLUMN_INDEXES = window.COLUMN_INDEXES;
const COLUMN_DEFAULTS = window.COLUMN_DEFAULTS;
const APP_MODELS = window.APP_MODELS;
const modelsForSelect = () => {
return APP_MODELS.map((model) => ({
value: model.underscore,
label: (
{model.name}
{model.table_name}
),
}));
};
const actionsForSelect = () => {
return COLUMN_ACTIONS.map((action) => ({
value: action.name,
label: (
{iconForAction(action.name)}
{action.label}
{action.example}
),
}));
};
const modelsForSelectWithout = (excludedModelNames) => {
return modelsForSelect().filter((entry) => {
return !excludedModelNames.includes(entry.value);
});
};
const snakeCase = (str) => {
return str
.replace(/\ /g, "_")
.replace(/[^a-zA-Z0-9\_]/g, "")
.replace(/\_\_/g, "_")
.toLowerCase();
};
const modelByValue = (value) => {
return APP_MODELS.find((model) => model.underscore === value);
};
const filterAttributes = (row) => {
let model = modelByValue(row.model_name);
return model ? model.attributes_list : [];
};
const filterIndexes = (row) => {
let model = modelByValue(row.model_name);
let indexes = [];
if (model) {
let attributes_with_index = model.attributes_list.filter(
(attribute) => attribute.index.length > 0
);
attributes_with_index.map((attribute) => {
attribute.index.map((index) => {
let columns = [index.columns.map((column) => column).join(", ")];
indexes.push({
value: index.name,
label: (
{columns}
{index.name}{" "}
{index.unique && (
unique
)}
),
});
});
});
}
return indexes;
};
const attributesForSelect = (attributes_list, additional = []) => {
// "additional" is used to add columns that are already in the migration file
const existingColumns = additional
.filter((row) => row.action_name === "add_column")
.filter((row) => row.column_name.length)
.map((row) => {
return {
name: row.column_name,
type: row.column_type,
index: [],
};
});
const list = existingColumns.concat(attributes_list);
return list.map((attr) => ({
value: attr.name,
label: (
{attr.name}
{attr.type}{" "}
{attr.index.length > 0 && (
index
)}
),
}));
};
const iconForAction = (action) => {
const size = 17;
const strokeWidth = 1;
if (action.indexOf("add") !== -1 || action === "belongs_to") {
return ;
}
if (action.indexOf("remove") !== -1 || action === "drop_table") {
return ;
}
if (action.indexOf("change") !== -1 || action === "rename_column") {
return ;
}
return;
};
const iconForType = (type) => {
const size = 17;
const strokeWidth = 1;
if (type === "string") {
return ;
}
if (type === "text") {
return ;
}
if (type === "datetime") {
return ;
}
if (type === "date") {
return ;
}
if (
type === "integer" ||
type === "bigint" ||
type === "float" ||
type === "numeric" ||
type === "decimal"
) {
return ;
}
if (type === "array") {
return ;
}
if (type === "uuid") {
return ;
}
if (type === "json" || type === "jsonb") {
return ;
}
if (type === "boolean") {
return ;
}
if (type === "binary") {
return ;
}
if (type === "blob") {
return ;
}
if (type === "references") {
return ;
}
return;
};
const typesForSelect = () => {
return COLUMN_TYPES.map((type) => ({
value: type.name,
label: (
{iconForType(type.name)}
{type.name}
{type.example}
),
}));
};
const indexesForSelect = () => {
return [{ value: "", label: "(none)" }].concat(
COLUMN_INDEXES.map((type) => ({
value: type.name,
label: (
{type.label || type.name}
{type.example}
),
}))
);
};
return {
actionsForSelect: actionsForSelect,
modelsForSelect: modelsForSelect,
typesForSelect: typesForSelect,
attributesForSelect: attributesForSelect,
indexesForSelect: indexesForSelect,
modelsForSelectWithout: modelsForSelectWithout,
modelByValue: modelByValue,
filterAttributes: filterAttributes,
filterIndexes: filterIndexes,
snakeCase: snakeCase,
APP_MODELS: APP_MODELS,
COLUMN_DEFAULTS: COLUMN_DEFAULTS,
COLUMN_TYPES: COLUMN_TYPES,
COLUMN_INDEXES: COLUMN_INDEXES,
};
};
export default Stores;