sidebar refactor

This commit is contained in:
2024-03-20 01:57:17 +02:00
parent 1f03eebd08
commit bb77a37ff7
11 changed files with 141 additions and 176 deletions
+47 -9
View File
@@ -4,35 +4,73 @@ namespace Lucent\Schema\Sidebar;
use Lucent\Channel\Channel;
use Lucent\Channel\ChannelService;
use Lucent\Primitive\Collection;
use Lucent\Schema\CollectionSchema;
use Lucent\Schema\FilesSchema;
use Lucent\Schema\Schema;
use Lucent\Schema\Sidebar\Item\Item;
use Lucent\Schema\Sidebar\Item\LinkItem;
use Lucent\Schema\Sidebar\Item\SchemaItem;
use Lucent\Schema\Sidebar\View\ItemData;
use Lucent\Schema\Sidebar\View\SectionData;
use PhpOption\Option;
use Symfony\Component\Yaml\Yaml;
class SidebarService
{
public function __construct()
public function __construct(
private ChannelService $channelService,
)
{
}
/**
* @param Channel $channel
* @param array<string> $readableSchemas
* @param string $currentSchema
* @return string
*/
public function render(Channel $channel, array $readableSchemas, string $currentSchema): string
public function render(array $readableSchemas, string $currentSchema): string
{
$schemas = $channel->schemas->whereIn("name", $readableSchemas);
$schemas = $this->channelService->channel->schemas->whereIn("name", $readableSchemas);
$sidebar = $this->fromConfig();
$sections = [];
$usedSchemaNames = [];
if ($sidebar->isDefined()) {
$sections = $sidebar->get()->sections->map(function (Section $section) use ($currentSchema): SectionData {
return new SectionData(
label: $section->label,
isOpen: $section->items->where("schema", $currentSchema)->isNotEmpty(),
items: array_map(fn($item) => $this->renderItem($currentSchema, $item), $section->items->toArray())
);
})->toArray();
$usedSchemaNames =$sidebar->get()->sections->reduce(fn(array $carry, Section $section) => array_merge($carry, $section->items->pluck("schema")->toArray()), []);
}
$sections[] = new SectionData(
label: $sidebar->isDefined() ? "Rest" : "Content",
isOpen: $schemas->whereNotIn("name",$usedSchemaNames)->where("name",$currentSchema)->isNotEmpty(),
items: array_map(fn($item) => $this->renderItem($currentSchema, $item), $schemas->whereNotIn("name",$usedSchemaNames)->toArray())
);
return view('lucent::sidebar.sidebar', [
'sidebar' => $this->fromConfig(),
'schemas' => $schemas,
'channel' => $channel,
'currentSchema' => $currentSchema,
'lucentUrl' => $channel->lucentUrl,
'channel' => $this->channelService->channel,
'sections' => $sections,
])->render();
}
private function renderItem(string $currentSchema, Item|Schema $item): ItemData
{
return match (get_class($item)) {
LinkItem::class => new ItemData($item->label, $item->href, false, true),
SchemaItem::class => new ItemData($item->label, $this->channelService->channel->lucentUrl . "/content/" . $item->schema, $currentSchema === $item->schema, false),
CollectionSchema::class => new ItemData($item->label, $this->channelService->channel->lucentUrl . "/content/" . $item->name, $currentSchema === $item->name, false),
FilesSchema::class => new ItemData($item->label, $this->channelService->channel->lucentUrl . "/content/" . $item->name, $currentSchema === $item->name, false),
};
}
/**
* @return Option<Sidebar>
*/