use App\Types\Model;
use Inertia\Inertia;
use Inertia\Response;
class Page
{
protected string $description = 'The smart way to write, share and discover the latest code tips & tricks.';
protected array $meta = [];
protected string $robots = '';
protected string $title = 'TipSea';
protected string $view = '';
public function __construct()
{
$this->meta = $this->defaultMetaTags();
$this->robots = $this->requiresAuthentication() ? 'NOINDEX, NOFOLLOW' : 'INDEX, FOLLOW';
}
public function description(string $text) : static
{
$this->description = $text;
return $this;
}
protected function defaultMetaTags() : array
{
return [
'title' => $this->title,
'description' => $this->description,
'type' => 'website',
'url' => request()->url(),
'image' => asset('img/card.png'),
'twitter' => [
'type' => 'summary_large_image',
],
];
}
protected function requiresAuthentication() : bool
{
$middleware = request()->route()?->controllerMiddleware() ?? [];
return in_array('auth', $middleware);
}
public static function make() : static
{
return new static();
}
public function meta(Model $model) : static
{
$this->meta = Meta::create($model);
return $this;
}
public function render() : Response
{
Inertia::setRootView('app.index');
return Inertia::render($this->view)
->with('title', $this->title)
->withViewData('meta', $this->meta)
->withViewData('title', $this->title)
->withViewData('robots', $this->robots)
->withViewData('description', $this->description);
}
public function robots(string $text) : static
{
$this->robots = $text;
return $this;
}
public function title(string $text) : static
{
$this->title = "TipSea - {$text}";
return $this;
}
public function view(string $text) : static
{
$this->view = str_replace('.', '/', $text);
return $this;
}
public function with(string $key, mixed $value) : Response
{
return $this->render()->with($key, $value);
}
public function withoutMeta() : static
{
$this->meta = [];
return $this;
}
public function withViewData(string $key, mixed $value) : Response
{
return $this->render()->withViewData($key, $value);
}
}