EmojiPicker.pm 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package Trog::Component::EmojiPicker;
  2. use v5.36;
  3. use re '/aa';
  4. use JSON::MaybeXS;
  5. use Trog::Renderer;
  6. =head1 Trog::Component::EmojiPicker
  7. The emoji picker component.
  8. =head1 Termination Conditions
  9. Dies if the emoji list isn't on disk, as there's no sensible picker to show
  10. without it. It's a frontend dependency, so C<make prereq-frontend> is what
  11. puts it there.
  12. =head1 FUNCTIONS
  13. =head2 render() = STRING
  14. Render the picker as an HTML component.
  15. The emoji list ships as a flat array, which is no use to a picker with tabs, so
  16. it gets bucketed by category on the first call and memoized thereafter -- the
  17. list can't change without a redeploy.
  18. Returns the rendered body, rather than a PSGI triplet, as components are meant
  19. to be composed into a page by their caller.
  20. =cut
  21. sub render () {
  22. state %categorized;
  23. if ( !%categorized ) {
  24. my $file = 'www/scripts/list.min.json';
  25. die "Run make prereq-frontend first" unless -f $file;
  26. my $raw = File::Slurper::read_binary($file);
  27. my $emojis = JSON::MaybeXS::decode_json($raw);
  28. foreach my $emoji ( @{ $emojis->{emojis} } ) {
  29. $categorized{ $emoji->{category} } //= [];
  30. push( @{ $categorized{ $emoji->{category} } }, $emoji->{emoji} );
  31. }
  32. }
  33. return Trog::Renderer->render(
  34. contenttype => 'text/html',
  35. component => 1,
  36. template => 'emojis.tx',
  37. data => {
  38. categories => \%categorized,
  39. },
  40. );
  41. }
  42. 1;