Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F3010927
list.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
list.ts
View Options
import
_locreq
from
"locreq"
;
import
{
curryImportPath
}
from
"../utils/import-path.js"
;
import
prompts
from
"prompts"
;
import
{
listCollections
}
from
"../utils/list-collections.js"
;
import
{
extractCollectionClassname
}
from
"../generate-collections.js"
;
import
extract_fields_from_collection
from
"../utils/extract-fields-from-collection.js"
;
import
{
DefaultListFilters
}
from
"../page/default-list-filters.js"
;
import
{
formatWithPrettier
}
from
"../utils/prettier.js"
;
import
{
toKebabCase
}
from
"js-convert-case"
;
const
target_locreq
=
_locreq
(
process
.
cwd
());
export
const
listTemplate
=
async
(
action_name
:
string
,
newfilefullpath
:
string
)
:
Promise
<
string
>
=>
{
const
response
=
await
prompts
({
type
:
"autocomplete"
,
name
:
"collection"
,
message
:
"Which sealious collection do you want to list?"
,
choices
:
(
await
listCollections
()
).
map
((
collection
)
=>
({
title
:
collection
,
value
:
collection
,
})),
});
const
collection_name
=
response
.
collection
as
string
;
const
[
uppercase_collection
,
collection_fields
]
=
await
Promise
.
all
([
extractCollectionClassname
(
target_locreq
.
resolve
(
"src/back/collections/"
+
collection_name
+
".ts"
)
),
extract_fields_from_collection
(
collection_name
),
]);
const
importPath
=
curryImportPath
(
newfilefullpath
);
const
result
=
`import type { Context } from "koa";
import type { CollectionItem } from "sealious";
import type { FlatTemplatable } from "tempstream";
import { TempstreamJSX } from "tempstream";
import {
${
uppercase_collection
}
} from "
${
importPath
(
target_locreq
.
resolve
(
"src/back/collections/collections.ts"
)
)
}
";
import html from "
${
importPath
(
target_locreq
.
resolve
(
"src/back/html.ts"
))
}
";
import type { ListFilterRender } from "@sealcode/sealgen";
import {
SealiousItemListPage,
BaseListPageFields,
DefaultListFilters,
} from "@sealcode/sealgen";
import qs from "qs";
import type { FilePointer } from "@sealcode/file-manager";
import { imageRouter } from "
${
importPath
(
target_locreq
.
resolve
(
"src/back/image-router.ts"
)
)
}
";
export const actionName = "
${
action_name
}
";
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const filterFields = [
${
collection_fields
.
map
(
(
field
)
=>
` { field: "
${
field
.
name
}
", render:
${
DefaultListFilters
[
field
.
type
]
?
`DefaultListFilters["
${
field
.
type
}
"]`
:
`DefaultListFilters.fallback`
}
}`
)
.
join
(
",\n"
)
}
] as {
field: keyof (typeof
${
uppercase_collection
}
)["fields"];
render?: ListFilterRender;
}[];
const displayFields = [
${
collection_fields
.
map
((
field
)
=>
{
if
(
field
.
type
==
"boolean"
)
{
return
` {
field: "
${
field
.
name
}
",
label: "
${
field
.
name
}
",
format: (v: boolean) => (v ? "YES" : "NO"),
}`
;
}
if (field.type == "datetime") {
return `
{
field
:
"${field.name}"
,
label
:
"${field.name}"
,
format
:
(
v
:
number
)
=>
{
const
d
=
new
Date
();
d
.
setTime
(
v
);
return
d
.
toString
().
split
(
" "
).
slice
(
1
,
5
).
join
(
" "
);
},
}
`;
}
if (field.type == "image") {
return `
{
field
:
"${field.name}"
,
label
:
"${field.name}"
,
format
:
async
(
value
:
FilePointer
)
=>
{
return
imageRouter
.
image
(
await
value
.
getPath
(),
{
container
:
{
width
:
45
,
height
:
45
},
});
},
}
`;
}
return `
{
field
:
"${field.name}"
,
label
:
"${field.name}"
}
`;
})
.join(",\n")}
] as {
field: string;
label: string;
format?: (value: unknown) => FlatTemplatable;
}[];
export default new (class
${
action_name
}
Page extends SealiousItemListPage<
typeof
${
uppercase_collection
}
,
typeof BaseListPageFields
> {
fields = BaseListPageFields;
async renderFilters(ctx: Context): Promise<FlatTemplatable> {
const query_params = qs.parse(ctx.search.slice(1));
query_params.page = "1";
const filter_values = await this.getFilterValues(ctx);
return <form>
{Object.entries(query_params).map(([key, value]) => {
if (key == "filter") {
return "";
}
return <input
type="hidden"
name={key}
value={value}
/>;
})}
{filterFields.map(({ field, render }) => {
if (!render) {
render = DefaultListFilters.fallback;
}
return (
render(filter_values[field] || "", this.collection.fields[field]) ||
""
);
})}
<input type="submit" />
</form>;
}
async renderItem(ctx: Context, item: CollectionItem<typeof
${
uppercase_collection
}
>) {
return <tr>
{displayFields.map(({ field, format }) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-explicit-any
const value = item.get(field as any);
return <td>{format ? format(value) : value}</td>;
})}
</tr>;
}
async render(ctx: Context) {
return html(
ctx,
"
${
action_name
}
",
<div class="sealious-list-wrapper
${
toKebabCase
(
action_name
)
}
--wrapper">
<h2>
${
action_name
}
List</h2>
<table class="sealious-list">
{this.renderTableHead(ctx, displayFields)}
<tbody>{super.render(ctx)}</tbody>
</table>
</div>
);
}
})(
${
uppercase_collection
}
);
`
;
return
formatWithPrettier
(
result
);
};
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Wed, May 7, 19:49 (22 h, 38 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
643308
Default Alt Text
list.ts (4 KB)
Attached To
Mode
rSGEN sealgen
Attached
Detach File
Event Timeline
Log In to Comment