Skip to content

Commit

Permalink
add own page for global search
Browse files Browse the repository at this point in the history
untested - and route needs cleanup (if possible)
  • Loading branch information
Boy132 committed Oct 8, 2024
1 parent 4c70292 commit 616a7be
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 80 deletions.
3 changes: 2 additions & 1 deletion app/Filament/App/Resources/FileResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class FileResource extends Resource
public static function getPages(): array
{
return [
'edit' => Pages\EditFiles::route('/edit/{path?}'),
'edit' => Pages\EditFiles::route('/edit/{path}'),
'search' => Pages\SearchFiles::route('/search/{searchTerm}/{path}'), // TODO: find better way?
'index' => Pages\ListFiles::route('/{path?}'),
];
}
Expand Down
84 changes: 10 additions & 74 deletions app/Filament/App/Resources/FileResource/Pages/ListFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,17 @@
use Carbon\CarbonImmutable;
use Filament\Actions\Action as HeaderAction;
use Filament\Facades\Filament;
use Filament\Forms\Components\Actions;
use Filament\Forms\Components\Actions\Action as FormAction;
use Filament\Forms\Components\CheckboxList;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Tabs;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Wizard;
use Filament\Forms\Components\Wizard\Step;
use Filament\Forms\Get;
use Filament\Forms\Set;
use Filament\Notifications\Notification;
use Filament\Panel;
use Filament\Resources\Pages\ListRecords;
use Filament\Resources\Pages\PageRegistration;
use Filament\Support\Enums\VerticalAlignment;
use Filament\Tables\Actions\Action;
use Filament\Tables\Actions\ActionGroup;
use Filament\Tables\Actions\BulkAction;
Expand All @@ -44,9 +37,7 @@
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\UploadedFile;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Route as RouteFacade;
use Illuminate\Support\HtmlString;
use Livewire\Attributes\Locked;

class ListFiles extends ListRecords
Expand Down Expand Up @@ -566,71 +557,16 @@ protected function getHeaderActions(): array
HeaderAction::make('search')
->authorize(auth()->user()->can(Permission::ACTION_FILE_READ, Filament::getTenant()))
->label('Global Search')
->modalSubmitAction(false)
->modalCancelAction(false)
->form([ // TODO: replace modal with custom page
Wizard::make([
Step::make('Input')
->schema([
TextInput::make('searchTerm')
->placeholder('Enter a search term, e.g. *.txt')
->minLength(3),
])
->afterValidation(function (Get $get, Set $set) {
/** @var Server $server */
$server = Filament::getTenant();

$foundFiles = app(DaemonFileRepository::class)
->setServer($server)
->search($get('searchTerm'), $this->path);

$set('foundFiles', $foundFiles);
}),
Step::make('Result')
->schema([
Repeater::make('foundFiles')
->disabled()
->addable(false)
->deletable(false)
->reorderable(false)
->defaultItems(0)
->columns(6)
->live()
->schema([
TextInput::make('name')
->hidden(),
TextInput::make('size')
->hidden(),
TextInput::make('symlink')
->hidden(),
TextInput::make('directory')
->hidden(),
Placeholder::make('icon')
->label('')
->content(fn (Get $get) => new HtmlString(Blade::render(<<<'BLADE'
<div @class(['fi-in-icon flex flex-wrap gap-1.5'])>
<x-filament::icon icon="{{ $icon }}" @class(['fi-in-icon-item', 'fi-in-icon-item-size-lg h-6 w-6', 'fi-color-custom text-custom-500 dark:text-custom-400', 'fi-color-primary'])/>
</div>
BLADE, ['icon' => $get('directory') ? 'tabler-folder' : ($get('symlink') ? 'tabler-file-symlink' : 'tabler-file')]))),
Placeholder::make('name')
->label('')
->content(fn (Get $get) => $get('name'))
->columnSpan(3),
Placeholder::make('size')
->label('')
->content(fn (Get $get) => $get('directory') ? '' : convert_bytes_to_readable($get('size'))),
Actions::make([
FormAction::make('edit')
->authorize(auth()->user()->can(Permission::ACTION_FILE_READ_CONTENT, Filament::getTenant()))
->icon('tabler-edit')
->url(fn (Get $get) => EditFiles::getUrl(['path' => join_paths($this->path, $get('name'))])),
])
->verticalAlignment(VerticalAlignment::End),
]),
]),
])
->nextAction(fn (FormAction $action) => $action->label('Search')),
]),
->modalSubmitActionLabel('Search')
->form([
TextInput::make('searchTerm')
->placeholder('Enter a search term, e.g. *.txt')
->minLength(3),
])
->action(fn ($data) => redirect(SearchFiles::getUrl([
'searchTerm' => $data['searchTerm'],
'path' => $this->path,
]))),
];
}

Expand Down
75 changes: 75 additions & 0 deletions app/Filament/App/Resources/FileResource/Pages/SearchFiles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace App\Filament\App\Resources\FileResource\Pages;

use App\Filament\App\Resources\FileResource;
use App\Models\File;
use App\Models\Server;
use Filament\Facades\Filament;
use Filament\Panel;
use Filament\Resources\Pages\ListRecords;
use Filament\Resources\Pages\PageRegistration;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\Route as RouteFacade;
use Livewire\Attributes\Locked;

class SearchFiles extends ListRecords
{
protected static string $resource = FileResource::class;

#[Locked]
public string $searchTerm;
#[Locked]
public string $path;

public function mount(string $searchTerm = null, string $path = null): void
{
parent::mount();
$this->searchTerm = $searchTerm;
$this->path = $path ?? '/';
}

public function table(Table $table): Table
{
return $table
->paginated(false)
->query(function () {
/** @var Server $server */
$server = Filament::getTenant();

return File::get($server, $this->path, $this->searchTerm)->orderByDesc('is_directory')->orderBy('name');
})
->columns([
TextColumn::make('name')
->searchable()
->icon(fn (File $file) => $file->getIcon()),
TextColumn::make('size')
->formatStateUsing(fn ($state, File $file) => $file->is_file ? convert_bytes_to_readable($state) : ''),
TextColumn::make('modified_at')
->dateTime()
->sortable()
->formatStateUsing(fn ($state) => $state->diffForHumans())
->tooltip(fn (File $file) => $file->modified_at),
])
->recordUrl(function (File $file) {
if ($file->is_directory) {
return ListFiles::getUrl(['path' => join_paths($this->path, $file->name)]);
}

return $file->canEdit() ? EditFiles::getUrl(['path' => join_paths($this->path, $file->name)]) : null;
});
}

public static function route(string $path): PageRegistration
{
return new PageRegistration(
page: static::class,
route: fn (Panel $panel): Route => RouteFacade::get($path, static::class)
->middleware(static::getRouteMiddleware($panel))
->withoutMiddleware(static::getWithoutRouteMiddleware($panel))
->where('path', '.*'),
);
}
}
13 changes: 8 additions & 5 deletions app/Models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ class File extends Model

protected static Server $server;
protected static string $path;
protected static ?string $searchTerm;

public static function get(Server $server, string $path = '/'): Builder
public static function get(Server $server, string $path = '/', string $searchTerm = null): Builder
{
self::$server = $server;
self::$path = $path;
self::$searchTerm = $searchTerm;

return self::query();
}
Expand Down Expand Up @@ -126,9 +128,10 @@ public function getSchema(): array
public function getRows(): array
{
try {
$contents = app(DaemonFileRepository::class)
->setServer($this->server())
->getDirectory(self::$path ?? '/');
/** @var DaemonFileRepository $fileRepository */
$fileRepository = app(DaemonFileRepository::class)->setServer($this->server);

$contents = is_null(self::$searchTerm) ? $fileRepository->getDirectory(self::$path ?? '/') : $fileRepository->search(self::$searchTerm, self::$path);

if (isset($contents['error'])) {
throw new Exception($contents['error']);
Expand Down Expand Up @@ -163,6 +166,6 @@ public function getRows(): array

protected function sushiShouldCache(): bool
{
return false;
return !is_null(self::$searchTerm);
}
}

0 comments on commit 616a7be

Please sign in to comment.